Debian Codegraph @

tests/constraint_messages_tests.c

raw symbols SCIP
1 /* Note: the whole point of these tests is that they should fail!
2 This makes it hard to think straight and to give the variables good
3 names ;-) */
4
5 #include <cgreen/cgreen.h>
6 #include <cgreen/mocks.h>
7 #include <signal.h>
8 #include <float.h>
9 #ifdef HAVE_SYS_RESOURCE_H
10 #include <sys/resource.h>
11 #endif
12
13
14 #ifdef __cplusplus
15 using namespace cgreen;
16 #endif
17
18
19 Describe(ConstraintMessage);
20 BeforeEach(ConstraintMessage) {}
21 AfterEach(ConstraintMessage) {}
22
23 Ensure(ConstraintMessage,for_is_null) {
24 int i;
25 int *pointer = &i;
26 assert_that(pointer, is_null);
27 }
28
29 Ensure(ConstraintMessage,for_is_non_null) {
30 int *pointer = NULL;
31 assert_that(pointer, is_non_null);
32 }
33
34 Ensure(ConstraintMessage,for_is_equal_to) {
35 int forty_five = 45, thirty_three = 33;
36 assert_that(forty_five, is_equal_to(thirty_three));
37 }
38
39 Ensure(ConstraintMessage, for_is_equal_to_hex) {
40 unsigned char bytes[4];
41 memset(bytes, 0xaa, sizeof(bytes));
42 assert_that((unsigned char) bytes[0], is_equal_to_hex(0xbb));
43 }
44
45 Ensure(ConstraintMessage, for_is_not_equal_to) {
46 int should_not_be_forty_five = 45, forty_five = 45;
47 assert_that(should_not_be_forty_five, is_not_equal_to(forty_five));
48 }
49
50 Ensure(ConstraintMessage, for_is_greater_than) {
51 int forty_five = 45, thirty_three = 33;
52 assert_that(thirty_three, is_greater_than(forty_five));
53 }
54
55 Ensure(ConstraintMessage, for_is_less_than) {
56 int forty_five = 45, thirty_three = 33;
57 assert_that(forty_five, is_less_than(thirty_three));
58 }
59
60
61 // Contents of struct/memory
62 Ensure(ConstraintMessage, for_is_equal_to_contents_of) {
63 char forty_five[45] = {45, 44, 43}, thirty_three[33] = {45, 44, 33};
64 assert_that(thirty_three, is_equal_to_contents_of(forty_five, 45));
65 }
66
67 Ensure(ConstraintMessage, for_is_not_equal_to_contents_of) {
68 int forty_five_and_up[3] = {45, 46, 47}, another_forty_five_and_up[3] = {45, 46, 47};
69 assert_that(forty_five_and_up, is_not_equal_to_contents_of(another_forty_five_and_up, 3));
70 }
71
72
73 // Strings
74 Ensure(ConstraintMessage, for_is_equal_to_string) {
75 const char *forty_five = "this string is fortyfive", *thirty_three = "this string is thirtythree";
76 assert_that(thirty_three, is_equal_to_string(forty_five));
77 }
78
79 Ensure(ConstraintMessage, for_is_not_equal_to_string) {
80 const char *forty_five = "this string is fortyfive", *another_forty_five = "this string is fortyfive";
81 assert_that(another_forty_five, is_not_equal_to_string(forty_five));
82 }
83
84 Ensure(ConstraintMessage, for_contains_string) {
85 const char *forty_five = "fortyfive", *not_containing_forty_five = "this text is thirtythree";
86 assert_that(not_containing_forty_five, contains_string(forty_five));
87 }
88
89 Ensure(ConstraintMessage, for_does_not_contain_string) {
90 const char *contains_forty_five = "this string is fortyfive", *forty_five = "fortyfive";
91 assert_that(contains_forty_five, does_not_contain_string(forty_five));
92 }
93
94 Ensure(ConstraintMessage, for_begins_with_string) {
95 const char *does_not_begin_with_forty_five = "this string does not begin with fortyfive", *forty_five = "fortyfive";
96 assert_that(does_not_begin_with_forty_five, begins_with_string(forty_five));
97 }
98
99 Ensure(ConstraintMessage, for_does_not_begin_with_string) {
100 const char *begins_with_forty_five = "fortyfive is the start of this", *forty_five = "fortyfive";
101 assert_that(begins_with_forty_five, does_not_begin_with_string(forty_five));
102 }
103
104 Ensure(ConstraintMessage, for_ends_with_string) {
105 const char *does_not_end_with_forty_five = "fortyfive is not the end of this string", *forty_five = "fortyfive";
106 assert_that(does_not_end_with_forty_five, ends_with_string(forty_five));
107 }
108
109 Ensure(ConstraintMessage, for_does_not_end_with_string) {
110 const char *ends_with_forty_five = "this string ends with fortyfive", *forty_five = "fortyfive";
111 assert_that(ends_with_forty_five, does_not_end_with_string(forty_five));
112 }
113
114
115 // Double
116
117 Ensure(ConstraintMessage, for_is_equal_to_double) {
118 double four_point_five = 4.5f, three_point_three = 3.3f;
119 assert_that_double(four_point_five, is_equal_to_double(three_point_three));
120 }
121
122 Ensure(ConstraintMessage, for_is_not_equal_to_double) {
123 significant_figures_for_assert_double_are(4);
124 double epsilon = 1.0e-4;
125 double four_point_five = 4.5, almost_four_point_five = 4.5 - epsilon;
126 assert_that_double(four_point_five, is_not_equal_to_double(almost_four_point_five));
127 }
128
129 Ensure(ConstraintMessage, for_is_greater_than_double) {
130 double four_point_five = 4.5f, three_point_three = 3.3f;
131 assert_that_double(three_point_three, is_greater_than_double(four_point_five));
132 }
133
134 Ensure(ConstraintMessage, for_is_less_than_double) {
135 double four_point_five = 4.5f, three_point_three = 3.3f;
136 assert_that_double(four_point_five, is_less_than_double(three_point_three));
137 }
138
139 Ensure(ConstraintMessage, for_equal_to_double) {
140 assert_that_double(0, is_equal_to_double(1));
141 }
142
143 Ensure(ConstraintMessage, for_equal_to_double_negative) {
144 assert_that_double(-1, is_equal_to_double(-2));
145 }
146
147 Ensure(ConstraintMessage, for_not_equal_to_double) {
148 assert_that_double(0, is_not_equal_to_double(0));
149 }
150
151 Ensure(ConstraintMessage, for_not_equal_to_double_negative) {
152 assert_that_double(-1, is_not_equal_to_double(-1));
153 }
154
155 Ensure(ConstraintMessage, for_is_less_than_double_with_accuracy) {
156 significant_figures_for_assert_double_are(4);
157 assert_that_double(1.0, is_less_than_double(1.0 - 1.0e-3 - DBL_EPSILON));
158 }
159
160 Ensure(ConstraintMessage, for_is_greater_than_double_with_accuracy) {
161 significant_figures_for_assert_double_are(4);
162 assert_that_double(1.0, is_greater_than_double(1.0 + 1.0e-3 + DBL_EPSILON));
163 }
164
165
166 // Basic core assert_that()
167
168 Ensure(ConstraintMessage, for_assert_that) {
169 assert_that(0 == 1);
170 }
171
172
173 // Mocks
174
175 void some_mock(int parameter) {
176 mock(parameter);
177 }
178
179 Ensure(ConstraintMessage, for_mock_called_without_expectation) {
180 some_mock(0);
181 }
182
183 Ensure(ConstraintMessage, for_mock_called_more_times_than_expected) {
184 expect(some_mock);
185 some_mock(0);
186 some_mock(0);
187 }
188
189 Ensure(ConstraintMessage, for_mock_called_with_unexpected_parameter_value) {
190 expect(some_mock, when(parameter, is_equal_to(1)));
191 some_mock(0);
192 }
193
194 /* Tests that will fail with "illegal" type of messages */
195 Ensure(ConstraintMessage, for_always_followed_by_expectation) {
196 always_expect(some_mock, when(parameter, is_equal_to(1)));
197 expect(some_mock, when(parameter, is_equal_to(0)));
198 }
199
200 Ensure(ConstraintMessage, for_violated_never_expect) {
201 never_expect(some_mock, when(parameter, is_equal_to(1)));
202 some_mock(1);
203 }
204
205 Ensure(ConstraintMessage, for_mock_parameter_name_not_matching_constraint_parameter_name) {
206 expect(some_mock, when(PARAMETER, is_equal_to(0)));
207 some_mock(0);
208 }
209
210 void forgot_to_pass_parameters_mock(int x) {
211 (void)x; // to silence compiler warning
212 mock();
213 }
214
215 Ensure(ConstraintMessage, for_no_mock_parameters_with_parameter_constraint) {
216 expect(forgot_to_pass_parameters_mock, when(x, is_equal_to(0)));
217 forgot_to_pass_parameters_mock(0);
218 }
219
220 Ensure(ConstraintMessage, increments_exception_count_when_terminating_via_SIGQUIT) {
221 #ifdef HAVE_SYS_RESOURCE_H
222 struct rlimit core_limit;
223 core_limit.rlim_cur = 1U;
224 core_limit.rlim_max = 1U;
225 setrlimit(RLIMIT_CORE, &core_limit);
226 #endif
227 raise(SIGQUIT);
228 }
229
230 Ensure(ConstraintMessage, increments_exception_count_when_terminating_via_SIGTERM) {
231 raise(SIGTERM);
232 }
233
234 #ifdef __cplusplus
235 Ensure(ConstraintMessage, for_incorrect_assert_throws) {
236 assert_throws(std::string, throw "something else");
237 }
238
239 Ensure(ConstraintMessage, for_exception_was_expected_but_nothing_thrown) {
240 assert_throws(std::string, (void)5);
241 }
242 #endif