Skip to content

Commit d583615

Browse files
mysterywolfRbb666
authored andcommitted
[utest] add rt_memset test case
1 parent a343e1d commit d583615

File tree

9 files changed

+439
-55
lines changed

9 files changed

+439
-55
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <rtthread.h>
2+
#include "utest.h"
3+
4+
static void TC_uassert_true_false(void)
5+
{
6+
uassert_true(1);
7+
uassert_false(0);
8+
}
9+
10+
static void TC_uassert_null_not_null(void)
11+
{
12+
int *ptr = RT_NULL;
13+
int value = 10;
14+
int *ptr2 = &value;
15+
16+
uassert_null(ptr);
17+
uassert_not_null(ptr2);
18+
}
19+
20+
static void TC_uassert_int_op(void)
21+
{
22+
int a = 5;
23+
int b = 10;
24+
25+
uassert_int_equal(a, a);
26+
uassert_int_not_equal(a, b);
27+
uassert_int_less(a, b);
28+
uassert_int_less_equal(a, b);
29+
uassert_int_less_equal(a, a);
30+
uassert_int_greater(b, a);
31+
uassert_int_greater_equal(b, a);
32+
uassert_int_greater_equal(b, b);
33+
}
34+
35+
static void TC_uassert_ptr_op(void)
36+
{
37+
int a = 5;
38+
int b = 10;
39+
int *ptr_a = &a;
40+
int *ptr_b = &b;
41+
42+
uassert_ptr_equal(ptr_a, ptr_a);
43+
uassert_ptr_not_equal(ptr_a, ptr_b);
44+
}
45+
46+
static void TC_uassert_str_op(void)
47+
{
48+
const char *str1 = "Hello";
49+
const char *str2 = "Hello";
50+
const char *str3 = "World";
51+
52+
uassert_str_equal(str1, str2);
53+
uassert_str_not_equal(str1, str3);
54+
}
55+
56+
static void TC_uassert_in_range(void)
57+
{
58+
int value = 5;
59+
uassert_in_range(value, 1, 10);
60+
uassert_not_in_range(value, 10, 20);
61+
}
62+
63+
static void utest_do_tc(void)
64+
{
65+
UTEST_UNIT_RUN(TC_uassert_true_false);
66+
UTEST_UNIT_RUN(TC_uassert_null_not_null);
67+
UTEST_UNIT_RUN(TC_uassert_int_op);
68+
UTEST_UNIT_RUN(TC_uassert_ptr_op);
69+
UTEST_UNIT_RUN(TC_uassert_str_op);
70+
UTEST_UNIT_RUN(TC_uassert_in_range);
71+
}
72+
73+
UTEST_TC_EXPORT(utest_do_tc, "utest.uassert", RT_NULL, RT_NULL, 10);

components/utilities/utest/utest.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static void utest_do_run(const char *utest_name)
231231
{
232232
if (tc_table[i].init() != RT_EOK)
233233
{
234-
LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
234+
LOG_E("[ FAILED ] [ result ] testcase init (%s)", tc_table[i].name);
235235
goto __tc_continue;
236236
}
237237
}
@@ -259,7 +259,7 @@ static void utest_do_run(const char *utest_name)
259259
{
260260
if (tc_table[i].cleanup() != RT_EOK)
261261
{
262-
LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
262+
LOG_E("[ FAILED ] [ result ] testcase cleanup (%s)", tc_table[i].name);
263263
goto __tc_continue;
264264
}
265265
}
@@ -382,7 +382,7 @@ utest_t utest_handle_get(void)
382382

383383
void utest_unit_run(test_unit_func func, const char *unit_func_name)
384384
{
385-
// LOG_I("[==========] utest unit name: (%s)", unit_func_name);
385+
LOG_I("[==========] utest unit name: (%s)", unit_func_name);
386386
local_utest.error = UTEST_PASSED;
387387
local_utest.passed_num = 0;
388388
local_utest.failed_num = 0;

components/utilities/utest/utest.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,10 @@ utest_t utest_handle_get(void);
172172
* @return None
173173
*
174174
*/
175-
#define UTEST_UNIT_RUN(test_unit_func) \
176-
utest_unit_run(test_unit_func, #test_unit_func); \
177-
if(utest_handle_get()->failed_num != 0) return;
175+
#define UTEST_UNIT_RUN(test_unit_func) \
176+
do { \
177+
utest_unit_run(test_unit_func, #test_unit_func); \
178+
} while (0)
178179

179180
#ifdef __cplusplus
180181
}

components/utilities/utest/utest_assert.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa
5050
*/
5151
#define uassert_true(value) __utest_assert(value, "(" #value ") is false")
5252
#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")
53+
5354
#define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null")
5455
#define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null")
5556

56-
#define uassert_int_equal(a, b) __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")")
57-
#define uassert_int_not_equal(a, b) __utest_assert((a) != (b), "(" #a ") equal to (" #b ")")
57+
#define __uassert_int_op(a, b, op) __utest_assert((a) op (b), "(" #a ") not " #op " (" #b ")")
58+
#define uassert_int_equal(a, b) __uassert_int_op(a, b, ==)
59+
#define uassert_int_not_equal(a, b) __uassert_int_op(a, b, !=)
60+
#define uassert_int_less(a, b) __uassert_int_op(a, b, <)
61+
#define uassert_int_less_equal(a, b) __uassert_int_op(a, b, <=)
62+
#define uassert_int_greater(a, b) __uassert_int_op(a, b, >)
63+
#define uassert_int_greater_equal(a, b) __uassert_int_op(a, b, >=)
5864

5965
#define uassert_ptr_equal(a, b) __utest_assert((const void*)(a) == (const void*)(b), "(" #a ") not equal to (" #b ")")
6066
#define uassert_ptr_not_equal(a, b) __utest_assert((const void*)(a) != (const void*)(b), "(" #a ") equal to (" #b ")")

src/klibc/utest/TC_rt_memcmp.c

+128-18
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@
1212
#include <rtklibc.h>
1313
#include <utest.h>
1414

15-
static rt_err_t utest_tc_init(void)
16-
{
17-
return RT_EOK;
18-
}
19-
20-
static rt_err_t utest_tc_cleanup(void)
21-
{
22-
return RT_EOK;
23-
}
24-
2515
static void TC_rt_memcmp_str(void)
2616
{
2717
const char* s = "abc 123";
@@ -32,20 +22,140 @@ static void TC_rt_memcmp_str(void)
3222

3323
/* The following tests intentionally use a length > 3 */
3424
/* To test what rt_memcmp does in such a situation */
35-
uassert_int_equal(!!(rt_memcmp(s, "abc", 6) > 0), 1);
36-
uassert_int_equal(!!(rt_memcmp("abc", s, 6) < 0), 1);
25+
uassert_int_greater(rt_memcmp(s, "abc", 6), 0);
26+
uassert_int_less(rt_memcmp("abc", s, 6), 0);
27+
}
28+
29+
static void TC_rt_memcmp_int_array(void)
30+
{
31+
int arr1[] = {1, 2, 3, 4, 5};
32+
int arr2[] = {1, 2, 3, 4, 5};
33+
int arr3[] = {1, 2, 3, 4, 6};
34+
35+
uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
36+
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
37+
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
38+
}
39+
40+
static void TC_rt_memcmp_float_array(void)
41+
{
42+
float arr1[] = {1.0f, 2.0f, 3.0f};
43+
float arr2[] = {1.0f, 2.0f, 3.0f};
44+
float arr3[] = {1.0f, 2.0f, 3.1f};
45+
46+
uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
47+
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
48+
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
49+
}
50+
51+
typedef struct {
52+
int id;
53+
float value;
54+
} Item;
55+
56+
static void TC_rt_memcmp_struct_array(void)
57+
{
58+
Item arr1[] = {{1, 1.0f}, {2, 2.0f}};
59+
Item arr2[] = {{1, 1.0f}, {2, 2.0f}};
60+
Item arr3[] = {{1, 1.0f}, {2, 2.1f}};
61+
62+
uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
63+
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
64+
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
65+
}
66+
67+
typedef struct {
68+
int id;
69+
float value;
70+
char name[10];
71+
} MixedItem;
72+
73+
static void TC_rt_memcmp_mixed_array(void)
74+
{
75+
MixedItem arr1[] = {{1, 1.0f, "item1"}, {2, 2.0f, "item2"}};
76+
MixedItem arr2[] = {{1, 1.0f, "item1"}, {2, 2.0f, "item2"}};
77+
MixedItem arr3[] = {{1, 1.0f, "item1"}, {2, 2.1f, "item2"}};
78+
79+
uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
80+
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
81+
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
82+
}
83+
84+
typedef struct {
85+
int id;
86+
float score;
87+
} Student;
88+
89+
typedef struct {
90+
Student students[3];
91+
char className[10];
92+
} Class;
93+
94+
static void TC_rt_memcmp_nested_struct_array(void)
95+
{
96+
Class class1 = {
97+
.students = {{1, 90.5}, {2, 85.0}, {3, 92.0}},
98+
.className = "ClassA"
99+
};
100+
101+
Class class2 = {
102+
.students = {{1, 90.5}, {2, 85.0}, {3, 92.0}},
103+
.className = "ClassA"
104+
};
105+
106+
Class class3 = {
107+
.students = {{1, 90.5}, {2, 85.1}, {3, 92.0}},
108+
.className = "ClassA"
109+
};
110+
111+
uassert_int_equal(rt_memcmp(&class1, &class2, sizeof(Class)), 0);
112+
uassert_int_not_equal(rt_memcmp(&class1, &class3, sizeof(Class)), 0);
113+
}
114+
115+
static void TC_rt_memcmp_partial_match(void)
116+
{
117+
char arr1[] = "abcdefghijklmnopqrstuvwxyz";
118+
char arr2[] = "abcdefghijklmxyznopqrstuvw";
119+
120+
uassert_int_equal(rt_memcmp(arr1, arr2, 13), 0);
121+
uassert_int_not_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
122+
}
123+
124+
#define LARGE_ARRAY_SIZE 1000
125+
126+
static void TC_rt_memcmp_large_array(void)
127+
{
128+
int *arr1 = rt_calloc(LARGE_ARRAY_SIZE, sizeof(int));
129+
int *arr2 = rt_calloc(LARGE_ARRAY_SIZE, sizeof(int));
130+
131+
uassert_not_null(arr1);
132+
uassert_not_null(arr2);
133+
134+
for (int i = 0; i < LARGE_ARRAY_SIZE; i++) {
135+
arr1[i] = i;
136+
arr2[i] = i;
137+
}
138+
139+
uassert_int_equal(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0);
140+
arr2[LARGE_ARRAY_SIZE - 1] = LARGE_ARRAY_SIZE;
37141

38-
/* Check RT_NULL input handling */
39-
uassert_int_not_equal(rt_memcmp("abc", RT_NULL, 3), 0);
40-
uassert_int_not_equal(rt_memcmp(RT_NULL, "abc", 3), 0);
142+
uassert_int_less(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0);
143+
uassert_int_greater(rt_memcmp(arr2, arr1, LARGE_ARRAY_SIZE * sizeof(int)), 0);
41144

42-
/* Check that two RT_NULL strings will match */
43-
uassert_int_equal(rt_memcmp(RT_NULL, RT_NULL, 0), 0);
145+
rt_free(arr1);
146+
rt_free(arr2);
44147
}
45148

46149
static void utest_do_tc(void)
47150
{
48151
UTEST_UNIT_RUN(TC_rt_memcmp_str);
152+
UTEST_UNIT_RUN(TC_rt_memcmp_int_array);
153+
UTEST_UNIT_RUN(TC_rt_memcmp_float_array);
154+
UTEST_UNIT_RUN(TC_rt_memcmp_struct_array);
155+
UTEST_UNIT_RUN(TC_rt_memcmp_mixed_array);
156+
UTEST_UNIT_RUN(TC_rt_memcmp_nested_struct_array);
157+
UTEST_UNIT_RUN(TC_rt_memcmp_partial_match);
158+
UTEST_UNIT_RUN(TC_rt_memcmp_large_array);
49159
}
50160

51-
UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcmp", utest_tc_init, utest_tc_cleanup, 1000);
161+
UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcmp", RT_NULL, RT_NULL, 1000);

src/klibc/utest/TC_rt_memcpy.c

+12-18
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,26 @@
99
* 2024-12-24 Meco Man port to utest
1010
*/
1111

12-
#include <rtklibc.h>
12+
#include <rtthread.h>
1313
#include <utest.h>
1414

15+
#define N 80 /**< Define the constant N for buffer size as 80 */
16+
#define TEST_BUF_SIZE 512 /**< Define the constant TEST_BUF_SIZE as 512 */
17+
static char *buf; /**< Define a static buffer of 512 bytes, initialized to 0 */
18+
1519
static rt_err_t utest_tc_init(void)
1620
{
21+
buf = rt_malloc(TEST_BUF_SIZE * sizeof(char)); /**< Allocate memory for the buffer */
22+
uassert_not_null(buf);
1723
return RT_EOK;
1824
}
1925

2026
static rt_err_t utest_tc_cleanup(void)
2127
{
28+
rt_free(buf);
2229
return RT_EOK;
2330
}
2431

25-
#define N 80 /**< Define the constant N for buffer size as 80 */
26-
static char buf[512] = {0}; /**< Define a static buffer of 512 bytes, initialized to 0 */
27-
28-
/**
29-
* Align a given pointer to a 64-byte boundary.
30-
* @param p The pointer to align.
31-
* @return The aligned pointer.
32-
*/
33-
static void* aligned(void* p)
34-
{
35-
return (void*)(((intptr_t)p + 63) & -64);
36-
}
37-
3832
/**
3933
* Test memory copy with alignment.
4034
* @param dalign The alignment offset for the destination buffer.
@@ -43,10 +37,10 @@ static void* aligned(void* p)
4337
*/
4438
static void test_align(unsigned dalign, unsigned salign, size_t len)
4539
{
46-
char* src = aligned(buf); /**< Source buffer starting address, 64-byte aligned */
47-
char* dst = aligned(buf + 128); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
48-
char* want = aligned(buf + 256); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
49-
char* p; /**< Pointer to receive the return value of rt_memcpy */
40+
char *src = (char *)RT_ALIGN((rt_ubase_t)buf, 64); /**< Source buffer starting address, 64-byte aligned */
41+
char *dst = (char *)RT_ALIGN(((rt_ubase_t)buf + 128), 64); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
42+
char *want = (char *)RT_ALIGN(((rt_ubase_t)buf + 256), 64); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
43+
char *p; /**< Pointer to receive the return value of rt_memcpy */
5044
unsigned i;
5145

5246
/** Assert that the source alignment offset plus length does not exceed N */

0 commit comments

Comments
 (0)