-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_asserts_example.c
164 lines (139 loc) · 3.68 KB
/
all_asserts_example.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "../gidunit.h"
#include <ctype.h>
BEGIN_TEST_SUITE(AllAsserts)
Test(AssertFailWithFormat,
StringEnumParam(str, "Hello", "World", "!")
EnumParam(i, 0, -1, 2)
UnsignedEnumParam(j, 0, 1, 2))
{
//Force assert failure with a specific message with variable values
assert_fail_format(
"Forced failure with str=%s, i=%"PRId64", j=%"PRIu64".\n",
str,
i,
j);
}
Test(AssertFailPlainMessage)
{
//Force assert failure with a plain message
assert_fail("I was forced to fail.");
}
Test(AssertMessageWithFormat, StringEnumParam(str, "Hello", "World"))
{
//Test a condition, print a formatted message upon failure
assert_message_format(
toupper(str[3])=='A',
"Expected the 4th character to be an A, but it was %c.",
toupper(str[3]));
}
Test(AssertPlainMessage)
{
//Test a condition, print a plain message upon failure
assert_message(0 == 1, "Expected nonsense.");
}
Test(AssertCondition,
StringRow("Alpha", "Bravo", "Charlie")
StringRow("alpha", "bravo", "charlie")
StringRow("ALPHA", "BRAVO", "CHARLIE"))
{
//Test a condition with no specific message
assert(toupper(string_row[0][0]) == 'Z');
}
Test(AssertIntEquals, EnumParam(i, -1, 2, -3, 4))
{
//Test that two integers are equal
assert_int_eq(-1, i);
}
Test(AssertIntNotEquals, EnumParam(i, -1, 2, -3, 4))
{
//Test that two integers are not equal
assert_int_not_eq(-1, i);
}
Test(AssertUIntEquals, UnsignedEnumParam(i, 0, 1, 2))
{
//Test that two unsigned integers are equal
assert_uint_eq(0, i);
}
Test(AssertUIntNotEquals, UnsignedEnumParam(i, 0, 1, 2))
{
//Test that two unsigned integers are not equal
assert_uint_not_eq(0, i);
}
Test(AssertPointerEquals, EnumParam(i, 0, 1, 2))
{
//Test that two pointers are equal
int64_t wrongAddr = i;
int64_t* wrongPtr = &wrongAddr;
int64_t* enumPtr = &i;
assert_pointer_eq(wrongPtr, enumPtr);
}
Test(AssertPointerNotEquals, EnumParam(i, 0, 1, 2))
{
//Test that two pointers are not equal
int64_t* iPtr = &i;
int64_t* iPtrClone = &i;
assert_pointer_not_eq(iPtr, iPtrClone);
}
Test(AssertNull)
{
//Test that a pointer is NULL
int i = 0;
int* ptr = &i;
assert_null(ptr);
}
Test(AssertNotNull)
{
//Test that a pointer is not NULL
int* ptr = NULL;
assert_not_null(ptr);
}
Test(AssertStringEquals, StringEnumParam(word, "Alpha", "Bravo"))
{
//Test that two strings have the same value
assert_string_eq("Bravo", word);
}
Test(AssertStringNotEquals, StringEnumParam(word, "Alpha", "Bravo"))
{
//Test that two strings do not have the same value
assert_string_not_eq("Alpha", word);
}
Test(AssertDoubleEquals)
{
//Test that two double precision floats are equal
assert_double_eq(0.1, 0.2);
}
Test(AssertDoubleNotEquals)
{
//Test that two double precision floats are not equal
assert_double_not_eq(0.1, 0.1);
}
Test(AssertMemoryEquals)
{
//Allocate two pieces of equal memory
size_t size = 128;
char* a = malloc(size);
char* b = malloc(size);
for(size_t i = 0; i < size; i++)
a[i] = b[i] = (char)i;
//Make one byte different
b[size/2]++;
//Test that they are both equal
assert_memory_eq(a, b, size);
}
Test(AsseryMemoryNotEquals)
{
//Allocate two pieces of equal memory
size_t size = 128;
char* a = malloc(size);
char* b = malloc(size);
for(size_t i = 0; i < size; i++)
a[i] = b[i] = (char)i;
//Test that they are not equal
assert_memory_not_eq(a, b, size);
}
END_TEST_SUITE()
int main()
{
ADD_TEST_SUITE(AllAsserts);
return gidunit();
}