blob: 0f10ecafadafb84c322f85c83659c8509ef8fd6f [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scullbc7189d2018-08-14 09:35:13 +010017#pragma once
18
19#include <stdbool.h>
20#include <stddef.h>
21#include <stdint.h>
22
23#include "hf/dlog.h"
24
25/*
26 * Define a set up function to be run before every test in a test suite.
27 */
28#define SET_UP(suite) HFTEST_SET_UP(suite)
29
30/*
31 * Define a tear down function to be run after every test in a test suite.
32 */
33#define TEAR_DOWN(suite) HFTEST_TEAR_DOWN(suite)
34
35/*
36 * Define a test as part of a test suite.
37 */
38#define TEST(suite, test) HFTEST_TEST(suite, test)
39
40/* Assertions. */
41#define ASSERT_EQ(x, y) ASSERT_OP(x, y, ==, true)
42#define ASSERT_NE(x, y) ASSERT_OP(x, y, !=, true)
43#define ASSERT_LE(x, y) ASSERT_OP(x, y, <=, true)
44#define ASSERT_LT(x, y) ASSERT_OP(x, y, <, true)
45#define ASSERT_GE(x, y) ASSERT_OP(x, y, >=, true)
46#define ASSERT_GT(x, y) ASSERT_OP(x, y, >, true)
47
48#define EXPECT_EQ(x, y) ASSERT_OP(x, y, ==, false)
49#define EXPECT_NE(x, y) ASSERT_OP(x, y, !=, false)
50#define EXPECT_LE(x, y) ASSERT_OP(x, y, <=, false)
51#define EXPECT_LT(x, y) ASSERT_OP(x, y, <, false)
52#define EXPECT_GE(x, y) ASSERT_OP(x, y, >=, false)
53#define EXPECT_GT(x, y) ASSERT_OP(x, y, >, false)
54
55/*
56 * This must be used exactly once in a test image to signal to the linker that
57 * the .hftest section is allowed to be included in the generated image.
58 */
59#define HFTEST_ENABLE() int hftest_enable
60
61/*
62 * Prefixed to log lines from tests for easy filtering in the console.
63 */
64#define HFTEST_LOG_PREFIX "[hftest] "
65
66/* Above this point is the public API. Below are the implementation details. */
67
68/* Log with the HFTEST_LOG_PREFIX and a new line. The zero is added so there is
69 * always at least one variadic argument. */
70#define HFTEST_LOG(...) HFTEST_LOG_IMPL(__VA_ARGS__, 0)
71#define HFTEST_LOG_IMPL(format, ...) \
72 dlog("%s" format "\n", HFTEST_LOG_PREFIX, __VA_ARGS__)
73
74/* Helper to wrap the argument in quotes. */
75#define HFTEST_STR(str) #str
76
77/* Sections are names such that when the linker sorts them, all entries for the
78 * same test suite are contiguous and the set up and tear down entries come
79 * before the tests. This order simplifies test discovery in the running image.
80 */
81#define HFTEST_SET_UP_SECTION(suite_name) \
82 HFTEST_STR(.hftest.suite_name .1set_up)
83#define HFTEST_TEAR_DOWN_SECTION(suite_name) \
84 HFTEST_STR(.hftest.suite_name .1tear_down)
85#define HFTEST_TEST_SECTION(suite_name, test_name) \
86 HFTEST_STR(.hftest.suite_name .2test.test_name)
87
88/* Helpers to construct unique identifiers. */
89#define HFTEST_SET_UP_STRUCT(suite_name) hftest_set_up_##suite_name
90#define HFTEST_TEAR_DOWN_STRUCT(suite_name) hftest_tear_down_##suite_name
91#define HFTEST_TEST_STRUCT(suite_name, test_name) \
92 hftest_test_##suite_name##_##test_name
93
94#define HFTEST_SET_UP_FN(suite_name) hftest_set_up_fn_##suite_name
95#define HFTEST_TEAR_DOWN_FN(suite_name) hftest_tear_down_fn_##suite_name
96#define HFTEST_TEST_FN(suite_name, test_name) \
97 hftest_test_fn_##suite_name##_##test_name
98
99/* Register test functions. */
100#define HFTEST_SET_UP(suite_name) \
101 static void HFTEST_SET_UP_FN(suite_name)(struct hftest_context * \
102 hftest_ctx); \
103 const struct hftest_test __attribute__((used)) \
104 __attribute__((section(HFTEST_SET_UP_SECTION(suite_name)))) \
105 HFTEST_SET_UP_STRUCT(suite_name) = { \
106 .suite = #suite_name, \
107 .kind = HFTEST_KIND_SET_UP, \
108 .fn = HFTEST_SET_UP_FN(suite_name), \
109 }; \
110 static void HFTEST_SET_UP_FN(suite_name)( \
111 __attribute__((unused)) struct hftest_context * hftest_ctx)
112
113#define HFTEST_TEAR_DOWN(suite_name) \
114 static void HFTEST_TEAR_DOWN_FN(suite_name)(struct hftest_context * \
115 hftest_ctx); \
116 const struct hftest_test __attribute__((used)) \
117 __attribute__((section(HFTEST_TEAR_DOWN_SECTION(suite_name)))) \
118 HFTEST_TEAR_DOWN_STRUCT(suite_name) = { \
119 .suite = #suite_name, \
120 .kind = HFTEST_KIND_TEAR_DOWN, \
121 .fn = HFTEST_TEAR_DOWN_FN(suite_name), \
122 }; \
123 static void HFTEST_TEAR_DOWN_FN(suite_name)( \
124 __attribute__((unused)) struct hftest_context * hftest_ctx)
125
126#define HFTEST_TEST(suite_name, test_name) \
127 static void HFTEST_TEST_FN( \
128 suite_name, test_name)(struct hftest_context * hftest_ctx); \
129 const struct hftest_test __attribute__((used)) __attribute__( \
130 (section(HFTEST_TEST_SECTION(suite_name, test_name)))) \
131 HFTEST_TEST_STRUCT(suite_name, test_name) = { \
132 .suite = #suite_name, \
133 .kind = HFTEST_KIND_TEST, \
134 .name = #test_name, \
135 .fn = HFTEST_TEST_FN(suite_name, test_name), \
136 }; \
137 static void HFTEST_TEST_FN(suite_name, test_name)( \
138 __attribute__((unused)) struct hftest_context * hftest_ctx)
139
140/* Context for tests. */
141struct hftest_context {
142 uint32_t failures;
143};
144
145/* A test case. */
146typedef void (*hftest_test_fn)(struct hftest_context *);
147
148enum hftest_kind {
149 HFTEST_KIND_SET_UP = 0,
150 HFTEST_KIND_TEST = 1,
151 HFTEST_KIND_TEAR_DOWN = 2,
152};
153
154struct hftest_test {
155 const char *suite;
156 enum hftest_kind kind;
157 const char *name;
158 hftest_test_fn fn;
159};
160
161/*
162 * This union can store any of the primitive types supported by the assertion
163 * macros.
164 *
165 * It does not include pointers as comparison of pointers is not often needed
166 * and could be a mistake for string comparison. If pointer comparison is needed
167 * and explicit assertion such as ASSERT_PTR_EQ() would be more appropriate.
168 */
169union hftest_any {
170 bool b;
171 char c;
172 signed char sc;
173 unsigned char uc;
174 signed short ss;
175 unsigned short us;
176 signed int si;
177 unsigned int ui;
178 signed long int sli;
179 unsigned long int uli;
180 signed long long int slli;
181 unsigned long long int ulli;
182};
183
184/* _Generic formatting doesn't seem to be supported so doing this manually. */
185/* clang-format off */
186
187/* Select the union member to match the type of the expression. */
188#define hftest_any_get(any, x) \
189 _Generic((x), \
190 bool: (any).b, \
191 char: (any).c, \
192 signed char: (any).sc, \
193 unsigned char: (any).uc, \
194 signed short: (any).ss, \
195 unsigned short: (any).us, \
196 signed int: (any).si, \
197 unsigned int: (any).ui, \
198 signed long int: (any).sli, \
199 unsigned long int: (any).uli, \
200 signed long long int: (any).slli, \
201 unsigned long long int: (any).ulli)
202
203/*
204 * dlog format specifier for types. Note, these aren't the standard specifiers
205 * for the types.
206 */
207#define hftest_dlog_format(x) \
208 _Generic((x), \
209 bool: "%u", \
210 char: "%c", \
211 signed char: "%d", \
212 unsigned char: "%u", \
213 signed short: "%d", \
214 unsigned short: "%u", \
215 signed int: "%d", \
216 unsigned int: "%u", \
217 signed long int: "%d", \
218 unsigned long int: "%u", \
219 signed long long int: "%d", \
220 unsigned long long int: "%u")
221
222/* clang-format on */
223
224#define ASSERT_OP(lhs, rhs, op, fatal) \
225 do { \
226 union hftest_any lhs_value; \
227 union hftest_any rhs_value; \
228 hftest_any_get(lhs_value, lhs) = (lhs); \
229 hftest_any_get(rhs_value, rhs) = (rhs); \
230 if (!(hftest_any_get(lhs_value, lhs) \
231 op hftest_any_get(rhs_value, rhs))) { \
232 ++hftest_ctx->failures; \
233 dlog(HFTEST_LOG_PREFIX " %s:%u: Failure\n", __FILE__, \
234 __LINE__); \
235 dlog(HFTEST_LOG_PREFIX " %s %s %s (%s=", #lhs, #op, \
236 #rhs, #lhs); \
237 dlog(hftest_dlog_format(lhs), \
238 hftest_any_get(lhs_value, lhs)); \
239 dlog(", %s=", #rhs); \
240 dlog(hftest_dlog_format(rhs), \
241 hftest_any_get(rhs_value, rhs)); \
242 dlog(")\n"); \
243 if (fatal) { \
244 return; \
245 } \
246 } \
247 } while (0)