-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utilities.h
71 lines (61 loc) · 1.92 KB
/
test_utilities.h
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
#ifndef TEST_UTILITIES_H_
#define TEST_UTILITIES_H_
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
/**
* These macros are here to help you create tests more easily and keep them
* clear
*
* The basic idea with unit-testing is create a test function for every real
* function and inside the test function declare some variables and execute the
* function under test.
*
* Use the ASSERT_TEST to verify correctness of values.
*/
/**
* Evaluates b and continues if b is true.
* If b is false, ends the test by returning false and prints a detailed
* message about the failure.
*/
#define ASSERT_TEST(b) do { \
if (!(b)) { \
printf("\nAssertion failed at %s:%d %s\n",__FILE__,__LINE__,#b); \
return false; \
} \
} while (0)
/**
* Macro used for running a test from the main function
*/
#define TEST_EQUALS(result, a, b) if ((result) && ((a) != (b))) { \
result = false; \
}
#define TEST_DIFFERENT(result, a, b) if ((result) && ((a) == (b))) { \
result = false; \
}
#define TEST_TRUE(result, bool) if ((result) && !(bool)) { \
result = false; \
assert(result == true); \
}
#define TEST_FALSE(result, bool) if ((result) && (bool)) { \
result = false; \
}
#define RUN_TEST(name) printf("Running %s... \n", #name); \
if (!name()) { \
printf("%s [FAILED]\n", #name); \
return false; \
} \
printf("%s [SUCCESS]\n", #name)
#define RUN_TEST_CHILD(test_child, name) test_child = fork(); \
if (test_child == 0) { \
RUN_TEST(name); \
return true; \
} \
wait(NULL)
/**
* These two macros are to help you initialize a set of examples. Look at
* list_example_test.h for an example of how they can be used to save a lot of code
*/
#define SET_UP(Typename) Typename examples = setUp()
#define TEAR_DOWN() tearDown(examples)
#endif /* TEST_UTILITIES_H_ */