Added missing .cc samples
This commit is contained in:
parent
b9292bb42d
commit
fd9cf1755a
12 changed files with 230 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,6 +6,7 @@
|
|||
!.ci/*
|
||||
|
||||
!*.c
|
||||
!*.cc
|
||||
!*.h
|
||||
!*.rst
|
||||
!samples/tests/*.sh
|
||||
|
|
80
samples/asserts.cc
Normal file
80
samples/asserts.cc
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
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
|
||||
}
|
8
samples/description.cc
Normal file
8
samples/description.cc
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
Test(misc, failing, .description = "Just a failing test") {
|
||||
cr_assert(0);
|
||||
}
|
||||
|
||||
Test(misc, skipped, .description = "This one is skipped", .disabled = true) {
|
||||
}
|
24
samples/exit.cc
Normal file
24
samples/exit.cc
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
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) {
|
||||
}
|
14
samples/fixtures.cc
Normal file
14
samples/fixtures.cc
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
5
samples/long-messages.cc
Normal file
5
samples/long-messages.cc
Normal file
|
@ -0,0 +1,5 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
Test(sample, long_msg) {
|
||||
cr_assert(0, "This is\nA long message\nSpawning multiple lines.\n\nFormatting is respected.");
|
||||
}
|
21
samples/more-suites.cc
Normal file
21
samples/more-suites.cc
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
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) {}
|
14
samples/other-crashes.cc
Normal file
14
samples/other-crashes.cc
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
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);
|
||||
}
|
26
samples/report.cc
Normal file
26
samples/report.cc
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
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");
|
||||
}
|
19
samples/signal.cc
Normal file
19
samples/signal.cc
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
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;
|
||||
}
|
9
samples/simple.cc
Normal file
9
samples/simple.cc
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
Test(misc, failing) {
|
||||
cr_assert(0);
|
||||
}
|
||||
|
||||
Test(misc, passing) {
|
||||
cr_assert(1);
|
||||
}
|
9
samples/suites.cc
Normal file
9
samples/suites.cc
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
Test(first_suite, test) {
|
||||
cr_assert(1);
|
||||
}
|
||||
|
||||
Test(second_suite, test) {
|
||||
cr_assert(1);
|
||||
}
|
Loading…
Add table
Reference in a new issue