-
Notifications
You must be signed in to change notification settings - Fork 182
/
test.c
87 lines (74 loc) · 1.67 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "util.h"
static int tests_run = 0;
static int tests_passed = 0;
static void test_passed(void)
{
tests_run++;
tests_passed++;
}
static void test_failed(void)
{
tests_run++;
}
void test_check_boolean(char *description, uint8_t passed)
{
if(passed)
{
test_passed();
}
else
{
test_failed();
printf("FAIL: %s\n", description);
}
}
void test_check_memory(char *description, uint8_t *expected, uint64_t expected_length, uint8_t *result, uint64_t result_length)
{
if(expected_length == result_length && !memcmp(expected, result, expected_length))
{
test_passed();
}
else
{
test_failed();
printf("FAIL: %s\n", description);
printf(" Expected: ");
print_hex(expected, expected_length);
printf(" --> (\"%s\")\n", expected);
printf(" Result: ");
print_hex(result, result_length);
printf(" --> (\"%s\")\n", result);
printf("\n");
}
}
void test_check_integer(char *description, uint32_t expected, uint32_t result)
{
if(expected == result)
{
test_passed();
}
else
{
test_failed();
printf("FAIL: %s\n", description);
printf(" Expected: 0x%08x\n", expected);
printf(" Result: 0x%08x\n", result);
printf("\n");
}
}
void test_report(void)
{
if(tests_run == 0)
{
printf("No tests run!\n");
}
else
{
printf("--------------------------------------------------------------------------------\n");
printf("TESTS PASSED: %d / %d [%2.4f%%]\n", tests_passed, tests_run, 100 * (float)tests_passed / tests_run);
printf("--------------------------------------------------------------------------------\n");
}
if (tests_passed != tests_run) {
exit(1);
}
}