Debian Codegraph @

tests/custom_constraint_messages_tests.c

raw symbols SCIP
1 #include <cgreen/cgreen.h>
2 #include <cgreen/message_formatting.h>
3 #include "constraint_internal.h"
4 #include "cgreen_value_internal.h"
5 #include "utils.h"
6
7 Describe(CustomConstraint);
8 BeforeEach(CustomConstraint) {}
9 AfterEach(CustomConstraint) {}
10
11
12 /* Example 1 from documentation : is_bigger_than_5()
13
14 No argument to the constraint.
15
16 */
17
18 bool compare_want_greater_than_5(Constraint *constraint, CgreenValue actual) {
19 (void)constraint;
20 return actual.value.integer_value > 5;
21 }
22
23 Constraint static_is_bigger_than_5 = {
24 /* .type */ CGREEN_VALUE_COMPARER_CONSTRAINT,
25 /* .name */ "be bigger than 5",
26 /* .destroy */ destroy_static_constraint,
27 /* .compare */ compare_want_greater_than_5,
28 /* .test */ test_want,
29 /* .format_failure_message_for */ failure_message_for,
30 /* .actual_value_message */ "",
31 /* .expected_value_message */ "",
32 /* .expected_value */ {CGREEN_INTEGER, {5}, 0},
33 /* .stored_value_name */ "null",
34 /* .parameter_name */ NULL,
35 /* .size_of_stored_value */ 0,
36 /* .side_effect_callback */ NULL,
37 /* .side_effect_data */ NULL
38 };
39
40 /* Remember: failing tests to get output */
41 Ensure(CustomConstraint, custom_constraint_using_static_function) {
42 Constraint * is_bigger_than_5 = &static_is_bigger_than_5;
43 assert_that(1, is_bigger_than_5);
44 }
45
46
47 /* Example 2 from documentation : is_smaller_than()
48
49 Standard data type argument to constraint.
50
51 */
52
53 bool compare_want_smaller_value(Constraint *constraint, CgreenValue actual) {
54 return actual.value.integer_value < constraint->expected_value.value.integer_value ;
55 }
56
57 Constraint *create_smaller_than_constraint(intptr_t expected_value, const char *expected_value_name) {
58 Constraint *constraint = create_constraint();
59
60 constraint->expected_value = make_cgreen_integer_value(expected_value);
61 constraint->expected_value_name = string_dup(expected_value_name);
62 constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT;
63
64 constraint->compare = &compare_want_smaller_value;
65 constraint->execute = &test_want;
66 constraint->name = "be smaller than";
67 constraint->size_of_expected_value = sizeof(intptr_t);
68
69 return constraint;
70 }
71 #define is_smaller_than(value) create_smaller_than_constraint(value, #value)
72
73
74 Ensure(CustomConstraint, custom_constraint_using_a_function_with_arguments_function) {
75 assert_that(19, is_smaller_than(10));
76 }
77
78
79 /* Example 3 from documentation : can_fit_in_box()
80
81 Using custom data types.
82
83 */
84 typedef struct Struct {
85 char id;
86 int size;
87 } Struct;
88
89 typedef struct Piece {
90 char id;
91 int size;
92 } Piece;
93
94 bool compare_piece_and_box_size(Constraint *constraint, CgreenValue actual) {
95 return ((Piece *)actual.value.pointer_value)->size
96 < ((Struct*)constraint->expected_value.value.pointer_value)->size ;
97 }
98
99 static void test_fit_piece(Constraint *constraint, const char *function_name, CgreenValue actual,
100 const char *test_file, int test_line, TestReporter *reporter) {
101 (*reporter->assert_true)(
102 reporter,
103 test_file,
104 test_line,
105 (*constraint->compare)(constraint, actual),
106 "Piece [%f], does not fit in [%f] in function [%s] parameter [%s]",
107 ((Piece *)constraint->expected_value.value.pointer_value)->id,
108 ((Struct *)actual.value.pointer_value)->id,
109 function_name,
110 constraint->parameter_name);
111 }
112
113 Constraint *create_piece_fit_in_box_constraint(intptr_t expected_value, const char *expected_value_name) {
114 Constraint *constraint = create_constraint();
115
116 constraint->expected_value = make_cgreen_pointer_value((void*)expected_value);
117 constraint->expected_value_name = string_dup(expected_value_name);
118 constraint->type = CGREEN_CONTENT_COMPARER_CONSTRAINT;
119
120 constraint->compare = &compare_piece_and_box_size;
121 constraint->execute = &test_fit_piece;
122 constraint->name = "fit in box";
123 constraint->size_of_expected_value = sizeof(intptr_t);
124
125 return constraint;
126 }
127 #define can_fit_in_box(box) create_piece_fit_in_box_constraint((intptr_t)box, #box)
128
129 Ensure(CustomConstraint, more_complex_custom_constraint_function) {
130 Struct box1 = {.id = (char)1, .size = 5};
131 Piece piece99 = {.id = (char)99, .size = 6};
132 assert_that(&piece99, can_fit_in_box(&box1));
133 }