1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "test_engine_impl.h"
4
5#include <mos_stdlib.h>
6#include <mos_string.h>
7
8MOS_TEST_CASE(test_lib_memcpy)
9{
10 char *src = kcalloc(nmemb: 500, size: sizeof(char));
11 char *dst = kcalloc(nmemb: 500, size: sizeof(char));
12
13 for (size_t i = 0; i < 500; i++)
14 src[i] = i;
15
16 memcpy(dest: dst, src, n: 500);
17
18 for (size_t i = 0; i < 500; i++)
19 MOS_TEST_CHECK(dst[i] == src[i], true);
20
21 kfree(ptr: src);
22 kfree(ptr: dst);
23}
24
25MOS_TEST_CASE(test_lib_memmove_simple)
26{
27 char *src = kcalloc(nmemb: 500, size: sizeof(char));
28 char *dst = kcalloc(nmemb: 500, size: sizeof(char));
29
30 for (size_t i = 0; i < 500; i++)
31 src[i] = i;
32
33 memmove(dest: dst, src, n: 500);
34 for (size_t i = 0; i < 500; i++)
35 MOS_TEST_CHECK(dst[i] == src[i], true);
36
37 kfree(ptr: src);
38 kfree(ptr: dst);
39}
40
41MOS_TEST_CASE(test_memmove_overlapped)
42{
43 size_t *src = kcalloc(nmemb: 500, size: sizeof(size_t));
44
45 for (size_t i = 0; i < 300; i++)
46 src[i] = i;
47
48 memmove(dest: src + 200, src, n: 300 * sizeof(size_t));
49
50 for (size_t i = 200; i < 500; i++)
51 MOS_TEST_CHECK(src[i], i - 200);
52
53 kfree(ptr: src);
54}
55
56MOS_TEST_CASE(test_memmove_overlapped_backwards)
57{
58 size_t *src = kcalloc(nmemb: 500, size: sizeof(size_t));
59
60 for (size_t i = 0; i < 500; i++)
61 src[i] = i;
62
63 // | 0 - 99 - 100 - 199 - 200 - 299| - 300 - 399 - 400 - 499
64 memmove(dest: src, src: src + 200, n: 300 * sizeof(size_t));
65 // |200 - 299 - 300 - 399 - 400 - 499| - 300 - 399 - 400 - 499
66
67 // the front 200 ~ 499 area
68 for (size_t i = 0; i < 300; i++)
69 MOS_TEST_CHECK(src[i], i + 200);
70
71 // 300 ~ 499 area
72 for (size_t i = 300; i < 500; i++)
73 MOS_TEST_CHECK(src[i], i);
74 kfree(ptr: src);
75}
76