6997568cc0762a4fa6f39c9b84e36b21c8acdd4b
[platform/kernel/u-boot.git] / include / test / test.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2013 Google, Inc.
4  */
5
6 #ifndef __TEST_TEST_H
7 #define __TEST_TEST_H
8
9 #include <malloc.h>
10 #include <linux/bitops.h>
11
12 /*
13  * struct unit_test_state - Entire state of test system
14  *
15  * @fail_count: Number of tests that failed
16  * @start: Store the starting mallinfo when doing leak test
17  * @priv: A pointer to some other info some suites want to track
18  * @of_live: true to use livetree if available, false to use flattree
19  * @of_root: Record of the livetree root node (used for setting up tests)
20  * @expect_str: Temporary string used to hold expected string value
21  * @actual_str: Temporary string used to hold actual string value
22  */
23 struct unit_test_state {
24         int fail_count;
25         struct mallinfo start;
26         void *priv;
27         struct device_node *of_root;
28         bool of_live;
29         char expect_str[256];
30         char actual_str[256];
31 };
32
33 /* Test flags for each test */
34 enum {
35         UT_TESTF_SCAN_PDATA     = BIT(0),       /* test needs platform data */
36         UT_TESTF_PROBE_TEST     = BIT(1),       /* probe test uclass */
37         UT_TESTF_SCAN_FDT       = BIT(2),       /* scan device tree */
38         UT_TESTF_FLAT_TREE      = BIT(3),       /* test needs flat DT */
39         UT_TESTF_LIVE_TREE      = BIT(4),       /* needs live device tree */
40         UT_TESTF_CONSOLE_REC    = BIT(5),       /* needs console recording */
41         /* do extra driver model init and uninit */
42         UT_TESTF_DM             = BIT(6),
43 };
44
45 /**
46  * struct unit_test - Information about a unit test
47  *
48  * @name: Name of test
49  * @func: Function to call to perform test
50  * @flags: Flags indicated pre-conditions for test
51  */
52 struct unit_test {
53         const char *file;
54         const char *name;
55         int (*func)(struct unit_test_state *state);
56         int flags;
57 };
58
59 /**
60  * UNIT_TEST() - create linker generated list entry for unit a unit test
61  *
62  * The macro UNIT_TEST() is used to create a linker generated list entry. These
63  * list entries are enumerate tests that can be execute using the ut command.
64  * The list entries are used both by the implementation of the ut command as
65  * well as in a related Python test.
66  *
67  * For Python testing the subtests are collected in Python function
68  * generate_ut_subtest() by applying a regular expression to the lines of file
69  * u-boot.sym. The list entries have to follow strict naming conventions to be
70  * matched by the expression.
71  *
72  * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
73  * foo that can be executed via command 'ut foo bar' and is implemented in
74  * function foo_test_bar().
75  *
76  * @_name:      concatenation of name of the test suite, "_test_", and the name
77  *              of the test
78  * @_flags:     an integer field that can be evaluated by the test suite
79  *              implementation
80  * @_suite:     name of the test suite concatenated with "_test"
81  */
82 #define UNIT_TEST(_name, _flags, _suite)                                \
83         ll_entry_declare(struct unit_test, _name, _suite) = {           \
84                 .file = __FILE__,                                       \
85                 .name = #_name,                                         \
86                 .flags = _flags,                                        \
87                 .func = _name,                                          \
88         }
89
90 /* Sizes for devres tests */
91 enum {
92         TEST_DEVRES_SIZE        = 100,
93         TEST_DEVRES_COUNT       = 10,
94         TEST_DEVRES_TOTAL       = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
95
96         /* A few different sizes */
97         TEST_DEVRES_SIZE2       = 15,
98         TEST_DEVRES_SIZE3       = 37,
99 };
100
101 /**
102  * testbus_get_clear_removed() - Test function to obtain removed device
103  *
104  * This is used in testbus to find out which device was removed. Calling this
105  * function returns a pointer to the device and then clears it back to NULL, so
106  * that a future test can check it.
107  */
108 struct udevice *testbus_get_clear_removed(void);
109
110 /**
111  * dm_test_run() - Run driver model tests
112  *
113  * Run all the available driver model tests, or a selection
114  *
115  * @test_name: Name of single test to run (e.g. "dm_test_fdt_pre_reloc" or just
116  *      "fdt_pre_reloc"), or NULL to run all
117  * @return 0 if all tests passed, 1 if not
118  */
119 int dm_test_run(const char *test_name);
120
121 #endif /* __TEST_TEST_H */