1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8
9 ZTEST_SUITE(framework_tests, NULL, NULL, NULL, NULL, NULL);
10
ZTEST(framework_tests,test_empty_test)11 ZTEST(framework_tests, test_empty_test)
12 {
13 }
14
ZTEST(framework_tests,test_assert_tests)15 ZTEST(framework_tests, test_assert_tests)
16 {
17 zassert_true(1);
18 zassert_false(0);
19 zassert_is_null(NULL, NULL);
20 zassert_not_null("foo", NULL);
21 zassert_equal(1, 1);
22 zassert_equal_ptr(NULL, NULL, NULL);
23 zassert_not_ok(-EIO);
24 }
25
ZTEST(framework_tests,test_assert_mem_equal)26 ZTEST(framework_tests, test_assert_mem_equal)
27 {
28 static const uint32_t expected[4] = {
29 0x1234,
30 0x5678,
31 0x9ABC,
32 0xDEF0
33 };
34 uint32_t actual[4] = {0};
35 memcpy(actual, expected, sizeof(actual));
36 zassert_mem_equal(actual, expected, sizeof(expected), NULL);
37 }
38
ZTEST(framework_tests,test_assert_str_equal)39 ZTEST(framework_tests, test_assert_str_equal)
40 {
41 const char *s1 = "asdf";
42 const char s2[] = {'a', 's', 'd', 'f', '\0'};
43
44 zassert_str_equal(s1, s2);
45 }
46
47 ZTEST_EXPECT_FAIL(framework_tests, test_assert_str_equal_fail);
ZTEST(framework_tests,test_assert_str_equal_fail)48 ZTEST(framework_tests, test_assert_str_equal_fail)
49 {
50 const char *s1 = "asdf";
51 const char s2[] = {'a', 's', 'd', 'f', 'q', '\0'};
52
53 zassert_str_equal(s1, s2);
54 }
55
56 ZTEST_EXPECT_SKIP(framework_tests, test_skip_config);
ZTEST(framework_tests,test_skip_config)57 ZTEST(framework_tests, test_skip_config)
58 {
59 Z_TEST_SKIP_IFDEF(CONFIG_BUGxxxxx);
60 ztest_test_fail();
61 }
62
63 ZTEST_EXPECT_SKIP(framework_tests, test_skip_no_config);
ZTEST(framework_tests,test_skip_no_config)64 ZTEST(framework_tests, test_skip_no_config)
65 {
66 Z_TEST_SKIP_IFNDEF(CONFIG_BUGyyyyy);
67 ztest_test_fail();
68 }
69
70 /***************************************************************************************************
71 * Sample fixture tests
72 **************************************************************************************************/
73
74 struct fixture_tests_fixture {
75 };
76
77 static struct fixture_tests_fixture test_fixture;
78
fixture_tests_setup(void)79 static void *fixture_tests_setup(void)
80 {
81 return &test_fixture;
82 }
83
84 ZTEST_SUITE(fixture_tests, NULL, fixture_tests_setup, NULL, NULL, NULL);
85
ZTEST_F(fixture_tests,test_fixture_pointer)86 ZTEST_F(fixture_tests, test_fixture_pointer)
87 {
88 zassert_equal_ptr(&test_fixture, fixture, "Test fixture should be at 0x%x but was at 0x%x",
89 &test_fixture, fixture);
90 }
91
92 /***************************************************************************************************
93 * Sample rule tests
94 **************************************************************************************************/
95
96 enum rule_state {
97 RULE_STATE_SETUP = 0,
98 RULE_STATE_BEFORE_EACH,
99 RULE_STATE_TEST,
100 RULE_STATE_AFTER_EACH,
101 };
102
103 struct rules_tests_fixture {
104 enum rule_state state;
105 int run_count;
106 };
107
108 static struct rules_tests_fixture rule_tests_fixture;
109
rule_before_each(const struct ztest_unit_test * test,void * data)110 static void rule_before_each(const struct ztest_unit_test *test, void *data)
111 {
112 if (strcmp(test->test_suite_name, "rules_tests") == 0 &&
113 strcmp(test->name, "test_rules_before_after") == 0) {
114 struct rules_tests_fixture *fixture = data;
115
116 zassert_equal_ptr(&rule_tests_fixture, data,
117 "Data expected to point to rule_state");
118 if (fixture->run_count == 0) {
119 zassert_equal(fixture->state, RULE_STATE_SETUP, "Unexpected state");
120 } else {
121 zassert_equal(fixture->state, RULE_STATE_AFTER_EACH, "Unexpected state");
122 }
123 fixture->state = RULE_STATE_BEFORE_EACH;
124 }
125 }
126
rule_after_each(const struct ztest_unit_test * test,void * data)127 static void rule_after_each(const struct ztest_unit_test *test, void *data)
128 {
129 if (strcmp(test->test_suite_name, "rules_tests") == 0 &&
130 strcmp(test->name, "test_rules_before_after") == 0) {
131 struct rules_tests_fixture *fixture = data;
132
133 zassert_equal_ptr(&rule_tests_fixture, data,
134 "Data expected to point to rule_state");
135 zassert_equal(fixture->state, RULE_STATE_TEST, "Unexpected state");
136 fixture->state = RULE_STATE_AFTER_EACH;
137 }
138 }
139
rule_test_setup(void)140 static void *rule_test_setup(void)
141 {
142 rule_tests_fixture.state = RULE_STATE_SETUP;
143 rule_tests_fixture.run_count = 0;
144 return &rule_tests_fixture;
145 }
146
rule_test_teardown(void * data)147 static void rule_test_teardown(void *data)
148 {
149 struct rules_tests_fixture *fixture = data;
150
151 /*
152 * Normally, we wouldn't assert here, but it's the only way to test that the rule's
153 * after_each function was called.
154 */
155 zassert_equal(fixture->state, RULE_STATE_AFTER_EACH, "Unexpected state");
156 #ifdef CONFIG_ZTEST_REPEAT
157 zassert_equal(fixture->run_count, CONFIG_ZTEST_TEST_REPEAT_COUNT);
158 #endif
159 }
160
161 ZTEST_RULE(verify_before_after_rule, rule_before_each, rule_after_each);
162
163 ZTEST_SUITE(rules_tests, NULL, rule_test_setup, NULL, NULL, rule_test_teardown);
164
ZTEST_F(rules_tests,test_rules_before_after)165 ZTEST_F(rules_tests, test_rules_before_after)
166 {
167 zassert_equal(fixture->state, RULE_STATE_BEFORE_EACH,
168 "Unexpected state");
169 fixture->state = RULE_STATE_TEST;
170 fixture->run_count++;
171 }
172
fail_in_setup_setup(void)173 static void *fail_in_setup_setup(void)
174 {
175 zassert_true(false);
176 return NULL;
177 }
178
179 ZTEST_EXPECT_FAIL(fail_in_setup, test_should_never_run);
ZTEST(fail_in_setup,test_should_never_run)180 ZTEST(fail_in_setup, test_should_never_run)
181 {
182 /* The following should pass, but the setup function will cause it to fail */
183 zassert_true(true);
184 }
185
186 ZTEST_SUITE(fail_in_setup, NULL, fail_in_setup_setup, NULL, NULL, NULL);
187