Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions components/utilities/utest/TC_uassert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <rtthread.h>
#include "utest.h"

static void TC_uassert_true_false(void)
{
uassert_true(1);
uassert_false(0);
}

static void TC_uassert_null_not_null(void)
{
int *ptr = RT_NULL;
int value = 10;
int *ptr2 = &value;

uassert_null(ptr);
uassert_not_null(ptr2);
}

static void TC_uassert_int_op(void)
{
int a = 5;
int b = 10;

uassert_int_equal(a, a);
uassert_int_not_equal(a, b);
uassert_int_less(a, b);
uassert_int_less_equal(a, b);
uassert_int_less_equal(a, a);
uassert_int_greater(b, a);
uassert_int_greater_equal(b, a);
uassert_int_greater_equal(b, b);
}

static void TC_uassert_ptr_op(void)
{
int a = 5;
int b = 10;
int *ptr_a = &a;
int *ptr_b = &b;

uassert_ptr_equal(ptr_a, ptr_a);
uassert_ptr_not_equal(ptr_a, ptr_b);
}

static void TC_uassert_str_op(void)
{
const char *str1 = "Hello";
const char *str2 = "Hello";
const char *str3 = "World";

uassert_str_equal(str1, str2);
uassert_str_not_equal(str1, str3);
}

static void TC_uassert_in_range(void)
{
int value = 5;
uassert_in_range(value, 1, 10);
uassert_not_in_range(value, 10, 20);
}

static void utest_do_tc(void)
{
UTEST_UNIT_RUN(TC_uassert_true_false);
UTEST_UNIT_RUN(TC_uassert_null_not_null);
UTEST_UNIT_RUN(TC_uassert_int_op);
UTEST_UNIT_RUN(TC_uassert_ptr_op);
UTEST_UNIT_RUN(TC_uassert_str_op);
UTEST_UNIT_RUN(TC_uassert_in_range);
}

UTEST_TC_EXPORT(utest_do_tc, "utest.uassert", RT_NULL, RT_NULL, 10);
6 changes: 3 additions & 3 deletions components/utilities/utest/utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ static void utest_do_run(const char *utest_name)
{
if (tc_table[i].init() != RT_EOK)
{
LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
LOG_E("[ FAILED ] [ result ] testcase init (%s)", tc_table[i].name);
goto __tc_continue;
}
}
Expand Down Expand Up @@ -259,7 +259,7 @@ static void utest_do_run(const char *utest_name)
{
if (tc_table[i].cleanup() != RT_EOK)
{
LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
LOG_E("[ FAILED ] [ result ] testcase cleanup (%s)", tc_table[i].name);
goto __tc_continue;
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ utest_t utest_handle_get(void)

void utest_unit_run(test_unit_func func, const char *unit_func_name)
{
// LOG_I("[==========] utest unit name: (%s)", unit_func_name);
LOG_I("[==========] utest unit name: (%s)", unit_func_name);
local_utest.error = UTEST_PASSED;
local_utest.passed_num = 0;
local_utest.failed_num = 0;
Expand Down
7 changes: 4 additions & 3 deletions components/utilities/utest/utest.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ utest_t utest_handle_get(void);
* @return None
*
*/
#define UTEST_UNIT_RUN(test_unit_func) \
utest_unit_run(test_unit_func, #test_unit_func); \
if(utest_handle_get()->failed_num != 0) return;
#define UTEST_UNIT_RUN(test_unit_func) \
do { \
utest_unit_run(test_unit_func, #test_unit_func); \
} while (0)

#ifdef __cplusplus
}
Expand Down
10 changes: 8 additions & 2 deletions components/utilities/utest/utest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa
*/
#define uassert_true(value) __utest_assert(value, "(" #value ") is false")
#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")

#define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null")
#define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null")

#define uassert_int_equal(a, b) __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")")
#define uassert_int_not_equal(a, b) __utest_assert((a) != (b), "(" #a ") equal to (" #b ")")
#define __uassert_int_op(a, b, op) __utest_assert((a) op (b), "(" #a ") not " #op " (" #b ")")
#define uassert_int_equal(a, b) __uassert_int_op(a, b, ==)
#define uassert_int_not_equal(a, b) __uassert_int_op(a, b, !=)
#define uassert_int_less(a, b) __uassert_int_op(a, b, <)
#define uassert_int_less_equal(a, b) __uassert_int_op(a, b, <=)
#define uassert_int_greater(a, b) __uassert_int_op(a, b, >)
#define uassert_int_greater_equal(a, b) __uassert_int_op(a, b, >=)

#define uassert_ptr_equal(a, b) __utest_assert((const void*)(a) == (const void*)(b), "(" #a ") not equal to (" #b ")")
#define uassert_ptr_not_equal(a, b) __utest_assert((const void*)(a) != (const void*)(b), "(" #a ") equal to (" #b ")")
Expand Down
146 changes: 128 additions & 18 deletions src/klibc/utest/TC_rt_memcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
#include <rtklibc.h>
#include <utest.h>

static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}

static void TC_rt_memcmp_str(void)
{
const char* s = "abc 123";
Expand All @@ -32,20 +22,140 @@ static void TC_rt_memcmp_str(void)

/* The following tests intentionally use a length > 3 */
/* To test what rt_memcmp does in such a situation */
uassert_int_equal(!!(rt_memcmp(s, "abc", 6) > 0), 1);
uassert_int_equal(!!(rt_memcmp("abc", s, 6) < 0), 1);
uassert_int_greater(rt_memcmp(s, "abc", 6), 0);
uassert_int_less(rt_memcmp("abc", s, 6), 0);
}

static void TC_rt_memcmp_int_array(void)
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {1, 2, 3, 4, 5};
int arr3[] = {1, 2, 3, 4, 6};

uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
}

static void TC_rt_memcmp_float_array(void)
{
float arr1[] = {1.0f, 2.0f, 3.0f};
float arr2[] = {1.0f, 2.0f, 3.0f};
float arr3[] = {1.0f, 2.0f, 3.1f};

uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
}

typedef struct {
int id;
float value;
} Item;

static void TC_rt_memcmp_struct_array(void)
{
Item arr1[] = {{1, 1.0f}, {2, 2.0f}};
Item arr2[] = {{1, 1.0f}, {2, 2.0f}};
Item arr3[] = {{1, 1.0f}, {2, 2.1f}};

uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
}

typedef struct {
int id;
float value;
char name[10];
} MixedItem;

static void TC_rt_memcmp_mixed_array(void)
{
MixedItem arr1[] = {{1, 1.0f, "item1"}, {2, 2.0f, "item2"}};
MixedItem arr2[] = {{1, 1.0f, "item1"}, {2, 2.0f, "item2"}};
MixedItem arr3[] = {{1, 1.0f, "item1"}, {2, 2.1f, "item2"}};

uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0);
uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0);
}

typedef struct {
int id;
float score;
} Student;

typedef struct {
Student students[3];
char className[10];
} Class;

static void TC_rt_memcmp_nested_struct_array(void)
{
Class class1 = {
.students = {{1, 90.5}, {2, 85.0}, {3, 92.0}},
.className = "ClassA"
};

Class class2 = {
.students = {{1, 90.5}, {2, 85.0}, {3, 92.0}},
.className = "ClassA"
};

Class class3 = {
.students = {{1, 90.5}, {2, 85.1}, {3, 92.0}},
.className = "ClassA"
};

uassert_int_equal(rt_memcmp(&class1, &class2, sizeof(Class)), 0);
uassert_int_not_equal(rt_memcmp(&class1, &class3, sizeof(Class)), 0);
}

static void TC_rt_memcmp_partial_match(void)
{
char arr1[] = "abcdefghijklmnopqrstuvwxyz";
char arr2[] = "abcdefghijklmxyznopqrstuvw";

uassert_int_equal(rt_memcmp(arr1, arr2, 13), 0);
uassert_int_not_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0);
}

#define LARGE_ARRAY_SIZE 1000

static void TC_rt_memcmp_large_array(void)
{
int *arr1 = rt_calloc(LARGE_ARRAY_SIZE, sizeof(int));
int *arr2 = rt_calloc(LARGE_ARRAY_SIZE, sizeof(int));

uassert_not_null(arr1);
uassert_not_null(arr2);

for (int i = 0; i < LARGE_ARRAY_SIZE; i++) {
arr1[i] = i;
arr2[i] = i;
}

uassert_int_equal(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0);
arr2[LARGE_ARRAY_SIZE - 1] = LARGE_ARRAY_SIZE;

/* Check RT_NULL input handling */
uassert_int_not_equal(rt_memcmp("abc", RT_NULL, 3), 0);
uassert_int_not_equal(rt_memcmp(RT_NULL, "abc", 3), 0);
uassert_int_less(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0);
uassert_int_greater(rt_memcmp(arr2, arr1, LARGE_ARRAY_SIZE * sizeof(int)), 0);

/* Check that two RT_NULL strings will match */
uassert_int_equal(rt_memcmp(RT_NULL, RT_NULL, 0), 0);
rt_free(arr1);
rt_free(arr2);
}

static void utest_do_tc(void)
{
UTEST_UNIT_RUN(TC_rt_memcmp_str);
UTEST_UNIT_RUN(TC_rt_memcmp_int_array);
UTEST_UNIT_RUN(TC_rt_memcmp_float_array);
UTEST_UNIT_RUN(TC_rt_memcmp_struct_array);
UTEST_UNIT_RUN(TC_rt_memcmp_mixed_array);
UTEST_UNIT_RUN(TC_rt_memcmp_nested_struct_array);
UTEST_UNIT_RUN(TC_rt_memcmp_partial_match);
UTEST_UNIT_RUN(TC_rt_memcmp_large_array);
}

UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcmp", utest_tc_init, utest_tc_cleanup, 1000);
UTEST_TC_EXPORT(utest_do_tc, "klibc.rt_memcmp", RT_NULL, RT_NULL, 1000);
30 changes: 12 additions & 18 deletions src/klibc/utest/TC_rt_memcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,26 @@
* 2024-12-24 Meco Man port to utest
*/

#include <rtklibc.h>
#include <rtthread.h>
#include <utest.h>

#define N 80 /**< Define the constant N for buffer size as 80 */
#define TEST_BUF_SIZE 512 /**< Define the constant TEST_BUF_SIZE as 512 */
static char *buf; /**< Define a static buffer of 512 bytes, initialized to 0 */

static rt_err_t utest_tc_init(void)
{
buf = rt_malloc(TEST_BUF_SIZE * sizeof(char)); /**< Allocate memory for the buffer */
uassert_not_null(buf);
return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
rt_free(buf);
return RT_EOK;
}

#define N 80 /**< Define the constant N for buffer size as 80 */
static char buf[512] = {0}; /**< Define a static buffer of 512 bytes, initialized to 0 */

/**
* Align a given pointer to a 64-byte boundary.
* @param p The pointer to align.
* @return The aligned pointer.
*/
static void* aligned(void* p)
{
return (void*)(((intptr_t)p + 63) & -64);
}

/**
* Test memory copy with alignment.
* @param dalign The alignment offset for the destination buffer.
Expand All @@ -43,10 +37,10 @@ static void* aligned(void* p)
*/
static void test_align(unsigned dalign, unsigned salign, size_t len)
{
char* src = aligned(buf); /**< Source buffer starting address, 64-byte aligned */
char* dst = aligned(buf + 128); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
char* want = aligned(buf + 256); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
char* p; /**< Pointer to receive the return value of rt_memcpy */
char *src = (char *)RT_ALIGN((rt_ubase_t)buf, 64); /**< Source buffer starting address, 64-byte aligned */
char *dst = (char *)RT_ALIGN(((rt_ubase_t)buf + 128), 64); /**< Destination buffer starting address, 64-byte aligned from buf+128 */
char *want = (char *)RT_ALIGN(((rt_ubase_t)buf + 256), 64); /**< Expected result buffer starting address, 64-byte aligned from buf+256 */
char *p; /**< Pointer to receive the return value of rt_memcpy */
unsigned i;

/** Assert that the source alignment offset plus length does not exceed N */
Expand Down
Loading
Loading