From f8bd7cfdb9ed7ec99d612dd03670e50aa0c60db2 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Thu, 24 Apr 2025 02:58:57 +0200 Subject: Refactor testing --- runtime.c | 501 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 231 insertions(+), 270 deletions(-) mode change 100755 => 100644 runtime.c (limited to 'runtime.c') diff --git a/runtime.c b/runtime.c old mode 100755 new mode 100644 index 03a50eb..4ba5c15 --- 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 -#/ -#/ 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 +// +// 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 @@ -357,6 +265,71 @@ i32 main(i32 argc, c8 **argv); #define MOUSE_WHEEL_FACTOR (1.0) #endif +// ================================================================ +// +// 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 // ================================================================ -- cgit v1.2.3