summaryrefslogtreecommitdiff
path: root/test/stdlib.c
blob: cd018af09ba07a05d757a846e3b19f7207473128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Copyright 2022 The ChromiumOS Authors.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test standard library functions.
 */

#include "common.h"
#include "console.h"
#include "system.h"
#include "printf.h"
#include "shared_mem.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

static int test_isalpha(void)
{
	TEST_CHECK(isalpha('a'));
	TEST_CHECK(isalpha('z'));
	TEST_CHECK(isalpha('A'));
	TEST_CHECK(isalpha('Z'));
	TEST_CHECK(!isalpha('0'));
	TEST_CHECK(!isalpha('~'));
	TEST_CHECK(!isalpha(' '));
	TEST_CHECK(!isalpha('\0'));
	TEST_CHECK(!isalpha('\n'));
}

static int test_isprint(void)
{
	TEST_CHECK(isprint('a'));
	TEST_CHECK(isprint('z'));
	TEST_CHECK(isprint('A'));
	TEST_CHECK(isprint('Z'));
	TEST_CHECK(isprint('0'));
	TEST_CHECK(isprint('~'));
	TEST_CHECK(isprint(' '));
	TEST_CHECK(!isprint('\0'));
	TEST_CHECK(!isprint('\n'));
}

static int test_strstr(void)
{
	const char s1[] = "abcde";

	TEST_ASSERT(strstr(s1, "ab") == s1);
	TEST_ASSERT(strstr(s1, "") == NULL);
	TEST_ASSERT(strstr("", "ab") == NULL);
	TEST_ASSERT(strstr("", "x") == NULL);
	TEST_ASSERT(strstr(s1, "de") == &s1[3]);
	TEST_ASSERT(strstr(s1, "def") == NULL);

	return EC_SUCCESS;
}

static int test_strtoull(void)
{
	char *e;

	TEST_ASSERT(strtoull("10", &e, 0) == 10);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("010", &e, 0) == 8);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+010", &e, 0) == 8);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-010", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0x1f z", &e, 0) == 31);
	TEST_ASSERT(e && (*e == ' '));
	TEST_ASSERT(strtoull("0X1f z", &e, 0) == 31);
	TEST_ASSERT(e && (*e == ' '));
	TEST_ASSERT(strtoull("10a", &e, 16) == 266);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("0x02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0x02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0x02C", &e, 16) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0x02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0x02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0x02C", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0X02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0X02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0X02C", &e, 16) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0X02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0X02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0X02C", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("   -12", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0x!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0X!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));

	return EC_SUCCESS;
}

static int test_strncpy(void)
{
	char dest[10];

	strncpy(dest, "test", 10);
	TEST_ASSERT_ARRAY_EQ("test", dest, 5);
	strncpy(dest, "12345", 6);
	TEST_ASSERT_ARRAY_EQ("12345", dest, 6);
	/*
	 * gcc complains:
	 * error: ‘__builtin_strncpy’ output truncated copying 10 bytes from a
	 * string of length 12 [-Werror=stringop-truncation]
	 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
	strncpy(dest, "testtesttest", 10);
#pragma GCC diagnostic pop
	TEST_ASSERT_ARRAY_EQ("testtestte", dest, 10);

	return EC_SUCCESS;
}

static int test_strncmp(void)
{
	TEST_ASSERT(strncmp("123", "123", 8) == 0);
	TEST_ASSERT(strncmp("789", "456", 8) > 0);
	TEST_ASSERT(strncmp("abc", "abd", 4) < 0);
	TEST_ASSERT(strncmp("abc", "abd", 2) == 0);
	return EC_SUCCESS;
}

static int test_strlen(void)
{
	TEST_CHECK(strlen("this is a string") == 16);
}

static int test_strnlen(void)
{
	TEST_ASSERT(strnlen("this is a string", 17) == 16);
	TEST_ASSERT(strnlen("this is a string", 16) == 16);
	TEST_ASSERT(strnlen("this is a string", 5) == 5);

	return EC_SUCCESS;
}

static int test_strcasecmp(void)
{
	TEST_CHECK(strcasecmp("test string", "TEST strIng") == 0);
	TEST_CHECK(strcasecmp("test123!@#", "TesT123!@#") == 0);
	TEST_CHECK(strcasecmp("lower", "UPPER") != 0);
}

static int test_strncasecmp(void)
{
	TEST_CHECK(strncasecmp("test string", "TEST str", 4) == 0);
	TEST_CHECK(strncasecmp("test string", "TEST str", 8) == 0);
	TEST_CHECK(strncasecmp("test123!@#", "TesT321!@#", 5) != 0);
	TEST_CHECK(strncasecmp("test123!@#", "TesT321!@#", 4) == 0);
	TEST_CHECK(strncasecmp("1test123!@#", "1TesT321!@#", 5) == 0);
	TEST_CHECK(strncasecmp("1test123", "teststr", 0) == 0);
}

static int test_atoi(void)
{
	TEST_CHECK(atoi("  901") == 901);
	TEST_CHECK(atoi("-12c") == -12);
	TEST_CHECK(atoi("   0  ") == 0);
	TEST_CHECK(atoi("\t111") == 111);
}

static int test_snprintf(void)
{
	char buffer[32];

	TEST_CHECK(snprintf(buffer, sizeof(buffer), "%u", 1234) == 4);
	TEST_CHECK(strncmp(buffer, "1234", sizeof(buffer)));
}

static int test_strcspn(void)
{
	const char str1[] = "abc";
	const char str2[] = "This is a string\nwith newlines!";

	TEST_EQ(strcspn(str1, "a"), (size_t)0, "%zu");
	TEST_EQ(strcspn(str1, "b"), (size_t)1, "%zu");
	TEST_EQ(strcspn(str1, "c"), (size_t)2, "%zu");
	TEST_EQ(strcspn(str1, "ccc"), (size_t)2, "%zu");
	TEST_EQ(strcspn(str1, "cba"), (size_t)0, "%zu");
	TEST_EQ(strcspn(str1, "cb"), (size_t)1, "%zu");
	TEST_EQ(strcspn(str1, "bc"), (size_t)1, "%zu");
	TEST_EQ(strcspn(str1, "cbc"), (size_t)1, "%zu");
	TEST_EQ(strcspn(str1, "z"), strlen(str1), "%zu");
	TEST_EQ(strcspn(str1, "xyz"), strlen(str1), "%zu");
	TEST_EQ(strcspn(str1, ""), strlen(str1), "%zu");

	TEST_EQ(strcspn(str2, " "), (size_t)4, "%zu");
	TEST_EQ(strcspn(str2, "\n"), (size_t)16, "%zu");
	TEST_EQ(strcspn(str2, "\n "), (size_t)4, "%zu");
	TEST_EQ(strcspn(str2, "!"), strlen(str2) - 1, "%zu");
	TEST_EQ(strcspn(str2, "z"), strlen(str2), "%zu");
	TEST_EQ(strcspn(str2, "z!"), strlen(str2) - 1, "%zu");

	return EC_SUCCESS;
}

static int test_memmove(void)
{
	int i;
	timestamp_t t0, t1, t2, t3;
	char *buf;
	const int buf_size = 1000;
	const int len = 400;
	const int iteration = 1000;

	TEST_ASSERT(shared_mem_acquire(buf_size, &buf) == EC_SUCCESS);

	for (i = 0; i < len; ++i)
		buf[i] = i & 0x7f;
	for (i = len; i < buf_size; ++i)
		buf[i] = 0;

	t0 = get_time();
	for (i = 0; i < iteration; ++i)
		memmove(buf + 101, buf, len); /* unaligned */
	t1 = get_time();
	TEST_ASSERT_ARRAY_EQ(buf + 101, buf, len);
	ccprintf(" (speed gain: %" PRId64 " ->", t1.val - t0.val);

	t2 = get_time();
	for (i = 0; i < iteration; ++i)
		memmove(buf + 100, buf, len); /* aligned */
	t3 = get_time();
	ccprintf(" %" PRId64 " us) ", t3.val - t2.val);
	TEST_ASSERT_ARRAY_EQ(buf + 100, buf, len);

	if (!IS_ENABLED(EMU_BUILD))
		TEST_ASSERT((t1.val - t0.val) > (t3.val - t2.val));

	/* Test small moves */
	memmove(buf + 1, buf, 1);
	TEST_ASSERT_ARRAY_EQ(buf + 1, buf, 1);
	memmove(buf + 5, buf, 4);
	memmove(buf + 1, buf, 4);
	TEST_ASSERT_ARRAY_EQ(buf + 1, buf + 5, 4);

	shared_mem_release(buf);
	return EC_SUCCESS;
}

static int test_memcpy(void)
{
	int i;
	timestamp_t t0, t1, t2, t3;
	char *buf;
	const int buf_size = 1000;
	const int len = 400;
	const int dest_offset = 500;
	const int iteration = 1000;

	TEST_ASSERT(shared_mem_acquire(buf_size, &buf) == EC_SUCCESS);

	for (i = 0; i < len; ++i)
		buf[i] = i & 0x7f;
	for (i = len; i < buf_size; ++i)
		buf[i] = 0;

	t0 = get_time();
	for (i = 0; i < iteration; ++i)
		memcpy(buf + dest_offset + 1, buf, len); /* unaligned */
	t1 = get_time();
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset + 1, buf, len);
	ccprintf(" (speed gain: %" PRId64 " ->", t1.val - t0.val);

	t2 = get_time();
	for (i = 0; i < iteration; ++i)
		memcpy(buf + dest_offset, buf, len); /* aligned */
	t3 = get_time();
	ccprintf(" %" PRId64 " us) ", t3.val - t2.val);
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset, buf, len);

	if (!IS_ENABLED(EMU_BUILD))
		TEST_ASSERT((t1.val - t0.val) > (t3.val - t2.val));

	memcpy(buf + dest_offset + 1, buf + 1, len - 1);
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset + 1, buf + 1, len - 1);

	/* Test small copies */
	memcpy(buf + dest_offset, buf, 1);
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset, buf, 1);
	memcpy(buf + dest_offset, buf, 4);
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset, buf, 4);
	memcpy(buf + dest_offset + 1, buf, 1);
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset + 1, buf, 1);
	memcpy(buf + dest_offset + 1, buf, 4);
	TEST_ASSERT_ARRAY_EQ(buf + dest_offset + 1, buf, 4);

	shared_mem_release(buf);
	return EC_SUCCESS;
}

/* Plain memset, used as a reference to measure speed gain */
static void *dumb_memset(void *dest, int c, int len)
{
	char *d = (char *)dest;
	while (len > 0) {
		*(d++) = c;
		len--;
	}
	return dest;
}

static int test_memset(void)
{
	int i;
	timestamp_t t0, t1, t2, t3;
	char *buf;
	const int buf_size = 1000;
	const int len = 400;
	const int iteration = 1000;

	TEST_ASSERT(shared_mem_acquire(buf_size, &buf) == EC_SUCCESS);

	t0 = get_time();
	for (i = 0; i < iteration; ++i)
		dumb_memset(buf, 1, len);
	t1 = get_time();
	TEST_ASSERT_MEMSET(buf, (char)1, len);
	ccprintf(" (speed gain: %" PRId64 " ->", t1.val - t0.val);

	t2 = get_time();
	for (i = 0; i < iteration; ++i)
		memset(buf, 1, len);
	t3 = get_time();
	TEST_ASSERT_MEMSET(buf, (char)1, len);
	ccprintf(" %" PRId64 " us) ", t3.val - t2.val);

	if (!IS_ENABLED(EMU_BUILD))
		TEST_ASSERT((t1.val - t0.val) > (t3.val - t2.val));

	memset(buf, 128, len);
	TEST_ASSERT_MEMSET(buf, (char)128, len);

	memset(buf, -2, len);
	TEST_ASSERT_MEMSET(buf, (char)-2, len);

	memset(buf + 1, 1, len - 2);
	TEST_ASSERT_MEMSET(buf + 1, (char)1, len - 2);

	shared_mem_release(buf);
	return EC_SUCCESS;
}

static int test_memchr(void)
{
	char *buf = "1234";

	TEST_ASSERT(memchr("123567890", '4', 8) == NULL);
	TEST_ASSERT(memchr("123", '3', 2) == NULL);
	TEST_ASSERT(memchr(buf, '3', 4) == buf + 2);
	TEST_ASSERT(memchr(buf, '4', 4) == buf + 3);
	return EC_SUCCESS;
}

void run_test(int argc, char **argv)
{
	test_reset();

	RUN_TEST(test_isalpha);
	RUN_TEST(test_isprint);
	RUN_TEST(test_strstr);
	RUN_TEST(test_strtoull);
	RUN_TEST(test_strncpy);
	RUN_TEST(test_strncmp);
	RUN_TEST(test_strlen);
	RUN_TEST(test_strnlen);
	RUN_TEST(test_strcasecmp);
	RUN_TEST(test_strncasecmp);
	RUN_TEST(test_atoi);
	RUN_TEST(test_snprintf);
	RUN_TEST(test_strcspn);
	RUN_TEST(test_memmove);
	RUN_TEST(test_memcpy);
	RUN_TEST(test_memset);
	RUN_TEST(test_memchr);

	test_print_result();
}