diff --git a/.gitignore b/.gitignore index 9e813d9..fdaf499 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ !.ci/* !*.c +!*.cc !*.h !*.rst !samples/tests/*.sh diff --git a/samples/asserts.cc b/samples/asserts.cc new file mode 100644 index 0000000..ef5bc98 --- /dev/null +++ b/samples/asserts.cc @@ -0,0 +1,80 @@ +#include + +Test(asserts, base) { + cr_assert(true); + cr_expect(true); + + cr_assert(true, "Assertions may take failure messages"); + + cr_expect(false, "assert is fatal, expect isn't"); + cr_assert(false, "This assert runs"); + cr_assert(false, "This does not"); +} + +Test(asserts, old_school) { + if (false) + cr_abort_test("You can abort the test with a message from anywhere"); + + cr_abort_test(NULL); // or without a message +} + +Test(asserts, string) { + cr_assert_strings_eq("hello", "hello"); + cr_assert_strings_neq("hello", "olleh"); + + cr_assert_strings_gt("hello", "hell"); + cr_assert_strings_geq("hello", "hell"); + cr_assert_strings_geq("hello", "hello"); + + cr_assert_strings_lt("hell", "hello"); + cr_assert_strings_leq("hell", "hello"); + cr_assert_strings_leq("hello", "hello"); +} + +Test(asserts, native) { + cr_assert_eq(1, 1); + cr_assert_neq(1, 2); + + cr_assert_lt(1, 2); + cr_assert_leq(1, 2); + cr_assert_leq(2, 2); + + cr_assert_gt(2, 1); + cr_assert_geq(2, 1); + cr_assert_geq(2, 2); +} + +Test(asserts, float) { + cr_assert_neq(0.1 * 0.1, 0.01); + cr_assert_float_eq(0.1 * 0.1, 0.01, 0.001); +} + +struct dummy_struct { + char a; + size_t b; +}; + +int eq_dummy(struct dummy_struct *a, struct dummy_struct *b) { + return a->a != b->a || a->b != b->b; +} + +Test(asserts, array) { + int arr1[] = {1, 2, 3, 4}; + int arr2[] = {4, 3, 2, 1}; + + cr_assert_arrays_eq(arr1, arr1, 4); + cr_assert_arrays_neq(arr1, arr2, 4); + +#ifdef __GNUC__ + struct dummy_struct s1[] = {{4, 2}, {2, 4}}; + struct dummy_struct s2[2]; + memset(s2, 0xFF, sizeof(s2)); + s2[0].a = 4; + s2[0].b = 2; + s2[1].a = 2; + s2[1].b = 4; + + cr_assert_arrays_neq(s1, s2, 2); + cr_assert_arrays_eq_cmp(s1, s2, 2, eq_dummy); +#endif +} diff --git a/samples/description.cc b/samples/description.cc new file mode 100644 index 0000000..55b7b49 --- /dev/null +++ b/samples/description.cc @@ -0,0 +1,8 @@ +#include + +Test(misc, failing, .description = "Just a failing test") { + cr_assert(0); +} + +Test(misc, skipped, .description = "This one is skipped", .disabled = true) { +} diff --git a/samples/exit.cc b/samples/exit.cc new file mode 100644 index 0000000..f9c95dd --- /dev/null +++ b/samples/exit.cc @@ -0,0 +1,24 @@ +#include +#include +#include + +Test(exit, normal, .exit_code = 0) { +} + +Test(exit, expected_exit, .exit_code = 42) { + exit(42); +} + +Test(exit, unexpected_exit) { + exit(127); +} + +void do_exit (void) { + exit(127); +} + +Test(exit_with_fixtures, init_exits, .init = do_exit) { +} + +Test(exit_with_fixtures, fini_exits, .fini = do_exit) { +} diff --git a/samples/fixtures.cc b/samples/fixtures.cc new file mode 100644 index 0000000..1f7851c --- /dev/null +++ b/samples/fixtures.cc @@ -0,0 +1,14 @@ +#include +#include + +void setup(void) { + puts("Runs before the test"); +} + +void teardown(void) { + puts("Runs after the test"); +} + +Test(simple, fixtures, .init = setup, .fini = teardown) { + cr_assert(1); +} diff --git a/samples/long-messages.cc b/samples/long-messages.cc new file mode 100644 index 0000000..02472e1 --- /dev/null +++ b/samples/long-messages.cc @@ -0,0 +1,5 @@ +#include + +Test(sample, long_msg) { + cr_assert(0, "This is\nA long message\nSpawning multiple lines.\n\nFormatting is respected."); +} diff --git a/samples/more-suites.cc b/samples/more-suites.cc new file mode 100644 index 0000000..95d7039 --- /dev/null +++ b/samples/more-suites.cc @@ -0,0 +1,21 @@ +#include + +void setup_suite(void) { +} + +void teardown_suite(void) { +} + +TestSuite(suite1, .init = setup_suite, .fini = teardown_suite); + +Test(suite1, test) { + cr_assert(1); +} + +Test(suite2, test) { + cr_assert(1); +} + +TestSuite(disabled, .disabled = true); + +Test(disabled, test) {} diff --git a/samples/other-crashes.cc b/samples/other-crashes.cc new file mode 100644 index 0000000..ab689bb --- /dev/null +++ b/samples/other-crashes.cc @@ -0,0 +1,14 @@ +#include + +void crash(void) { + int *i = NULL; + *i = 42; +} + +Test(misc, setup_crash, .init = crash) { + cr_assert(true); +} + +Test(misc, teardown_crash, .fini = crash) { + cr_assert(true); +} diff --git a/samples/report.cc b/samples/report.cc new file mode 100644 index 0000000..8c9ace8 --- /dev/null +++ b/samples/report.cc @@ -0,0 +1,26 @@ +#include +#include + +Test(sample, test) { + cr_expect(0); + cr_assert(1); +} + +ReportHook(PRE_INIT)(struct criterion_test *test) { + printf("testing %s in category %s\n", test->name, test->category); +} + +ReportHook(POST_TEST)(struct criterion_test_stats *stats) { + printf("Asserts: [%d passed, %d failed, %d total]\n", + stats->passed_asserts, stats->failed_asserts, stats->passed_asserts + stats->failed_asserts); +} + +ReportHook(PRE_ALL)(struct criterion_test_set *tests) { + (void) tests; + puts("criterion_init"); +} + +ReportHook(POST_ALL)(struct criterion_global_stats *stats) { + (void) stats; + puts("criterion_fini"); +} diff --git a/samples/signal.cc b/samples/signal.cc new file mode 100644 index 0000000..9dcc99d --- /dev/null +++ b/samples/signal.cc @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +Test(simple, caught, .signal = SIGSEGV) { + int *i = NULL; + *i = 42; +} + +Test(simple, wrong_signal, .signal = SIGINT) { + int *i = NULL; + *i = 42; +} + +Test(simple, uncaught) { + int *i = NULL; + *i = 42; +} diff --git a/samples/simple.cc b/samples/simple.cc new file mode 100644 index 0000000..d5916f5 --- /dev/null +++ b/samples/simple.cc @@ -0,0 +1,9 @@ +#include + +Test(misc, failing) { + cr_assert(0); +} + +Test(misc, passing) { + cr_assert(1); +} diff --git a/samples/suites.cc b/samples/suites.cc new file mode 100644 index 0000000..d19d7b2 --- /dev/null +++ b/samples/suites.cc @@ -0,0 +1,9 @@ +#include + +Test(first_suite, test) { + cr_assert(1); +} + +Test(second_suite, test) { + cr_assert(1); +}