xref: /aosp_15_r20/trusty/kernel/include/shared/lk/trusty_unittest.h (revision 344aa361028b423587d4ef3fa52a23d194628137)
1*344aa361SAndroid Build Coastguard Worker /*
2*344aa361SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*344aa361SAndroid Build Coastguard Worker  *
4*344aa361SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining
5*344aa361SAndroid Build Coastguard Worker  * a copy of this software and associated documentation files
6*344aa361SAndroid Build Coastguard Worker  * (the "Software"), to deal in the Software without restriction,
7*344aa361SAndroid Build Coastguard Worker  * including without limitation the rights to use, copy, modify, merge,
8*344aa361SAndroid Build Coastguard Worker  * publish, distribute, sublicense, and/or sell copies of the Software,
9*344aa361SAndroid Build Coastguard Worker  * and to permit persons to whom the Software is furnished to do so,
10*344aa361SAndroid Build Coastguard Worker  * subject to the following conditions:
11*344aa361SAndroid Build Coastguard Worker  *
12*344aa361SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*344aa361SAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*344aa361SAndroid Build Coastguard Worker  *
15*344aa361SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*344aa361SAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*344aa361SAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*344aa361SAndroid Build Coastguard Worker  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19*344aa361SAndroid Build Coastguard Worker  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20*344aa361SAndroid Build Coastguard Worker  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21*344aa361SAndroid Build Coastguard Worker  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*344aa361SAndroid Build Coastguard Worker  */
23*344aa361SAndroid Build Coastguard Worker 
24*344aa361SAndroid Build Coastguard Worker #pragma once
25*344aa361SAndroid Build Coastguard Worker 
26*344aa361SAndroid Build Coastguard Worker #include <inttypes.h>
27*344aa361SAndroid Build Coastguard Worker #include <lk/compiler.h>
28*344aa361SAndroid Build Coastguard Worker #include <lk/list.h>
29*344aa361SAndroid Build Coastguard Worker #include <stdbool.h>
30*344aa361SAndroid Build Coastguard Worker #include <string.h>
31*344aa361SAndroid Build Coastguard Worker 
32*344aa361SAndroid Build Coastguard Worker __BEGIN_CDECLS
33*344aa361SAndroid Build Coastguard Worker 
34*344aa361SAndroid Build Coastguard Worker /*
35*344aa361SAndroid Build Coastguard Worker  * This function returns a time in nanoseconds based on hardware counters
36*344aa361SAndroid Build Coastguard Worker  * it is expected to:
37*344aa361SAndroid Build Coastguard Worker  *  - Be non-wrapping or have very long (years) roll-over period
38*344aa361SAndroid Build Coastguard Worker  *  - Have a resolution below 100nsc
39*344aa361SAndroid Build Coastguard Worker  */
40*344aa361SAndroid Build Coastguard Worker uint64_t get_current_time_ns(void);
41*344aa361SAndroid Build Coastguard Worker 
42*344aa361SAndroid Build Coastguard Worker /*
43*344aa361SAndroid Build Coastguard Worker  * Test functions can be defined with:
44*344aa361SAndroid Build Coastguard Worker  * TEST(SuiteName, TestName) {
45*344aa361SAndroid Build Coastguard Worker  *   ... test body ...
46*344aa361SAndroid Build Coastguard Worker  * }
47*344aa361SAndroid Build Coastguard Worker  * or with:
48*344aa361SAndroid Build Coastguard Worker  * TEST_F(SuiteName, TestName) {
49*344aa361SAndroid Build Coastguard Worker  *   ... test body ...
50*344aa361SAndroid Build Coastguard Worker  * }
51*344aa361SAndroid Build Coastguard Worker  * or with:
52*344aa361SAndroid Build Coastguard Worker  * TEST_P(SuiteName, TestName) {
53*344aa361SAndroid Build Coastguard Worker  *   ... test body ...
54*344aa361SAndroid Build Coastguard Worker  * }
55*344aa361SAndroid Build Coastguard Worker  *
56*344aa361SAndroid Build Coastguard Worker  * NOTE: SuiteName and TestName should not contain underscores.
57*344aa361SAndroid Build Coastguard Worker  *
58*344aa361SAndroid Build Coastguard Worker  * Use EXPECT_<op> or ASSERT_<op> directly in test functions or from nested
59*344aa361SAndroid Build Coastguard Worker  * functions to check test conditions. Where <op> can be:
60*344aa361SAndroid Build Coastguard Worker  *   EQ for ==
61*344aa361SAndroid Build Coastguard Worker  *   NE for !=
62*344aa361SAndroid Build Coastguard Worker  *   LT for <
63*344aa361SAndroid Build Coastguard Worker  *   LE for <=
64*344aa361SAndroid Build Coastguard Worker  *   GT for >
65*344aa361SAndroid Build Coastguard Worker  *   GE for >=
66*344aa361SAndroid Build Coastguard Worker  *
67*344aa361SAndroid Build Coastguard Worker  * The test functions follows this pattern:
68*344aa361SAndroid Build Coastguard Worker  *   <EXPECT|ASSERT>_<op>(val1, val2 [, format, ...])
69*344aa361SAndroid Build Coastguard Worker  * If val1 <op> val2 is not true, then both values will be printed and a test
70*344aa361SAndroid Build Coastguard Worker  * failure will be recorded. For ASSERT_<op> it will also jump to a test_abort
71*344aa361SAndroid Build Coastguard Worker  * label in the calling function.
72*344aa361SAndroid Build Coastguard Worker  *
73*344aa361SAndroid Build Coastguard Worker  * Call RUN_ALL_TESTS() to run all tests defined by TEST (or
74*344aa361SAndroid Build Coastguard Worker  * RUN_ALL_SUITE_TESTS("SuiteName") to only run tests with the specified
75*344aa361SAndroid Build Coastguard Worker  * SuiteName). RUN_ALL_TESTS and RUN_ALL_SUITE_TESTS return true if all the
76*344aa361SAndroid Build Coastguard Worker  * tests passed.
77*344aa361SAndroid Build Coastguard Worker  *
78*344aa361SAndroid Build Coastguard Worker  * Test functions defined with TEST_F or TEST_P expect the type <SuiteName>_t
79*344aa361SAndroid Build Coastguard Worker  * and <SuiteName>_SetUp and <SuiteName>_TearDown functions to be defined.
80*344aa361SAndroid Build Coastguard Worker  * The <SuiteName>_SetUp function will be called once before each test in
81*344aa361SAndroid Build Coastguard Worker  * SuiteName in run and the <SuiteName>_TearDown function will be called once
82*344aa361SAndroid Build Coastguard Worker  * after each test in SuiteName is run. These functions can be defined with
83*344aa361SAndroid Build Coastguard Worker  * TEST_F_SETUP(<SuiteName>) {
84*344aa361SAndroid Build Coastguard Worker  *  ... setup body ...
85*344aa361SAndroid Build Coastguard Worker  * }
86*344aa361SAndroid Build Coastguard Worker  * and with:
87*344aa361SAndroid Build Coastguard Worker  * TEST_F_TEARDOWN(<SuiteName>) {
88*344aa361SAndroid Build Coastguard Worker  *  ... teardown body ...
89*344aa361SAndroid Build Coastguard Worker  * }
90*344aa361SAndroid Build Coastguard Worker  * A pointer to a <SuiteName>_t variable will be passed as "_state" to the
91*344aa361SAndroid Build Coastguard Worker  * setup, teardown and test functions.
92*344aa361SAndroid Build Coastguard Worker  *
93*344aa361SAndroid Build Coastguard Worker  * TEST_FIXTURE_ALIAS(NewSuiteName, OldSuiteName) can be used to use the test
94*344aa361SAndroid Build Coastguard Worker  * fixture defined for OldSuiteName with NewSuiteName.
95*344aa361SAndroid Build Coastguard Worker  *
96*344aa361SAndroid Build Coastguard Worker  * Tests defined with TEST_P will only run when their suite is run if they have
97*344aa361SAndroid Build Coastguard Worker  * been instantiated with parameters using INSTANTIATE_TEST_SUITE_P. These tests
98*344aa361SAndroid Build Coastguard Worker  * can access their parameter using GetParam()
99*344aa361SAndroid Build Coastguard Worker  */
100*344aa361SAndroid Build Coastguard Worker 
101*344aa361SAndroid Build Coastguard Worker #ifndef trusty_unittest_printf
102*344aa361SAndroid Build Coastguard Worker #error trusty_unittest_printf must be defined
103*344aa361SAndroid Build Coastguard Worker #endif
104*344aa361SAndroid Build Coastguard Worker 
105*344aa361SAndroid Build Coastguard Worker /**
106*344aa361SAndroid Build Coastguard Worker  * struct test_context - struct representing the state of a test run.
107*344aa361SAndroid Build Coastguard Worker  * @tests_total:      Number of conditions checked
108*344aa361SAndroid Build Coastguard Worker  * @tests_skipped:    Number of tests skipped
109*344aa361SAndroid Build Coastguard Worker  * @tests_disabled:   Number of disabled tests skipped
110*344aa361SAndroid Build Coastguard Worker  * @tests_failed:     Number of conditions failed
111*344aa361SAndroid Build Coastguard Worker  * @inst_name:        Name of the current parameter instantiation
112*344aa361SAndroid Build Coastguard Worker  * @suite_name:       Name of the current test suite
113*344aa361SAndroid Build Coastguard Worker  * @param_name:       Name of the current parameter
114*344aa361SAndroid Build Coastguard Worker  * @test_name:        Name of current test case
115*344aa361SAndroid Build Coastguard Worker  * @test_param:       The current test parameter
116*344aa361SAndroid Build Coastguard Worker  * @all_ok:           State of current test case
117*344aa361SAndroid Build Coastguard Worker  * @skipped:          Current test was skipped.
118*344aa361SAndroid Build Coastguard Worker  * @hard_fail:        Type of test failure (when @all_ok is false)
119*344aa361SAndroid Build Coastguard Worker  * @test_start_time:  Test Start Time in ns
120*344aa361SAndroid Build Coastguard Worker  * @suite_duration_ms:Test Suite duration in ms
121*344aa361SAndroid Build Coastguard Worker  */
122*344aa361SAndroid Build Coastguard Worker struct test_context {
123*344aa361SAndroid Build Coastguard Worker     unsigned int tests_total;
124*344aa361SAndroid Build Coastguard Worker     unsigned int tests_skipped;
125*344aa361SAndroid Build Coastguard Worker     unsigned int tests_disabled;
126*344aa361SAndroid Build Coastguard Worker     unsigned int tests_failed;
127*344aa361SAndroid Build Coastguard Worker     const char* inst_name;
128*344aa361SAndroid Build Coastguard Worker     const char* suite_name;
129*344aa361SAndroid Build Coastguard Worker     const char* param_name;
130*344aa361SAndroid Build Coastguard Worker     const char* test_name;
131*344aa361SAndroid Build Coastguard Worker     const void* test_param;
132*344aa361SAndroid Build Coastguard Worker     bool all_ok;
133*344aa361SAndroid Build Coastguard Worker     bool skipped;
134*344aa361SAndroid Build Coastguard Worker     bool hard_fail;
135*344aa361SAndroid Build Coastguard Worker     uint64_t test_start_time;
136*344aa361SAndroid Build Coastguard Worker     uint64_t suite_duration_ms;
137*344aa361SAndroid Build Coastguard Worker };
138*344aa361SAndroid Build Coastguard Worker 
139*344aa361SAndroid Build Coastguard Worker /**
140*344aa361SAndroid Build Coastguard Worker  * struct test_list_node - node to hold test function in list of tests
141*344aa361SAndroid Build Coastguard Worker  * @node:           List node
142*344aa361SAndroid Build Coastguard Worker  * @suite:          Name of test suite (optionally used for filtering)
143*344aa361SAndroid Build Coastguard Worker  * @name:           Name of test (optionally used for filtering)
144*344aa361SAndroid Build Coastguard Worker  * @func:           Test function
145*344aa361SAndroid Build Coastguard Worker  * @needs_param:    Indicates if the test function is parameterized
146*344aa361SAndroid Build Coastguard Worker  */
147*344aa361SAndroid Build Coastguard Worker 
148*344aa361SAndroid Build Coastguard Worker struct test_list_node {
149*344aa361SAndroid Build Coastguard Worker     struct list_node node;
150*344aa361SAndroid Build Coastguard Worker     const char* suite;
151*344aa361SAndroid Build Coastguard Worker     const char* name;
152*344aa361SAndroid Build Coastguard Worker     void (*func)(void);
153*344aa361SAndroid Build Coastguard Worker     bool needs_param;
154*344aa361SAndroid Build Coastguard Worker };
155*344aa361SAndroid Build Coastguard Worker 
156*344aa361SAndroid Build Coastguard Worker /**
157*344aa361SAndroid Build Coastguard Worker  * struct test_param_gen -  struct representing a parameter generator
158*344aa361SAndroid Build Coastguard Worker  * @gen_param:              Function to generate the parameter for a test
159*344aa361SAndroid Build Coastguard Worker  * @priv:                   Private data passed to gen_param
160*344aa361SAndroid Build Coastguard Worker  */
161*344aa361SAndroid Build Coastguard Worker struct test_param_gen {
162*344aa361SAndroid Build Coastguard Worker     const void* (*gen_param)(void*, int);
163*344aa361SAndroid Build Coastguard Worker     void* priv;
164*344aa361SAndroid Build Coastguard Worker };
165*344aa361SAndroid Build Coastguard Worker 
166*344aa361SAndroid Build Coastguard Worker /**
167*344aa361SAndroid Build Coastguard Worker  * typedef test_param_to_string_t - Converts a test parameter to its string form
168*344aa361SAndroid Build Coastguard Worker  * @param:      Parameter to convert
169*344aa361SAndroid Build Coastguard Worker  * @buf:        Buffer to fill with a NULL terminated string representation of
170*344aa361SAndroid Build Coastguard Worker  *              @param
171*344aa361SAndroid Build Coastguard Worker  * @buf_size:   Size in bytes of @buf
172*344aa361SAndroid Build Coastguard Worker  *
173*344aa361SAndroid Build Coastguard Worker  * When called, this function is passed a pointer to the parameter for the test
174*344aa361SAndroid Build Coastguard Worker  * that is being executed in @param and must return a null-terminated string
175*344aa361SAndroid Build Coastguard Worker  * representing the passed in parameter in @buf of at most size @buf_size.
176*344aa361SAndroid Build Coastguard Worker  */
177*344aa361SAndroid Build Coastguard Worker typedef void (*test_param_to_string_t)(const void* param,
178*344aa361SAndroid Build Coastguard Worker                                        char* buf,
179*344aa361SAndroid Build Coastguard Worker                                        size_t buf_size);
180*344aa361SAndroid Build Coastguard Worker 
181*344aa361SAndroid Build Coastguard Worker /**
182*344aa361SAndroid Build Coastguard Worker  * struct test_param_list_node - holds parameter generators
183*344aa361SAndroid Build Coastguard Worker  * @node:               List node
184*344aa361SAndroid Build Coastguard Worker  * @param_gen:          Parameter generator
185*344aa361SAndroid Build Coastguard Worker  * @to_string:          Function to convert a parameter to its string form
186*344aa361SAndroid Build Coastguard Worker  * @inst_name:          Name of the instantiation associated with the generator
187*344aa361SAndroid Build Coastguard Worker  * @suite:              Name of test suite associated with the generator
188*344aa361SAndroid Build Coastguard Worker  */
189*344aa361SAndroid Build Coastguard Worker 
190*344aa361SAndroid Build Coastguard Worker struct test_param_list_node {
191*344aa361SAndroid Build Coastguard Worker     struct list_node node;
192*344aa361SAndroid Build Coastguard Worker     struct test_param_gen param_gen;
193*344aa361SAndroid Build Coastguard Worker     test_param_to_string_t to_string;
194*344aa361SAndroid Build Coastguard Worker     const char* inst_name;
195*344aa361SAndroid Build Coastguard Worker     const char* suite;
196*344aa361SAndroid Build Coastguard Worker };
197*344aa361SAndroid Build Coastguard Worker 
198*344aa361SAndroid Build Coastguard Worker static struct test_context _test_context;
199*344aa361SAndroid Build Coastguard Worker 
200*344aa361SAndroid Build Coastguard Worker /*
201*344aa361SAndroid Build Coastguard Worker  * List of tests. Tests are added by a __attribute__((constructor)) function
202*344aa361SAndroid Build Coastguard Worker  * per test defined by the TEST macro.
203*344aa361SAndroid Build Coastguard Worker  */
204*344aa361SAndroid Build Coastguard Worker static struct list_node _test_list = LIST_INITIAL_VALUE(_test_list);
205*344aa361SAndroid Build Coastguard Worker 
206*344aa361SAndroid Build Coastguard Worker /*
207*344aa361SAndroid Build Coastguard Worker  * List of parameter generators. Parameter generators  are added by a
208*344aa361SAndroid Build Coastguard Worker  * __attribute__((constructor)) function per instantiation defined with
209*344aa361SAndroid Build Coastguard Worker  * INSTANTIATE_TEST_SUITE_P.
210*344aa361SAndroid Build Coastguard Worker  */
211*344aa361SAndroid Build Coastguard Worker static struct list_node _test_param_list = LIST_INITIAL_VALUE(_test_param_list);
212*344aa361SAndroid Build Coastguard Worker 
trusty_unittest_print_status_name_param_duration(const char * status,const char * param_gen_inst_name,const char * suite_name,const char * test_name,const char * param_name,const char * duration_ms)213*344aa361SAndroid Build Coastguard Worker static inline void trusty_unittest_print_status_name_param_duration(
214*344aa361SAndroid Build Coastguard Worker         const char* status,
215*344aa361SAndroid Build Coastguard Worker         const char* param_gen_inst_name, /* parameter generator instance name */
216*344aa361SAndroid Build Coastguard Worker         const char* suite_name,
217*344aa361SAndroid Build Coastguard Worker         const char* test_name,
218*344aa361SAndroid Build Coastguard Worker         const char* param_name,
219*344aa361SAndroid Build Coastguard Worker         const char* duration_ms) {
220*344aa361SAndroid Build Coastguard Worker     if (param_gen_inst_name) {
221*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[ %s ] %s/%s.%s/%s%s\n", status,
222*344aa361SAndroid Build Coastguard Worker                                param_gen_inst_name, suite_name, test_name,
223*344aa361SAndroid Build Coastguard Worker                                param_name, duration_ms);
224*344aa361SAndroid Build Coastguard Worker     } else {
225*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[ %s ] %s.%s%s\n", status, suite_name,
226*344aa361SAndroid Build Coastguard Worker                                test_name, duration_ms);
227*344aa361SAndroid Build Coastguard Worker     }
228*344aa361SAndroid Build Coastguard Worker }
229*344aa361SAndroid Build Coastguard Worker 
trusty_unittest_print_status_name(const char * suite_name,const char * test_name,const char * status)230*344aa361SAndroid Build Coastguard Worker static inline void trusty_unittest_print_status_name(const char* suite_name,
231*344aa361SAndroid Build Coastguard Worker                                                      const char* test_name,
232*344aa361SAndroid Build Coastguard Worker                                                      const char* status) {
233*344aa361SAndroid Build Coastguard Worker     trusty_unittest_print_status_name_param_duration(
234*344aa361SAndroid Build Coastguard Worker             status, _test_context.inst_name, suite_name, test_name,
235*344aa361SAndroid Build Coastguard Worker             _test_context.param_name, "");
236*344aa361SAndroid Build Coastguard Worker }
237*344aa361SAndroid Build Coastguard Worker 
trusty_unittest_print_status(const char * status)238*344aa361SAndroid Build Coastguard Worker static inline void trusty_unittest_print_status(const char* status) {
239*344aa361SAndroid Build Coastguard Worker     trusty_unittest_print_status_name_param_duration(
240*344aa361SAndroid Build Coastguard Worker             status, _test_context.inst_name, _test_context.suite_name,
241*344aa361SAndroid Build Coastguard Worker             _test_context.test_name, _test_context.param_name, "");
242*344aa361SAndroid Build Coastguard Worker }
243*344aa361SAndroid Build Coastguard Worker 
trusty_unittest_print_status_duration(const char * status,uint64_t test_duration_ms)244*344aa361SAndroid Build Coastguard Worker static inline void trusty_unittest_print_status_duration(
245*344aa361SAndroid Build Coastguard Worker         const char* status,
246*344aa361SAndroid Build Coastguard Worker         uint64_t test_duration_ms) {
247*344aa361SAndroid Build Coastguard Worker     char duration_str[16] = "";
248*344aa361SAndroid Build Coastguard Worker 
249*344aa361SAndroid Build Coastguard Worker     /* print duration at end of test case */
250*344aa361SAndroid Build Coastguard Worker     snprintf(duration_str, sizeof(duration_str), " (%" PRIu64 " ms)",
251*344aa361SAndroid Build Coastguard Worker              test_duration_ms);
252*344aa361SAndroid Build Coastguard Worker     trusty_unittest_print_status_name_param_duration(
253*344aa361SAndroid Build Coastguard Worker             status, _test_context.inst_name, _test_context.suite_name,
254*344aa361SAndroid Build Coastguard Worker             _test_context.test_name, _test_context.param_name,
255*344aa361SAndroid Build Coastguard Worker             (const char*)duration_str);
256*344aa361SAndroid Build Coastguard Worker }
257*344aa361SAndroid Build Coastguard Worker 
TEST_BEGIN_FUNC(const char * suite_name,const char * test_name)258*344aa361SAndroid Build Coastguard Worker static inline void TEST_BEGIN_FUNC(const char* suite_name,
259*344aa361SAndroid Build Coastguard Worker                                    const char* test_name) {
260*344aa361SAndroid Build Coastguard Worker     _test_context.suite_name = suite_name;
261*344aa361SAndroid Build Coastguard Worker     _test_context.test_name = test_name;
262*344aa361SAndroid Build Coastguard Worker     _test_context.all_ok = true;
263*344aa361SAndroid Build Coastguard Worker     _test_context.hard_fail = false;
264*344aa361SAndroid Build Coastguard Worker     _test_context.skipped = false;
265*344aa361SAndroid Build Coastguard Worker     _test_context.tests_total++;
266*344aa361SAndroid Build Coastguard Worker     trusty_unittest_print_status("RUN     ");
267*344aa361SAndroid Build Coastguard Worker     /*
268*344aa361SAndroid Build Coastguard Worker      * initialize the test start time
269*344aa361SAndroid Build Coastguard Worker      * (after the print status is slightly better)
270*344aa361SAndroid Build Coastguard Worker      */
271*344aa361SAndroid Build Coastguard Worker     _test_context.test_start_time = get_current_time_ns();
272*344aa361SAndroid Build Coastguard Worker }
273*344aa361SAndroid Build Coastguard Worker 
TEST_END_FUNC(void)274*344aa361SAndroid Build Coastguard Worker static inline void TEST_END_FUNC(void) {
275*344aa361SAndroid Build Coastguard Worker     uint64_t test_duration_ms =
276*344aa361SAndroid Build Coastguard Worker             (get_current_time_ns() - _test_context.test_start_time) / 1000000;
277*344aa361SAndroid Build Coastguard Worker     _test_context.suite_duration_ms += test_duration_ms;
278*344aa361SAndroid Build Coastguard Worker     if (_test_context.skipped) {
279*344aa361SAndroid Build Coastguard Worker         trusty_unittest_print_status_duration(" SKIPPED", test_duration_ms);
280*344aa361SAndroid Build Coastguard Worker     } else if (_test_context.all_ok) {
281*344aa361SAndroid Build Coastguard Worker         trusty_unittest_print_status_duration("      OK", test_duration_ms);
282*344aa361SAndroid Build Coastguard Worker     } else {
283*344aa361SAndroid Build Coastguard Worker         trusty_unittest_print_status_duration(" FAILED ", test_duration_ms);
284*344aa361SAndroid Build Coastguard Worker     }
285*344aa361SAndroid Build Coastguard Worker     _test_context.test_name = NULL;
286*344aa361SAndroid Build Coastguard Worker }
287*344aa361SAndroid Build Coastguard Worker 
288*344aa361SAndroid Build Coastguard Worker #define STRINGIFY(x) #x
289*344aa361SAndroid Build Coastguard Worker 
290*344aa361SAndroid Build Coastguard Worker #define TEST_FIXTURE_ALIAS(new_suite_name, old_suite_name)              \
291*344aa361SAndroid Build Coastguard Worker     typedef old_suite_name##_t new_suite_name##_t;                      \
292*344aa361SAndroid Build Coastguard Worker                                                                         \
293*344aa361SAndroid Build Coastguard Worker     static void new_suite_name##_SetUp(new_suite_name##_t* _state) {    \
294*344aa361SAndroid Build Coastguard Worker         old_suite_name##_SetUp(_state);                                 \
295*344aa361SAndroid Build Coastguard Worker     }                                                                   \
296*344aa361SAndroid Build Coastguard Worker     static void new_suite_name##_TearDown(new_suite_name##_t* _state) { \
297*344aa361SAndroid Build Coastguard Worker         old_suite_name##_TearDown(_state);                              \
298*344aa361SAndroid Build Coastguard Worker     }
299*344aa361SAndroid Build Coastguard Worker 
300*344aa361SAndroid Build Coastguard Worker #define TEST_INTERNAL(suite_name, test_name, w_param, pre, post, arg, argp)  \
301*344aa361SAndroid Build Coastguard Worker     static void suite_name##_##test_name##_inner argp;                       \
302*344aa361SAndroid Build Coastguard Worker                                                                              \
303*344aa361SAndroid Build Coastguard Worker     static void suite_name##_##test_name(void) {                             \
304*344aa361SAndroid Build Coastguard Worker         TEST_BEGIN_FUNC(STRINGIFY(suite_name), STRINGIFY(test_name));        \
305*344aa361SAndroid Build Coastguard Worker         {                                                                    \
306*344aa361SAndroid Build Coastguard Worker             pre;                                                             \
307*344aa361SAndroid Build Coastguard Worker             if (!_test_context.hard_fail && !_test_context.skipped) {        \
308*344aa361SAndroid Build Coastguard Worker                 suite_name##_##test_name##_inner arg;                        \
309*344aa361SAndroid Build Coastguard Worker             }                                                                \
310*344aa361SAndroid Build Coastguard Worker             post;                                                            \
311*344aa361SAndroid Build Coastguard Worker         }                                                                    \
312*344aa361SAndroid Build Coastguard Worker         TEST_END_FUNC();                                                     \
313*344aa361SAndroid Build Coastguard Worker     }                                                                        \
314*344aa361SAndroid Build Coastguard Worker                                                                              \
315*344aa361SAndroid Build Coastguard Worker     static struct test_list_node suite_name##_##test_name##_node = {         \
316*344aa361SAndroid Build Coastguard Worker             .node = LIST_INITIAL_CLEARED_VALUE,                              \
317*344aa361SAndroid Build Coastguard Worker             .suite = #suite_name,                                            \
318*344aa361SAndroid Build Coastguard Worker             .name = #test_name,                                              \
319*344aa361SAndroid Build Coastguard Worker             .func = suite_name##_##test_name,                                \
320*344aa361SAndroid Build Coastguard Worker             .needs_param = w_param,                                          \
321*344aa361SAndroid Build Coastguard Worker     };                                                                       \
322*344aa361SAndroid Build Coastguard Worker                                                                              \
323*344aa361SAndroid Build Coastguard Worker     __attribute__((constructor)) void suite_name##_##test_name##_add(void) { \
324*344aa361SAndroid Build Coastguard Worker         list_add_tail(&_test_list, &suite_name##_##test_name##_node.node);   \
325*344aa361SAndroid Build Coastguard Worker     }                                                                        \
326*344aa361SAndroid Build Coastguard Worker                                                                              \
327*344aa361SAndroid Build Coastguard Worker     static void suite_name##_##test_name##_inner argp
328*344aa361SAndroid Build Coastguard Worker 
329*344aa361SAndroid Build Coastguard Worker #define TEST_F_SETUP(suite_name) \
330*344aa361SAndroid Build Coastguard Worker     static void suite_name##_SetUp(suite_name##_t* _state)
331*344aa361SAndroid Build Coastguard Worker 
332*344aa361SAndroid Build Coastguard Worker #define TEST_F_TEARDOWN(suite_name) \
333*344aa361SAndroid Build Coastguard Worker     static void suite_name##_TearDown(suite_name##_t* _state)
334*344aa361SAndroid Build Coastguard Worker 
335*344aa361SAndroid Build Coastguard Worker #define TEST(suite_name, test_name) \
336*344aa361SAndroid Build Coastguard Worker     TEST_INTERNAL(suite_name, test_name, false, , , (), (void))
337*344aa361SAndroid Build Coastguard Worker 
338*344aa361SAndroid Build Coastguard Worker #define TEST_F_CUSTOM_ARGS(suite_name, test_name, arg, argp)                  \
339*344aa361SAndroid Build Coastguard Worker     TEST_INTERNAL(suite_name, test_name, false, suite_name##_t state;         \
340*344aa361SAndroid Build Coastguard Worker                   suite_name##_SetUp(&state);, suite_name##_TearDown(&state); \
341*344aa361SAndroid Build Coastguard Worker                   , arg, argp)
342*344aa361SAndroid Build Coastguard Worker 
343*344aa361SAndroid Build Coastguard Worker #define TEST_F(suite_name, test_name)                   \
344*344aa361SAndroid Build Coastguard Worker     TEST_F_CUSTOM_ARGS(suite_name, test_name, (&state), \
345*344aa361SAndroid Build Coastguard Worker                        (suite_name##_t * _state))
346*344aa361SAndroid Build Coastguard Worker 
347*344aa361SAndroid Build Coastguard Worker #define TEST_P_CUSTOM_ARGS(suite_name, test_name, arg, argp)                  \
348*344aa361SAndroid Build Coastguard Worker     TEST_INTERNAL(suite_name, test_name, true, suite_name##_t state;          \
349*344aa361SAndroid Build Coastguard Worker                   suite_name##_SetUp(&state);, suite_name##_TearDown(&state); \
350*344aa361SAndroid Build Coastguard Worker                   , arg, argp)
351*344aa361SAndroid Build Coastguard Worker 
352*344aa361SAndroid Build Coastguard Worker #define TEST_P(suite_name, test_name)                   \
353*344aa361SAndroid Build Coastguard Worker     TEST_P_CUSTOM_ARGS(suite_name, test_name, (&state), \
354*344aa361SAndroid Build Coastguard Worker                        (suite_name##_t * _state))
355*344aa361SAndroid Build Coastguard Worker 
356*344aa361SAndroid Build Coastguard Worker struct test_array_param {
357*344aa361SAndroid Build Coastguard Worker     const void* arr;
358*344aa361SAndroid Build Coastguard Worker     int elem_size;
359*344aa361SAndroid Build Coastguard Worker     int count;
360*344aa361SAndroid Build Coastguard Worker };
361*344aa361SAndroid Build Coastguard Worker 
test_gen_array_param(void * priv,int i)362*344aa361SAndroid Build Coastguard Worker static inline const void* test_gen_array_param(void* priv, int i) {
363*344aa361SAndroid Build Coastguard Worker     struct test_array_param* param = (struct test_array_param*)priv;
364*344aa361SAndroid Build Coastguard Worker 
365*344aa361SAndroid Build Coastguard Worker     if (i >= param->count) {
366*344aa361SAndroid Build Coastguard Worker         return NULL;
367*344aa361SAndroid Build Coastguard Worker     }
368*344aa361SAndroid Build Coastguard Worker 
369*344aa361SAndroid Build Coastguard Worker     return (uint8_t*)param->arr + param->elem_size * i;
370*344aa361SAndroid Build Coastguard Worker }
371*344aa361SAndroid Build Coastguard Worker 
372*344aa361SAndroid Build Coastguard Worker struct test_range_param {
373*344aa361SAndroid Build Coastguard Worker     long begin;
374*344aa361SAndroid Build Coastguard Worker     long end;
375*344aa361SAndroid Build Coastguard Worker     long step;
376*344aa361SAndroid Build Coastguard Worker     long current;
377*344aa361SAndroid Build Coastguard Worker };
378*344aa361SAndroid Build Coastguard Worker 
test_gen_range_param(void * priv,int i)379*344aa361SAndroid Build Coastguard Worker static inline const void* test_gen_range_param(void* priv, int i) {
380*344aa361SAndroid Build Coastguard Worker     struct test_range_param* range_param = (struct test_range_param*)priv;
381*344aa361SAndroid Build Coastguard Worker 
382*344aa361SAndroid Build Coastguard Worker     range_param->current = range_param->begin + range_param->step * i;
383*344aa361SAndroid Build Coastguard Worker 
384*344aa361SAndroid Build Coastguard Worker     if (range_param->current >= range_param->end) {
385*344aa361SAndroid Build Coastguard Worker         return NULL;
386*344aa361SAndroid Build Coastguard Worker     }
387*344aa361SAndroid Build Coastguard Worker 
388*344aa361SAndroid Build Coastguard Worker     return &range_param->current;
389*344aa361SAndroid Build Coastguard Worker }
390*344aa361SAndroid Build Coastguard Worker 
391*344aa361SAndroid Build Coastguard Worker struct combined_params {
392*344aa361SAndroid Build Coastguard Worker     struct test_param_gen* generators;
393*344aa361SAndroid Build Coastguard Worker     int generator_count;
394*344aa361SAndroid Build Coastguard Worker     int* idxs;
395*344aa361SAndroid Build Coastguard Worker     const void** current;
396*344aa361SAndroid Build Coastguard Worker };
397*344aa361SAndroid Build Coastguard Worker 
update_combined_params(struct combined_params * params,int j,bool reset)398*344aa361SAndroid Build Coastguard Worker static inline void update_combined_params(struct combined_params* params,
399*344aa361SAndroid Build Coastguard Worker                                           int j,
400*344aa361SAndroid Build Coastguard Worker                                           bool reset) {
401*344aa361SAndroid Build Coastguard Worker     if (reset) {
402*344aa361SAndroid Build Coastguard Worker         params->idxs[j] = 0;
403*344aa361SAndroid Build Coastguard Worker     }
404*344aa361SAndroid Build Coastguard Worker 
405*344aa361SAndroid Build Coastguard Worker     params->current[j] = params->generators[j].gen_param(
406*344aa361SAndroid Build Coastguard Worker             params->generators[j].priv, params->idxs[j]);
407*344aa361SAndroid Build Coastguard Worker     params->idxs[j]++;
408*344aa361SAndroid Build Coastguard Worker }
409*344aa361SAndroid Build Coastguard Worker 
test_gen_combined_param(void * priv,int i)410*344aa361SAndroid Build Coastguard Worker static inline const void* test_gen_combined_param(void* priv, int i) {
411*344aa361SAndroid Build Coastguard Worker     struct combined_params* params = (struct combined_params*)priv;
412*344aa361SAndroid Build Coastguard Worker 
413*344aa361SAndroid Build Coastguard Worker     if (i == 0) {
414*344aa361SAndroid Build Coastguard Worker         for (int j = 0; j < params->generator_count; j++) {
415*344aa361SAndroid Build Coastguard Worker             update_combined_params(params, j, true);
416*344aa361SAndroid Build Coastguard Worker         }
417*344aa361SAndroid Build Coastguard Worker         return params->current;
418*344aa361SAndroid Build Coastguard Worker     }
419*344aa361SAndroid Build Coastguard Worker 
420*344aa361SAndroid Build Coastguard Worker     for (int j = 0; j < params->generator_count; j++) {
421*344aa361SAndroid Build Coastguard Worker         update_combined_params(params, j, false);
422*344aa361SAndroid Build Coastguard Worker 
423*344aa361SAndroid Build Coastguard Worker         if (params->current[j] != NULL) {
424*344aa361SAndroid Build Coastguard Worker             return params->current;
425*344aa361SAndroid Build Coastguard Worker         }
426*344aa361SAndroid Build Coastguard Worker 
427*344aa361SAndroid Build Coastguard Worker         update_combined_params(params, j, true);
428*344aa361SAndroid Build Coastguard Worker     }
429*344aa361SAndroid Build Coastguard Worker 
430*344aa361SAndroid Build Coastguard Worker     return NULL;
431*344aa361SAndroid Build Coastguard Worker }
432*344aa361SAndroid Build Coastguard Worker 
433*344aa361SAndroid Build Coastguard Worker #define FIRST_ARG(arg0, args...) arg0
434*344aa361SAndroid Build Coastguard Worker #define SECOND_ARG(arg0, arg1, args...) arg1
435*344aa361SAndroid Build Coastguard Worker /* Parentheses are used to prevent commas from being interpreted when they are
436*344aa361SAndroid Build Coastguard Worker  * passed in macro arguments. DELETE_PAREN is used to remove these parentheses
437*344aa361SAndroid Build Coastguard Worker  * inside the macro that uses the commas e.g.:
438*344aa361SAndroid Build Coastguard Worker  *
439*344aa361SAndroid Build Coastguard Worker  * MY_MACRO((1, 2, 3))
440*344aa361SAndroid Build Coastguard Worker  *
441*344aa361SAndroid Build Coastguard Worker  * #define MY_MACRO(arg)
442*344aa361SAndroid Build Coastguard Worker  *      DELETE_PAREN arg
443*344aa361SAndroid Build Coastguard Worker  */
444*344aa361SAndroid Build Coastguard Worker #define DELETE_PAREN(args...) args
445*344aa361SAndroid Build Coastguard Worker 
446*344aa361SAndroid Build Coastguard Worker #define testing_Range(_begin, end_step...)                  \
447*344aa361SAndroid Build Coastguard Worker     (static struct test_range_param range_param =           \
448*344aa361SAndroid Build Coastguard Worker              {                                              \
449*344aa361SAndroid Build Coastguard Worker                      .begin = _begin,                       \
450*344aa361SAndroid Build Coastguard Worker                      .end = FIRST_ARG(end_step, ),          \
451*344aa361SAndroid Build Coastguard Worker                      .step = SECOND_ARG(end_step, 1, ),     \
452*344aa361SAndroid Build Coastguard Worker              };                                             \
453*344aa361SAndroid Build Coastguard Worker      param_node.param_gen.gen_param = test_gen_range_param; \
454*344aa361SAndroid Build Coastguard Worker      param_node.param_gen.priv = &range_param;)
455*344aa361SAndroid Build Coastguard Worker 
456*344aa361SAndroid Build Coastguard Worker #define testing_ValuesIn(array)                             \
457*344aa361SAndroid Build Coastguard Worker     (static struct test_array_param array_param =           \
458*344aa361SAndroid Build Coastguard Worker              {                                              \
459*344aa361SAndroid Build Coastguard Worker                      .arr = array,                          \
460*344aa361SAndroid Build Coastguard Worker                      .elem_size = sizeof(array[0]),         \
461*344aa361SAndroid Build Coastguard Worker                      .count = countof(array),               \
462*344aa361SAndroid Build Coastguard Worker              };                                             \
463*344aa361SAndroid Build Coastguard Worker                                                             \
464*344aa361SAndroid Build Coastguard Worker      param_node.param_gen.gen_param = test_gen_array_param; \
465*344aa361SAndroid Build Coastguard Worker      param_node.param_gen.priv = &array_param;)
466*344aa361SAndroid Build Coastguard Worker 
467*344aa361SAndroid Build Coastguard Worker /*
468*344aa361SAndroid Build Coastguard Worker  * (args, args) is passed to __typeof__ to guarantee that it resolves to const
469*344aa361SAndroid Build Coastguard Worker  * char* instead of const char[] in cases where args contains a single string.
470*344aa361SAndroid Build Coastguard Worker  * When args is a single string, it is inlined and typeof will resolve to const
471*344aa361SAndroid Build Coastguard Worker  * char[].
472*344aa361SAndroid Build Coastguard Worker  */
473*344aa361SAndroid Build Coastguard Worker #define testing_Values(args...)                        \
474*344aa361SAndroid Build Coastguard Worker     (static __typeof__(args, args) new_arr[] = {args}; \
475*344aa361SAndroid Build Coastguard Worker      DELETE_PAREN testing_ValuesIn(new_arr))
476*344aa361SAndroid Build Coastguard Worker 
477*344aa361SAndroid Build Coastguard Worker #define testing_Bool() testing_Values(false, true)
478*344aa361SAndroid Build Coastguard Worker 
479*344aa361SAndroid Build Coastguard Worker #define test_set_combine_params(generator, i, count)                  \
480*344aa361SAndroid Build Coastguard Worker     {                                                                 \
481*344aa361SAndroid Build Coastguard Worker         DELETE_PAREN generator;                                       \
482*344aa361SAndroid Build Coastguard Worker         if (i < count) {                                              \
483*344aa361SAndroid Build Coastguard Worker             param_gens[i].gen_param = param_node.param_gen.gen_param; \
484*344aa361SAndroid Build Coastguard Worker             param_gens[i].priv = param_node.param_gen.priv;           \
485*344aa361SAndroid Build Coastguard Worker         }                                                             \
486*344aa361SAndroid Build Coastguard Worker     }
487*344aa361SAndroid Build Coastguard Worker 
488*344aa361SAndroid Build Coastguard Worker #define testing_Combine_internal(arg0, arg1, arg2, arg3, arg4, arg5, arg6,   \
489*344aa361SAndroid Build Coastguard Worker                                  arg7, arg8, arg9, da0, da1, da2, da3, da4,  \
490*344aa361SAndroid Build Coastguard Worker                                  da5, da6, da7, da8, da9, count, args...)    \
491*344aa361SAndroid Build Coastguard Worker     (static struct test_param_gen param_gens[count]; static int idxs[count]; \
492*344aa361SAndroid Build Coastguard Worker      static const void* current_params[count];                               \
493*344aa361SAndroid Build Coastguard Worker      static struct combined_params combined_params =                         \
494*344aa361SAndroid Build Coastguard Worker              {                                                               \
495*344aa361SAndroid Build Coastguard Worker                      param_gens,                                             \
496*344aa361SAndroid Build Coastguard Worker                      count,                                                  \
497*344aa361SAndroid Build Coastguard Worker                      idxs,                                                   \
498*344aa361SAndroid Build Coastguard Worker                      current_params,                                         \
499*344aa361SAndroid Build Coastguard Worker              };                                                              \
500*344aa361SAndroid Build Coastguard Worker                                                                              \
501*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg0, 0, count);                                \
502*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg1, 1, count);                                \
503*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg2, 2, count);                                \
504*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg3, 3, count);                                \
505*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg4, 4, count);                                \
506*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg5, 5, count);                                \
507*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg6, 6, count);                                \
508*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg7, 7, count);                                \
509*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg8, 8, count);                                \
510*344aa361SAndroid Build Coastguard Worker      test_set_combine_params(arg9, 9, count);                                \
511*344aa361SAndroid Build Coastguard Worker      param_node.param_gen.gen_param = test_gen_combined_param;               \
512*344aa361SAndroid Build Coastguard Worker      param_node.param_gen.priv = &combined_params;)
513*344aa361SAndroid Build Coastguard Worker 
514*344aa361SAndroid Build Coastguard Worker #define testing_Combine(generators...)                                       \
515*344aa361SAndroid Build Coastguard Worker     testing_Combine_internal(generators, (), (), (), (), (), (), (), (), (), \
516*344aa361SAndroid Build Coastguard Worker                              (), 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
517*344aa361SAndroid Build Coastguard Worker 
518*344aa361SAndroid Build Coastguard Worker #define INSTANTIATE_TEST_SUITE_P_INTERNAL(_inst_name, suite_name, param_gen, \
519*344aa361SAndroid Build Coastguard Worker                                           param_to_string, args...)          \
520*344aa361SAndroid Build Coastguard Worker                                                                              \
521*344aa361SAndroid Build Coastguard Worker     __attribute__((constructor)) void suite_name##_##_inst_name##param_add(  \
522*344aa361SAndroid Build Coastguard Worker             void) {                                                          \
523*344aa361SAndroid Build Coastguard Worker         static struct test_param_list_node param_node = {                    \
524*344aa361SAndroid Build Coastguard Worker                 .node = LIST_INITIAL_CLEARED_VALUE,                          \
525*344aa361SAndroid Build Coastguard Worker                 .to_string = param_to_string,                                \
526*344aa361SAndroid Build Coastguard Worker                 .inst_name = STRINGIFY(_inst_name),                          \
527*344aa361SAndroid Build Coastguard Worker                 .suite = #suite_name,                                        \
528*344aa361SAndroid Build Coastguard Worker         };                                                                   \
529*344aa361SAndroid Build Coastguard Worker                                                                              \
530*344aa361SAndroid Build Coastguard Worker         DELETE_PAREN param_gen;                                              \
531*344aa361SAndroid Build Coastguard Worker                                                                              \
532*344aa361SAndroid Build Coastguard Worker         list_add_tail(&_test_param_list, &param_node.node);                  \
533*344aa361SAndroid Build Coastguard Worker     }
534*344aa361SAndroid Build Coastguard Worker 
has_disabled_prefix(const char * str)535*344aa361SAndroid Build Coastguard Worker static inline bool has_disabled_prefix(const char* str) {
536*344aa361SAndroid Build Coastguard Worker     const char disabled_prefix[] = "DISABLED_";
537*344aa361SAndroid Build Coastguard Worker     return strncmp(str, disabled_prefix, strlen(disabled_prefix)) == 0;
538*344aa361SAndroid Build Coastguard Worker }
539*344aa361SAndroid Build Coastguard Worker 
trusty_unittest_test_is_disabled(struct test_list_node * entry)540*344aa361SAndroid Build Coastguard Worker static inline bool trusty_unittest_test_is_disabled(
541*344aa361SAndroid Build Coastguard Worker         struct test_list_node* entry) {
542*344aa361SAndroid Build Coastguard Worker     return has_disabled_prefix(entry->suite) ||
543*344aa361SAndroid Build Coastguard Worker            has_disabled_prefix(entry->name);
544*344aa361SAndroid Build Coastguard Worker }
545*344aa361SAndroid Build Coastguard Worker 
546*344aa361SAndroid Build Coastguard Worker /**
547*344aa361SAndroid Build Coastguard Worker  * trusty_unittest_count_param_entries - Count parameter entries for a given
548*344aa361SAndroid Build Coastguard Worker  * test suite.
549*344aa361SAndroid Build Coastguard Worker  *
550*344aa361SAndroid Build Coastguard Worker  * @suite: Name of test suite associated with the parameters
551*344aa361SAndroid Build Coastguard Worker  *         suite is never going to be NULL (see invocation),
552*344aa361SAndroid Build Coastguard Worker  *         no need to guard against this case.
553*344aa361SAndroid Build Coastguard Worker  *
554*344aa361SAndroid Build Coastguard Worker  * For each parameter generator associated with the suite, accrue
555*344aa361SAndroid Build Coastguard Worker  * the number of parameter entries.
556*344aa361SAndroid Build Coastguard Worker  *
557*344aa361SAndroid Build Coastguard Worker  * Not meant for external use.
558*344aa361SAndroid Build Coastguard Worker  *
559*344aa361SAndroid Build Coastguard Worker  * Return: count of parameter entries
560*344aa361SAndroid Build Coastguard Worker  */
trusty_unittest_count_param_entries(struct test_list_node * test_case)561*344aa361SAndroid Build Coastguard Worker static int trusty_unittest_count_param_entries(
562*344aa361SAndroid Build Coastguard Worker         struct test_list_node* test_case) {
563*344aa361SAndroid Build Coastguard Worker     int param_count = 0;
564*344aa361SAndroid Build Coastguard Worker     int i;
565*344aa361SAndroid Build Coastguard Worker     struct test_param_list_node* param_entry;
566*344aa361SAndroid Build Coastguard Worker     bool has_param_gen = 0;
567*344aa361SAndroid Build Coastguard Worker     bool invalid_run = false;
568*344aa361SAndroid Build Coastguard Worker     /*
569*344aa361SAndroid Build Coastguard Worker      * for each parameter generator associated with the suite,
570*344aa361SAndroid Build Coastguard Worker      * accrue the number of parameter entries
571*344aa361SAndroid Build Coastguard Worker      */
572*344aa361SAndroid Build Coastguard Worker     list_for_every_entry(&_test_param_list, param_entry,
573*344aa361SAndroid Build Coastguard Worker                          struct test_param_list_node, node) {
574*344aa361SAndroid Build Coastguard Worker         if (!strcmp(test_case->suite, param_entry->suite)) {
575*344aa361SAndroid Build Coastguard Worker             i = 0;
576*344aa361SAndroid Build Coastguard Worker             has_param_gen = true;
577*344aa361SAndroid Build Coastguard Worker             /* For each parameter from the generator */
578*344aa361SAndroid Build Coastguard Worker             while (param_entry->param_gen.gen_param(param_entry->param_gen.priv,
579*344aa361SAndroid Build Coastguard Worker                                                     i)) {
580*344aa361SAndroid Build Coastguard Worker                 i++;
581*344aa361SAndroid Build Coastguard Worker             }
582*344aa361SAndroid Build Coastguard Worker             if (!i) {
583*344aa361SAndroid Build Coastguard Worker                 /*
584*344aa361SAndroid Build Coastguard Worker                  * No parameter entries: parameterized test case exist
585*344aa361SAndroid Build Coastguard Worker                  * but the test generator is empty
586*344aa361SAndroid Build Coastguard Worker                  */
587*344aa361SAndroid Build Coastguard Worker                 trusty_unittest_print_status_name_param_duration(
588*344aa361SAndroid Build Coastguard Worker                         " FAILED ", param_entry->inst_name, test_case->suite,
589*344aa361SAndroid Build Coastguard Worker                         test_case->name,
590*344aa361SAndroid Build Coastguard Worker                         "NO PARAMS: Parameterized Test Case Generator without Params!",
591*344aa361SAndroid Build Coastguard Worker                         "");
592*344aa361SAndroid Build Coastguard Worker                 invalid_run = true;
593*344aa361SAndroid Build Coastguard Worker             }
594*344aa361SAndroid Build Coastguard Worker             param_count += i;
595*344aa361SAndroid Build Coastguard Worker         }
596*344aa361SAndroid Build Coastguard Worker     }
597*344aa361SAndroid Build Coastguard Worker     /*
598*344aa361SAndroid Build Coastguard Worker      * No parameter generator: parameterized test case exist
599*344aa361SAndroid Build Coastguard Worker      * but the test suite is not associated with any param generator
600*344aa361SAndroid Build Coastguard Worker      */
601*344aa361SAndroid Build Coastguard Worker     if (!has_param_gen) {
602*344aa361SAndroid Build Coastguard Worker         trusty_unittest_print_status_name_param_duration(
603*344aa361SAndroid Build Coastguard Worker                 " FAILED ", "NO PARAM GENERATOR", test_case->suite,
604*344aa361SAndroid Build Coastguard Worker                 test_case->name,
605*344aa361SAndroid Build Coastguard Worker                 "NO PARAMS: Parameterized Test Case without Param Generator!",
606*344aa361SAndroid Build Coastguard Worker                 "");
607*344aa361SAndroid Build Coastguard Worker     }
608*344aa361SAndroid Build Coastguard Worker     return invalid_run ? 0 : param_count;
609*344aa361SAndroid Build Coastguard Worker }
610*344aa361SAndroid Build Coastguard Worker 
611*344aa361SAndroid Build Coastguard Worker /**
612*344aa361SAndroid Build Coastguard Worker  * trusty_unittest_has_parameterized_test_case - test suite with parameterized
613*344aa361SAndroid Build Coastguard Worker  * test cases
614*344aa361SAndroid Build Coastguard Worker  *
615*344aa361SAndroid Build Coastguard Worker  * @suite: Name of test suite associated with the parameters.
616*344aa361SAndroid Build Coastguard Worker  *         suite is never going to be NULL (see invocation),
617*344aa361SAndroid Build Coastguard Worker  *         no need to guard against this case.
618*344aa361SAndroid Build Coastguard Worker  *
619*344aa361SAndroid Build Coastguard Worker  * Check whether a test suite has parameterized test cases.
620*344aa361SAndroid Build Coastguard Worker  * Not meant for external use.
621*344aa361SAndroid Build Coastguard Worker  *
622*344aa361SAndroid Build Coastguard Worker  * Return: True if parameterized test
623*344aa361SAndroid Build Coastguard Worker  */
trusty_unittest_has_parameterized_test_case(const char * suite)624*344aa361SAndroid Build Coastguard Worker static bool trusty_unittest_has_parameterized_test_case(const char* suite) {
625*344aa361SAndroid Build Coastguard Worker     struct test_list_node* test_case;
626*344aa361SAndroid Build Coastguard Worker     list_for_every_entry(&_test_list, test_case, struct test_list_node, node) {
627*344aa361SAndroid Build Coastguard Worker         if (!strcmp(suite, test_case->suite)) {
628*344aa361SAndroid Build Coastguard Worker             if (test_case->needs_param) {
629*344aa361SAndroid Build Coastguard Worker                 return true;
630*344aa361SAndroid Build Coastguard Worker             }
631*344aa361SAndroid Build Coastguard Worker         }
632*344aa361SAndroid Build Coastguard Worker     }
633*344aa361SAndroid Build Coastguard Worker     return false;
634*344aa361SAndroid Build Coastguard Worker }
635*344aa361SAndroid Build Coastguard Worker 
636*344aa361SAndroid Build Coastguard Worker /**
637*344aa361SAndroid Build Coastguard Worker  * trusty_unittest_count_test_cases - Count test cases associated with test
638*344aa361SAndroid Build Coastguard Worker  * suite.
639*344aa361SAndroid Build Coastguard Worker  *
640*344aa361SAndroid Build Coastguard Worker  * @suite: Name of test suite.
641*344aa361SAndroid Build Coastguard Worker  *         When suite is NULL, all test cases from all test suites are counted.
642*344aa361SAndroid Build Coastguard Worker  *
643*344aa361SAndroid Build Coastguard Worker  * This test case count shall comply to the GTest parser requirements
644*344aa361SAndroid Build Coastguard Worker  * and thus shall not include disabled test cases.
645*344aa361SAndroid Build Coastguard Worker  * Not meant for external use.
646*344aa361SAndroid Build Coastguard Worker  *
647*344aa361SAndroid Build Coastguard Worker  * Return: count of test cases, -1 in case of detected test suite coding error
648*344aa361SAndroid Build Coastguard Worker  */
trusty_unittest_count_test_cases(const char * suite)649*344aa361SAndroid Build Coastguard Worker static int trusty_unittest_count_test_cases(const char* suite) {
650*344aa361SAndroid Build Coastguard Worker     struct test_list_node* test_case;
651*344aa361SAndroid Build Coastguard Worker     struct test_param_list_node* param_entry;
652*344aa361SAndroid Build Coastguard Worker     bool test_code_error = false;
653*344aa361SAndroid Build Coastguard Worker     size_t test_case_count = 0;
654*344aa361SAndroid Build Coastguard Worker     bool disabled;
655*344aa361SAndroid Build Coastguard Worker     int param_entries = 0;
656*344aa361SAndroid Build Coastguard Worker     const char* param_entries_suite = NULL;
657*344aa361SAndroid Build Coastguard Worker     int current_test_case_count;
658*344aa361SAndroid Build Coastguard Worker     /* count all non parameterized and parameterized test cases */
659*344aa361SAndroid Build Coastguard Worker     list_for_every_entry(&_test_list, test_case, struct test_list_node, node) {
660*344aa361SAndroid Build Coastguard Worker         /* exclude tests not part of the requested suite */
661*344aa361SAndroid Build Coastguard Worker         if (suite && strcmp(suite, test_case->suite)) {
662*344aa361SAndroid Build Coastguard Worker             continue;
663*344aa361SAndroid Build Coastguard Worker         }
664*344aa361SAndroid Build Coastguard Worker         /* only count non-disabled test case as required by the GTest parser */
665*344aa361SAndroid Build Coastguard Worker         disabled = trusty_unittest_test_is_disabled(test_case);
666*344aa361SAndroid Build Coastguard Worker         if (test_case->needs_param) {
667*344aa361SAndroid Build Coastguard Worker             if (!param_entries_suite || !param_entries ||
668*344aa361SAndroid Build Coastguard Worker                 strcmp(param_entries_suite, test_case->suite)) {
669*344aa361SAndroid Build Coastguard Worker                 /* count param_entries for test_case->suite */
670*344aa361SAndroid Build Coastguard Worker                 param_entries = trusty_unittest_count_param_entries(test_case);
671*344aa361SAndroid Build Coastguard Worker                 param_entries_suite = test_case->suite;
672*344aa361SAndroid Build Coastguard Worker             }
673*344aa361SAndroid Build Coastguard Worker             if (!param_entries) {
674*344aa361SAndroid Build Coastguard Worker                 /*
675*344aa361SAndroid Build Coastguard Worker                  * Test code error shall be fixed and will prevent test
676*344aa361SAndroid Build Coastguard Worker                  * execution however we don't bail right away with the goal of
677*344aa361SAndroid Build Coastguard Worker                  * logging all erroneous test cases.
678*344aa361SAndroid Build Coastguard Worker                  */
679*344aa361SAndroid Build Coastguard Worker                 test_code_error = true;
680*344aa361SAndroid Build Coastguard Worker                 continue;
681*344aa361SAndroid Build Coastguard Worker             }
682*344aa361SAndroid Build Coastguard Worker             current_test_case_count = param_entries;
683*344aa361SAndroid Build Coastguard Worker         } else {
684*344aa361SAndroid Build Coastguard Worker             current_test_case_count = 1;
685*344aa361SAndroid Build Coastguard Worker         }
686*344aa361SAndroid Build Coastguard Worker         if (!disabled) {
687*344aa361SAndroid Build Coastguard Worker             /* non parameterized (singular) test case */
688*344aa361SAndroid Build Coastguard Worker             test_case_count += current_test_case_count;
689*344aa361SAndroid Build Coastguard Worker         }
690*344aa361SAndroid Build Coastguard Worker     }
691*344aa361SAndroid Build Coastguard Worker     /*
692*344aa361SAndroid Build Coastguard Worker      * Search for a test coding issue where a test generator exists
693*344aa361SAndroid Build Coastguard Worker      * but is not backed by existing test case
694*344aa361SAndroid Build Coastguard Worker      */
695*344aa361SAndroid Build Coastguard Worker     list_for_every_entry(&_test_param_list, param_entry,
696*344aa361SAndroid Build Coastguard Worker                          struct test_param_list_node, node) {
697*344aa361SAndroid Build Coastguard Worker         if (!trusty_unittest_has_parameterized_test_case(param_entry->suite)) {
698*344aa361SAndroid Build Coastguard Worker             test_code_error = true;
699*344aa361SAndroid Build Coastguard Worker             trusty_unittest_print_status_name_param_duration(
700*344aa361SAndroid Build Coastguard Worker                     " FAILED ", param_entry->inst_name, param_entry->suite,
701*344aa361SAndroid Build Coastguard Worker                     "NO_TESTS", "Parameter Generator without tests!", "");
702*344aa361SAndroid Build Coastguard Worker         }
703*344aa361SAndroid Build Coastguard Worker     }
704*344aa361SAndroid Build Coastguard Worker     return test_code_error ? -1 : test_case_count;
705*344aa361SAndroid Build Coastguard Worker }
706*344aa361SAndroid Build Coastguard Worker 
707*344aa361SAndroid Build Coastguard Worker /**
708*344aa361SAndroid Build Coastguard Worker  * trusty_unittest_run_test_suite - run each test case associated with test
709*344aa361SAndroid Build Coastguard Worker  * suite.
710*344aa361SAndroid Build Coastguard Worker  *
711*344aa361SAndroid Build Coastguard Worker  * @suite:      Name of test suite
712*344aa361SAndroid Build Coastguard Worker  *              when suite is NULL, all test cases
713*344aa361SAndroid Build Coastguard Worker  *              from all test suites are executed.
714*344aa361SAndroid Build Coastguard Worker  * @needs_param: when true run the parameterised test cases,
715*344aa361SAndroid Build Coastguard Worker  *               otherwise run the non-parameterised test cases
716*344aa361SAndroid Build Coastguard Worker  *
717*344aa361SAndroid Build Coastguard Worker  * Not meant for external use.
718*344aa361SAndroid Build Coastguard Worker  *
719*344aa361SAndroid Build Coastguard Worker  * Return: count of executed test cases
720*344aa361SAndroid Build Coastguard Worker  */
trusty_unittest_run_test_suite(const char * suite,bool needs_param)721*344aa361SAndroid Build Coastguard Worker static int trusty_unittest_run_test_suite(const char* suite, bool needs_param) {
722*344aa361SAndroid Build Coastguard Worker     struct test_list_node* entry;
723*344aa361SAndroid Build Coastguard Worker     int test_case_count = 0;
724*344aa361SAndroid Build Coastguard Worker 
725*344aa361SAndroid Build Coastguard Worker     list_for_every_entry(&_test_list, entry, struct test_list_node, node) {
726*344aa361SAndroid Build Coastguard Worker         if ((!suite || !strcmp(suite, entry->suite)) &&
727*344aa361SAndroid Build Coastguard Worker             (entry->needs_param == needs_param)) {
728*344aa361SAndroid Build Coastguard Worker             if (trusty_unittest_test_is_disabled(entry)) {
729*344aa361SAndroid Build Coastguard Worker                 trusty_unittest_print_status_name(entry->suite, entry->name,
730*344aa361SAndroid Build Coastguard Worker                                                   "DISABLED");
731*344aa361SAndroid Build Coastguard Worker                 _test_context.tests_disabled++;
732*344aa361SAndroid Build Coastguard Worker             } else {
733*344aa361SAndroid Build Coastguard Worker                 test_case_count++;
734*344aa361SAndroid Build Coastguard Worker                 entry->func();
735*344aa361SAndroid Build Coastguard Worker             }
736*344aa361SAndroid Build Coastguard Worker         }
737*344aa361SAndroid Build Coastguard Worker     }
738*344aa361SAndroid Build Coastguard Worker     return test_case_count;
739*344aa361SAndroid Build Coastguard Worker }
740*344aa361SAndroid Build Coastguard Worker 
741*344aa361SAndroid Build Coastguard Worker /*
742*344aa361SAndroid Build Coastguard Worker  * The testing framework uses 3 global variables to keep track of tests and
743*344aa361SAndroid Build Coastguard Worker  * related data:
744*344aa361SAndroid Build Coastguard Worker  *
745*344aa361SAndroid Build Coastguard Worker  * _test_context: contains information about the overall execution of the
746*344aa361SAndroid Build Coastguard Worker  * framework (e.g. total tests run) and information about the currently
747*344aa361SAndroid Build Coastguard Worker  * executing test (e.g. test name, suite name).
748*344aa361SAndroid Build Coastguard Worker  *
749*344aa361SAndroid Build Coastguard Worker  * _test_list: contains a list of tests that can be run. Each test belongs to a
750*344aa361SAndroid Build Coastguard Worker  * test suite and may require parameters to be run.
751*344aa361SAndroid Build Coastguard Worker  *
752*344aa361SAndroid Build Coastguard Worker  * _test_param_list: contains a list of parameter generators for tests that
753*344aa361SAndroid Build Coastguard Worker  * require parameters. Each generator is associated with a specific test suite.
754*344aa361SAndroid Build Coastguard Worker  * Parameter generators are functions that return parameters that apply to all
755*344aa361SAndroid Build Coastguard Worker  * the tests that require parameters (i.e. parameterized tests) in a given test
756*344aa361SAndroid Build Coastguard Worker  * suite.
757*344aa361SAndroid Build Coastguard Worker  *
758*344aa361SAndroid Build Coastguard Worker  * Tests are only run as part of test suites. When a test suite is run all of
759*344aa361SAndroid Build Coastguard Worker  * the non-paremeterized tests belonging to that suite are run first followed by
760*344aa361SAndroid Build Coastguard Worker  * the parameterized tests in the suite. All of the parameterized tests in a
761*344aa361SAndroid Build Coastguard Worker  * suite are run once for each value returned by a parameter generator
762*344aa361SAndroid Build Coastguard Worker  * associated with that suite.
763*344aa361SAndroid Build Coastguard Worker  */
RUN_ALL_SUITE_TESTS(const char * suite)764*344aa361SAndroid Build Coastguard Worker static inline bool RUN_ALL_SUITE_TESTS(const char* suite) {
765*344aa361SAndroid Build Coastguard Worker     struct test_param_list_node* param_entry;
766*344aa361SAndroid Build Coastguard Worker     const void* test_param;
767*344aa361SAndroid Build Coastguard Worker     int i;
768*344aa361SAndroid Build Coastguard Worker     char param_str[64];
769*344aa361SAndroid Build Coastguard Worker     int actual_test_count = 0;
770*344aa361SAndroid Build Coastguard Worker     int expected_test_count = trusty_unittest_count_test_cases(suite);
771*344aa361SAndroid Build Coastguard Worker     if (expected_test_count == -1) {
772*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("Test Coding Error - aborting execution.\n");
773*344aa361SAndroid Build Coastguard Worker         return false;
774*344aa361SAndroid Build Coastguard Worker     }
775*344aa361SAndroid Build Coastguard Worker 
776*344aa361SAndroid Build Coastguard Worker     trusty_unittest_printf(
777*344aa361SAndroid Build Coastguard Worker             "[==========] Running %d tests from %s test suite%s.\n",
778*344aa361SAndroid Build Coastguard Worker             expected_test_count, suite ? suite : "all", suite ? "" : "s");
779*344aa361SAndroid Build Coastguard Worker 
780*344aa361SAndroid Build Coastguard Worker     _test_context.tests_total = 0;
781*344aa361SAndroid Build Coastguard Worker     _test_context.tests_disabled = 0;
782*344aa361SAndroid Build Coastguard Worker     _test_context.tests_failed = 0;
783*344aa361SAndroid Build Coastguard Worker     _test_context.test_param = NULL;
784*344aa361SAndroid Build Coastguard Worker     _test_context.inst_name = NULL;
785*344aa361SAndroid Build Coastguard Worker     _test_context.param_name = param_str;
786*344aa361SAndroid Build Coastguard Worker     _test_context.suite_duration_ms = 0;
787*344aa361SAndroid Build Coastguard Worker     /* Run all the non-parameterized tests in the suite */
788*344aa361SAndroid Build Coastguard Worker     actual_test_count = trusty_unittest_run_test_suite(suite, false);
789*344aa361SAndroid Build Coastguard Worker 
790*344aa361SAndroid Build Coastguard Worker     /* For each parameter generator associated with the suite */
791*344aa361SAndroid Build Coastguard Worker     list_for_every_entry(&_test_param_list, param_entry,
792*344aa361SAndroid Build Coastguard Worker                          struct test_param_list_node, node) {
793*344aa361SAndroid Build Coastguard Worker         if (!suite || !strcmp(suite, param_entry->suite)) {
794*344aa361SAndroid Build Coastguard Worker             i = 0;
795*344aa361SAndroid Build Coastguard Worker             /* For each parameter from the generator */
796*344aa361SAndroid Build Coastguard Worker             while ((test_param = param_entry->param_gen.gen_param(
797*344aa361SAndroid Build Coastguard Worker                             param_entry->param_gen.priv, i))) {
798*344aa361SAndroid Build Coastguard Worker                 /* Set the parameter for the next run */
799*344aa361SAndroid Build Coastguard Worker                 _test_context.inst_name = param_entry->inst_name;
800*344aa361SAndroid Build Coastguard Worker                 _test_context.test_param = test_param;
801*344aa361SAndroid Build Coastguard Worker                 if (param_entry->to_string) {
802*344aa361SAndroid Build Coastguard Worker                     param_entry->to_string(test_param, param_str,
803*344aa361SAndroid Build Coastguard Worker                                            sizeof(param_str));
804*344aa361SAndroid Build Coastguard Worker                 } else {
805*344aa361SAndroid Build Coastguard Worker                     snprintf(param_str, sizeof(param_str), "%d", i);
806*344aa361SAndroid Build Coastguard Worker                 }
807*344aa361SAndroid Build Coastguard Worker                 /* Run all the parameterized tests in the suite */
808*344aa361SAndroid Build Coastguard Worker                 actual_test_count += trusty_unittest_run_test_suite(
809*344aa361SAndroid Build Coastguard Worker                         param_entry->suite, true);
810*344aa361SAndroid Build Coastguard Worker                 i++;
811*344aa361SAndroid Build Coastguard Worker             }
812*344aa361SAndroid Build Coastguard Worker         }
813*344aa361SAndroid Build Coastguard Worker     }
814*344aa361SAndroid Build Coastguard Worker     if (actual_test_count != expected_test_count) {
815*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[ RUN      ] %s.test_count_match_check\n",
816*344aa361SAndroid Build Coastguard Worker                                suite ? suite : "all_suites");
817*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf(
818*344aa361SAndroid Build Coastguard Worker                 "[----------] %d tests ran, but expected %d tests.\n",
819*344aa361SAndroid Build Coastguard Worker                 actual_test_count, expected_test_count);
820*344aa361SAndroid Build Coastguard Worker         trusty_unittest_print_status_name(suite ? suite : "all_suites",
821*344aa361SAndroid Build Coastguard Worker                                           "test_count_match_check", " FAILED ");
822*344aa361SAndroid Build Coastguard Worker         ++_test_context.tests_failed;
823*344aa361SAndroid Build Coastguard Worker         ++_test_context.tests_total;
824*344aa361SAndroid Build Coastguard Worker     } else if (actual_test_count == 0 && _test_context.tests_disabled == 0) {
825*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[ RUN      ] %s.test_count_empty_check\n",
826*344aa361SAndroid Build Coastguard Worker                                suite ? suite : "all_suites");
827*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[----------] 0 tests but none disabled.\n");
828*344aa361SAndroid Build Coastguard Worker         trusty_unittest_print_status_name(suite ? suite : "all_suites",
829*344aa361SAndroid Build Coastguard Worker                                           "test_count_empty_check", " FAILED ");
830*344aa361SAndroid Build Coastguard Worker         ++_test_context.tests_failed;
831*344aa361SAndroid Build Coastguard Worker         ++_test_context.tests_total;
832*344aa361SAndroid Build Coastguard Worker     }
833*344aa361SAndroid Build Coastguard Worker 
834*344aa361SAndroid Build Coastguard Worker     trusty_unittest_printf(
835*344aa361SAndroid Build Coastguard Worker             "[==========] %d tests ran (%" PRIu64 " ms total).\n",
836*344aa361SAndroid Build Coastguard Worker             _test_context.tests_total, _test_context.suite_duration_ms);
837*344aa361SAndroid Build Coastguard Worker 
838*344aa361SAndroid Build Coastguard Worker     if (_test_context.tests_total != _test_context.tests_failed) {
839*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf(
840*344aa361SAndroid Build Coastguard Worker                 "[  PASSED  ] %d tests.\n",
841*344aa361SAndroid Build Coastguard Worker                 _test_context.tests_total - _test_context.tests_failed);
842*344aa361SAndroid Build Coastguard Worker     }
843*344aa361SAndroid Build Coastguard Worker     if (_test_context.tests_skipped) {
844*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[  SKIPPED ] %d tests.\n",
845*344aa361SAndroid Build Coastguard Worker                                _test_context.tests_skipped);
846*344aa361SAndroid Build Coastguard Worker     }
847*344aa361SAndroid Build Coastguard Worker     if (_test_context.tests_disabled) {
848*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[ DISABLED ] %d tests.\n",
849*344aa361SAndroid Build Coastguard Worker                                _test_context.tests_disabled);
850*344aa361SAndroid Build Coastguard Worker     }
851*344aa361SAndroid Build Coastguard Worker     if (_test_context.tests_failed) {
852*344aa361SAndroid Build Coastguard Worker         trusty_unittest_printf("[  FAILED  ] %d tests.\n",
853*344aa361SAndroid Build Coastguard Worker                                _test_context.tests_failed);
854*344aa361SAndroid Build Coastguard Worker     }
855*344aa361SAndroid Build Coastguard Worker     return _test_context.tests_failed == 0;
856*344aa361SAndroid Build Coastguard Worker }
857*344aa361SAndroid Build Coastguard Worker 
RUN_ALL_TESTS(void)858*344aa361SAndroid Build Coastguard Worker static inline bool RUN_ALL_TESTS(void) {
859*344aa361SAndroid Build Coastguard Worker     return RUN_ALL_SUITE_TESTS(NULL);
860*344aa361SAndroid Build Coastguard Worker }
861*344aa361SAndroid Build Coastguard Worker 
862*344aa361SAndroid Build Coastguard Worker /**
863*344aa361SAndroid Build Coastguard Worker  * GTEST_SKIP() - Skip current test
864*344aa361SAndroid Build Coastguard Worker  *
865*344aa361SAndroid Build Coastguard Worker  * This will skip the current test without triggering a failure. It will use
866*344aa361SAndroid Build Coastguard Worker  * same test_abort label as the ASSERT_... macros. Calling this after a test has
867*344aa361SAndroid Build Coastguard Worker  * failed or calling ASSERT_.../EXPECT_... macros after GTEST_SKIP has jumped
868*344aa361SAndroid Build Coastguard Worker  * to test_abort is not supported.
869*344aa361SAndroid Build Coastguard Worker  */
870*344aa361SAndroid Build Coastguard Worker #define GTEST_SKIP()                       \
871*344aa361SAndroid Build Coastguard Worker     {                                      \
872*344aa361SAndroid Build Coastguard Worker         if (!_test_context.skipped) {      \
873*344aa361SAndroid Build Coastguard Worker             _test_context.skipped = true;  \
874*344aa361SAndroid Build Coastguard Worker             _test_context.tests_skipped++; \
875*344aa361SAndroid Build Coastguard Worker         }                                  \
876*344aa361SAndroid Build Coastguard Worker         goto test_abort;                   \
877*344aa361SAndroid Build Coastguard Worker     }
878*344aa361SAndroid Build Coastguard Worker 
879*344aa361SAndroid Build Coastguard Worker #define ASSERT_EXPECT_TEST(op, op_pre, op_sep, op_args, is_hard_fail,        \
880*344aa361SAndroid Build Coastguard Worker                            fail_action, vals_type, vals_format_placeholder,  \
881*344aa361SAndroid Build Coastguard Worker                            print_cast, print_op, val1, val2, extra_msg...)   \
882*344aa361SAndroid Build Coastguard Worker     {                                                                        \
883*344aa361SAndroid Build Coastguard Worker         vals_type _val1 = val1;                                              \
884*344aa361SAndroid Build Coastguard Worker         vals_type _val2 = val2;                                              \
885*344aa361SAndroid Build Coastguard Worker         if (!op_pre(_val1 DELETE_PAREN op_sep _val2 DELETE_PAREN op_args)) { \
886*344aa361SAndroid Build Coastguard Worker             trusty_unittest_printf("%s: @ %s:%d\n", _test_context.test_name, \
887*344aa361SAndroid Build Coastguard Worker                                    __FILE__, __LINE__);                      \
888*344aa361SAndroid Build Coastguard Worker             trusty_unittest_printf(                                          \
889*344aa361SAndroid Build Coastguard Worker                     "  expected: %s (" vals_format_placeholder ") " print_op \
890*344aa361SAndroid Build Coastguard Worker                     " %s (" vals_format_placeholder ")\n",                   \
891*344aa361SAndroid Build Coastguard Worker                     #val1, print_cast _val1, #val2, print_cast _val2);       \
892*344aa361SAndroid Build Coastguard Worker             trusty_unittest_printf("  " extra_msg);                          \
893*344aa361SAndroid Build Coastguard Worker             trusty_unittest_printf("\n");                                    \
894*344aa361SAndroid Build Coastguard Worker             if (_test_context.all_ok) {                                      \
895*344aa361SAndroid Build Coastguard Worker                 _test_context.all_ok = false;                                \
896*344aa361SAndroid Build Coastguard Worker                 _test_context.tests_failed++;                                \
897*344aa361SAndroid Build Coastguard Worker             }                                                                \
898*344aa361SAndroid Build Coastguard Worker             _test_context.hard_fail |= is_hard_fail;                         \
899*344aa361SAndroid Build Coastguard Worker             fail_action                                                      \
900*344aa361SAndroid Build Coastguard Worker         }                                                                    \
901*344aa361SAndroid Build Coastguard Worker     }
902*344aa361SAndroid Build Coastguard Worker 
HasFailure(void)903*344aa361SAndroid Build Coastguard Worker static inline bool HasFailure(void) {
904*344aa361SAndroid Build Coastguard Worker     return !_test_context.all_ok;
905*344aa361SAndroid Build Coastguard Worker }
906*344aa361SAndroid Build Coastguard Worker 
907*344aa361SAndroid Build Coastguard Worker /**
908*344aa361SAndroid Build Coastguard Worker  * INSTANTIATE_TEST_SUITE_P - Instantiate parameters for a test suite
909*344aa361SAndroid Build Coastguard Worker  * @inst_name:          Name for instantiation of parameters. Should not contain
910*344aa361SAndroid Build Coastguard Worker  *                      underscores.
911*344aa361SAndroid Build Coastguard Worker  * @suite_name:         Name of test suite associated with the parameters
912*344aa361SAndroid Build Coastguard Worker  * @param_gen:          One of the parameter generators (see below)
913*344aa361SAndroid Build Coastguard Worker  * @param_to_string:    Function of type &typedef test_param_to_string_t
914*344aa361SAndroid Build Coastguard Worker  *                      used to convert a parameter to its string form. This
915*344aa361SAndroid Build Coastguard Worker  *                      argument is optional.
916*344aa361SAndroid Build Coastguard Worker  *
917*344aa361SAndroid Build Coastguard Worker  * Parameter Generators:
918*344aa361SAndroid Build Coastguard Worker  *  testing_Range(being, end, step):
919*344aa361SAndroid Build Coastguard Worker  *  Returns the values {begin, being+step, being+step+step, ...} up to but not
920*344aa361SAndroid Build Coastguard Worker  *  including end. step is optional and defaults to 1.
921*344aa361SAndroid Build Coastguard Worker  *
922*344aa361SAndroid Build Coastguard Worker  *  testing_Values(v1, v2, ..., vN):
923*344aa361SAndroid Build Coastguard Worker  *  Returns the values {v1, v2, ..., vN)
924*344aa361SAndroid Build Coastguard Worker  *
925*344aa361SAndroid Build Coastguard Worker  *  testing_ValuesIn(array)
926*344aa361SAndroid Build Coastguard Worker  *  Returns the values in array
927*344aa361SAndroid Build Coastguard Worker  *
928*344aa361SAndroid Build Coastguard Worker  *  testing_Bool()
929*344aa361SAndroid Build Coastguard Worker  *  Returns {false, true}
930*344aa361SAndroid Build Coastguard Worker  *
931*344aa361SAndroid Build Coastguard Worker  *  testing_Combine(g1, [g2, g3, g4, g5]):
932*344aa361SAndroid Build Coastguard Worker  *  Returns the values of the combinations of the provided generators
933*344aa361SAndroid Build Coastguard Worker  *  (min 1, max 5) an as an array.
934*344aa361SAndroid Build Coastguard Worker  */
935*344aa361SAndroid Build Coastguard Worker #define INSTANTIATE_TEST_SUITE_P(inst_name, suite_name, param_gen_args...)   \
936*344aa361SAndroid Build Coastguard Worker     INSTANTIATE_TEST_SUITE_P_INTERNAL(inst_name, suite_name, param_gen_args, \
937*344aa361SAndroid Build Coastguard Worker                                       NULL, )
938*344aa361SAndroid Build Coastguard Worker 
939*344aa361SAndroid Build Coastguard Worker /**
940*344aa361SAndroid Build Coastguard Worker  * GetParam() - Returns a pointer to the current test parameter
941*344aa361SAndroid Build Coastguard Worker  *
942*344aa361SAndroid Build Coastguard Worker  * Context: This function can be called within a parameterized test to
943*344aa361SAndroid Build Coastguard Worker  *          retrieve the current parameter to the test.
944*344aa361SAndroid Build Coastguard Worker  *
945*344aa361SAndroid Build Coastguard Worker  * Return: a pointer to the current test parameter.
946*344aa361SAndroid Build Coastguard Worker  *
947*344aa361SAndroid Build Coastguard Worker  * This pointer should be cast to the expected parameter type for the executing
948*344aa361SAndroid Build Coastguard Worker  * test.
949*344aa361SAndroid Build Coastguard Worker  */
GetParam(void)950*344aa361SAndroid Build Coastguard Worker static inline const void* GetParam(void) {
951*344aa361SAndroid Build Coastguard Worker     return _test_context.test_param;
952*344aa361SAndroid Build Coastguard Worker }
953*344aa361SAndroid Build Coastguard Worker 
954*344aa361SAndroid Build Coastguard Worker #define ASSERT_EXPECT_LONG_TEST(op, is_hard_fail, fail_action, val1, val2, \
955*344aa361SAndroid Build Coastguard Worker                                 args...)                                   \
956*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_TEST(op, , (op), (), is_hard_fail, fail_action,          \
957*344aa361SAndroid Build Coastguard Worker                        __typeof__(val2), "%ld", (long), #op, val1, val2, args)
958*344aa361SAndroid Build Coastguard Worker 
959*344aa361SAndroid Build Coastguard Worker #define ASSERT_EXPECT_STR_TEST(func, is_hard_fail, fail_action, args...) \
960*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_TEST(func, func, (, ), (), is_hard_fail, fail_action,  \
961*344aa361SAndroid Build Coastguard Worker                        const char*, "\"%s\"", , args)
962*344aa361SAndroid Build Coastguard Worker 
963*344aa361SAndroid Build Coastguard Worker #define ASSERT_EXPECT_STRN_TEST(func, n, is_hard_fail, fail_action, args...) \
964*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_TEST(func, func, (, ), (, n), is_hard_fail, fail_action,   \
965*344aa361SAndroid Build Coastguard Worker                        const char*, "\"%s\"", , args)
966*344aa361SAndroid Build Coastguard Worker 
967*344aa361SAndroid Build Coastguard Worker #define EXPECT_TEST(op, args...) ASSERT_EXPECT_LONG_TEST(op, false, , args)
968*344aa361SAndroid Build Coastguard Worker #define EXPECT_EQ(args...) EXPECT_TEST(==, args)
969*344aa361SAndroid Build Coastguard Worker #define EXPECT_NE(args...) EXPECT_TEST(!=, args)
970*344aa361SAndroid Build Coastguard Worker #define EXPECT_LT(args...) EXPECT_TEST(<, args)
971*344aa361SAndroid Build Coastguard Worker #define EXPECT_LE(args...) EXPECT_TEST(<=, args)
972*344aa361SAndroid Build Coastguard Worker #define EXPECT_GT(args...) EXPECT_TEST(>, args)
973*344aa361SAndroid Build Coastguard Worker #define EXPECT_GE(args...) EXPECT_TEST(>=, args)
974*344aa361SAndroid Build Coastguard Worker #define EXPECT_STR_TEST(func, args...) \
975*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_STR_TEST(func, false, , args)
976*344aa361SAndroid Build Coastguard Worker #define EXPECT_STREQ(args...) EXPECT_STR_TEST(!strcmp, "==", args)
977*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRNE(args...) EXPECT_STR_TEST(strcmp, "!=", args)
978*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRCASEEQ(args...) \
979*344aa361SAndroid Build Coastguard Worker     EXPECT_STR_TEST(!strcasecmp, "== (ignoring case)", args)
980*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRCASENE(args...) \
981*344aa361SAndroid Build Coastguard Worker     EXPECT_STR_TEST(strcasecmp, "!= (ignoring case)", args)
982*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRN_TEST(func, n, args...) \
983*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_STRN_TEST(func, n, false, , args)
984*344aa361SAndroid Build Coastguard Worker #define EXPECT_STREQN(val1, val2, n, args...) \
985*344aa361SAndroid Build Coastguard Worker     EXPECT_STR_TEST(!strncmp, n, "==", val1, val2, args)
986*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRNEN(val1, val2, n, args...) \
987*344aa361SAndroid Build Coastguard Worker     EXPECT_STR_TEST(strncmp, n, "!=", val1, val2, args)
988*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRCASEEQN(val1, val2, n, args...) \
989*344aa361SAndroid Build Coastguard Worker     EXPECT_STR_TEST(!strncasecmp, n, "== (ignoring case)", val1, val2, args)
990*344aa361SAndroid Build Coastguard Worker #define EXPECT_STRCASENEN(val1, val2, n, args...) \
991*344aa361SAndroid Build Coastguard Worker     EXPECT_STR_TEST(strncasecmp, n, "!= (ignoring case)", val1, val2, args)
992*344aa361SAndroid Build Coastguard Worker 
993*344aa361SAndroid Build Coastguard Worker #define ASSERT_TEST(op, args...) \
994*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_LONG_TEST(op, true, goto test_abort;, args)
995*344aa361SAndroid Build Coastguard Worker #define ASSERT_EQ(args...) ASSERT_TEST(==, args)
996*344aa361SAndroid Build Coastguard Worker #define ASSERT_NE(args...) ASSERT_TEST(!=, args)
997*344aa361SAndroid Build Coastguard Worker #define ASSERT_LT(args...) ASSERT_TEST(<, args)
998*344aa361SAndroid Build Coastguard Worker #define ASSERT_LE(args...) ASSERT_TEST(<=, args)
999*344aa361SAndroid Build Coastguard Worker #define ASSERT_GT(args...) ASSERT_TEST(>, args)
1000*344aa361SAndroid Build Coastguard Worker #define ASSERT_GE(args...) ASSERT_TEST(>=, args)
1001*344aa361SAndroid Build Coastguard Worker #define ASSERT_STR_TEST(func, args...) \
1002*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_STR_TEST(func, true, goto test_abort;, args)
1003*344aa361SAndroid Build Coastguard Worker #define ASSERT_STREQ(args...) ASSERT_STR_TEST(!strcmp, "==", args)
1004*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRNE(args...) ASSERT_STR_TEST(strcmp, "!=", args)
1005*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRCASEEQ(args...) \
1006*344aa361SAndroid Build Coastguard Worker     ASSERT_STR_TEST(!strcasecmp, "== (ignoring case)", args)
1007*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRCASENE(args...) \
1008*344aa361SAndroid Build Coastguard Worker     ASSERT_STR_TEST(strcasecmp, "!= (ignoring case)", args)
1009*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRN_TEST(func, n, args...) \
1010*344aa361SAndroid Build Coastguard Worker     ASSERT_EXPECT_STRN_TEST(func, n, true, goto test_abort;, args)
1011*344aa361SAndroid Build Coastguard Worker #define ASSERT_STREQN(val1, val2, n, args...) \
1012*344aa361SAndroid Build Coastguard Worker     ASSERT_STRN_TEST(!strncmp, n, "==", val1, val2, args)
1013*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRNEN(val1, val2, n, args...) \
1014*344aa361SAndroid Build Coastguard Worker     ASSERT_STRN_TEST(strncmp, n, "!=", val1, val2, args)
1015*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRCASEEQN(val1, val2, n, args...) \
1016*344aa361SAndroid Build Coastguard Worker     ASSERT_STRN_TEST(!strncasecmp, n, "== (ignoring case)", val1, val2, args)
1017*344aa361SAndroid Build Coastguard Worker #define ASSERT_STRCASENEN(val1, val2, n, args...) \
1018*344aa361SAndroid Build Coastguard Worker     ASSERT_STRN_TEST(strncasecmp, n, "!= (ignoring case)", val1, val2, args)
1019*344aa361SAndroid Build Coastguard Worker 
1020*344aa361SAndroid Build Coastguard Worker __END_CDECLS
1021