rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013, Google, Inc. All rights reserved |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | /* |
| 24 | * All unit tests get registered here. A call to run_all_tests() will run |
| 25 | * them and provide results. |
| 26 | */ |
| 27 | #include <unittest.h> |
| 28 | #include <assert.h> |
| 29 | |
| 30 | static struct test_case_element *test_case_list = NULL; |
| 31 | static struct test_case_element *failed_test_case_list = NULL; |
| 32 | |
| 33 | /* |
| 34 | * Registers a test case with the unit test framework. |
| 35 | */ |
| 36 | void unittest_register_test_case(struct test_case_element *elem) |
| 37 | { |
| 38 | DEBUG_ASSERT(elem); |
| 39 | DEBUG_ASSERT(elem->next == NULL); |
| 40 | elem->next = test_case_list; |
| 41 | test_case_list = elem; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Runs all registered test cases. |
| 46 | */ |
| 47 | bool run_all_tests(void) |
| 48 | { |
| 49 | unsigned int n_tests = 0; |
| 50 | unsigned int n_success = 0; |
| 51 | unsigned int n_failed = 0; |
| 52 | |
| 53 | bool all_success = true; |
| 54 | struct test_case_element *current = test_case_list; |
| 55 | while (current) { |
| 56 | if (!current->test_case()) { |
| 57 | current->failed_next = failed_test_case_list; |
| 58 | failed_test_case_list = current; |
| 59 | all_success = false; |
| 60 | } |
| 61 | current = current->next; |
| 62 | n_tests++; |
| 63 | } |
| 64 | |
| 65 | if (all_success) { |
| 66 | n_success = n_tests; |
| 67 | unittest_printf("SUCCESS! All test cases passed!\n"); |
| 68 | } else { |
| 69 | struct test_case_element *failed = failed_test_case_list; |
| 70 | while (failed) { |
| 71 | struct test_case_element *failed_next = |
| 72 | failed->failed_next; |
| 73 | failed->failed_next = NULL; |
| 74 | failed = failed_next; |
| 75 | n_failed++; |
| 76 | } |
| 77 | n_success = n_tests - n_failed; |
| 78 | failed_test_case_list = NULL; |
| 79 | } |
| 80 | |
| 81 | unittest_printf("\n====================================================\n"); |
| 82 | unittest_printf (" CASES: %d SUCCESS: %d FAILED: %d ", |
| 83 | n_tests, n_success, n_failed); |
| 84 | unittest_printf("\n====================================================\n"); |
| 85 | |
| 86 | return all_success; |
| 87 | } |