| 1 | #include <cgreen/cgreen.h> |
| 2 | #include <cgreen/legacy.h> |
| 3 | |
| 4 | #ifndef __cplusplus |
| 5 | #include <stdbool.h> |
| 6 | #endif |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | using namespace cgreen; |
| 12 | #endif |
| 13 | |
| 14 | Ensure(integer_one_should_assert_true) { |
| 15 | assert_that(1, is_true); |
| 16 | } |
| 17 | |
| 18 | Ensure(integer_one_should_assert_true_short) { |
| 19 | assert_that(1); |
| 20 | } |
| 21 | |
| 22 | Ensure(integer_two_should_assert_true) { |
| 23 | assert_that(2, is_true); |
| 24 | } |
| 25 | |
| 26 | Ensure(integer_zero_should_assert_false) { |
| 27 | assert_that(0, is_false); |
| 28 | } |
| 29 | |
| 30 | Ensure(one_should_assert_equal_to_one) { |
| 31 | assert_that(1, is_equal_to(1)); |
| 32 | } |
| 33 | |
| 34 | Ensure(zero_should_assert_not_equal_to_one) { |
| 35 | assert_that(0, is_not_equal_to(1)); |
| 36 | } |
| 37 | |
| 38 | Ensure(one_should_assert_long_equal_to_one) { |
| 39 | long x = 1; |
| 40 | long y = 1; |
| 41 | assert_that(x, is_equal_to(y)); |
| 42 | } |
| 43 | |
| 44 | Ensure(zero_should_assert_long_not_equal_to_one) { |
| 45 | long x = 0; |
| 46 | long y = 1; |
| 47 | assert_that(x, is_not_equal_to(y)); |
| 48 | } |
| 49 | |
| 50 | Ensure(one_should_assert_unsigned_long_equal_to_one) { |
| 51 | unsigned long x = 1; |
| 52 | unsigned long y = 1; |
| 53 | assert_that(x, is_equal_to(y)); |
| 54 | } |
| 55 | |
| 56 | Ensure(zero_should_assert_unsigned_long_not_equal_to_one) { |
| 57 | unsigned long x = 0; |
| 58 | unsigned long y = 1; |
| 59 | assert_that(x, is_not_equal_to(y)); |
| 60 | } |
| 61 | |
| 62 | Ensure(one_should_assert_long_long_equal_to_one) { |
| 63 | long long x = 1; |
| 64 | long long y = 1; |
| 65 | assert_that(x, is_equal_to(y)); |
| 66 | } |
| 67 | |
| 68 | Ensure(zero_should_assert_long_long_not_equal_to_one) { |
| 69 | long long x = 0; |
| 70 | long long y = 1; |
| 71 | assert_that(x, is_not_equal_to(y)); |
| 72 | } |
| 73 | |
| 74 | Ensure(one_should_assert_unsigned_long_long_equal_to_one) { |
| 75 | unsigned long long x = 1; |
| 76 | unsigned long long y = 1; |
| 77 | assert_equal(x, y); |
| 78 | } |
| 79 | |
| 80 | Ensure(zero_should_assert_unsigned_long_long_not_equal_to_one) { |
| 81 | unsigned long long x = 0; |
| 82 | unsigned long long y = 1; |
| 83 | assert_that(x, is_not_equal_to(y)); |
| 84 | } |
| 85 | |
| 86 | Ensure(one_should_assert_short_equal_to_one) { |
| 87 | short x = 1; |
| 88 | short y = 1; |
| 89 | assert_equal(x, y); |
| 90 | } |
| 91 | |
| 92 | Ensure(zero_should_assert_short_not_equal_to_one) { |
| 93 | short x = 0; |
| 94 | short y = 1; |
| 95 | assert_not_equal(x, y); |
| 96 | } |
| 97 | |
| 98 | Ensure(one_should_assert_unsigned_short_equal_to_one) { |
| 99 | unsigned short x = 1; |
| 100 | unsigned short y = 1; |
| 101 | assert_equal(x, y); |
| 102 | } |
| 103 | |
| 104 | Ensure(zero_should_assert_unsigned_short_not_equal_to_one) { |
| 105 | unsigned short x = 0; |
| 106 | unsigned short y = 1; |
| 107 | assert_not_equal(x, y); |
| 108 | } |
| 109 | |
| 110 | Ensure(one_should_assert_char_equal_to_one) { |
| 111 | char x = 1; |
| 112 | char y = 1; |
| 113 | assert_equal(x, y); |
| 114 | } |
| 115 | |
| 116 | Ensure(zero_should_assert_char_not_equal_to_one) { |
| 117 | char x = 0; |
| 118 | char y = 1; |
| 119 | assert_not_equal(x, y); |
| 120 | } |
| 121 | |
| 122 | Ensure(one_should_assert_unsigned_char_equal_to_one) { |
| 123 | unsigned char x = 1; |
| 124 | unsigned char y = 1; |
| 125 | assert_equal(x, y); |
| 126 | } |
| 127 | |
| 128 | Ensure(zero_should_assert_unsigned_char_not_equal_to_one) { |
| 129 | unsigned char x = 0; |
| 130 | unsigned char y = 1; |
| 131 | assert_not_equal(x, y); |
| 132 | } |
| 133 | |
| 134 | Ensure(different_pointers_with_different_contents_should_not_assert_equal) { |
| 135 | int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 666 }; |
| 136 | int other_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 666 }; |
| 137 | |
| 138 | assert_that(data, |
| 139 | is_equal_to_contents_of(other_data, sizeof(other_data)) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | Ensure(different_pointers_with_same_contents_should_assert_equal) { |
| 144 | int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 145 | int same_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 146 | |
| 147 | assert_that(data, |
| 148 | is_equal_to_contents_of(same_data, sizeof(same_data)) |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | Ensure(support_for_pointer_arithmetic) { |
| 153 | |
| 154 | int data[2][4] = {{0,1,2,3},{7,8,9,10}}; |
| 155 | int same_data[2][4] = {{0,1,2,3}, {7,8,9,10}}; |
| 156 | size_t data_sz = sizeof(data)/sizeof(data[0]); |
| 157 | for (size_t i = 0; i < data_sz; i++) |
| 158 | { |
| 159 | assert_that(data + i, |
| 160 | is_equal_to_contents_of(same_data + i, sizeof(same_data[i])) |
| 161 | ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | Ensure(one_should_assert_float_equal_to_one) { |
| 166 | float x = 1; |
| 167 | float y = 1; |
| 168 | assert_that_double(x, is_equal_to_double(y)); |
| 169 | } |
| 170 | |
| 171 | Ensure(zero_should_assert_float_not_equal_to_one) { |
| 172 | float x = 0; |
| 173 | float y = 1; |
| 174 | assert_that_double(x, is_not_equal_to_double(y)); |
| 175 | } |
| 176 | |
| 177 | Ensure(one_should_assert_double_equal_to_one) { |
| 178 | assert_that_double(1, is_equal_to_double(1)); |
| 179 | } |
| 180 | |
| 181 | Ensure(zero_should_assert_double_not_equal_to_one) { |
| 182 | assert_double_not_equal(0, 1); |
| 183 | } |
| 184 | |
| 185 | Ensure(zero_should_assert_double_equal_to_zero) { |
| 186 | assert_double_equal(0, 0); |
| 187 | } |
| 188 | |
| 189 | Ensure(one_should_assert_long_double_equal_to_one) { |
| 190 | long double x = 1; |
| 191 | long double y = 1; |
| 192 | assert_that_double(x, is_equal_to_double(y)); |
| 193 | } |
| 194 | |
| 195 | Ensure(zero_should_assert_long_double_not_equal_to_one) { |
| 196 | long double x = 0; |
| 197 | long double y = 1; |
| 198 | assert_that_double(x, is_not_equal_to_double(y)); |
| 199 | } |
| 200 | |
| 201 | Ensure(double_differences_do_not_matter_past_significant_figures) { |
| 202 | significant_figures_for_assert_double_are(3); |
| 203 | assert_that_double(1.113, is_equal_to_double(1.115)); |
| 204 | assert_that_double(1113, is_equal_to_double(1115)); |
| 205 | assert_that_double(1113000, is_equal_to_double(1115000)); |
| 206 | assert_that_double(1.1199999999, is_less_than_double(1.11)); |
| 207 | assert_that_double(1.11, is_greater_than_double(1.119999)); |
| 208 | } |
| 209 | |
| 210 | Ensure(can_check_equality_of_negative_floating_point_numbers) { |
| 211 | significant_figures_for_assert_double_are(3); |
| 212 | assert_that_double(-1.113, is_equal_to_double(-1.115)); |
| 213 | assert_that_double(-1.13, is_not_equal_to_double(-1.15)); |
| 214 | } |
| 215 | |
| 216 | Ensure(double_one_is_less_than_two) { |
| 217 | assert_that_double(1.0, is_less_than_double(2.0)); |
| 218 | } |
| 219 | |
| 220 | Ensure(double_one_is_greater_than_zero) { |
| 221 | assert_that_double(1.0, is_greater_than_double(0.0)); |
| 222 | } |
| 223 | |
| 224 | Ensure(double_can_compare_negative_numbers) { |
| 225 | assert_that_double(1.0, is_greater_than_double(-10.0)); |
| 226 | assert_that_double(-2.0, is_less_than_double(1.0)); |
| 227 | assert_that_double(-21.0, is_less_than_double(-10.0)); |
| 228 | assert_that_double(-11.0, is_greater_than_double(-12.0)); |
| 229 | } |
| 230 | |
| 231 | Ensure(double_differences_matter_past_significant_figures) { |
| 232 | significant_figures_for_assert_double_are(4); |
| 233 | assert_that_double(1.113, is_not_equal_to_double(1.115)); |
| 234 | assert_that_double(1.113, is_less_than_double(1.115)); |
| 235 | assert_that_double(1.115, is_greater_than_double(1.113)); |
| 236 | assert_that_double(1113, is_not_equal_to_double(1115)); |
| 237 | assert_that_double(1113000, is_not_equal_to_double(1115000)); |
| 238 | } |
| 239 | |
| 240 | Ensure(double_assertions_can_have_custom_messages) { |
| 241 | significant_figures_for_assert_double_are(3); |
| 242 | |
| 243 | /* this passes because the difference is below the set significant figure */ |
| 244 | assert_that_double(1.113, is_equal_to_double(1.115)); |
| 245 | } |
| 246 | |
| 247 | Ensure(identical_string_copies_should_match) { |
| 248 | assert_that("Hello", is_equal_to_string("Hello")); |
| 249 | } |
| 250 | |
| 251 | Ensure(case_different_strings_should_not_match) { |
| 252 | assert_that("Hello", is_not_equal_to_string("hello")); |
| 253 | } |
| 254 | |
| 255 | Ensure(identical_strings_contain_eachother) { |
| 256 | assert_that("Hello", contains_string("Hello")); |
| 257 | } |
| 258 | |
| 259 | Ensure(contains_substring_at_beginning) { |
| 260 | assert_that("Hello", contains_string("Hell")); |
| 261 | } |
| 262 | |
| 263 | Ensure(contains_substring_at_end) { |
| 264 | assert_that("Hello", contains_string("llo")); |
| 265 | } |
| 266 | |
| 267 | Ensure(contains_substring_after_newline) { |
| 268 | assert_that("Hello\nagain", contains_string("gai")); |
| 269 | } |
| 270 | |
| 271 | Ensure(null_string_should_only_match_another_null_string) { |
| 272 | assert_string_equal(NULL, NULL); |
| 273 | assert_string_not_equal(NULL, ""); |
| 274 | assert_string_not_equal("", NULL); |
| 275 | |
| 276 | // fluent syntax technically doesn't allow NULL on left-hand side |
| 277 | // we force the issue to make sure we are testing both syntaxes |
| 278 | assert_that((intptr_t)NULL, is_equal_to_string((char *)NULL)); |
| 279 | assert_that((intptr_t)NULL, is_not_equal_to_string("")); |
| 280 | assert_that("", is_not_equal_to_string((char *)NULL)); |
| 281 | } |
| 282 | |
| 283 | Ensure(null_string_should_only_match_another_null_string_even_with_messages) { |
| 284 | assert_string_equal_with_message(NULL, NULL, "Oh dear"); |
| 285 | assert_string_equal_with_message("", "", "Oh dear"); |
| 286 | assert_string_not_equal_with_message(NULL, "", "Oh dear"); |
| 287 | assert_string_not_equal_with_message("", NULL, "Oh dear"); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * TODO: make TestContext more easily injectable so we can trap the failure |
| 292 | * and compare the message. for now, manually uncomment and verify. |
| 293 | */ |
| 294 | |
| 295 | Ensure(inspecting_contents_of_expected_value_of_null_produces_nice_error_message) { |
| 296 | // char buffer[0]; |
| 297 | // const int NON_ZERO = !0; |
| 298 | // assert_that(buffer, is_equal_to_contents_of(NULL, NON_ZERO)); |
| 299 | } |
| 300 | |
| 301 | Ensure(inspecting_contents_of_actual_value_of_null_produces_nice_error_message) { |
| 302 | // char buffer[0]; |
| 303 | // const int NON_ZERO = !0; |
| 304 | // assert_that(NULL, is_equal_to_contents_of(buffer, NON_ZERO)); |
| 305 | } |
| 306 | |
| 307 | Ensure(inspecting_contents_of_with_zero_size_produces_nice_error_message) { |
| 308 | // char buffer[0]; |
| 309 | // const int ZERO = 0; |
| 310 | // assert_that(buffer, is_equal_to_contents_of(buffer, ZERO)); |
| 311 | } |
| 312 | |
| 313 | Ensure(fail_reports_message) { |
| 314 | // fail("\n*** correctly failing ***\n"); |
| 315 | } |
| 316 | |
| 317 | Ensure(return_value_constraints_are_not_allowed) { |
| 318 | // assert_that(0, will_return(1)); |
| 319 | } |
| 320 | |
| 321 | TestSuite *assertion_tests(void) { |
| 322 | TestSuite *suite = create_test_suite(); |
| 323 | add_test(suite, integer_one_should_assert_true); |
| 324 | add_test(suite, integer_two_should_assert_true); |
| 325 | add_test(suite, integer_zero_should_assert_false); |
| 326 | add_test(suite, one_should_assert_equal_to_one); |
| 327 | add_test(suite, zero_should_assert_not_equal_to_one); |
| 328 | add_test(suite, one_should_assert_long_equal_to_one); |
| 329 | add_test(suite, zero_should_assert_long_not_equal_to_one); |
| 330 | add_test(suite, one_should_assert_unsigned_long_equal_to_one); |
| 331 | add_test(suite, zero_should_assert_unsigned_long_not_equal_to_one); |
| 332 | add_test(suite, one_should_assert_long_long_equal_to_one); |
| 333 | add_test(suite, zero_should_assert_long_long_not_equal_to_one); |
| 334 | add_test(suite, one_should_assert_unsigned_long_long_equal_to_one); |
| 335 | add_test(suite, zero_should_assert_unsigned_long_long_not_equal_to_one); |
| 336 | add_test(suite, one_should_assert_short_equal_to_one); |
| 337 | add_test(suite, zero_should_assert_short_not_equal_to_one); |
| 338 | add_test(suite, one_should_assert_unsigned_short_equal_to_one); |
| 339 | add_test(suite, zero_should_assert_unsigned_short_not_equal_to_one); |
| 340 | add_test(suite, one_should_assert_char_equal_to_one); |
| 341 | add_test(suite, zero_should_assert_char_not_equal_to_one); |
| 342 | add_test(suite, one_should_assert_unsigned_char_equal_to_one); |
| 343 | add_test(suite, zero_should_assert_unsigned_char_not_equal_to_one); |
| 344 | add_test(suite, different_pointers_with_different_contents_should_not_assert_equal); |
| 345 | add_test(suite, different_pointers_with_same_contents_should_assert_equal); |
| 346 | add_test(suite, one_should_assert_float_equal_to_one); |
| 347 | add_test(suite, zero_should_assert_float_not_equal_to_one); |
| 348 | add_test(suite, one_should_assert_double_equal_to_one); |
| 349 | add_test(suite, zero_should_assert_double_not_equal_to_one); |
| 350 | add_test(suite, zero_should_assert_double_equal_to_zero); |
| 351 | add_test(suite, one_should_assert_long_double_equal_to_one); |
| 352 | add_test(suite, zero_should_assert_long_double_not_equal_to_one); |
| 353 | add_test(suite, double_differences_do_not_matter_past_significant_figures); |
| 354 | add_test(suite, can_check_equality_of_negative_floating_point_numbers); |
| 355 | add_test(suite, double_one_is_less_than_two); |
| 356 | add_test(suite, double_one_is_greater_than_zero); |
| 357 | add_test(suite, double_can_compare_negative_numbers); |
| 358 | add_test(suite, double_differences_matter_past_significant_figures); |
| 359 | add_test(suite, double_assertions_can_have_custom_messages); |
| 360 | add_test(suite, identical_string_copies_should_match); |
| 361 | add_test(suite, case_different_strings_should_not_match); |
| 362 | add_test(suite, identical_strings_contain_eachother); |
| 363 | add_test(suite, null_string_should_only_match_another_null_string); |
| 364 | add_test(suite, null_string_should_only_match_another_null_string_even_with_messages); |
| 365 | add_test(suite, inspecting_contents_of_expected_value_of_null_produces_nice_error_message); |
| 366 | add_test(suite, inspecting_contents_of_actual_value_of_null_produces_nice_error_message); |
| 367 | add_test(suite, inspecting_contents_of_with_zero_size_produces_nice_error_message); |
| 368 | add_test(suite, fail_reports_message); |
| 369 | add_test(suite, return_value_constraints_are_not_allowed); |
| 370 | add_test(suite, support_for_pointer_arithmetic); |
| 371 | return suite; |
| 372 | } |