Skip to content

Commit 7a550d0

Browse files
committed
ut: Reuse kstdlib tests for libcm string functions
1 parent 06a3d6f commit 7a550d0

File tree

2 files changed

+78
-167
lines changed

2 files changed

+78
-167
lines changed

src/unittests/cm/string_test.c

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/unittests/cm/string_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../kernel/kstdlib_test.c

src/unittests/kernel/kstdlib_test.c

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
#include <stdbool.h>
22
#include <types.h>
3-
#include <kstdlib.h>
43
#include <utils.h>
54
#include <unittest/unittest.h>
6-
#include <mock/kernel/paging.h>
7-
8-
extern bool panic_invoked;
95

6+
#ifdef LIBCM
7+
#include <cm/cm.h>
8+
9+
#define MEMSET_FN_UNDER_TEST cm_memset
10+
#define MEMCPY_FN_UNDER_TEST cm_memcpy
11+
#define STRLEN_FN_UNDER_TEST cm_strlen
12+
#define STRCPY_FN_UNDER_TEST cm_strncpy
13+
#else
14+
#include <kstdlib.h>
15+
#include <mock/kernel/paging.h>
16+
17+
#define MEMSET_FN_UNDER_TEST k_memset
18+
#define MEMCPY_FN_UNDER_TEST k_memcpy
19+
#define STRLEN_FN_UNDER_TEST k_strlen
20+
#define STRCPY_FN_UNDER_TEST k_strncpy
21+
#endif
22+
23+
#ifndef LIBCM
24+
// The corresponding functions are not available in libcm. So following tests are only for the
25+
// Kernel.
1026
TEST (MEM, memcmp_one_byte)
1127
{
1228
U8 dest[1] = { 10 };
@@ -43,49 +59,6 @@ TEST (MEM, memcmp_mismatch_within_range)
4359
END();
4460
}
4561

46-
TEST(MEM, memset_one_byte)
47-
{
48-
U8 dest;
49-
EQ_SCALAR((PTR)k_memset (&dest, 0x1A, 1), (PTR)&dest);
50-
EQ_SCALAR(dest, 0x1A);
51-
END();
52-
}
53-
54-
TEST(STRING, string_length)
55-
{
56-
EQ_SCALAR(k_strlen(""), 0);
57-
EQ_SCALAR(k_strlen("ABCD"), 4);
58-
EQ_SCALAR(k_strlen("A\n\tB"), 4);
59-
END();
60-
}
61-
62-
TEST(STRING, string_copy)
63-
{
64-
char destination[10];
65-
char *source = NULL;
66-
67-
// Copy empty string
68-
source = "";
69-
EQ_SCALAR((PTR)k_strncpy(destination,source,ARRAY_LENGTH(destination)), (PTR)destination);
70-
EQ_STRING(source, destination);
71-
72-
// Copy < size of destination
73-
source = "ABC";
74-
EQ_SCALAR((PTR)k_strncpy(destination,source,ARRAY_LENGTH(destination)), (PTR)destination);
75-
EQ_STRING(source, destination);
76-
77-
//// Copy = size of destination
78-
source = "123456789";
79-
EQ_SCALAR((PTR)k_strncpy(destination,source,ARRAY_LENGTH(destination)), (PTR)destination);
80-
EQ_STRING(source, destination);
81-
82-
//// Copy > size of destination
83-
//source = "123456789ABCD";
84-
EQ_SCALAR((PTR)k_strncpy(destination,source,ARRAY_LENGTH(destination)), (PTR)destination);
85-
EQ_STRING("123456789", destination); // Not more than size of destination is copied.
86-
END();
87-
}
88-
8962
TEST (MEMSET_PAT4, memset_pat4_2_bytes_pat)
9063
{
9164
U32 pattern = 0x10FF;
@@ -124,6 +97,54 @@ TEST (MEMSET_PAT4, memset_pat4_4_bytes_pat)
12497

12598
END();
12699
}
100+
#endif
101+
102+
TEST(MEM, memset_one_byte)
103+
{
104+
U8 dest;
105+
EQ_SCALAR((PTR)MEMSET_FN_UNDER_TEST (&dest, 0x1A, 1), (PTR)&dest);
106+
EQ_SCALAR(dest, 0x1A);
107+
END();
108+
}
109+
110+
TEST(STRING, string_length)
111+
{
112+
EQ_SCALAR(STRLEN_FN_UNDER_TEST(""), 0);
113+
EQ_SCALAR(STRLEN_FN_UNDER_TEST("ABCD"), 4);
114+
EQ_SCALAR(STRLEN_FN_UNDER_TEST("A\n\tB"), 4);
115+
END();
116+
}
117+
118+
TEST (STRING, string_copy)
119+
{
120+
char destination[10];
121+
char* source = NULL;
122+
123+
// Copy empty string
124+
source = "";
125+
EQ_SCALAR ((PTR)STRCPY_FN_UNDER_TEST (destination, source, ARRAY_LENGTH (destination)),
126+
(PTR)destination);
127+
EQ_STRING (source, destination);
128+
129+
// Copy < size of destination
130+
source = "ABC";
131+
EQ_SCALAR ((PTR)STRCPY_FN_UNDER_TEST (destination, source, ARRAY_LENGTH (destination)),
132+
(PTR)destination);
133+
EQ_STRING (source, destination);
134+
135+
//// Copy = size of destination
136+
source = "123456789";
137+
EQ_SCALAR ((PTR)STRCPY_FN_UNDER_TEST (destination, source, ARRAY_LENGTH (destination)),
138+
(PTR)destination);
139+
EQ_STRING (source, destination);
140+
141+
//// Copy > size of destination
142+
// source = "123456789ABCD";
143+
EQ_SCALAR ((PTR)STRCPY_FN_UNDER_TEST (destination, source, ARRAY_LENGTH (destination)),
144+
(PTR)destination);
145+
EQ_STRING ("123456789", destination); // Not more than size of destination is copied.
146+
END();
147+
}
127148

128149
TEST(MEM, memcpy_overlap)
129150
{
@@ -139,7 +160,7 @@ TEST(MEM, memcpy_overlap)
139160
CHAR *src = &memory[0];
140161
CHAR *dest = &memory[5];
141162

142-
EQ_SCALAR((PTR)k_memcpy (dest, src, 10), (PTR)dest);
163+
EQ_SCALAR((PTR)MEMCPY_FN_UNDER_TEST (dest, src, 10), (PTR)dest);
143164

144165
for (int i = 0; i < 10; i++)
145166
EQ_SCALAR(dest[i], i + 1); // Dest[0 to 9] has 1 to 10
@@ -157,7 +178,7 @@ TEST(MEM, memcpy_normal)
157178
CHAR *src = "123456789ABCDEF0";
158179
CHAR dest[10];
159180

160-
EQ_SCALAR((PTR)k_memcpy (dest, src, ARRAY_LENGTH(dest)), (PTR)&dest);
181+
EQ_SCALAR((PTR)MEMCPY_FN_UNDER_TEST (dest, src, ARRAY_LENGTH(dest)), (PTR)&dest);
161182

162183
for (int i = 0; i < ARRAY_LENGTH(dest); i++)
163184
EQ_SCALAR(dest[i], src[i]);
@@ -169,7 +190,7 @@ TEST(MEM, memset_normal)
169190
{
170191
U8 dest[10];
171192

172-
EQ_SCALAR((PTR)k_memset (dest, 0x1A, ARRAY_LENGTH(dest)), (PTR)&dest);
193+
EQ_SCALAR((PTR)MEMSET_FN_UNDER_TEST (dest, 0x1A, ARRAY_LENGTH(dest)), (PTR)&dest);
173194

174195
for (int i = 0; i < ARRAY_LENGTH(dest); i++)
175196
EQ_SCALAR(dest[i], 0x1A);
@@ -179,7 +200,9 @@ TEST(MEM, memset_normal)
179200

180201
void reset()
181202
{
203+
#ifndef LIBCM
182204
resetPagingFake();
205+
#endif
183206
}
184207

185208
int main()
@@ -188,13 +211,15 @@ int main()
188211
memset_normal();
189212
memcpy_normal();
190213
memcpy_overlap();
214+
#ifndef LIBCM
191215
memset_pat4_2_bytes_pat();
192216
memset_pat4_3_bytes_pat();
193217
memset_pat4_4_bytes_pat();
194-
string_length();
195-
string_copy();
196218
memcmp_one_byte();
197219
memcmp_empty();
198220
memcmp_mismatch_outside_range();
199221
memcmp_mismatch_within_range();
222+
#endif
223+
string_length();
224+
string_copy();
200225
}

0 commit comments

Comments
 (0)