| 1 | #include <cgreen/boxed_double.h> |
| 2 | #include <cgreen/cgreen.h> |
| 3 | #include <cgreen/constraint.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | #ifdef __cplusplus |
| 7 | using namespace cgreen; |
| 8 | #endif |
| 9 | |
| 10 | #include "../src/cgreen_value_internal.h" |
| 11 | #include "../src/constraint_internal.h" |
| 12 | |
| 13 | static bool compare_integer_constraint(Constraint *c, int x) { |
| 14 | CgreenValue v = make_cgreen_integer_value(x); |
| 15 | bool r = (*c->compare)(c, v); |
| 16 | destroy_cgreen_value(v); |
| 17 | return r; |
| 18 | } |
| 19 | |
| 20 | static bool compare_pointer_constraint(Constraint *c, void *x) { |
| 21 | CgreenValue v = make_cgreen_pointer_value(x); |
| 22 | bool r = (*c->compare)(c, v); |
| 23 | destroy_cgreen_value(v); |
| 24 | return r; |
| 25 | } |
| 26 | |
| 27 | static bool compare_string_constraint(Constraint *c, const char *x) { |
| 28 | CgreenValue v = make_cgreen_string_value(x); |
| 29 | bool r = (*c->compare)(c, v); |
| 30 | destroy_cgreen_value(v); |
| 31 | return r; |
| 32 | } |
| 33 | |
| 34 | static bool compare_double_constraint(Constraint *c, double x) { |
| 35 | CgreenValue v = make_cgreen_double_value(x); |
| 36 | bool r = (*c->compare)(c, v); |
| 37 | destroy_cgreen_value(v); |
| 38 | return r; |
| 39 | } |
| 40 | |
| 41 | Describe(Constraint); |
| 42 | BeforeEach(Constraint) {} |
| 43 | AfterEach(Constraint) {} |
| 44 | |
| 45 | Ensure(Constraint, default_destroy_clears_state) { |
| 46 | Constraint *constraint = create_constraint(); |
| 47 | destroy_constraint(constraint); |
| 48 | |
| 49 | /* these tests correctly trip valgrind's use-after-free check, so |
| 50 | * uncomment and check manually |
| 51 | |
| 52 | assert_that(constraint->name, NULL); |
| 53 | assert_that(constraint->parameter, NULL); |
| 54 | assert_that(constraint->storedValue, NULL); |
| 55 | assert_that(constraint->test, NULL); |
| 56 | assert_that(constraint->compare, NULL); |
| 57 | assert_that(constraint->destroy, NULL); |
| 58 | */ |
| 59 | } |
| 60 | |
| 61 | Ensure(Constraint, parameter_name_matches_correctly) { |
| 62 | Constraint *constraint = |
| 63 | create_constraint(); |
| 64 | constraint->type = CGREEN_VALUE_COMPARER_CONSTRAINT; |
| 65 | constraint->parameter_name = "label"; |
| 66 | |
| 67 | assert_that(constraint_is_not_for_parameter(constraint, "wrong_label"), is_true); |
| 68 | assert_that(constraint_is_not_for_parameter(constraint, "label"), is_false); |
| 69 | |
| 70 | destroy_constraint(constraint); |
| 71 | } |
| 72 | |
| 73 | Ensure(Constraint, compare_contents_is_correct_on_larger_than_intptr_array) { |
| 74 | int content[] = { 0, 1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10, 11, 12, 13, 14, 15 }; |
| 75 | int also_content[] = { 0, 1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10, 11, 12, 13, 14, 15 }; |
| 76 | Constraint *is_equal_to_contents_constraint = |
| 77 | create_equal_to_contents_constraint(content, sizeof(content), "content"); |
| 78 | |
| 79 | int not_content[] = { 0, 1, 2, 3, 4, 5, 6, 7, 108, 109, 110, 111, 112, 113, 114, 115 }; |
| 80 | assert_that(compare_pointer_constraint(is_equal_to_contents_constraint, also_content), is_true); |
| 81 | assert_that(compare_pointer_constraint(is_equal_to_contents_constraint, not_content), is_false); |
| 82 | |
| 83 | destroy_constraint(is_equal_to_contents_constraint); |
| 84 | } |
| 85 | |
| 86 | Ensure(Constraint, compare_is_correct_when_using_integers) { |
| 87 | Constraint *is_equal_to_37 = create_equal_to_value_constraint(37, "37"); |
| 88 | |
| 89 | assert_that(compare_integer_constraint(is_equal_to_37, 37), is_true); |
| 90 | assert_that(compare_integer_constraint(is_equal_to_37, 36), is_false); |
| 91 | |
| 92 | destroy_constraint(is_equal_to_37); |
| 93 | } |
| 94 | |
| 95 | Ensure(Constraint, compare_to_is_null_correctly) { |
| 96 | assert_that(0, is_null); |
| 97 | assert_that(14, is_not_null); |
| 98 | } |
| 99 | |
| 100 | Ensure(Constraint, string_constraint_destroy_clears_state) { |
| 101 | Constraint *string_constraint = |
| 102 | create_equal_to_string_constraint("Hello", "user_greeting"); |
| 103 | destroy_constraint(string_constraint); |
| 104 | |
| 105 | /* these checks correctly trip valgrind's use-after-free check, so |
| 106 | * uncomment and check manually |
| 107 | |
| 108 | assert_that(string_constraint->name, is_null); |
| 109 | assert_that(string_constraint->parameter, is_null); |
| 110 | assert_that(string_constraint->storedValue, is_null); |
| 111 | assert_that(string_constraint->test, is_null); |
| 112 | assert_that(string_constraint->compare, is_null); |
| 113 | assert_that(string_constraint->destroy, is_null); |
| 114 | */ |
| 115 | } |
| 116 | |
| 117 | Ensure(Constraint, matching_strings_as_equal) { |
| 118 | Constraint *equals_string_hello_constraint = |
| 119 | create_equal_to_string_constraint("Hello", "user_greeting"); |
| 120 | |
| 121 | assert_that(compare_string_constraint(equals_string_hello_constraint, "Hello"), is_true); |
| 122 | assert_that(compare_string_constraint(equals_string_hello_constraint, "Goodbye"), is_false); |
| 123 | |
| 124 | destroy_constraint(equals_string_hello_constraint); |
| 125 | } |
| 126 | |
| 127 | Ensure(Constraint, matching_beginning_of_string) { |
| 128 | Constraint *beginning_of_string_hello_constraint = |
| 129 | create_begins_with_string_constraint("Hell", "user_greeting"); |
| 130 | |
| 131 | assert_that(compare_string_constraint(beginning_of_string_hello_constraint, "Hello"), is_true); |
| 132 | assert_that(compare_string_constraint(beginning_of_string_hello_constraint, "Goodbye"), is_false); |
| 133 | |
| 134 | destroy_constraint(beginning_of_string_hello_constraint); |
| 135 | } |
| 136 | |
| 137 | Ensure(Constraint, not_matching_beginning_of_string) { |
| 138 | Constraint *beginning_of_string_hello_constraint = |
| 139 | create_does_not_begin_with_string_constraint("Goodby", "user_greeting"); |
| 140 | |
| 141 | assert_that(compare_string_constraint(beginning_of_string_hello_constraint, "Hello"), is_true); |
| 142 | assert_that(compare_string_constraint(beginning_of_string_hello_constraint, "Goodbye"), is_false); |
| 143 | |
| 144 | destroy_constraint(beginning_of_string_hello_constraint); |
| 145 | } |
| 146 | |
| 147 | Ensure(Constraint, matching_end_of_string) { |
| 148 | Constraint *end_of_string_hello_constraint = |
| 149 | create_ends_with_string_constraint("ello", "user_greeting"); |
| 150 | |
| 151 | assert_that(compare_string_constraint(end_of_string_hello_constraint, "Hello"), is_true); |
| 152 | assert_that(compare_string_constraint(end_of_string_hello_constraint, "Goodbye"), is_false); |
| 153 | assert_that(compare_string_constraint(end_of_string_hello_constraint, "Hello ello"), is_true); |
| 154 | |
| 155 | destroy_constraint(end_of_string_hello_constraint); |
| 156 | } |
| 157 | |
| 158 | Ensure(Constraint, not_matching_end_of_string) { |
| 159 | Constraint *end_of_string_hello_constraint = |
| 160 | create_does_not_end_with_string_constraint("oodbye", "user_greeting"); |
| 161 | |
| 162 | assert_that(compare_string_constraint(end_of_string_hello_constraint, "Hello"), is_true); |
| 163 | assert_that(compare_string_constraint(end_of_string_hello_constraint, "Goodbye"), is_false); |
| 164 | |
| 165 | destroy_constraint(end_of_string_hello_constraint); |
| 166 | } |
| 167 | |
| 168 | Ensure(Constraint, matching_null_string_against_non_null_string) { |
| 169 | Constraint *equals_string_hello_constraint = |
| 170 | create_equal_to_string_constraint("Hello", "user_greeting"); |
| 171 | |
| 172 | assert_that(compare_string_constraint(equals_string_hello_constraint, NULL), is_false); |
| 173 | |
| 174 | destroy_constraint(equals_string_hello_constraint); |
| 175 | } |
| 176 | |
| 177 | Ensure(Constraint, matching_against_null_string) { |
| 178 | Constraint *equals_null_string_constraint = |
| 179 | create_equal_to_string_constraint((const char *)NULL, "user_greeting"); |
| 180 | |
| 181 | assert_that(compare_string_constraint(equals_null_string_constraint, NULL), is_true); |
| 182 | assert_that(compare_string_constraint(equals_null_string_constraint, "Hello"), is_false); |
| 183 | |
| 184 | destroy_constraint(equals_null_string_constraint); |
| 185 | } |
| 186 | |
| 187 | Ensure(Constraint, matching_doubles_as_equal_with_default_significance) { |
| 188 | Constraint *equal_to_double_37 = create_equal_to_double_constraint(37.0, "height"); |
| 189 | |
| 190 | assert_that(compare_double_constraint(equal_to_double_37, 37.0), is_true); |
| 191 | assert_that(compare_double_constraint(equal_to_double_37, 36.0), is_false); |
| 192 | |
| 193 | destroy_constraint(equal_to_double_37); |
| 194 | } |
| 195 | |
| 196 | Ensure(Constraint, matching_doubles_respects_significant_figure_setting) { |
| 197 | Constraint *want_337 = create_equal_to_double_constraint(337.0, "height"); |
| 198 | |
| 199 | significant_figures_for_assert_double_are(2); |
| 200 | assert_that(compare_double_constraint(want_337, 339.0), is_true); |
| 201 | |
| 202 | significant_figures_for_assert_double_are(3); |
| 203 | assert_that(compare_double_constraint(want_337, 339.0), is_false); |
| 204 | |
| 205 | destroy_constraint(want_337); |
| 206 | } |
| 207 | |
| 208 | // TODO: convert to create_constraint_for_comparator("non-empty string", &is_non_empty_string); |
| 209 | //typedef struct { |
| 210 | // char* value; |
| 211 | // unsigned int length; |
| 212 | //} String; |
| 213 | // |
| 214 | //static int is_non_empty_string(const void* other) { |
| 215 | // String* our = (String*)other; |
| 216 | // return our->length != 0; |
| 217 | //} |
| 218 | // |
| 219 | //Ensure(Constraint, unequal_structs_with_same_value_for_specific_field_compare_true) { |
| 220 | // String name; |
| 221 | // name.value = "bob"; |
| 222 | // name.length = 3; |
| 223 | // |
| 224 | // Constraint *string_constraint = with(name, is_non_empty_string); |
| 225 | // |
| 226 | // assert_that(compare_constraint(string_constraint, is_non_empty_string), is_equal_to(1)); |
| 227 | // |
| 228 | // destroy_constraint(string_constraint); |
| 229 | // |
| 230 | // name.value = "tim"; |
| 231 | // name.length = 3; |
| 232 | // |
| 233 | // string_constraint = with(name, is_non_empty_string); |
| 234 | // assert_that(compare_constraint(string_constraint, is_non_empty_string), is_equal_to(1)); |
| 235 | // |
| 236 | // destroy_constraint(string_constraint); |
| 237 | //} |
| 238 | |
| 239 | |
| 240 | /* these misuse scenarios should be prevented by checks in higher-level constructs */ |
| 241 | #ifdef CANNOT_CREATE_NULL_CONSTRAINTS |
| 242 | Ensure(Constraint, cannot_create_contents_constraint_with_null_content) { |
| 243 | const size_t NON_ZERO = !0; |
| 244 | Constraint *is_equal_to_contents_constraint = create_equal_to_contents_constraint(NULL, NON_ZERO, "NULL"); |
| 245 | |
| 246 | assert_that(is_equal_to_contents_constraint, is_null); |
| 247 | } |
| 248 | |
| 249 | Ensure(Constraint, cannot_create_contents_constraint_with_zero_size) { |
| 250 | int content[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; |
| 251 | Constraint *is_equal_to_contents_constraint = create_equal_to_contents_constraint(content, 0, "content"); |
| 252 | |
| 253 | assert_that(is_equal_to_contents_constraint, is_null); |
| 254 | } |
| 255 | #endif |
| 256 | |
| 257 | Ensure(Constraint, compare_equal_to_contents_is_false_on_null) { |
| 258 | int content[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; |
| 259 | Constraint *is_equal_to_contents_constraint = |
| 260 | create_equal_to_contents_constraint(content, sizeof(content), "content"); |
| 261 | |
| 262 | assert_that(compare_pointer_constraint(is_equal_to_contents_constraint, NULL), is_false); |
| 263 | |
| 264 | destroy_constraint(is_equal_to_contents_constraint); |
| 265 | } |
| 266 | |
| 267 | Ensure(Constraint, compare_not_equal_to_contents_is_false_on_null) { |
| 268 | int content[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; |
| 269 | Constraint *is_not_equal_to_contents = |
| 270 | create_not_equal_to_contents_constraint(content, sizeof(content), "content"); |
| 271 | |
| 272 | assert_that(compare_pointer_constraint(is_not_equal_to_contents, NULL), is_false); |
| 273 | |
| 274 | destroy_constraint(is_not_equal_to_contents); |
| 275 | } |
| 276 | |
| 277 | Ensure(Constraint, can_compare_to_hex) { |
| 278 | char chars[3]; |
| 279 | memset(chars, 0xaa, sizeof(chars)); |
| 280 | assert_that((unsigned char)chars[0], is_equal_to_hex(0xaa)); |
| 281 | } |
| 282 | |
| 283 | TestSuite *constraint_tests(void) { |
| 284 | TestSuite *suite = create_test_suite(); |
| 285 | add_test_with_context(suite, Constraint, default_destroy_clears_state); |
| 286 | add_test_with_context(suite, Constraint, parameter_name_matches_correctly); |
| 287 | add_test_with_context(suite, Constraint, compare_contents_is_correct_on_larger_than_intptr_array); |
| 288 | add_test_with_context(suite, Constraint, compare_is_correct_when_using_integers); |
| 289 | add_test_with_context(suite, Constraint, compare_to_is_null_correctly); |
| 290 | add_test_with_context(suite, Constraint, string_constraint_destroy_clears_state); |
| 291 | add_test_with_context(suite, Constraint, matching_strings_as_equal); |
| 292 | add_test_with_context(suite, Constraint, matching_beginning_of_string); |
| 293 | add_test_with_context(suite, Constraint, not_matching_beginning_of_string); |
| 294 | add_test_with_context(suite, Constraint, matching_end_of_string); |
| 295 | add_test_with_context(suite, Constraint, not_matching_end_of_string); |
| 296 | add_test_with_context(suite, Constraint, matching_null_string_against_non_null_string); |
| 297 | add_test_with_context(suite, Constraint, matching_against_null_string); |
| 298 | add_test_with_context(suite, Constraint, matching_doubles_as_equal_with_default_significance); |
| 299 | add_test_with_context(suite, Constraint, matching_doubles_respects_significant_figure_setting); |
| 300 | add_test_with_context(suite, Constraint, compare_equal_to_contents_is_false_on_null); |
| 301 | add_test_with_context(suite, Constraint, compare_not_equal_to_contents_is_false_on_null); |
| 302 | add_test_with_context(suite, Constraint, can_compare_to_hex); |
| 303 | // add_test_with_context(suite, Constraint, unequal_structs_with_same_value_for_specific_field_compare_true); |
| 304 | |
| 305 | return suite; |
| 306 | } |