diff --git a/include/ci2.h b/include/ci2.h index 938d22e..585a094 100644 --- a/include/ci2.h +++ b/include/ci2.h @@ -26,6 +26,7 @@ SOFTWARE. #include "ci2_window.h" // Includes base #include "ci2_exception.h" #include "ci2_mem.h" +/* - | COMMENT | ------------------------------------------------------------*/ #ifdef CI2_WINDOWS #ifdef CI2_GUI @@ -46,6 +47,5 @@ SOFTWARE. #endif - #endif // ci2.h diff --git a/tests/01_ci2_static_assert.c b/tests/01_ci2_static_assert.c new file mode 100644 index 0000000..4255c79 --- /dev/null +++ b/tests/01_ci2_static_assert.c @@ -0,0 +1,57 @@ +/* - | Copyright | -------------------------------------------------------------- + Copyright (c) 2026 Randy Jordan + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + * --------------------------------------------------------------------------*/ +#define DEBUG +#include "../include/ci2.h" +#include +#include /* INT_MAX, CHAR_BIT — C standard */ +#include /* FLT_MANT_DIG — C standard */ + +int main(void){ + /* Fundamental type sizes that almost all C code silently assumes. */ + CI2_STATIC_ASSERT(CHAR_BIT == 8); + CI2_STATIC_ASSERT(sizeof(char) == 1); + CI2_STATIC_ASSERT(sizeof(short) == 2); + CI2_STATIC_ASSERT(sizeof(int) == 4); + CI2_STATIC_ASSERT(sizeof(float) == 4); + CI2_STATIC_ASSERT(sizeof(double) == 8); + + /* Pointer and size_t width — catches ILP64 oddities. */ + CI2_STATIC_ASSERT(sizeof(void *) == sizeof(size_t)); + + /* Fixed-width types from */ + CI2_STATIC_ASSERT(sizeof(int8_t) == 1); + CI2_STATIC_ASSERT(sizeof(int16_t) == 2); + CI2_STATIC_ASSERT(sizeof(int32_t) == 4); + CI2_STATIC_ASSERT(sizeof(int64_t) == 8); + + CI2_STATIC_ASSERT(sizeof(int8_t) == 1); + CI2_STATIC_ASSERT(sizeof(int16_t) == 2); + CI2_STATIC_ASSERT(sizeof(int32_t) == 4); + CI2_STATIC_ASSERT(sizeof(int64_t) == 8); + + /* IEEE-754 single precision: 24 significand bits. */ + CI2_STATIC_ASSERT(FLT_MANT_DIG == 24); + + return EXIT_SUCCESS; +} + diff --git a/tests/02_ci2_assert.c b/tests/02_ci2_assert.c new file mode 100644 index 0000000..262e61c --- /dev/null +++ b/tests/02_ci2_assert.c @@ -0,0 +1,72 @@ +/* - | Copyright | -------------------------------------------------------------- + Copyright (c) 2026 Randy Jordan + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + * --------------------------------------------------------------------------*/ +#define DEBUG +#include "../include/ci2.h" +#include +#include + +static int test_assert_failing(void){ +#ifdef CI2_ASSERT_CRASH + printf("ASSERT_MSG — Deliberate Failure (expect a debug break)"); + printf(" Triggering ASSERT_MSG(0, ...) now...\n"); + fflush(stdout); + + CI2_ASSERT_MSG(0, "This failure is intentional — testing the message path"); + + printf(" [FAIL] Execution continued past a failing ASSERT_MSG!\n"); + return EXIT_FAILURE; +#else + return EXIT_SUCCESS; +#endif +} + +int main(void) +{ + int result = -1; + CI2_ASSERT(ci2_true); + + CI2_ASSERT(1); + CI2_ASSERT(1 == 1); + + /* Pointer non-null */ + const char *str = "hello"; + CI2_ASSERT(str != NULL); + + /* Arithmetic */ + int x = 42; + CI2_ASSERT(x > 0); + CI2_ASSERT(x * 2 == 84); + + /* CI2_ASSERT_MSG variants */ + CI2_ASSERT_MSG(x == 42, "x must be 42"); + CI2_ASSERT_MSG(sizeof(int) == 4, "int must be 4 bytes on this platform"); + + /* Boolean */ + bool flag = true; + CI2_ASSERT(flag); + + result = test_assert_failing(); + + return result; +} + diff --git a/tests/03_ci2_exception.c b/tests/03_ci2_exception.c new file mode 100644 index 0000000..ff5da2c --- /dev/null +++ b/tests/03_ci2_exception.c @@ -0,0 +1,77 @@ +/* - | Copyright | -------------------------------------------------------------- + Copyright (c) 2026 Randy Jordan + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + * --------------------------------------------------------------------------*/ +#define DEBUG +#define GUI_DEBUG_CLOSE + +#include "../include/ci2.h" +#include + +static int test_error_num; +const struct CI2_Exception e = { EOWNERDEAD , "Owner died"}; + +static void test_exception(){ + test_error_num = EOWNERDEAD; // Oops better revive them. + CI2_RAISE(e); +} +static int test_caught_exception(){ + CI2_TRY { + printf("Trying to catch a failure.\n"); + // Oops + test_exception(); + } + CI2_EXCEPT(e) { + /* handle error failure gracefully */ + fprintf(stderr, "Caught: %d: %s.\n", e.num, e.msg); + test_error_num = 0; + } + CI2_FINALLY { + /* Runs no matter what */ + printf("Hope we caught something.\n"); + } + CI2_END_TRY; + + return test_error_num; +} +static int test_uncaught_exception(void){ +#ifdef CI2_EXCEPTION_CRASH + printf("CI2_MSG — Deliberate Failure (expect a debug break)\n"); + printf(" Triggering ci2_raise_exception() now...\n"); + fflush(stdout); + + struct CI2_Exception u = { EFAULT, "Deliberate uncaught exception test."}; + CI2_RAISE(u); + printf(" [FAIL] Execution continued past a failing ASSERT_MSG!\n"); + return EXIT_FAILURE; // Shouldn't but still. +#endif + return EXIT_SUCCESS; +} + +int main(void) +{ + int result = -1; + result = test_uncaught_exception(); + result = test_caught_exception(); + + return result; +} + diff --git a/tests/04_ci2_memory_exception.c b/tests/04_ci2_memory_exception.c new file mode 100644 index 0000000..240f1e6 --- /dev/null +++ b/tests/04_ci2_memory_exception.c @@ -0,0 +1,80 @@ +/* - | Copyright | -------------------------------------------------------------- + Copyright (c) 2026 Randy Jordan + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + * --------------------------------------------------------------------------*/ +#define DEBUG +#define GUI_DEBUG_CLOSE +#include "../include/ci2.h" +#include +#include + +static int test_caught_exception(){ + size_t nbytes = CI2_GB(1024); + void *buf = NULL; + const char *str = "Hello World"; + buf = CI2_CALLOC(1,strlen(str+1)); /* You have no enable asan options. */ + strncpy(buf,str,strlen(str)); + + char *opts = "ASAN_OPTIONS=allocator_may_fail=1"; + + printf("Attempting oom re-allocation of %ld at %p\n", nbytes, buf); + printf("Adding %s to env....%d\n",opts, putenv(opts)); + + CI2_TRY { + printf("Trying something dumb.\n"); + CI2_RESIZE(buf, nbytes); + } + CI2_EXCEPT(oom) { + /* handle memory failure gracefully */ + printf("Caught: %d: %s\n", oom.num, oom.msg); + return EXIT_SUCCESS; /* requested behavior */ + } + CI2_FINALLY { + /* cleanup if needed, runs always */ + printf("Have to free allocation of %ld at %p\n",strlen(buf), buf ); + if (buf) free(buf); + } + CI2_END_TRY; + return EXIT_FAILURE; +} +static int test_uncaught_exception(void){ +#ifdef CI2_EXCEPTION_CRASH + printf("CI2_MSG — Deliberate Failure (expect a debug break)\n"); + printf(" Triggering ci2_raise_exception() now...\n"); + fflush(stdout); + + struct CI2_Exception u = { EFAULT, "Deliberate uncaught exception test."}; + CI2_RAISE(u); + printf(" [FAIL] Execution continued past a failing ASSERT_MSG!\n"); + return EXIT_FAILURE; // Shouldn't but still. +#endif + return EXIT_SUCCESS; +} + +int main(void) +{ + int result = -1; + result = test_uncaught_exception(); + result = test_caught_exception(); + + return result; +} + diff --git a/tests/01_main.c b/tests/99_main.c similarity index 94% rename from tests/01_main.c rename to tests/99_main.c index 4355dc4..0da81b9 100644 --- a/tests/01_main.c +++ b/tests/99_main.c @@ -45,11 +45,13 @@ SOFTWARE. #else // NOT CI2_WINDOWS #ifdef CI2_GUI - int main(int argc, ci2_sys_char *argv[]){ + int main(int argc, ci2_sys_char *argv[]) + { return ci2_gui(argc, argv); } #else - int main(int argc, ci2_sys_char *argv[]){ + int main(int argc, ci2_sys_char *argv[]) + { return ci2_cli(argc,argv); } #endif