Debian Codegraph @

src/constraint.c

raw symbols SCIP
1 #include <cgreen/assertions.h>
2 #include <cgreen/boxed_double.h>
3 #include <cgreen/constraint.h>
4 #include <cgreen/message_formatting.h>
5 #include <cgreen/string_comparison.h>
6 #include <cgreen/vector.h>
7 #include <inttypes.h>
8 #include <float.h>
9 #include <limits.h>
10 #include <math.h>
11 #ifndef __cplusplus
12 #include <stdbool.h>
13 #endif
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #ifdef __ANDROID__
19 #include "cgreen/internal/android_headers/androidcompat.h"
20 #endif // #ifdef __ANDROID__
21
22
23 #ifdef _MSC_VER
24 #include "wincompat.h"
25 #endif
26
27 #include "constraint_internal.h"
28 #include "parameters.h"
29 #include "utils.h"
30 #include "cgreen_value_internal.h"
31
32
33 #ifdef max
34 #undef max
35 #endif
36
37 #ifdef min
38 #undef min
39 #endif
40
41 #define max(a,b) ((a) > (b) ? (a) : (b))
42 #define min(a,b) ((a) > (b) ? (b) : (a))
43
44
45 static int significant_figures = 8;
46 static double absolute_tolerance = DBL_MIN / 1.0e-8;
47
48
49 static double accuracy(int significant_figures, double largest);
50
51 static bool compare_want_greater_value(Constraint *constraint, CgreenValue actual);
52
53 static bool compare_want_lesser_value(Constraint *constraint, CgreenValue actual);
54
55 static bool compare_want_contents(Constraint *constraint, CgreenValue actual);
56
57 static bool compare_do_not_want_contents(Constraint *constraint, CgreenValue actual);
58
59 static bool compare_true(Constraint *constraint, CgreenValue actual);
60 static void test_true(Constraint *constraint, const char *function, CgreenValue actual,
61 const char *test_file, int test_line, TestReporter *reporter);
62
63 static bool compare_want_string(Constraint *constraint, CgreenValue actual);
64 static bool compare_do_not_want_string(Constraint *constraint, CgreenValue actual);
65 static bool compare_want_substring(Constraint *constraint, CgreenValue actual);
66 static bool compare_do_not_want_substring(Constraint *constraint, CgreenValue actual);
67 static bool compare_want_beginning_of_string(Constraint *constraint, CgreenValue actual);
68 static bool compare_do_not_want_beginning_of_string(Constraint *constraint, CgreenValue actual);
69 static bool compare_want_end_of_string(Constraint *constraint, CgreenValue actual);
70 static bool compare_do_not_want_end_of_string(Constraint *constraint, CgreenValue actual);
71
72 static bool compare_want_double(Constraint *constraint, CgreenValue actual);
73 static void test_want_double(Constraint *constraint, const char *function, CgreenValue actual,
74 const char *test_file, int test_line, TestReporter *reporter);
75 static bool compare_do_not_want_double(Constraint *constraint, CgreenValue actual);
76 static void test_do_not_want_double(Constraint *constraint, const char *function, CgreenValue actual,
77 const char *test_file, int test_line, TestReporter *reporter);
78 static bool compare_want_lesser_double(Constraint *constraint, CgreenValue actual);
79 static bool compare_want_greater_double(Constraint *constraint, CgreenValue actual);
80
81 static void set_contents(Constraint *constraint, const char *function, CgreenValue actual,
82 const char *test_file, int test_line, TestReporter *reporter);
83 static void execute_sideeffect(Constraint *constraint, const char *function, CgreenValue actual,
84 const char *test_file, int test_line, TestReporter *reporter);
85 static void capture_parameter(Constraint *constraint, const char *function, CgreenValue actual,
86 const char *test_file, int test_line, TestReporter *reporter);
87
88 static const char *default_actual_value_message = "\n\t\tactual value:\t\t\t[%" PRIdPTR "]";
89 static const char *default_expected_value_message = "\t\texpected value:\t\t\t[%" PRIdPTR "]";
90
91 Constraint *create_constraint(void) {
92 Constraint *constraint = (Constraint *)malloc(sizeof(Constraint));
93 /* TODO: setting this to NULL as an implicit type check :( */
94 constraint->parameter_name = NULL;
95 constraint->destroy = &destroy_empty_constraint;
96 constraint->failure_message = &failure_message_for;
97 constraint->expected_value_name = NULL;
98 constraint->actual_value_message = default_actual_value_message;
99 constraint->expected_value_message = default_expected_value_message;
100
101 return constraint;
102 }
103
104 static Constraint *create_constraint_expecting(CgreenValue expected_value, const char *expected_value_name) {
105 Constraint *constraint = create_constraint();
106
107 constraint->expected_value = expected_value;
108 constraint->expected_value_name = string_dup(expected_value_name);
109
110 return constraint;
111 }
112
113 void destroy_empty_constraint(Constraint *constraint) {
114 constraint->name = NULL;
115 constraint->parameter_name = NULL;
116 constraint->compare = NULL;
117 constraint->execute = NULL;
118 constraint->destroy = NULL;
119
120 if (constraint->expected_value_name != NULL)
121 free((void *)constraint->expected_value_name);
122
123 free(constraint);
124 }
125
126 void destroy_static_constraint(Constraint *constraint) {
127 /* static constraints helpers (e.g. is_null) act as singletons, and are never destroyed */
128 (void)constraint;
129 }
130
131 void destroy_constraint(Constraint *constraint) {
132 if (constraint->destroy != NULL)
133 (*constraint->destroy)(constraint);
134 }
135
136 void destroy_constraints(va_list constraints) {
137 Constraint *constraint = NULL;
138 while ((constraint = va_arg(constraints, Constraint *)) != (Constraint *)0) {
139 destroy_constraint(constraint);
140 }
141 }
142
143 bool constraint_is_for_parameter(const Constraint *constraint, const char *parameter) {
144 return !constraint_is_not_for_parameter(constraint, parameter);
145 }
146
147 bool constraint_is_not_for_parameter(const Constraint *constraint, const char *parameter) {
148 /* TODO: This should not test for types of constraints... Or it actually checks for something else ... */
149 if (is_not_comparing(constraint) && is_not_content_setting(constraint)) {
150 return true;
151 }
152
153 return strcmp(constraint->parameter_name, parameter) != 0;
154 }
155
156 Constraint *create_not_null_constraint(void) {
157 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(0), "null");
158 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
159
160 constraint->name = "be non null";
161 constraint->compare = &compare_do_not_want_value;
162 constraint->execute = &test_want;
163 constraint->actual_value_message = "";
164 constraint->expected_value_message = "";
165
166 return constraint;
167 }
168
169 Constraint *create_is_null_constraint(void) {
170 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(0), "null");
171 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
172
173 constraint->name = "be null";
174 constraint->compare = &compare_want_value;
175 constraint->execute = &test_want;
176 constraint->actual_value_message = "";
177 constraint->expected_value_message = "";
178
179 return constraint;
180 }
181
182 Constraint *create_is_false_constraint(void) {
183 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(false), "false");
184 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
185
186 constraint->name = "be false";
187 constraint->compare = &compare_want_value;
188 constraint->execute = &test_want;
189 constraint->actual_value_message = "";
190 constraint->expected_value_message = "";
191
192 return constraint;
193 }
194
195 Constraint *create_is_true_constraint(void) {
196 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(false), "true");
197 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
198
199 constraint->name = "be true";
200 constraint->compare = &compare_do_not_want_value;
201 constraint->execute = &test_want;
202 constraint->actual_value_message = "";
203 constraint->expected_value_message = "";
204
205 return constraint;
206 }
207
208 Constraint *create_equal_to_value_constraint(intptr_t expected_value, const char *expected_value_name) {
209 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(expected_value), expected_value_name);
210 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
211
212 constraint->compare = &compare_want_value;
213 constraint->execute = &test_want;
214 constraint->name = "equal";
215 constraint->size_of_expected_value = sizeof(intptr_t);
216
217 return constraint;
218 }
219
220 Constraint *create_equal_to_hexvalue_constraint(intptr_t expected_value, const char *expected_value_name) {
221 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(expected_value), expected_value_name);
222 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
223
224 constraint->compare = &compare_want_value;
225 constraint->execute = &test_want;
226 constraint->name = "equal";
227 constraint->size_of_expected_value = sizeof(intptr_t);
228 constraint->actual_value_message = "\n\t\tactual value:\t\t\t[0x%" PRIxPTR "]";
229 constraint->expected_value_message = "\t\texpected value:\t\t\t[0x%" PRIxPTR "]";
230
231 return constraint;
232 }
233
234 Constraint *create_not_equal_to_value_constraint(intptr_t expected_value, const char *expected_value_name) {
235 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(expected_value), expected_value_name);
236 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
237
238 constraint->compare = &compare_do_not_want_value;
239 constraint->execute = &test_want;
240 constraint->name = "not equal";
241 constraint->size_of_expected_value = sizeof(intptr_t);
242
243 return constraint;
244 }
245
246 Constraint *create_less_than_value_constraint(intptr_t expected_value, const char *expected_value_name) {
247 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(expected_value), expected_value_name);
248 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
249
250 constraint->compare = &compare_want_lesser_value;
251 constraint->execute = &test_true;
252 constraint->name = "be less than";
253 constraint->expected_value_message = "\t\texpected to be less than:\t[%" PRIdPTR "]";
254 constraint->size_of_expected_value = sizeof(intptr_t);
255
256 return constraint;
257 }
258
259 Constraint *create_greater_than_value_constraint(intptr_t expected_value, const char *expected_value_name) {
260 Constraint *constraint = create_constraint_expecting(make_cgreen_integer_value(expected_value), expected_value_name);
261 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
262
263 constraint->compare = &compare_want_greater_value;
264 constraint->execute = &test_true;
265 constraint->name = "be greater than";
266 constraint->expected_value_message = "\t\texpected to be greater than:\t[%" PRIdPTR "]";
267 constraint->size_of_expected_value = sizeof(intptr_t);
268
269 return constraint;
270 }
271
272 Constraint *create_equal_to_contents_constraint(void *pointer_to_compare, size_t size_to_compare, const char *compared_pointer_name) {
273 Constraint *constraint = create_constraint_expecting(make_cgreen_pointer_value(pointer_to_compare), compared_pointer_name);
274 constraint->type = CGREEN_CONTENT_COMPARER_CONSTRAINT;
275
276 constraint->compare = &compare_want_contents;
277 constraint->execute = &test_want;
278 constraint->name = "equal contents of";
279 constraint->size_of_expected_value = size_to_compare;
280
281 return constraint;
282 }
283
284 Constraint *create_not_equal_to_contents_constraint(void *pointer_to_compare, size_t size_to_compare, const char *compared_pointer_name) {
285 Constraint *constraint = create_constraint_expecting(make_cgreen_pointer_value(pointer_to_compare), compared_pointer_name);
286 constraint->type = CGREEN_CONTENT_COMPARER_CONSTRAINT;
287
288 constraint->compare = &compare_do_not_want_contents;
289 constraint->execute = &test_want;
290 constraint->name = "not equal contents of";
291 constraint->size_of_expected_value = size_to_compare;
292
293 return constraint;
294 }
295
296 Constraint *create_equal_to_string_constraint(const char* expected_value, const char *expected_value_name) {
297 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
298 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
299
300 constraint->compare = &compare_want_string;
301 constraint->execute = &test_want;
302 constraint->name = "equal string";
303 constraint->destroy = &destroy_string_constraint;
304 constraint->expected_value_message = "\t\texpected to equal:\t\t[\"%s\"]";
305
306 return constraint;
307 }
308
309 Constraint *create_not_equal_to_string_constraint(const char* expected_value, const char *expected_value_name) {
310 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
311 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
312
313 constraint->compare = &compare_do_not_want_string;
314 constraint->execute = &test_want;
315 constraint->name = "not equal string";
316 constraint->destroy = &destroy_string_constraint;
317 constraint->expected_value_message = "\t\texpected to not equal:\t[\"%s\"]";
318
319 return constraint;
320 }
321
322 Constraint *create_contains_string_constraint(const char* expected_value, const char *expected_value_name) {
323 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
324 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
325
326 constraint->compare = &compare_want_substring;
327 constraint->execute = &test_want;
328 constraint->name = "contain string";
329 constraint->destroy = &destroy_string_constraint;
330 constraint->expected_value_message = "\t\texpected to contain:\t\t[\"%s\"]";
331
332 return constraint;
333 }
334
335 Constraint *create_does_not_contain_string_constraint(const char* expected_value, const char *expected_value_name) {
336 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
337 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
338
339 constraint->compare = &compare_do_not_want_substring;
340 constraint->execute = &test_want;
341 constraint->name = "not contain string";
342 constraint->destroy = &destroy_string_constraint;
343 constraint->expected_value_message = "\t\texpected to not contain:\t[\"%s\"]";
344
345 return constraint;
346 }
347
348 Constraint *create_begins_with_string_constraint(const char* expected_value, const char *expected_value_name) {
349 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
350 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
351
352 constraint->compare = &compare_want_beginning_of_string;
353 constraint->execute = &test_want;
354 constraint->name = "begin with string";
355 constraint->destroy = &destroy_string_constraint;
356 constraint->expected_value_message = "\t\texpected to begin with:\t\t[\"%s\"]";
357
358 return constraint;
359 }
360
361 Constraint *create_does_not_begin_with_string_constraint(const char* expected_value, const char *expected_value_name) {
362 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
363 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
364
365 constraint->compare = &compare_do_not_want_beginning_of_string;
366 constraint->execute = &test_want;
367 constraint->name = "not begin with string";
368 constraint->destroy = &destroy_string_constraint;
369 constraint->expected_value_message = "\t\texpected to not begin with:\t[\"%s\"]";
370
371 return constraint;
372 }
373
374 Constraint *create_ends_with_string_constraint(const char* expected_value, const char *expected_value_name) {
375 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
376 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
377
378 constraint->compare = &compare_want_end_of_string;
379 constraint->execute = &test_want;
380 constraint->name = "end with string";
381 constraint->destroy = &destroy_string_constraint;
382 constraint->expected_value_message = "\t\texpected to end with:\t\t[\"%s\"]";
383
384 return constraint;
385 }
386
387 Constraint *create_does_not_end_with_string_constraint(const char* expected_value, const char *expected_value_name) {
388 Constraint *constraint = create_constraint_expecting(make_cgreen_string_value(expected_value), expected_value_name);
389 constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT;
390
391 constraint->compare = &compare_do_not_want_end_of_string;
392 constraint->execute = &test_want;
393 constraint->name = "not end with string";
394 constraint->destroy = &destroy_string_constraint;
395 constraint->expected_value_message = "\t\texpected to not end with:\t[\"%s\"]";
396
397 return constraint;
398 }
399
400 Constraint *create_equal_to_double_constraint(double expected_value, const char *expected_value_name) {
401 Constraint *constraint = create_constraint_expecting(make_cgreen_double_value(expected_value), expected_value_name);
402 constraint->type = CGREEN_DOUBLE_COMPARER_CONSTRAINT;
403
404 constraint->compare = &compare_want_double;
405 constraint->execute = &test_want_double;
406 constraint->name = "equal double";
407 constraint->destroy = &destroy_double_constraint;
408
409 return constraint;
410 }
411
412 Constraint *create_not_equal_to_double_constraint(double expected_value, const char *expected_value_name) {
413 Constraint *constraint = create_constraint_expecting(make_cgreen_double_value(expected_value), expected_value_name);
414 constraint->type = CGREEN_DOUBLE_COMPARER_CONSTRAINT;
415
416 constraint->compare = &compare_do_not_want_double;
417 constraint->execute = &test_do_not_want_double;
418 constraint->name = "not equal double";
419 constraint->destroy = &destroy_double_constraint;
420
421 return constraint;
422 }
423
424 Constraint *create_less_than_double_constraint(double expected_value, const char *expected_value_name) {
425 Constraint *constraint = create_constraint_expecting(make_cgreen_double_value(expected_value), expected_value_name);
426 constraint->type = CGREEN_DOUBLE_COMPARER_CONSTRAINT;
427
428 constraint->compare = &compare_want_lesser_double;
429 constraint->execute = &test_true;
430 constraint->name = "be less than double";
431 constraint->destroy = &destroy_double_constraint;
432 constraint->expected_value_message = "\t\texpected to be less than:\t[%08f]";
433
434 return constraint;
435 }
436
437 Constraint *create_greater_than_double_constraint(double expected_value, const char *expected_value_name) {
438 Constraint *constraint = create_constraint_expecting(make_cgreen_double_value(expected_value), expected_value_name);
439 constraint->type = CGREEN_DOUBLE_COMPARER_CONSTRAINT;
440
441 constraint->compare = &compare_want_greater_double;
442 constraint->execute = &test_true;
443 constraint->name = "be greater than double";
444 constraint->destroy = &destroy_double_constraint;
445 constraint->expected_value_message = "\t\texpected to be greater than:\t[%08f]";
446
447 return constraint;
448 }
449
450 Constraint *create_return_value_constraint(intptr_t value_to_return) {
451 Constraint* constraint = create_constraint();
452 constraint->type = CGREEN_RETURN_VALUE_CONSTRAINT;
453
454 constraint->compare = &compare_true;
455 constraint->execute = &test_true;
456 constraint->name = "return value";
457 constraint->expected_value = make_cgreen_integer_value(value_to_return);
458
459 return constraint;
460 }
461
462 Constraint *create_return_by_value_constraint(intptr_t value_to_return, size_t size) {
463 intptr_t actual_return = (intptr_t) malloc(size);
464 memcpy((void*)actual_return, (void*)value_to_return, size);
465 Constraint* constraint = create_constraint();
466 constraint->type = CGREEN_RETURN_BY_VALUE_CONSTRAINT;
467
468 constraint->compare = &compare_true;
469 constraint->execute = &test_true;
470 constraint->name = "return by value";
471 constraint->expected_value = make_cgreen_by_value((void*)actual_return, size);
472 constraint->destroy = &destroy_by_value_constraint;
473
474 return constraint;
475 }
476
477 Constraint *create_return_double_value_constraint(double value_to_return) {
478 Constraint* constraint = create_constraint();
479 constraint->type = CGREEN_RETURN_VALUE_CONSTRAINT;
480
481 constraint->compare = &compare_true;
482 constraint->execute = &test_true;
483 constraint->name = "return value";
484 constraint->expected_value = make_cgreen_double_value(value_to_return);
485
486 return constraint;
487 }
488
489 Constraint *create_set_parameter_value_constraint(const char *parameter_name, intptr_t value_to_set, size_t size_to_set) {
490 Constraint* constraint = create_constraint();
491 constraint->type = CGREEN_CONTENT_SETTER_CONSTRAINT;
492
493 /* Issue #337: copy the source value now, at expect() time, into an owned
494 buffer. Storing the source pointer let a local or computed source dangle
495 before the mock ran (and repeated helper calls reused one stack slot).
496 The copy is freed via destroy_by_value_constraint when the expectation
497 is retired -- same pattern as create_return_by_value_constraint(). */
498 intptr_t value_copy = (intptr_t)malloc(size_to_set);
499 memcpy((void *)value_copy, (void *)value_to_set, size_to_set);
500
501 constraint->compare = &compare_true;
502 constraint->execute = &set_contents;
503 constraint->destroy = &destroy_by_value_constraint;
504 constraint->name = "set parameter value";
505 constraint->expected_value = make_cgreen_by_value((void *)value_copy, size_to_set);
506 constraint->size_of_expected_value = size_to_set;
507 constraint->parameter_name = parameter_name;
508
509 return constraint;
510 }
511
512 static void report_unsupported_scalar_size(Constraint *constraint, const char *function, CgreenValue actual,
513 const char *test_file, int test_line, TestReporter *reporter) {
514 (void)constraint; (void)function; (void)actual;
515 (*reporter->assert_true)(
516 reporter, test_file, test_line, false,
517 "will_set_scalar_output_parameter() was given an unsupported scalar size;\n"
518 "\t\tuse will_set_contents_of_output_parameter() for values wider than 8 bytes or for non-scalars.");
519 }
520
521 Constraint *create_set_scalar_output_parameter_constraint(const char *parameter_name, intmax_t value, size_t size) {
522 /* Re-narrow the widened value to a real typed temporary so the stored bytes
523 are native-endian, then reuse the (copy-at-expect-time) contents setter.
524 The temporary only has to survive the delegated call, which copies it. */
525 switch (size) {
526 case 1: { int8_t v = (int8_t)value; return create_set_parameter_value_constraint(parameter_name, (intptr_t)&v, size); }
527 case 2: { int16_t v = (int16_t)value; return create_set_parameter_value_constraint(parameter_name, (intptr_t)&v, size); }
528 case 4: { int32_t v = (int32_t)value; return create_set_parameter_value_constraint(parameter_name, (intptr_t)&v, size); }
529 case 8: { int64_t v = (int64_t)value; return create_set_parameter_value_constraint(parameter_name, (intptr_t)&v, size); }
530 default: {
531 /* Only reachable for scalars wider than intmax_t (e.g. __int128);
532 report at mock time rather than silently truncating. */
533 Constraint *constraint = create_constraint();
534 constraint->type = CGREEN_CONTENT_SETTER_CONSTRAINT;
535 constraint->compare = &compare_true;
536 constraint->execute = &report_unsupported_scalar_size;
537 constraint->name = "set parameter value";
538 constraint->parameter_name = parameter_name;
539 constraint->size_of_expected_value = size;
540 return constraint;
541 }
542 }
543 }
544
545 Constraint *create_with_side_effect_constraint(void (*callback)(void *), void *data) {
546 Constraint* constraint = create_constraint();
547 constraint->type = CGREEN_CALL_CONSTRAINT;
548
549 constraint->name = "cause side effect";
550 constraint->side_effect_callback = callback;
551 constraint->side_effect_data = data;
552 constraint->execute = &execute_sideeffect;
553
554 return constraint;
555 }
556
557 Constraint *create_capture_parameter_constraint(const char *parameter_name, void *capture_to, size_t size_to_capture) {
558 Constraint* constraint = create_constraint();
559 constraint->type = CGREEN_CAPTURE_PARAMETER_CONSTRAINT;
560
561 constraint->compare = &compare_true;
562 constraint->execute = &capture_parameter;
563 constraint->name = "capture parameter";
564 constraint->expected_value = make_cgreen_pointer_value(capture_to);
565 constraint->size_of_expected_value = size_to_capture;
566 constraint->parameter_name = parameter_name;
567
568 return constraint;
569 }
570
571 bool compare_want_value(Constraint *constraint, CgreenValue actual) {
572 return constraint->expected_value.value.integer_value == actual.value.integer_value;
573 }
574
575 bool compare_do_not_want_value(Constraint *constraint, CgreenValue actual) {
576 return !compare_want_value(constraint, actual);
577 }
578
579 bool compare_want_greater_value(Constraint *constraint, CgreenValue actual) {
580 return actual.value.integer_value > constraint->expected_value.value.integer_value ;
581 }
582
583 bool compare_want_lesser_value(Constraint *constraint, CgreenValue actual) {
584 return actual.value.integer_value < constraint->expected_value.value.integer_value;
585 }
586
587
588 bool compare_want_contents(Constraint *constraint, CgreenValue actual) {
589 /* We can't inspect the contents of a NULL pointer, so comparison always fails */
590 /* TODO: This should really be .pointer_value */
591 if ((void *)actual.value.integer_value == NULL) {
592 return 0;
593 }
594
595 return 0 == memcmp(constraint->expected_value.value.pointer_value,
596 (void *)actual.value.integer_value, constraint->size_of_expected_value);
597 }
598
599 bool compare_do_not_want_contents(Constraint *constraint, CgreenValue actual) {
600 /* we can't inspect the contents of a NULL pointer, so comparison always fails */
601 if ((void *)actual.value.integer_value == NULL) {
602 return 0;
603 }
604
605 return !compare_want_contents(constraint, actual);
606 }
607
608 static void set_contents(Constraint *constraint, const char *function, CgreenValue actual,
609 const char *test_file, int test_line, TestReporter *reporter) {
610 char *message;
611 (void)function;
612
613 /* TODO: should propagate the whole actual */
614 if (parameters_are_not_valid_for(constraint, actual.value.integer_value)) {
615 message = validation_failure_message_for(constraint, actual.value.integer_value);
616 (*reporter->assert_true)(
617 reporter,
618 test_file,
619 test_line,
620 false,
621 message);
622
623 free(message);
624 return;
625 }
626
627 if (actual.value.pointer_value == NULL) {
628 return; /* Issue #202: a NULL out-parameter is a no-op, not a crash */
629 }
630
631 memmove((void *)actual.value.pointer_value, constraint->expected_value.value.pointer_value, constraint->size_of_expected_value);
632 }
633
634 static void execute_sideeffect(Constraint *constraint, const char *function, CgreenValue actual,
635 const char *test_file, int test_line, TestReporter *reporter) {
636 (void)function;
637 (void)actual;
638
639 if (constraint->side_effect_callback == NULL) {
640 (*reporter->assert_true)(
641 reporter,
642 test_file,
643 test_line,
644 false,
645 "no side effect function was set");
646 }
647 (constraint->side_effect_callback)(constraint->side_effect_data);
648 }
649
650 #define IS_BIG_ENDIAN (!*(unsigned char *)&(uint16_t){1})
651 static bool bigendian(void) { return IS_BIG_ENDIAN; }
652
653 static void capture_parameter(Constraint *constraint, const char *function, CgreenValue actual,
654 const char *test_file, int test_line, TestReporter *reporter) {
655 (void)function;
656 (void)test_file;
657 (void)test_line;
658 (void)reporter;
659
660 if ((sizeof(intptr_t) != constraint->size_of_expected_value) && bigendian()) {
661 // Then the beginning of a smaller value is not stored at the beginning of the actual.value union
662 size_t offset = sizeof(intptr_t) - constraint->size_of_expected_value;
663 // Offset is in bytes so we need to cast &actual.value to that before adding the offset
664 void *start_address = (unsigned char *)&actual.value + offset;
665 memmove(constraint->expected_value.value.pointer_value, start_address, constraint->size_of_expected_value);
666
667 } else
668 memmove(constraint->expected_value.value.pointer_value, &actual.value,
669 constraint->size_of_expected_value);
670 }
671
672
673 void test_want(Constraint *constraint, const char *function, CgreenValue actual,
674 const char *test_file, int test_line, TestReporter *reporter) {
675 char *message;
676 char parameter_name_actual_string[255];
677
678 if (parameters_are_not_valid_for(constraint, actual.value.integer_value)) {
679 message = validation_failure_message_for(constraint, actual.value.integer_value);
680
681 (*reporter->assert_true)(
682 reporter,
683 test_file,
684 test_line,
685 false,
686 message);
687
688 free(message);
689
690 return;
691 }
692
693 snprintf(parameter_name_actual_string, sizeof(parameter_name_actual_string) - 1, "[%s] parameter in [%s]", constraint->parameter_name, function);
694 message = constraint->failure_message(constraint, parameter_name_actual_string, actual.value.integer_value);
695
696 (*reporter->assert_true)(
697 reporter,
698 test_file,
699 test_line,
700 (*constraint->compare)(constraint, actual),
701 message);
702
703 free(message);
704 }
705
706 static bool compare_want_string(Constraint *constraint, CgreenValue actual) {
707 return strings_are_equal(constraint->expected_value.value.string_value, actual.value.string_value);
708 }
709
710 static bool compare_do_not_want_string(Constraint *constraint, CgreenValue actual) {
711 return !compare_want_string(constraint, actual);
712 }
713
714 static bool compare_do_not_want_substring(Constraint *constraint, CgreenValue actual) {
715 return !compare_want_substring(constraint, actual);
716 }
717
718 static bool compare_want_substring(Constraint *constraint, CgreenValue actual) {
719 return string_contains(actual.value.string_value, constraint->expected_value.value.string_value);
720 }
721
722
723 const unsigned int NOT_FOUND = UINT_MAX;
724
725 static unsigned int strpos(const char *haystack, const char *needle)
726 {
727 const char *offset = strstr(haystack, needle);
728 if (offset != NULL) {
729 return offset - haystack;
730 }
731
732 return NOT_FOUND;
733 }
734
735 static bool compare_want_beginning_of_string(Constraint *constraint, CgreenValue actual) {
736 return strpos(actual.value.string_value, constraint->expected_value.value.string_value) == 0;
737 }
738
739 static bool compare_do_not_want_beginning_of_string(Constraint *constraint, CgreenValue actual) {
740 return strpos(actual.value.string_value, constraint->expected_value.value.string_value) != 0;
741 }
742
743 static bool compare_want_end_of_string(Constraint *constraint, CgreenValue actual) {
744 int match_length = strlen(constraint->expected_value.value.string_value);
745 int start_position = strlen(actual.value.string_value) - match_length;
746 if (start_position < 0)
747 return false;
748 return strcmp(&actual.value.string_value[start_position], constraint->expected_value.value.string_value) == 0;
749 }
750
751 static bool compare_do_not_want_end_of_string(Constraint *constraint, CgreenValue actual) {
752 return !compare_want_end_of_string(constraint, actual);
753 }
754
755
756
757 // Double
758
759 static bool compare_want_double(Constraint *constraint, CgreenValue actual) {
760 return doubles_are_equal(constraint->expected_value.value.double_value, actual.value.double_value);
761 }
762
763 static bool compare_want_lesser_double(Constraint *constraint, CgreenValue actual) {
764 return double_is_lesser(constraint->expected_value.value.double_value, actual.value.double_value);
765 }
766
767 static bool compare_want_greater_double(Constraint *constraint, CgreenValue actual) {
768 return double_is_greater(constraint->expected_value.value.double_value, actual.value.double_value);
769 }
770
771 static void test_want_double(Constraint *constraint, const char *function, CgreenValue actual,
772 const char *test_file, int test_line, TestReporter *reporter) {
773 (*reporter->assert_true)(
774 reporter,
775 test_file,
776 test_line,
777 (*constraint->compare)(constraint, actual),
778 "Wanted [%f], but got [%f] in function [%s] parameter [%s]",
779 constraint->expected_value.value.double_value,
780 actual.value.double_value,
781 function,
782 constraint->parameter_name);
783 }
784
785 static bool compare_do_not_want_double(Constraint *constraint, CgreenValue actual) {
786 return !compare_want_double(constraint, actual);
787 }
788
789 static void test_do_not_want_double(Constraint *constraint, const char *function, CgreenValue actual,
790 const char *test_file, int test_line, TestReporter *reporter) {
791 (*reporter->assert_true)(
792 reporter,
793 test_file,
794 test_line,
795 (*constraint->compare)(constraint, actual),
796 "Did not want [%f], but got [%f] in function [%s] parameter [%s]",
797 constraint->expected_value.value.double_value,
798 actual.value.double_value,
799 function,
800 constraint->parameter_name);
801 }
802
803 void destroy_double_constraint(Constraint *constraint) {
804 destroy_empty_constraint(constraint);
805 }
806
807 void destroy_string_constraint(Constraint *constraint) {
808 destroy_cgreen_value(constraint->expected_value);
809 destroy_empty_constraint(constraint);
810 }
811
812 void destroy_by_value_constraint(Constraint *constraint) {
813 // TODO: we should return the allocated area, but that conflicts with reporter printing it
814 // Now, how does the string constraints do it?
815 destroy_cgreen_value(constraint->expected_value);
816 destroy_empty_constraint(constraint);
817 }
818
819 static bool compare_true(Constraint *constraint, CgreenValue actual) {
820 (void)constraint;
821 (void)actual;
822
823 return true;
824 }
825
826 static void test_true(Constraint *constraint, const char *function, CgreenValue actual,
827 const char *test_file, int test_line, TestReporter *reporter) {
828 (void)constraint;
829 (void)function;
830 (void)actual;
831 (void)test_file;
832 (void)test_line;
833 (void)reporter;
834 }
835
836 bool values_are_strings_in(const Constraint *constraint) {
837 return is_string_comparing(constraint) &&
838 (constraint->expected_value.value.string_value != NULL);
839 }
840
841 bool no_expected_value_in(const Constraint *constraint) {
842 return strlen(constraint->expected_value_message) == 0;
843 }
844
845 bool is_content_comparing(const Constraint *constraint) {
846 return constraint->type == CGREEN_CONTENT_COMPARER_CONSTRAINT;
847 }
848
849 bool is_content_setting(const Constraint *constraint) {
850 return constraint->type == CGREEN_CONTENT_SETTER_CONSTRAINT;
851 }
852
853 bool is_not_content_setting(const Constraint *constraint) {
854 return !is_content_setting(constraint);
855 }
856
857 bool is_string_comparing(const Constraint *constraint) {
858 return constraint->type == CGREEN_STRING_COMPARER_CONSTRAINT;
859 }
860
861 bool is_double_comparing(const Constraint *constraint) {
862 return constraint->type == CGREEN_DOUBLE_COMPARER_CONSTRAINT;
863 }
864
865 bool is_comparing(const Constraint *constraint) {
866 return is_string_comparing(constraint) ||
867 is_content_comparing(constraint) ||
868 is_double_comparing(constraint) ||
869 constraint->type == CGREEN_VALUE_COMPARER_CONSTRAINT ||
870 constraint->type == CGREEN_CAPTURE_PARAMETER_CONSTRAINT;
871 }
872
873 bool is_not_comparing(const Constraint *constraint) {
874 return !is_comparing(constraint);
875 }
876
877 bool is_parameter(const Constraint *constraint) {
878 return is_comparing(constraint) || is_content_setting(constraint);
879 }
880
881 bool constraint_is_for_parameter_in(const Constraint *constraint, const char *names) {
882 int i;
883 bool found = false;
884
885 CgreenVector *parameter_names = create_vector_of_names(names);
886 if (!is_parameter(constraint)) return false;
887
888 for (i = 0; i < cgreen_vector_size(parameter_names); i++) {
889 const char *mock_parameter_name = (const char *)cgreen_vector_get(parameter_names, i);
890
891 if (constraint_is_for_parameter(constraint, mock_parameter_name)) {
892 found = true;
893 break;
894 }
895 }
896
897 destroy_cgreen_vector(parameter_names);
898 return found;
899 }
900
901 bool doubles_are_equal(double tried, double expected) {
902 double abs_diff = fabs(tried - expected);
903 if (abs_diff < absolute_tolerance) return true;
904 return abs_diff < accuracy(significant_figures, max(fabs(tried), fabs(expected)));
905 }
906
907 bool double_is_lesser(double actual, double expected) {
908 return expected < actual + accuracy(significant_figures, max(actual, expected));
909 }
910
911 bool double_is_greater(double actual, double expected) {
912 return expected > actual - accuracy(significant_figures, max(actual, expected));
913 }
914
915 static double accuracy(int figures, double largest) {
916 return pow(10.0, 1.0 + floor(log10(fabs(largest))) - figures);
917 }
918
919 void significant_figures_for_assert_double_are(int figures) {
920 significant_figures = figures;
921 }
922
923 int get_significant_figures(void) {
924 return significant_figures;
925 }
926
927 /* vim: set ts=4 sw=4 et cindent: */