Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 1 | /* |
Raef Coles | e1a8c9f | 2021-06-09 14:11:34 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2021, Arm Limited. All rights reserved. |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "test_framework.h" |
| 9 | |
| 10 | #include <assert.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
Milosz Wasilewski | c3076a3 | 2020-12-07 10:15:28 +0000 | [diff] [blame] | 15 | static void test_failed(const struct test_t *p_test) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 16 | { |
Milosz Wasilewski | c3076a3 | 2020-12-07 10:15:28 +0000 | [diff] [blame] | 17 | const struct test_result_t *ret = &p_test->ret; |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 18 | printf_set_color(RED); |
| 19 | if (ret->info_msg != 0) { |
| 20 | TEST_LOG(" %s", ret->info_msg); |
| 21 | if (ret->filename != 0) { |
| 22 | TEST_LOG(" (Failed at %s:%d)\r\n", ret->filename, ret->line); |
| 23 | } |
| 24 | } else { |
| 25 | if (ret->filename != 0) { |
| 26 | TEST_LOG(" Failed at %s:%d\r\n", ret->filename, ret->line); |
| 27 | } |
| 28 | } |
| 29 | |
Milosz Wasilewski | c3076a3 | 2020-12-07 10:15:28 +0000 | [diff] [blame] | 30 | TEST_LOG(" TEST: %s - FAILED!\r\n", p_test->name); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void print_error(const char *err_msg) |
| 34 | { |
| 35 | printf_set_color(RED); |
| 36 | TEST_LOG("Error ( %s )\r\n", err_msg); |
| 37 | } |
| 38 | |
| 39 | const char *test_err_to_str(enum test_suite_err_t err) |
| 40 | { |
| 41 | switch (err) { |
| 42 | case TEST_SUITE_ERR_NO_ERROR: |
| 43 | return "TEST_SUITE_ERR_NO_ERROR"; |
| 44 | case TEST_SUITE_ERR_INVALID_DATA: |
| 45 | return "TEST_SUITE_ERR_INVALID_DATA"; |
| 46 | case TEST_SUITE_ERR_INVALID_TEST_DATA: |
| 47 | return "TEST_SUITE_ERR_INVALID_TEST_DATA"; |
| 48 | case TEST_SUITE_ERR_TEST_FAILED: |
| 49 | return "TEST_SUITE_ERR_TEST_FAILED"; |
| 50 | /* default: The default is not defined intentionally to force the |
| 51 | * compiler to check that all the enumeration values are |
| 52 | * covered in the switch. |
| 53 | */ |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | enum test_suite_err_t set_testsuite(const char *name, |
| 58 | struct test_t *test_list, uint32_t size, |
| 59 | struct test_suite_t *p_ts) |
| 60 | { |
| 61 | if (p_ts == 0) { |
| 62 | print_error("TEST_SUITE_ERR_INVALID_DATA!"); |
| 63 | return TEST_SUITE_ERR_INVALID_DATA; |
| 64 | } |
| 65 | |
| 66 | p_ts->name = name; |
| 67 | p_ts->test_list = test_list; |
| 68 | p_ts->list_size = size; |
| 69 | |
| 70 | return TEST_SUITE_ERR_NO_ERROR; |
| 71 | } |
| 72 | |
| 73 | void set_test_failed(const char *info_msg, const char *filename, uint32_t line, |
| 74 | struct test_result_t *ret) |
| 75 | { |
| 76 | if (ret == 0) { |
| 77 | print_error("TEST_SUITE_ERR_INVALID_TEST_DATA!"); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | ret->val = TEST_FAILED; |
| 82 | ret->info_msg = info_msg; |
| 83 | ret->filename = filename; |
| 84 | ret->line = line; |
| 85 | } |
| 86 | |
| 87 | enum test_suite_err_t run_testsuite(struct test_suite_t *test_suite) |
| 88 | { |
| 89 | uint32_t failed_tests = 0; |
| 90 | uint32_t i; |
| 91 | struct test_t *p_test; |
| 92 | |
| 93 | if (test_suite == 0 || test_suite->freg == 0) { |
| 94 | print_error("TEST_SUITE_ERR_INVALID_DATA!"); |
| 95 | return TEST_SUITE_ERR_INVALID_DATA; |
| 96 | } |
| 97 | |
| 98 | /* Sets test suite parameters */ |
| 99 | test_suite->freg(test_suite); |
| 100 | if (test_suite->name == 0 || test_suite->list_size == 0) { |
| 101 | print_error("TEST_SUITE_ERR_INVALID_DATA!"); |
| 102 | return TEST_SUITE_ERR_INVALID_DATA; |
| 103 | } |
| 104 | |
| 105 | printf_set_color(YELLOW); |
| 106 | TEST_LOG("Running Test Suite %s...\r\n", test_suite->name); |
| 107 | |
| 108 | /* Sets pointer to the first test */ |
| 109 | p_test = test_suite->test_list; |
| 110 | |
| 111 | for (i = 0; i < test_suite->list_size; i++) { |
| 112 | |
| 113 | if (p_test->test == 0 || p_test->name == 0) { |
| 114 | print_error("TEST_SUITE_ERR_INVALID_TEST_DATA!"); |
| 115 | return TEST_SUITE_ERR_INVALID_TEST_DATA; |
| 116 | } |
| 117 | |
Raef Coles | e1a8c9f | 2021-06-09 14:11:34 +0100 | [diff] [blame^] | 118 | printf_set_color(DEFAULT); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 119 | TEST_LOG("> Executing '%s' \r\n Description: '%s'\r\n", |
| 120 | p_test->name, p_test->desc); |
| 121 | |
| 122 | /* Sets the default value before the test */ |
| 123 | p_test->ret.val = TEST_PASSED; |
| 124 | |
| 125 | /* Executes the test */ |
| 126 | p_test->test(&p_test->ret); |
| 127 | if (p_test->ret.val == TEST_FAILED) { |
Milosz Wasilewski | c3076a3 | 2020-12-07 10:15:28 +0000 | [diff] [blame] | 128 | test_failed(p_test); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 129 | failed_tests++; |
| 130 | } else { |
| 131 | printf_set_color(GREEN); |
Milosz Wasilewski | c3076a3 | 2020-12-07 10:15:28 +0000 | [diff] [blame] | 132 | TEST_LOG(" TEST: %s - PASSED!\r\n", p_test->name); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* Sets pointer to the next test */ |
| 136 | p_test++; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | if (failed_tests == 0) { |
| 141 | printf_set_color(GREEN); |
| 142 | TEST_LOG("TESTSUITE PASSED!\r\n"); |
| 143 | test_suite->val = TEST_PASSED; |
| 144 | } else { |
| 145 | printf_set_color(RED); |
| 146 | TEST_LOG("TESTSUITE FAILED!\r\n"); |
| 147 | printf_set_color(YELLOW); |
| 148 | TEST_LOG("Number of failed tests: %d of %d\r\n", |
| 149 | failed_tests, test_suite->list_size); |
| 150 | test_suite->val = TEST_FAILED; |
| 151 | } |
| 152 | |
| 153 | return TEST_SUITE_ERR_NO_ERROR; |
| 154 | } |