Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c9c14cf

Browse filesBrowse files
committed
[core][utest] add WMath test cases
1 parent 885342c commit c9c14cf
Copy full SHA for c9c14cf

File tree

Expand file treeCollapse file tree

2 files changed

+227
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+227
-0
lines changed

‎core/utest/SConscript

Copy file name to clipboardExpand all lines: core/utest/SConscript
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ src = []
44

55
if GetDepend('RT_USING_CI_ACTION') or GetDepend('RTDUINO_UTEST_CORE'):
66
src += Glob('TC_*.c')
7+
src += Glob('TC_*.cpp')
78

89
group = DefineGroup('utestcases', src, depend = [''])
910

‎core/utest/TC_WMath.cpp

Copy file name to clipboard
+226Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright (c) 2021-2025, RTduino Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* https://github.com/RTduino/RTduino
7+
* https://gitee.com/rtduino/RTduino
8+
*
9+
* Change Logs:
10+
* Date Author Notes
11+
* 2025-01-04 Meco Man first version
12+
*/
13+
14+
#include <RTduino.h>
15+
#include <utest.h>
16+
17+
static void TC_makeWord(void)
18+
{
19+
uassert_int_equal(makeWord(0xDEAD), 0xDEAD);
20+
uassert_int_equal(makeWord(0xDE), 0x00DE);
21+
uassert_int_equal(makeWord(0xDE, 0xAD), 0xDEAD);
22+
}
23+
24+
static void TC_word(void)
25+
{
26+
uassert_int_equal(word(0xDEAD), 0xDEAD);
27+
uassert_int_equal(word(0xDE), 0x00DE);
28+
uassert_int_equal(word(0xDE, 0xAD), 0xDEAD);
29+
}
30+
31+
static void TC_map(void)
32+
{
33+
uassert_int_equal(map(50, 0, 100, 0, 10), 5);
34+
uassert_int_equal(map(5, 0, 10, 0, 100), 50);
35+
uassert_int_equal(map(1, 0, 100, 0, 10), 0);
36+
uassert_int_equal(map(0, 0, -256, 0, 1024), 0);
37+
uassert_int_equal(map(-256, 0, -256, 0, 1024), 1024);
38+
uassert_int_equal(map(-128, 0, -256, 0, 1024), 512);
39+
uassert_int_equal(map(0, 0, 256, 0, -1024), 0);
40+
uassert_int_equal(map(256, 0, 256, 0, -1024), -1024);
41+
uassert_int_equal(map(128, 0, 256, 0, -1024), -512);
42+
}
43+
44+
static void TC_random(void)
45+
{
46+
/* Test randomSeed with a known seed */
47+
randomSeed(1);
48+
49+
/* Test random(long howbig) */
50+
long result = random(10);
51+
uassert_in_range(result, 0, 9); /* The result should be between 0 and 9 inclusive */
52+
53+
result = random(1);
54+
uassert_int_equal(result, 0); /* The result should be 0 since 1 % 1 is 0 */
55+
56+
result = random(100);
57+
uassert_in_range(result, 0, 99); /* The result should be between 0 and 99 inclusive */
58+
59+
/* Test random(long howsmall, long howbig) */
60+
result = random(5, 15);
61+
uassert_in_range(result, 5, 14); /* The result should be between 5 and 14 inclusive */
62+
63+
result = random(10, 10);
64+
uassert_int_equal(result, 10); /* The result should be 10 since howsmall is equal to howbig */
65+
66+
result = random(15, 5);
67+
uassert_int_equal(result, 15); /* The result should be 15 since howsmall is greater than howbig */
68+
69+
/* Test edge cases */
70+
result = random(0, 0);
71+
uassert_int_equal(result, 0); /* Both howsmall and howbig are 0, should return 0 */
72+
73+
result = random(-5, 5);
74+
uassert_in_range(result, -5, 4); /* Negative range test */
75+
76+
result = random(1000);
77+
uassert_in_range(result, 0, 999); /* Ensure large range works */
78+
79+
result = random(990, 1000);
80+
uassert_in_range(result, 990, 999); /* Small range test */
81+
}
82+
83+
static void TC_constrain(void)
84+
{
85+
/* Test values within the range */
86+
uassert_int_equal(constrain(50, 0, 100), 50);
87+
88+
/* Test value below the range */
89+
uassert_int_equal(constrain(-10, 0, 100), 0);
90+
91+
/* Test value above the range */
92+
uassert_int_equal(constrain(150, 0, 100), 100);
93+
94+
/* Test boundary values */
95+
uassert_int_equal(constrain(0, 0, 100), 0);
96+
uassert_int_equal(constrain(100, 0, 100), 100);
97+
}
98+
99+
static void TC_radians(void)
100+
{
101+
/* Test case 1: 0 degrees should be 0 radians */
102+
double result = radians(0);
103+
uassert_float_equal(result, 0.0); /* Exact match for 0 radians */
104+
105+
/* Test case 2: 90 degrees should be approximately PI/2 radians */
106+
result = radians(90);
107+
uassert_float_equal(result, HALF_PI); /* Allowing a small tolerance for floating-point comparison */
108+
109+
/* Test case 3: 180 degrees should be approximately PI radians */
110+
result = radians(180);
111+
uassert_float_equal(result, PI); /* Allowing a small tolerance */
112+
113+
/* Test case 4: 360 degrees should be approximately 2*PI radians */
114+
result = radians(360);
115+
uassert_float_equal(result, TWO_PI); /* Allowing a small tolerance */
116+
117+
/* Additional test case: -90 degrees should be approximately -PI/2 radians */
118+
result = radians(-90);
119+
uassert_float_equal(result, -HALF_PI); /* Allowing a small tolerance */
120+
}
121+
122+
static void TC_degrees(void)
123+
{
124+
/* Test case 1: 0 radians should be 0 degrees */
125+
double result = degrees(0);
126+
uassert_float_equal(result, 0.0); /* Exact match for 0 degrees */
127+
128+
/* Test case 2: PI/2 radians should be approximately 90 degrees */
129+
result = degrees(HALF_PI);
130+
uassert_float_equal(result, 90.0); /* Allowing a small tolerance for floating-point comparison */
131+
132+
/* Test case 3: PI radians should be approximately 180 degrees */
133+
result = degrees(PI);
134+
uassert_float_equal(result, 180.0); /* Allowing a small tolerance */
135+
136+
/* Test case 4: 2*PI radians should be approximately 360 degrees */
137+
result = degrees(TWO_PI);
138+
uassert_float_equal(result, 360.0); /* Allowing a small tolerance */
139+
140+
/* Additional test case: -PI/2 radians should be approximately -90 degrees */
141+
result = degrees(-HALF_PI);
142+
uassert_float_equal(result, -90.0); /* Allowing a small tolerance */
143+
}
144+
145+
static void TC_sq(void)
146+
{
147+
int rst_int = 0;
148+
float rst_float = 0.0f;
149+
150+
/* Test with positive integer */
151+
rst_int = sq(3);
152+
uassert_int_equal(rst_int, 9); /* Expected 3^2 = 9 */
153+
154+
/* Test with negative integer */
155+
rst_int = sq(-2);
156+
uassert_int_equal(rst_int, 4); /* Expected (-2)^2 = 4 */
157+
158+
/* Test with zero */
159+
rst_int = sq(0);
160+
uassert_int_equal(rst_int, 0); /* Expected 0^2 = 0 */
161+
162+
/* Test to ensure inequality for a wrong result */
163+
rst_int = sq(4);
164+
uassert_int_not_equal(rst_int, 15); /* 4^2 should not be 15, it should be 16 */
165+
166+
/* Test with positive floating-point number */
167+
rst_float = sq(2.5f);
168+
uassert_float_equal(rst_float, 6.25); /* Expected 2.5^2 = 6.25, allowing for minor floating-point precision differences */
169+
170+
/* Test with negative floating-point number */
171+
rst_float = sq(-1.7f);
172+
uassert_float_equal(rst_float, 2.89); /* Expected (-1.7)^2 = 2.89 */
173+
174+
/* Test with zero */
175+
rst_float = sq(0.0f);
176+
uassert_float_equal(rst_float, 0); /* Expected 0.0^2 = 0.0 */
177+
178+
/* Test to ensure inequality for a wrong result (using not_in_range for a clearly wrong value) */
179+
rst_float = sq(3.0f);
180+
uassert_float_equal(rst_float, 9); /* 3.0^2 should not be in the range 8.9 to 9.1, it should be 9.0 */
181+
}
182+
183+
#if defined(min) && !defined(RTDUINO_NO_MIN)
184+
static void TC_min(void)
185+
{
186+
uassert_int_equal(min(5, 10), 5);
187+
uassert_int_equal(min(10, 5), 5);
188+
uassert_int_equal(min(5, 5), 5);
189+
uassert_int_equal(min(-5, 10), -5);
190+
uassert_int_equal(min(-10, -5), -10);
191+
uassert_int_equal(min(0, 0), 0);
192+
}
193+
#endif /* defined(min) && !defined(RTDUINO_NO_MIN) */
194+
195+
#if defined(max) && !defined(RTDUINO_NO_MAX)
196+
static void TC_max(void)
197+
{
198+
uassert_int_equal(max(5, 10), 10);
199+
uassert_int_equal(max(10, 5), 10);
200+
uassert_int_equal(max(5, 5), 5);
201+
uassert_int_equal(max(-5, 10), 10);
202+
uassert_int_equal(max(-10, -5), -5);
203+
uassert_int_equal(max(0, 0), 0);
204+
}
205+
#endif /* defined(max) && !defined(RTDUINO_NO_MAX) */
206+
207+
/* Utest function to run all test cases */
208+
static void utest_do_tc(void)
209+
{
210+
UTEST_UNIT_RUN(TC_makeWord);
211+
UTEST_UNIT_RUN(TC_word);
212+
UTEST_UNIT_RUN(TC_map);
213+
UTEST_UNIT_RUN(TC_random);
214+
UTEST_UNIT_RUN(TC_constrain);
215+
UTEST_UNIT_RUN(TC_radians);
216+
UTEST_UNIT_RUN(TC_degrees);
217+
UTEST_UNIT_RUN(TC_sq);
218+
#if defined(min) && !defined(RTDUINO_NO_MIN)
219+
UTEST_UNIT_RUN(TC_min);
220+
#endif /* defined(min) && !defined(RTDUINO_NO_MIN) */
221+
#if defined(max) && !defined(RTDUINO_NO_MAX)
222+
UTEST_UNIT_RUN(TC_max);
223+
#endif /* defined(max) && !defined(RTDUINO_NO_MAX) */
224+
}
225+
226+
UTEST_TC_EXPORT(utest_do_tc, "RTduino.core.WMath", RT_NULL, RT_NULL, 1000);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.