Jon A. Cruz | 5a75a41 | 2015-07-02 23:36:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Samsung Electronics Co., Ltd |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | /* |
| 29 | * A simple file to show tests being setup and run. |
| 30 | */ |
| 31 | |
| 32 | #include <stddef.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "zunitc/zunitc.h" |
| 38 | |
| 39 | /* |
| 40 | * The SKIP and FAIL sets of tests are those that will cause 'make check' |
| 41 | * to fail so are disabled by default. They can be re-enabled when working |
| 42 | * on the test framework itself. |
| 43 | */ |
| 44 | |
| 45 | /* #define ENABLE_FAIL_TESTS */ |
| 46 | /* #define ENABLE_SKIP_TESTS */ |
| 47 | |
| 48 | ZUC_TEST(base_test, math_is_sane) |
| 49 | { |
| 50 | ZUC_ASSERT_EQ(4, 2 + 2); |
| 51 | } |
| 52 | |
| 53 | ZUC_TEST(base_test, math_is_hard) |
| 54 | { |
| 55 | ZUC_TRACEPOINT("Tracepoint here."); |
| 56 | |
| 57 | ZUC_TRACEPOINT("Checking %d", 4); |
| 58 | |
| 59 | #ifdef ENABLE_FAIL_TESTS |
| 60 | |
| 61 | ZUC_ASSERT_EQ(5, 2 + 2); |
| 62 | ZUC_TRACEPOINT("flip %1.3f", 3.1415927); /* not seen */ |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | #ifdef ENABLE_FAIL_TESTS |
| 67 | ZUC_TEST(base_test, tracepoint_after_assert) |
| 68 | { |
| 69 | ZUC_TRACEPOINT("Should be seen in output"); |
| 70 | |
| 71 | ZUC_ASSERT_EQ(5, 2 + 2); |
| 72 | |
| 73 | ZUC_TRACEPOINT("Should NOT be seen in output"); |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | #ifdef ENABLE_FAIL_TESTS |
| 78 | ZUC_TEST(base_test, math_is_more_hard) |
| 79 | { |
| 80 | ZUC_ASSERT_EQ(5, 2 + 2); |
| 81 | } |
| 82 | |
| 83 | ZUC_TEST(base_test, math_is_more_hard2) |
| 84 | { |
| 85 | ZUC_ASSERT_EQ(7, 9); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | ZUC_TEST(base_test, time_counted) |
| 90 | { |
| 91 | ZUC_TRACEPOINT("Never seen"); |
| 92 | |
| 93 | ZUC_TRACEPOINT("Sleepy Time %d", 10000 * 5); |
| 94 | ZUC_ASSERT_EQ(0, usleep(10000 * 5)); /* 50ms to show up in reporting */ |
| 95 | } |
| 96 | |
| 97 | ZUC_TEST(other_test, math_monkey) |
| 98 | { |
| 99 | ZUC_ASSERT_TRUE(1); |
| 100 | ZUC_ASSERT_TRUE(3); |
| 101 | ZUC_ASSERT_FALSE(0); |
| 102 | |
| 103 | ZUC_ASSERT_TRUE(1); |
| 104 | ZUC_ASSERT_TRUE(3); |
| 105 | ZUC_ASSERT_FALSE(0); |
| 106 | |
| 107 | ZUC_ASSERT_EQ(5, 2 + 3); |
| 108 | ZUC_ASSERT_EQ(5, 2 + 3); |
| 109 | |
| 110 | int b = 9; |
| 111 | ZUC_ASSERT_NE(1, 2); |
| 112 | ZUC_ASSERT_NE(b, b + 2); |
| 113 | |
| 114 | ZUC_ASSERT_NE(1, 2); |
| 115 | ZUC_ASSERT_NE(b, b + 1); |
| 116 | |
| 117 | ZUC_ASSERT_LT(1, 2); |
| 118 | ZUC_ASSERT_LT(1, 3); |
| 119 | |
| 120 | ZUC_ASSERT_LE(1, 2); |
| 121 | ZUC_ASSERT_LE(1, 3); |
| 122 | |
| 123 | ZUC_ASSERT_LE(1, 1); |
| 124 | ZUC_ASSERT_LE(1, 1); |
| 125 | |
| 126 | ZUC_ASSERT_GT(2, 1); |
| 127 | ZUC_ASSERT_GT(3, 1); |
| 128 | |
| 129 | ZUC_ASSERT_GE(1, 1); |
| 130 | ZUC_ASSERT_GE(1, 1); |
| 131 | |
| 132 | ZUC_ASSERT_GE(2, 1); |
| 133 | ZUC_ASSERT_GE(3, 1); |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | force_fatal_failure(void) |
| 138 | { |
| 139 | #ifdef ENABLE_FAIL_TESTS |
| 140 | bool expected_to_fail_here = true; |
| 141 | ZUC_ASSERT_FALSE(expected_to_fail_here); |
| 142 | |
| 143 | ZUC_FATAL("Should never reach here"); |
| 144 | ZUC_ASSERT_NE(1, 1); |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | #ifdef ENABLE_FAIL_TESTS |
| 149 | ZUC_TEST(infrastructure, fail_keeps_testing) |
| 150 | { |
| 151 | ZUC_FATAL("Should always reach here"); |
| 152 | ZUC_ASSERT_NE(1, 1); /* in case ZUC_FATAL doesn't work. */ |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | #ifdef ENABLE_FAIL_TESTS |
| 157 | ZUC_TEST(infrastructure, fatal_stops_test) |
| 158 | { |
| 159 | ZUC_FATAL("Time to kill testing"); |
| 160 | |
| 161 | ZUC_FATAL("Should never reach here"); |
| 162 | ZUC_ASSERT_NE(1, 1); /* in case ZUC_FATAL doesn't work. */ |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | #ifdef ENABLE_SKIP_TESTS |
| 167 | ZUC_TEST(infrastructure, skip_stops_test) |
| 168 | { |
| 169 | ZUC_SKIP("Time to skip testing"); |
| 170 | |
| 171 | ZUC_FATAL("Should never reach here"); |
| 172 | ZUC_ASSERT_NE(1, 1); /* in case ZUC_FATAL doesn't work. */ |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | struct fixture_data { |
| 177 | int case_counter; |
| 178 | int test_counter; |
| 179 | }; |
| 180 | |
| 181 | static struct fixture_data fixture_info = {0, 0}; |
| 182 | |
| 183 | static void * |
| 184 | complex_test_set_up_case(const void *data) |
| 185 | { |
| 186 | fixture_info.case_counter++; |
| 187 | return &fixture_info; |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | complex_test_tear_down_case(void *data) |
| 192 | { |
| 193 | ZUC_ASSERT_TRUE(&fixture_info == data); |
| 194 | fixture_info.case_counter--; |
| 195 | } |
| 196 | |
| 197 | static void * |
| 198 | complex_test_set_up(void *data) |
| 199 | { |
| 200 | fixture_info.test_counter = fixture_info.case_counter; |
| 201 | return &fixture_info; |
| 202 | } |
| 203 | |
| 204 | static void |
| 205 | complex_test_tear_down(void *data) |
| 206 | { |
| 207 | ZUC_ASSERT_EQ(1, fixture_info.case_counter); |
| 208 | } |
| 209 | |
| 210 | struct zuc_fixture complex_test = { |
| 211 | .set_up = complex_test_set_up, |
| 212 | .tear_down = complex_test_tear_down, |
| 213 | .set_up_test_case = complex_test_set_up_case, |
| 214 | .tear_down_test_case = complex_test_tear_down_case |
| 215 | }; |
| 216 | |
| 217 | /* |
| 218 | * Note that these next cases all try to modify the test_counter member, |
| 219 | * but the fixture should reset that. |
| 220 | */ |
| 221 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 222 | ZUC_TEST_F(complex_test, bases_cenario, data) |
Jon A. Cruz | 5a75a41 | 2015-07-02 23:36:44 -0700 | [diff] [blame] | 223 | { |
| 224 | struct fixture_data *fdata = data; |
| 225 | ZUC_ASSERT_NOT_NULL(fdata); |
| 226 | |
| 227 | ZUC_ASSERT_EQ(4, 3 + 1); |
| 228 | ZUC_ASSERT_EQ(1, fdata->case_counter); |
| 229 | ZUC_ASSERT_EQ(1, fdata->test_counter); |
| 230 | fdata->test_counter++; |
| 231 | ZUC_ASSERT_EQ(2, fdata->test_counter); |
| 232 | } |
| 233 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 234 | ZUC_TEST_F(complex_test, something, data) |
Jon A. Cruz | 5a75a41 | 2015-07-02 23:36:44 -0700 | [diff] [blame] | 235 | { |
| 236 | struct fixture_data *fdata = data; |
| 237 | ZUC_ASSERT_NOT_NULL(fdata); |
| 238 | |
| 239 | ZUC_ASSERT_EQ(4, 3 + 1); |
| 240 | ZUC_ASSERT_EQ(1, fdata->case_counter); |
| 241 | ZUC_ASSERT_EQ(1, fdata->test_counter); |
| 242 | fdata->test_counter++; |
| 243 | ZUC_ASSERT_EQ(2, fdata->test_counter); |
| 244 | } |
| 245 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 246 | ZUC_TEST_F(complex_test, else_here, data) |
Jon A. Cruz | 5a75a41 | 2015-07-02 23:36:44 -0700 | [diff] [blame] | 247 | { |
| 248 | struct fixture_data *fdata = data; |
| 249 | ZUC_ASSERT_NOT_NULL(fdata); |
| 250 | |
| 251 | ZUC_ASSERT_EQ(4, 3 + 1); |
| 252 | ZUC_ASSERT_EQ(1, fdata->case_counter); |
| 253 | ZUC_ASSERT_EQ(1, fdata->test_counter); |
| 254 | fdata->test_counter++; |
| 255 | ZUC_ASSERT_EQ(2, fdata->test_counter); |
| 256 | } |
| 257 | |
| 258 | ZUC_TEST(more, DISABLED_not_run) |
| 259 | { |
| 260 | ZUC_ASSERT_EQ(1, 2); |
| 261 | } |
| 262 | |
| 263 | ZUC_TEST(more, failure_states) |
| 264 | { |
| 265 | #ifdef ENABLE_FAIL_TESTS |
| 266 | bool expected_to_fail_here = true; |
| 267 | #endif |
| 268 | |
| 269 | ZUC_ASSERT_FALSE(zuc_has_failure()); |
| 270 | |
| 271 | #ifdef ENABLE_FAIL_TESTS |
| 272 | ZUC_ASSERT_FALSE(expected_to_fail_here); /* should fail */ |
| 273 | |
| 274 | ZUC_ASSERT_TRUE(zuc_has_failure()); |
| 275 | #endif |
| 276 | } |
| 277 | |
| 278 | ZUC_TEST(more, failure_sub_fatal) |
| 279 | { |
| 280 | ZUC_ASSERT_FALSE(zuc_has_failure()); |
| 281 | |
| 282 | force_fatal_failure(); |
| 283 | |
| 284 | #ifdef ENABLE_FAIL_TESTS |
| 285 | ZUC_ASSERT_TRUE(zuc_has_failure()); |
| 286 | #endif |
| 287 | } |
| 288 | |
| 289 | ZUC_TEST(pointers, null) |
| 290 | { |
| 291 | const char *a = NULL; |
| 292 | |
| 293 | ZUC_ASSERT_NULL(NULL); |
| 294 | ZUC_ASSERT_NULL(0); |
| 295 | ZUC_ASSERT_NULL(a); |
| 296 | } |
| 297 | |
| 298 | #ifdef ENABLE_FAIL_TESTS |
| 299 | ZUC_TEST(pointers, null_fail) |
| 300 | { |
| 301 | const char *a = "a"; |
| 302 | |
| 303 | ZUC_ASSERT_NULL(!NULL); |
| 304 | ZUC_ASSERT_NULL(!0); |
| 305 | ZUC_ASSERT_NULL(a); |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | ZUC_TEST(pointers, not_null) |
| 310 | { |
| 311 | const char *a = "a"; |
| 312 | |
| 313 | ZUC_ASSERT_NOT_NULL(!NULL); |
| 314 | ZUC_ASSERT_NOT_NULL(!0); |
| 315 | ZUC_ASSERT_NOT_NULL(a); |
| 316 | } |
| 317 | |
| 318 | #ifdef ENABLE_FAIL_TESTS |
| 319 | ZUC_TEST(pointers, not_null_fail) |
| 320 | { |
| 321 | const char *a = NULL; |
| 322 | |
| 323 | ZUC_ASSERT_NOT_NULL(NULL); |
| 324 | ZUC_ASSERT_NOT_NULL(0); |
| 325 | ZUC_ASSERT_NOT_NULL(a); |
| 326 | } |
| 327 | #endif |
| 328 | |
| 329 | ZUC_TEST(strings, eq) |
| 330 | { |
| 331 | /* Note that we use strdup() to ensure different addresses. */ |
| 332 | char *str_a = strdup("a"); |
| 333 | const char *str_nil = NULL; |
| 334 | |
| 335 | ZUC_ASSERT_STREQ(str_a, str_a); |
| 336 | ZUC_ASSERT_STREQ("a", str_a); |
| 337 | ZUC_ASSERT_STREQ(str_a, "a"); |
| 338 | |
| 339 | ZUC_ASSERT_STREQ(str_nil, str_nil); |
| 340 | ZUC_ASSERT_STREQ(NULL, str_nil); |
| 341 | ZUC_ASSERT_STREQ(str_nil, NULL); |
| 342 | |
| 343 | free(str_a); |
| 344 | } |
| 345 | |
| 346 | #ifdef ENABLE_FAIL_TESTS |
| 347 | ZUC_TEST(strings, eq_fail) |
| 348 | { |
| 349 | /* Note that we use strdup() to ensure different addresses. */ |
| 350 | char *str_a = strdup("a"); |
| 351 | char *str_b = strdup("b"); |
| 352 | const char *str_nil = NULL; |
| 353 | |
| 354 | ZUC_ASSERT_STREQ(str_a, str_b); |
| 355 | ZUC_ASSERT_STREQ("b", str_a); |
| 356 | ZUC_ASSERT_STREQ(str_a, "b"); |
| 357 | |
| 358 | ZUC_ASSERT_STREQ(str_nil, str_a); |
| 359 | ZUC_ASSERT_STREQ(str_nil, str_b); |
| 360 | ZUC_ASSERT_STREQ(str_a, str_nil); |
| 361 | ZUC_ASSERT_STREQ(str_b, str_nil); |
| 362 | |
| 363 | ZUC_ASSERT_STREQ(NULL, str_a); |
| 364 | ZUC_ASSERT_STREQ(NULL, str_b); |
| 365 | ZUC_ASSERT_STREQ(str_a, NULL); |
| 366 | ZUC_ASSERT_STREQ(str_b, NULL); |
| 367 | |
| 368 | free(str_a); |
| 369 | free(str_b); |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | ZUC_TEST(strings, ne) |
| 374 | { |
| 375 | /* Note that we use strdup() to ensure different addresses. */ |
| 376 | char *str_a = strdup("a"); |
| 377 | char *str_b = strdup("b"); |
| 378 | const char *str_nil = NULL; |
| 379 | |
| 380 | ZUC_ASSERT_STRNE(str_a, str_b); |
| 381 | ZUC_ASSERT_STRNE("b", str_a); |
| 382 | ZUC_ASSERT_STRNE(str_a, "b"); |
| 383 | |
| 384 | ZUC_ASSERT_STRNE(str_nil, str_a); |
| 385 | ZUC_ASSERT_STRNE(str_nil, str_b); |
| 386 | ZUC_ASSERT_STRNE(str_a, str_nil); |
| 387 | ZUC_ASSERT_STRNE(str_b, str_nil); |
| 388 | |
| 389 | ZUC_ASSERT_STRNE(NULL, str_a); |
| 390 | ZUC_ASSERT_STRNE(NULL, str_b); |
| 391 | ZUC_ASSERT_STRNE(str_a, NULL); |
| 392 | ZUC_ASSERT_STRNE(str_b, NULL); |
| 393 | |
| 394 | free(str_a); |
| 395 | free(str_b); |
| 396 | } |
| 397 | |
| 398 | #ifdef ENABLE_FAIL_TESTS |
| 399 | ZUC_TEST(strings, ne_fail01) |
| 400 | { |
| 401 | /* Note that we use strdup() to ensure different addresses. */ |
| 402 | char *str_a = strdup("a"); |
| 403 | |
| 404 | ZUC_ASSERTG_STRNE(str_a, str_a, err); |
| 405 | |
| 406 | err: |
| 407 | free(str_a); |
| 408 | } |
| 409 | |
| 410 | ZUC_TEST(strings, ne_fail02) |
| 411 | { |
| 412 | /* Note that we use strdup() to ensure different addresses. */ |
| 413 | char *str_a = strdup("a"); |
| 414 | |
| 415 | ZUC_ASSERTG_STRNE("a", str_a, err); |
| 416 | |
| 417 | err: |
| 418 | free(str_a); |
| 419 | } |
| 420 | |
| 421 | ZUC_TEST(strings, ne_fail03) |
| 422 | { |
| 423 | /* Note that we use strdup() to ensure different addresses. */ |
| 424 | char *str_a = strdup("a"); |
| 425 | |
| 426 | ZUC_ASSERTG_STRNE(str_a, "a", err); |
| 427 | |
| 428 | err: |
| 429 | free(str_a); |
| 430 | } |
| 431 | |
| 432 | ZUC_TEST(strings, ne_fail04) |
| 433 | { |
| 434 | const char *str_nil = NULL; |
| 435 | |
| 436 | ZUC_ASSERT_STRNE(str_nil, str_nil); |
| 437 | } |
| 438 | |
| 439 | ZUC_TEST(strings, ne_fail05) |
| 440 | { |
| 441 | const char *str_nil = NULL; |
| 442 | |
| 443 | ZUC_ASSERT_STRNE(NULL, str_nil); |
| 444 | } |
| 445 | |
| 446 | ZUC_TEST(strings, ne_fail06) |
| 447 | { |
| 448 | const char *str_nil = NULL; |
| 449 | |
| 450 | ZUC_ASSERT_STRNE(str_nil, NULL); |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | ZUC_TEST(base_test, later) |
| 455 | { |
| 456 | /* an additional test for the same case but later in source */ |
| 457 | ZUC_ASSERT_EQ(3, 5 - 2); |
| 458 | } |
| 459 | |
| 460 | ZUC_TEST(base_test, zed) |
| 461 | { |
| 462 | /* an additional test for the same case but later in source */ |
| 463 | ZUC_ASSERT_EQ(3, 5 - 2); |
| 464 | } |