Files
ci2/tests/03_ci2_exception.c
2026-05-12 20:32:13 -05:00

81 lines
2.6 KiB
C

/* - | 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 <stdlib.h>
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(int argc, ci2_sys_char *argv[])
{
UNUSED(argc);
UNUSED(argv);
int result = -1;
result = test_uncaught_exception();
result = test_caught_exception();
return result;
}