diff options
| author | Mosder <xmosder12@gmail.com> | 2026-08-21 14:41:26 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-08-21 14:41:26 +0200 |
| commit | 06954da1e847072f9e95df0f183d9327a0f05b5b (patch) | |
| tree | bd10a6e5e960dc73d11de0b91ae73d8b6fb1a1a0 /include | |
| parent | b6b7a01987dca604a161c8c73a240ec4430baed0 (diff) | |
feat: added some for safe wrappers + added ability to capture and test whether output is what was expected
Diffstat (limited to 'include')
| -rw-r--r-- | include/moslib/safe.h | 13 | ||||
| -rw-r--r-- | include/moslib/tester.h | 34 |
2 files changed, 43 insertions, 4 deletions
diff --git a/include/moslib/safe.h b/include/moslib/safe.h index 8074c31..18ab5b0 100644 --- a/include/moslib/safe.h +++ b/include/moslib/safe.h @@ -3,6 +3,7 @@ // aka "I don't want to check for NULL every single time" header #include <stddef.h> +#include <stdio.h> #ifndef MOSLIB_SAFE_H #define MOSLIB_SAFE_H @@ -12,6 +13,10 @@ #define safe_malloc mos_safe_malloc #define safe_calloc mos_safe_calloc #define safe_realloc mos_safe_realloc +#define safe_fopen mos_safe_fopen +#define safe_tmpfile mos_safe_tmpfile +#define safe_dup mos_safe_dup +#define safe_dup2 mos_safe_dup2 #endif // MOS_FORCE_PREFIXES @@ -28,10 +33,18 @@ typedef struct { #define mos_safe_malloc(size) mos_safe_malloc_fn(size, mos_err_info) #define mos_safe_calloc(count, size) mos_safe_calloc_fn(count, size, mos_err_info) #define mos_safe_realloc(ptr, size) mos_safe_realloc_fn(ptr, size, mos_err_info) +#define mos_safe_fopen(path, mode) mos_safe_fopen_fn(path, mode, mos_err_info) +#define mos_safe_tmpfile() mos_safe_tmpfile_fn(mos_err_info) +#define mos_safe_dup(fd) mos_safe_dup_fn(fd, mos_err_info) +#define mos_safe_dup2(fd, fd2) mos_safe_dup2_fn(fd, fd2, mos_err_info) // Function prototypes for macros extern void *mos_safe_malloc_fn(size_t size, MosSafeErrInfo info); extern void *mos_safe_calloc_fn(size_t count, size_t size, MosSafeErrInfo info); extern void *mos_safe_realloc_fn(void *ptr, size_t size, MosSafeErrInfo info); +extern FILE *mos_safe_fopen_fn(const char *path, const char *mode, MosSafeErrInfo info); +extern FILE *mos_safe_tmpfile_fn(MosSafeErrInfo info); +extern int mos_safe_dup_fn(int fd, MosSafeErrInfo info); +extern int mos_safe_dup2_fn(int fd, int fd2, MosSafeErrInfo info); #endif // MOSLIB_SAFE_H diff --git a/include/moslib/tester.h b/include/moslib/tester.h index 9423684..a522b9d 100644 --- a/include/moslib/tester.h +++ b/include/moslib/tester.h @@ -46,12 +46,23 @@ // test_assert_exit(helper_test, EXIT_FAILURE); // } // +// You can also test if a function writes correct output. +// You need to create a helper test and pass it into test_assert_out in main test. +// Pass the helper test, output stream, and expected output to it. +// Example: +// Test helper_test() { +// printf("output"); +// } +// Test main_test() { +// test_assert_out(helper_test, stdout, "output"); +// } +// // If your functions have output and you want to suppress them for the sake of tests, // you can do so using suppress_output and unsupress_outputs. // Example: // Test error_test() { -// suppress_output(&stdout); -// suppress_output(&stderr); +// suppress_output(stdout); +// suppress_output(stderr); // int code = fun_that_fails_with_output(); // unsuppress_outputs(); // test_assert(code == 0, "function didn't fail"); @@ -93,6 +104,7 @@ #define add_test_group mos_add_test_group #define test_assert mos_test_assert #define test_assert_exit mos_test_assert_exit +#define test_assert_out mos_test_assert_out #define suppress_output mos_suppress_output #define unsuppress_outputs mos_unsuppress_outputs #define add_test mos_add_test @@ -148,12 +160,25 @@ extern void mos_test_assert(int expression, const char *fail_message); // expected exit code #define mos_test_assert_exit(function, code) mos_test_assert_exit_fn(function, code, #function); +// Assert that function writes correct output to specified stream +// +// Arguments: +// function +// function that is supposed to write output +// +// stream +// stream to write the output to +// +// output +// expected output of the function +#define mos_test_assert_out(function, stream, output) mos_test_assert_out_fn(function, stream, output, #function, #stream); + // Suppress given output // // Arguments: // out -// pointer to outpout to suppress -extern void mos_suppress_output(FILE **out); +// outpout to suppress +extern void mos_suppress_output(FILE *out); // Unuppress previously suppressed outputs // Will unsupress all of the outputs that were suppressed @@ -189,6 +214,7 @@ extern void mos_free_tester(MosTester *tester); // Function prototypes for macros extern MosTestGroup *mos_add_test_group_fn(MosTester *tester, const char *group_name); extern void mos_test_assert_exit_fn(MosTestFn function, int code, const char *name); +extern void mos_test_assert_out_fn(MosTestFn function, FILE *stream, const char *output, const char *fn_name, const char *stream_name); extern void mos_add_test_fn(MosTestGroup *group, MosTestFn test, const char *name); #endif // MOSLIB_TESTER_H |
