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