1 /*
2  * Copyright (c) 2022 Google Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <inttypes.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 
11 #include <zephyr/ztest.h>
12 
13 ZTEST_SUITE(expect, NULL, NULL, NULL, NULL, NULL);
14 
15 ZTEST_EXPECT_FAIL(expect, test_fail_later);
ZTEST(expect,test_fail_later)16 ZTEST(expect, test_fail_later)
17 {
18 	void *empty_ptr = NULL;
19 	uint32_t val = 5;
20 
21 	zexpect_equal(val, 2);
22 	zexpect_not_equal(val, 5);
23 
24 	zexpect_not_null(empty_ptr);
25 
26 	zassert_true(true);
27 }
28 
ZTEST(expect,test_pass_expect_true)29 ZTEST(expect, test_pass_expect_true)
30 {
31 	zexpect_true(true);
32 }
33 
34 ZTEST_EXPECT_FAIL(expect, test_fail_expect_true);
ZTEST(expect,test_fail_expect_true)35 ZTEST(expect, test_fail_expect_true)
36 {
37 	zexpect_true(false);
38 }
39 
ZTEST(expect,test_expect_false)40 ZTEST(expect, test_expect_false)
41 {
42 	zexpect_false(false);
43 }
44 
45 ZTEST_EXPECT_FAIL(expect, test_fail_expect_false);
ZTEST(expect,test_fail_expect_false)46 ZTEST(expect, test_fail_expect_false)
47 {
48 	zexpect_false(true);
49 }
50 
ZTEST(expect,test_expect_ok)51 ZTEST(expect, test_expect_ok)
52 {
53 	zexpect_ok(0);
54 }
55 
56 ZTEST_EXPECT_FAIL(expect, test_fail_expect_ok);
ZTEST(expect,test_fail_expect_ok)57 ZTEST(expect, test_fail_expect_ok)
58 {
59 	zexpect_ok(5);
60 }
61 
ZTEST(expect,test_expect_not_ok)62 ZTEST(expect, test_expect_not_ok)
63 {
64 	zexpect_not_ok(-EIO);
65 }
66 
67 ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_ok);
ZTEST(expect,test_fail_expect_not_ok)68 ZTEST(expect, test_fail_expect_not_ok)
69 {
70 	zexpect_not_ok(0);
71 }
72 
ZTEST(expect,test_expect_is_null)73 ZTEST(expect, test_expect_is_null)
74 {
75 	void *ptr = NULL;
76 
77 	zexpect_is_null(ptr);
78 }
79 
80 ZTEST_EXPECT_FAIL(expect, test_fail_expect_is_null);
ZTEST(expect,test_fail_expect_is_null)81 ZTEST(expect, test_fail_expect_is_null)
82 {
83 	void *ptr = (void *)0x32137899;
84 
85 	zexpect_is_null(ptr);
86 }
87 
ZTEST(expect,test_expect_not_null)88 ZTEST(expect, test_expect_not_null)
89 {
90 	void *ptr = (void *)0x91517141;
91 
92 	zexpect_not_null(ptr);
93 }
94 
95 ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_null);
ZTEST(expect,test_fail_expect_not_null)96 ZTEST(expect, test_fail_expect_not_null)
97 {
98 	zexpect_not_null(NULL);
99 }
100 
ZTEST(expect,test_expect_equal)101 ZTEST(expect, test_expect_equal)
102 {
103 	zexpect_equal(5, 5);
104 }
105 
106 ZTEST_EXPECT_FAIL(expect, test_fail_expect_equal);
ZTEST(expect,test_fail_expect_equal)107 ZTEST(expect, test_fail_expect_equal)
108 {
109 	zexpect_equal(5, 1);
110 }
111 
ZTEST(expect,test_expect_not_equal)112 ZTEST(expect, test_expect_not_equal)
113 {
114 	zexpect_not_equal(5, 1);
115 }
116 
117 ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_equal);
ZTEST(expect,test_fail_expect_not_equal)118 ZTEST(expect, test_fail_expect_not_equal)
119 {
120 	zexpect_not_equal(5, 5);
121 }
122 
ZTEST(expect,test_expect_equal_ptr)123 ZTEST(expect, test_expect_equal_ptr)
124 {
125 	int v = 9;
126 	int *a = &v;
127 	int *b = &v;
128 
129 	zexpect_equal_ptr(a, b);
130 }
131 
132 ZTEST_EXPECT_FAIL(expect, test_fail_expect_equal_ptr);
ZTEST(expect,test_fail_expect_equal_ptr)133 ZTEST(expect, test_fail_expect_equal_ptr)
134 {
135 	int v = 9;
136 	int *a = &v;
137 	int *b = NULL;
138 
139 	zexpect_equal_ptr(a, b);
140 }
141 
ZTEST(expect,test_expect_within)142 ZTEST(expect, test_expect_within)
143 {
144 	zexpect_within(7, 5, 2);
145 	zexpect_within(7, 7, 0);
146 	zexpect_within(7, 7, 3);
147 	zexpect_within(7, 7 + 3, 3);
148 }
149 
150 ZTEST_EXPECT_FAIL(expect, test_fail_expect_within);
ZTEST(expect,test_fail_expect_within)151 ZTEST(expect, test_fail_expect_within)
152 {
153 	zexpect_within(7, 5, 1);
154 }
155 
ZTEST(expect,test_expect_between_inclusive)156 ZTEST(expect, test_expect_between_inclusive)
157 {
158 	zexpect_between_inclusive(-5, -10, 0);
159 
160 	zexpect_between_inclusive(5, 0, 10);
161 	zexpect_between_inclusive(0, 0, 10);
162 	zexpect_between_inclusive(10, 0, 10);
163 }
164 
165 ZTEST_EXPECT_FAIL(expect, test_fail_expect_between_inclusive);
ZTEST(expect,test_fail_expect_between_inclusive)166 ZTEST(expect, test_fail_expect_between_inclusive)
167 {
168 	zexpect_between_inclusive(-50, -20, 30);
169 
170 	zexpect_between_inclusive(5, 6, 10);
171 	zexpect_between_inclusive(5, 0, 4);
172 	zexpect_between_inclusive(5, 0, 4);
173 	zexpect_between_inclusive(5, 6, 10);
174 }
175 
ZTEST(expect,test_expect_str_equal)176 ZTEST(expect, test_expect_str_equal)
177 {
178 	const char *s1 = "asdf";
179 	const char s2[] = {'a', 's', 'd', 'f', '\0'};
180 
181 	zexpect_str_equal(s1, s2);
182 }
183 
184 ZTEST_EXPECT_FAIL(expect, test_expect_str_equal_fail);
ZTEST(expect,test_expect_str_equal_fail)185 ZTEST(expect, test_expect_str_equal_fail)
186 {
187 	const char *s1 = "asdf";
188 	const char s2[] = {'a', 's', 'd', 'f', 'q', '\0'};
189 
190 	zexpect_str_equal(s1, s2);
191 }
192