DISTFILES = atf.h LICENSE.md Makefile README.md tests.c
tests: tests.c
+ @echo "============================================"
+ @echo "NOTE: It is expected that 3 tests will fail."
+ @echo "============================================"
$(CC) -I. -o $@ $^
-./$@
#include <stddef.h>
#include <stdbool.h>
+#include <setjmp.h>
+#include <assert.h>
extern char* Curr_Test;
void atf_init(int argc, char** argv);
#define CHECK(expr) \
if(atf_test_assert((expr), #expr, __FILE__, __LINE__)) break
+
+#define CHECK_EXITCODE(code) \
+ CHECK(ExitCode == code)
#define TEST_SUITE(name) \
void name(void)
#define PRINT_TEST_RESULTS \
atf_print_results
+
+#define EXPECT_EXIT \
+ if ((ExitExpected = true, 0 == setjmp(ExitPad)))
/* Function Definitions
*****************************************************************************/
unsigned int Curr_Line = 0;
static unsigned int Total = 0;
static unsigned int Failed = 0;
+bool ExitExpected;
+int ExitCode;
+jmp_buf ExitPad;
#ifndef NO_SIGNALS
static void handle_signal(int sig) {
fprintf(stderr,"%s:%d:0:%s:CRASH (signal: %d - %s)\n", Curr_File, Curr_Line, Curr_Test, sig, sig_name);
Failed++;
(void)atf_print_results();
- exit(1);
+ _Exit(1);
}
#endif
return Failed;
}
+void exit(int code) {
+ if (ExitExpected) {
+ ExitCode = code;
+ ExitExpected = false;
+ longjmp(ExitPad, 1);
+ } else {
+ assert(!"Unexpected exit. Something went wrong");
+ }
+}
+
#undef INCLUDE_DEFS
#endif
}
TEST_SUITE(External_Suite) {
+ TEST(CHECK_EXITCODE should pass if exit called with correct code) {
+ EXPECT_EXIT { exit(42); }
+ CHECK_EXITCODE(42);
+ }
+
+ TEST(CHECK_EXITCODE should fail if exit called with incorrect code) {
+ EXPECT_EXIT { exit(42); }
+ CHECK_EXITCODE(41);
+ }
+
TEST(Should_handle_SIGABRT) {
raise(SIGABRT);
}