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_is_null)62 ZTEST(expect, test_expect_is_null)
63 {
64 void *ptr = NULL;
65
66 zexpect_is_null(ptr);
67 }
68
69 ZTEST_EXPECT_FAIL(expect, test_fail_expect_is_null);
ZTEST(expect,test_fail_expect_is_null)70 ZTEST(expect, test_fail_expect_is_null)
71 {
72 void *ptr = (void *)0x32137899;
73
74 zexpect_is_null(ptr);
75 }
76
ZTEST(expect,test_expect_not_null)77 ZTEST(expect, test_expect_not_null)
78 {
79 void *ptr = (void *)0x91517141;
80
81 zexpect_not_null(ptr);
82 }
83
84 ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_null);
ZTEST(expect,test_fail_expect_not_null)85 ZTEST(expect, test_fail_expect_not_null)
86 {
87 zexpect_not_null(NULL);
88 }
89
ZTEST(expect,test_expect_equal)90 ZTEST(expect, test_expect_equal)
91 {
92 zexpect_equal(5, 5);
93 }
94
95 ZTEST_EXPECT_FAIL(expect, test_fail_expect_equal);
ZTEST(expect,test_fail_expect_equal)96 ZTEST(expect, test_fail_expect_equal)
97 {
98 zexpect_equal(5, 1);
99 }
100
ZTEST(expect,test_expect_not_equal)101 ZTEST(expect, test_expect_not_equal)
102 {
103 zexpect_not_equal(5, 1);
104 }
105
106 ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_equal);
ZTEST(expect,test_fail_expect_not_equal)107 ZTEST(expect, test_fail_expect_not_equal)
108 {
109 zexpect_not_equal(5, 5);
110 }
111
ZTEST(expect,test_expect_equal_ptr)112 ZTEST(expect, test_expect_equal_ptr)
113 {
114 int v = 9;
115 int *a = &v;
116 int *b = &v;
117
118 zexpect_equal_ptr(a, b);
119 }
120
121 ZTEST_EXPECT_FAIL(expect, test_fail_expect_equal_ptr);
ZTEST(expect,test_fail_expect_equal_ptr)122 ZTEST(expect, test_fail_expect_equal_ptr)
123 {
124 int v = 9;
125 int *a = &v;
126 int *b = NULL;
127
128 zexpect_equal_ptr(a, b);
129 }
130
ZTEST(expect,test_expect_within)131 ZTEST(expect, test_expect_within)
132 {
133 zexpect_within(7, 5, 2);
134 zexpect_within(7, 7, 0);
135 zexpect_within(7, 7, 3);
136 zexpect_within(7, 7 + 3, 3);
137 }
138
139 ZTEST_EXPECT_FAIL(expect, test_fail_expect_within);
ZTEST(expect,test_fail_expect_within)140 ZTEST(expect, test_fail_expect_within)
141 {
142 zexpect_within(7, 5, 1);
143 }
144
ZTEST(expect,test_expect_between_inclusive)145 ZTEST(expect, test_expect_between_inclusive)
146 {
147 zexpect_between_inclusive(-5, -10, 0);
148
149 zexpect_between_inclusive(5, 0, 10);
150 zexpect_between_inclusive(0, 0, 10);
151 zexpect_between_inclusive(10, 0, 10);
152 }
153
154 ZTEST_EXPECT_FAIL(expect, test_fail_expect_between_inclusive);
ZTEST(expect,test_fail_expect_between_inclusive)155 ZTEST(expect, test_fail_expect_between_inclusive)
156 {
157 zexpect_between_inclusive(-50, -20, 30);
158
159 zexpect_between_inclusive(5, 6, 10);
160 zexpect_between_inclusive(5, 0, 4);
161 zexpect_between_inclusive(5, 0, 4);
162 zexpect_between_inclusive(5, 6, 10);
163 }
164