diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2025-04-24 02:58:57 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2025-04-24 02:58:57 +0200 |
commit | f8bd7cfdb9ed7ec99d612dd03670e50aa0c60db2 (patch) | |
tree | a6b7df9d6e547262b86db964f11dd6b7e56cb403 | |
parent | 5dc100fde9e30f4424d77ced522df58ac03f82c6 (diff) | |
download | reduced_system_layer-f8bd7cfdb9ed7ec99d612dd03670e50aa0c60db2.zip |
Refactor testing
-rw-r--r--[-rwxr-xr-x] | graphics.c | 70 | ||||
-rwxr-xr-x | run_tests.c | 9 | ||||
-rw-r--r--[-rwxr-xr-x] | runtime.c | 501 | ||||
-rw-r--r--[-rwxr-xr-x] | stackless_coroutine.c | 77 |
4 files changed, 281 insertions, 376 deletions
diff --git a/graphics.c b/graphics.c index 52c0432..f4bc699 100755..100644 --- a/graphics.c +++ b/graphics.c @@ -1,50 +1,18 @@ -#if 0 /* -#/ ================================================================ -#/ -#/ graphics.c -#/ -#/ ---------------------------------------------------------------- -#/ -#/ (C) 2025 Mitya Selivanov <guattari.tech> -#/ -#/ Any use of this code is prohibited. -#/ -#/ ================================================================ -#/ -#/ Self-testing shell script -#/ -#/ ================================================================ - -SRC=${0##*./} -BIN=${SRC%.*} -gcc \ - -Wall -Wextra -Werror -pedantic \ - -Wno-missing-braces \ - -Wno-old-style-declaration \ - -Wno-overlength-strings \ - -Wno-unused-parameter \ - -Wno-unused-variable \ - -Wno-unused-but-set-variable \ - -O3 -D NDEBUG \ - -D ENABLE_WAYLAND=0 \ - -D GRAPHICS_TEST_SUITE \ - -lX11 -lm -lasound \ - -o $BIN $SRC && \ - ./$BIN $@ -STATUS=$? -rm -f $BIN -exit $STATUS # */ -#endif - +// ================================================================ +// +// graphics.c +// +// ---------------------------------------------------------------- +// +// (C) 2025 Mitya Selivanov <guattari.tech> +// +// Any use of this code is prohibited. +// // ================================================================ #ifndef GRAPHICS_HEADER_GUARD_ #define GRAPHICS_HEADER_GUARD_ -#ifdef EVERY_TEST_SUITE -#define GRAPHICS_TEST_SUITE -#endif - #ifdef GRAPHICS_HEADER #define RUNTIME_HEADER #endif @@ -853,6 +821,8 @@ void font_render_to_buffer_dispatch(Pixel_Buffer dst, i32 font, vec4_f32 color, #endif // !defined(GRAPHICS_ENABLE_FONT_CUSTOM_DISPATCH) void draw_text_area_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text) { + (void) font; + if (text == NULL || num_chars <= 0 || max_size.x < EPSILON || max_size.y < EPSILON) return; @@ -878,6 +848,8 @@ void draw_text_area_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box ar } void draw_text_cursor_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text, i64 cursor, i64 selection) { + (void) font; + if (max_size.x < EPSILON || max_size.y < EPSILON) return; @@ -2168,11 +2140,11 @@ void clean_graphics_requests_cache(i64 amount) { // ================================================================ // -// Test suite +// Test suite // // ================================================================ -#ifdef GRAPHICS_TEST_SUITE +#if ENABLE_TESTING #define TEST_FILE graphics #include "test.c" @@ -2572,16 +2544,8 @@ TEST("text digits 42") { REQUIRE(ok); } -#ifndef EVERY_TEST_SUITE -void update_and_render_frame(void) { } - -i32 main(i32 argc, c8 **argv) { - return run_tests(argc, argv); -} -#endif - #undef TEST_FILE -#endif // GRAPHICS_TEST_SUITE +#endif // ENABLE_TESTING // ================================================================ diff --git a/run_tests.c b/run_tests.c index 43898da..0729304 100755 --- a/run_tests.c +++ b/run_tests.c @@ -11,8 +11,8 @@ gcc \ -Wno-unused-but-set-variable \ -Wno-unused-function \ -O3 -D NDEBUG \ - -fsanitize=undefined,address,leak \ -lm \ + -fsanitize=undefined,address,leak \ -o $BIN $SRC && \ ./$BIN $@ STATUS=$? @@ -20,10 +20,7 @@ rm -f $BIN exit $STATUS # */ #endif -#ifndef EVERY_TEST_SUITE -#define EVERY_TEST_SUITE -#endif - +#define ENABLE_TESTING 1 #define ENABLE_X11 0 #define ENABLE_WAYLAND 0 #define ENABLE_ALSA 0 @@ -32,6 +29,8 @@ exit $STATUS # */ #include "graphics.c" #include "stackless_coroutine.c" +void update_and_render_frame(void) { } + i32 main(i32 argc, c8 **argv) { return run_tests(argc, argv); } diff --git a/runtime.c b/runtime.c index 03a50eb..4ba5c15 100755..100644 --- a/runtime.c +++ b/runtime.c @@ -1,270 +1,178 @@ -#if 0 /* -#/ ================================================================ -#/ -#/ runtime.c -#/ -#/ This is a reduced system layer. -#/ It allows you to create a window, draw graphics in it, handle -#/ input events, write samples to audio output, send and receive -#/ UDP packets, etc. All code is single-threaded. -#/ -#/ Primary target platforms: Linux (X11), Windows, Web. -#/ -#/ ---------------------------------------------------------------- -#/ -#/ DESIGN PRINCIPLES -#/ -#/ - Minimalistic feature set. For graphics, you have access to the -#/ pixel buffer, and that's it. -#/ -#/ - No implicit control flow. No callbacks. You write your own -#/ main and call everything explicitly. But the number of things -#/ you have to call to do something is as little as possible. -#/ -#/ - Optimized to use in a single source file. -#/ Installation process? Ctrl+C, Ctrl+V, done. -#/ -#/ STYLE CONVENTIONS -#/ -#/ - Pascal_Snake_Case - Type name. -#/ - snake_case - Non-type name. -#/ - UPPER_SNAKE_CASE - Macro or constant. -#/ -#/ - g_ prefix - Global variable name. -#/ - _ prefix - Name of a global variable that is not part of the user API. -#/ - _ suffix - Name of a procedure that is not part of the user API. -#/ -#/ Most procedures have long and descriptive names. -#/ Some procedures have prefixes according to their domain. -#/ -#/ There may be exceptions if convenient. -#/ -#/ ---------------------------------------------------------------- -#/ -#/ To-Do list -#/ -#/ - Work in progress -#/ - Graphics perf - request cache -#/ Requires: -#/ + [ ] Graphics tests -#/ + [x] Graphics requests -#/ + [x] Memory buffer allocator -#/ + [x] Blake2 hash -#/ + [x] Requests cache -#/ + [x] Global anti-aliasing -#/ + [x] Fill triangles -#/ - Examples -#/ - Conway's Game of Life -#/ - Julia Set -#/ - Labyrinth -#/ - Chat -#/ - Graphics -#/ - Gamma correction -#/ - Custom fonts - integrate stb_truetype.h -#/ - UI -#/ - Icons -#/ - Folder widget -#/ - Clipboard -#/ - Images - BMP, PPM -#/ - Sound - WAV -#/ - Clipboard -#/ - Images - BMP, PPM -#/ - Sound - WAV -#/ - Dynamic libraries - load dependencies conditionally -#/ - X11 -#/ - Wayland -#/ - ALSA -#/ - Sockets -#/ - Windows -#/ - Cross-platform networking - UDP + TCP + WebSocket -#/ Requires: -#/ - [ ] Sockets - UDP, TCP -#/ - [ ] HTTP client -#/ - [ ] HTTP server -#/ - [ ] Web sockets -#/ - [ ] Key exchange -#/ - [ ] Cipher suite - TLS_AES_128_GCM_SHA256 -#/ - [ ] TLS -#/ - [ ] Web sockets over TLS -#/ - Long term -#/ - Utility -#/ - Improve microbenchmarks library -#/ - Parsing -#/ - Printing -#/ - Logging -#/ - Terminal colors -#/ - Big integer -#/ - Mersenne Twister 64 -#/ - Arithmetic coding -#/ - A* search -#/ - Graphics -#/ - Vector math -#/ - Bezier curves -#/ - CMYK color -#/ - Textures -#/ - Vulkan boilerplate -#/ - System -#/ - Window -#/ - Windows graphics -#/ - Wayland -#/ - Sound -#/ - Windows audio -#/ - Recording -#/ - Device selection -#/ - Networking -#/ - Windows sockets -#/ - fetch - via libcurl on native platforms -#/ - Lattice-based cipher suite -#/ - Switching canvas - Web -#/ - File system -#/ - Secure random -#/ - Process -#/ - Shared memory -#/ - Shared mutex -#/ - Threads - https://nachtimwald.com/2019/04/05/cross-platform-thread-wrapper -#/ - Cryptography - https://github.com/jedisct1/supercop -#/ - macOS support -#/ - Mobile devices support -#/ -#/ Done -#/ -#/ - Examples -#/ - Echo -#/ - UI -#/ - Particles -#/ - Graph -#/ - Sine Wave -#/ - Utility -#/ - UTF-8 -#/ - Testing -#/ - Stackless coroutines -#/ - Allocator -#/ - Profiling -#/ - Graphics -#/ - Font -#/ - Adaptive resolution -#/ - Oklab color -#/ - Relative coordinates -#/ - Alpha blending -#/ - Self-contained impl -#/ - Anti-aliasing -#/ - System -#/ - Window - X11, Web -#/ - Screenshot - X11, Wayland -#/ - Clipboard -#/ - Text - X11, Web -#/ - Sound - ALSA, Web -#/ - Networking -#/ - Unix UDP sockets -#/ - Drop files - X11, Web -#/ -#/ ---------------------------------------------------------------- -#/ -#/ (C) 2025 Mitya Selivanov <guattari.tech> -#/ -#/ Any use of this code is prohibited. -#/ -#/ ================================================================ -#/ -#/ Self-testing shell script -#/ -#/ ================================================================ - -SRC=${0##*./} -BIN=${SRC%.*} -gcc \ - -Wall -Wextra -Werror -pedantic \ - -Wno-missing-braces \ - -Wno-old-style-declaration \ - -Wno-overlength-strings \ - -Wno-unused-parameter \ - -Wno-unused-variable \ - -Wno-unused-but-set-variable \ - -O3 -D NDEBUG \ - -fsanitize=undefined,address,leak \ - -D RUNTIME_TEST_SUITE \ - -lm -lX11 -lasound \ - -lwayland-client \ - -o $BIN $SRC && \ - ./$BIN $@ -STATUS=$? -rm -f $BIN -exit $STATUS # */ -#endif - // ================================================================ // -// Types +// runtime.c // -// ================================================================ - -#ifndef TYPES_HEADER_GUARD_ -#define TYPES_HEADER_GUARD_ - -typedef signed char i8; -typedef signed short i16; -typedef signed i32; -typedef signed long long i64; -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned u32; -typedef unsigned long long u64; -typedef char c8; -typedef int c32; -typedef unsigned char b8; -typedef float f32; -typedef double f64; - -typedef union { struct { f64 v[ 2]; }; struct { f64 x, y; }; } vec2; -typedef union { struct { f64 v[ 3]; }; struct { f64 x, y, z; }; } vec3; -typedef union { struct { f64 v[ 4]; }; struct { f64 x, y, z, w; }; } vec4; -typedef union { struct { f32 v[ 2]; }; struct { f32 x, y; }; } vec2_f32; -typedef union { struct { f32 v[ 3]; }; struct { f32 x, y, z; }; } vec3_f32; -typedef union { struct { f32 v[ 4]; }; struct { f32 x, y, z, w; }; } vec4_f32; -typedef union { struct { i64 v[ 2]; }; struct { i64 x, y; }; } vec2_i64; -typedef union { struct { i64 v[ 3]; }; struct { i64 x, y, z; }; } vec3_i64; -typedef union { struct { i64 v[ 4]; }; struct { i64 x, y, z, w; }; } vec4_i64; -typedef union { struct { f64 v[ 4]; }; struct { f64 m[2][2]; }; } mat2; -typedef union { struct { f64 v[ 9]; }; struct { f64 m[3][3]; }; } mat3; -typedef union { struct { f64 v[16]; }; struct { f64 m[4][4]; }; } mat4; -typedef union { struct { f32 v[ 4]; }; struct { f32 m[2][2]; }; } mat2_f32; -typedef union { struct { f32 v[ 9]; }; struct { f32 m[3][3]; }; } mat3_f32; -typedef union { struct { f32 v[16]; }; struct { f32 m[4][4]; }; } mat4_f32; - -#endif // TYPES_HEADER_GUARD_ - -// ================================================================ - -#ifndef RUNTIME_HEADER_GUARD_ -#define RUNTIME_HEADER_GUARD_ - -#ifdef EVERY_TEST_SUITE -#define RUNTIME_TEST_SUITE -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -// ================================================================ +// This is a reduced system layer. +// It allows you to create a window, draw graphics in it, handle +// input events, write samples to audio output, send and receive +// UDP packets, etc. All code is single-threaded. // -// User program interface +// Primary target platforms: Linux (X11), Windows, Web. +// +// ---------------------------------------------------------------- +// +// DESIGN PRINCIPLES +// +// - Minimalistic feature set. For graphics, you have access to the +// pixel buffer, and that's it. +// +// - No implicit control flow. No callbacks. You write your own +// main and call everything explicitly. But the number of things +// you have to call to do something is as little as possible. +// +// - Optimized to use in a single source file. +// Installation process? Ctrl+C, Ctrl+V, done. +// +// STYLE CONVENTIONS +// +// - Pascal_Snake_Case - Type name. +// - snake_case - Non-type name. +// - UPPER_SNAKE_CASE - Macro or constant. +// +// - g_ prefix - Global variable name. +// - _ prefix - Name of a global variable that is not part of the user API. +// - _ suffix - Name of a procedure that is not part of the user API. +// +// Most procedures have long and descriptive names. +// Some procedures have prefixes according to their domain. +// +// There may be exceptions if convenient. +// +// ---------------------------------------------------------------- +// +// To-Do list +// +// - Work in progress +// - Graphics perf - request cache +// Requires: +// + [ ] Graphics tests +// + [x] Graphics requests +// + [x] Memory buffer allocator +// + [x] Blake2 hash +// + [x] Requests cache +// + [x] Global anti-aliasing +// + [x] Fill triangles +// - Examples +// - Conway's Game of Life +// - Julia Set +// - Labyrinth +// - Chat +// - Graphics +// - Gamma correction +// - Custom fonts - integrate stb_truetype.h +// - UI +// - Icons +// - Folder widget +// - Clipboard +// - Images - BMP, PPM +// - Sound - WAV +// - Clipboard +// - Images - BMP, PPM +// - Sound - WAV +// - Dynamic libraries - load dependencies conditionally +// - X11 +// - Wayland +// - ALSA +// - Sockets +// - Windows +// - Cross-platform networking - UDP + TCP + WebSocket +// Requires: +// - [ ] Sockets - UDP, TCP +// - [ ] HTTP client +// - [ ] HTTP server +// - [ ] Web sockets +// - [ ] Key exchange +// - [ ] Cipher suite - TLS_AES_128_GCM_SHA256 +// - [ ] TLS +// - [ ] Web sockets over TLS +// - Long term +// - Utility +// - Improve microbenchmarks library +// - Parsing +// - Printing +// - Logging +// - Terminal colors +// - Big integer +// - Mersenne Twister 64 +// - Arithmetic coding +// - A* search +// - Graphics +// - Vector math +// - Bezier curves +// - CMYK color +// - Textures +// - Vulkan boilerplate +// - System +// - Window +// - Windows graphics +// - Wayland +// - Sound +// - Windows audio +// - Recording +// - Device selection +// - Networking +// - Windows sockets +// - fetch - via libcurl on native platforms +// - Lattice-based cipher suite +// - Switching canvas - Web +// - File system +// - Secure random +// - Process +// - Shared memory +// - Shared mutex +// - Threads - https://nachtimwald.com/2019/04/05/cross-platform-thread-wrapper +// - Cryptography - https://github.com/jedisct1/supercop +// - macOS support +// - Mobile devices support +// +// Done +// +// - Examples +// - Echo +// - UI +// - Particles +// - Graph +// - Sine Wave +// - Utility +// - UTF-8 +// - Testing +// - Stackless coroutines +// - Allocator +// - Profiling +// - Graphics +// - Font +// - Adaptive resolution +// - Oklab color +// - Relative coordinates +// - Alpha blending +// - Self-contained impl +// - Anti-aliasing +// - System +// - Window - X11, Web +// - Screenshot - X11, Wayland +// - Clipboard +// - Text - X11, Web +// - Sound - ALSA, Web +// - Networking +// - Unix UDP sockets +// - Drop files - X11, Web +// +// ---------------------------------------------------------------- +// +// (C) 2025 Mitya Selivanov <guattari.tech> +// +// Any use of this code is prohibited. // -// ================================================================ - -// NOTE: This procedure is required for the Web compatibility. -void update_and_render_frame(void); - -#if defined(__wasm__) -i32 main(i32 argc, c8 **argv); -#endif - // ================================================================ // // Options // // ================================================================ +#ifndef ENABLE_TESTING +#define ENABLE_TESTING 0 +#endif + #ifndef ENABLE_WAYLAND #define ENABLE_WAYLAND 1 #endif @@ -359,6 +267,71 @@ i32 main(i32 argc, c8 **argv); // ================================================================ // +// Types +// +// ================================================================ + +#ifndef TYPES_HEADER_GUARD_ +#define TYPES_HEADER_GUARD_ + +// NOTE: Simple types can be redefined. + +typedef signed char i8; +typedef signed short i16; +typedef signed i32; +typedef signed long long i64; +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned u32; +typedef unsigned long long u64; +typedef char c8; +typedef int c32; +typedef unsigned char b8; +typedef float f32; +typedef double f64; + +typedef union { struct { f64 v[ 2]; }; struct { f64 x, y; }; } vec2; +typedef union { struct { f64 v[ 3]; }; struct { f64 x, y, z; }; } vec3; +typedef union { struct { f64 v[ 4]; }; struct { f64 x, y, z, w; }; } vec4; +typedef union { struct { f32 v[ 2]; }; struct { f32 x, y; }; } vec2_f32; +typedef union { struct { f32 v[ 3]; }; struct { f32 x, y, z; }; } vec3_f32; +typedef union { struct { f32 v[ 4]; }; struct { f32 x, y, z, w; }; } vec4_f32; +typedef union { struct { i64 v[ 2]; }; struct { i64 x, y; }; } vec2_i64; +typedef union { struct { i64 v[ 3]; }; struct { i64 x, y, z; }; } vec3_i64; +typedef union { struct { i64 v[ 4]; }; struct { i64 x, y, z, w; }; } vec4_i64; +typedef union { struct { f64 v[ 4]; }; struct { f64 m[2][2]; }; } mat2; +typedef union { struct { f64 v[ 9]; }; struct { f64 m[3][3]; }; } mat3; +typedef union { struct { f64 v[16]; }; struct { f64 m[4][4]; }; } mat4; +typedef union { struct { f32 v[ 4]; }; struct { f32 m[2][2]; }; } mat2_f32; +typedef union { struct { f32 v[ 9]; }; struct { f32 m[3][3]; }; } mat3_f32; +typedef union { struct { f32 v[16]; }; struct { f32 m[4][4]; }; } mat4_f32; + +#endif // TYPES_HEADER_GUARD_ + +// ================================================================ + +#ifndef RUNTIME_HEADER_GUARD_ +#define RUNTIME_HEADER_GUARD_ + +#ifdef __cplusplus +extern "C" { +#endif + +// ================================================================ +// +// User program interface +// +// ================================================================ + +// NOTE: This procedure is required for the Web compatibility. +void update_and_render_frame(void); + +#if defined(__wasm__) +i32 main(i32 argc, c8 **argv); +#endif + +// ================================================================ +// // Basic declarations // // ================================================================ @@ -4670,10 +4643,6 @@ void init_main_window(void) { LOG_error("Not implemented."); } -void update_and_render_frame(void) { - LOG_error("Not implemented."); -} - void shutdown_all_systems(void) { // TODO: Factor out common code. @@ -4692,7 +4661,7 @@ void shutdown_all_systems(void) { // // ================================================================ -#ifdef RUNTIME_TEST_SUITE +#if ENABLE_TESTING #define TEST_FILE runtime #include "test.c" @@ -4981,16 +4950,8 @@ TEST("average frame duration") { REQUIRE_EQ(average_frame_duration_(100), 100); } -#ifndef EVERY_TEST_SUITE -void update_and_render_frame(void) {} - -i32 main(i32 argc, c8 **argv) { - return run_tests(argc, argv); -} -#endif - #undef TEST_FILE -#endif // RUNTIME_TEST_SUITE +#endif // ENABLE_TESTING // ================================================================ diff --git a/stackless_coroutine.c b/stackless_coroutine.c index cb169e1..73a55e4 100755..100644 --- a/stackless_coroutine.c +++ b/stackless_coroutine.c @@ -1,51 +1,33 @@ -#if 0 /* -#/ ================================================================ -#/ -#/ stackless_coroutine.c -#/ -#/ ---------------------------------------------------------------- -#/ -#/ (C) 2025 Mitya Selivanov <guattari.tech> -#/ -#/ Any use of this code is prohibited. -#/ -#/ ================================================================ -#/ -#/ Self-testing shell script -#/ -#/ ================================================================ -SRC=${0##*./} -BIN=${SRC%.*} -gcc \ - -Wall -Wextra -Werror -pedantic \ - -Wno-missing-braces \ - -Wno-old-style-declaration \ - -Wno-overlength-strings \ - -Wno-unused-parameter \ - -Wno-unused-variable \ - -Wno-unused-but-set-variable \ - -O3 -D NDEBUG \ - -D STACKLESS_COROUTINE_TEST_SUITE \ - -o $BIN $SRC && \ - ./$BIN $@ -STATUS=$? -rm -f $BIN -exit $STATUS # */ -#endif // ================================================================ - -typedef signed i32; -typedef unsigned char b8; - +// +// stackless_coroutine.c +// +// ---------------------------------------------------------------- +// +// (C) 2025 Mitya Selivanov <guattari.tech> +// +// Any use of this code is prohibited. +// // ================================================================ #ifndef STACKLESS_COROUTINE_HEADER_GUARD_ #define STACKLESS_COROUTINE_HEADER_GUARD_ -#ifdef EVERY_TEST_SUITE -#define STACKLESS_COROUTINE_TEST_SUITE +// ================================================================ +// +// Options +// +// ================================================================ + +#ifndef ENABLE_TESTING +#define ENABLE_TESTING 0 #endif +// ================================================================ + +typedef signed i32; +typedef unsigned char b8; + #ifndef NULL #define NULL ((void *) 0) #endif @@ -251,12 +233,16 @@ static void stackless_coroutine_dispatch(void *promise) { #endif // STACKLESS_COROUTINE_HEADER_GUARD_ // ================================================================ +// +// Test suite +// +// ================================================================ #ifndef STACKLESS_COROUTINE_HEADER #ifndef STACKLESS_COROUTINE_IMPL_GUARD_ #define STACKLESS_COROUTINE_IMPL_GUARD_ -#ifdef STACKLESS_COROUTINE_TEST_SUITE +#if ENABLE_TESTING #define TEST_FILE stackless_coroutine #include "test.c" @@ -403,13 +389,8 @@ TEST("coroutine nested generator") { REQUIRE(coro_finished(promise)); } -#ifndef EVERY_TEST_SUITE -i32 main(i32 argc, c8 **argv) { - return run_tests(argc, argv); -} -#endif - -#endif // STACKLESS_COROUTINE_TEST_SUITE +#undef TEST_FILE +#endif // ENABLE_TESTING #endif // STACKLESS_COROUTINE_IMPL_GUARD_ #endif // STACKLESS_COROUTINE_HEADER |