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
|
/// \file memory.test.cc
/// \brief Memory Manager API tests.
/// \author Amlal El Mahrouss (amlal at nekernel dot org)
#include <libSystem/SystemKit/System.h>
#include <public/frameworks/KernelTest.fwrk/headers/TestCase.h>
/// \note Memory allocation tests
KT_DECL_TEST(MmCreateHeapSuccess, []() -> bool {
auto heap = MmCreateHeap(1024, 0);
if (!heap) return NO;
MmDestroyHeap(heap);
return YES;
});
KT_DECL_TEST(MmCreateHeapZeroSize, []() -> bool {
auto heap = MmCreateHeap(0, 0);
return heap == nullptr;
});
KT_DECL_TEST(MmDestroyHeapValid, []() -> bool {
auto heap = MmCreateHeap(1024, 0);
if (!heap) return NO;
return MmDestroyHeap(heap) == 0;
});
KT_DECL_TEST(MmDestroyHeapNull, []() -> bool { return MmDestroyHeap(nullptr) != 0; });
/// \note Memory operations tests
KT_DECL_TEST(MmCopyMemoryNonOverlapping, []() -> bool {
char src[16] = "Hello, World!";
char dst[16] = {0};
auto result = MmCopyMemory(dst, src, 13);
if (!result) return NO;
return MmStrCmp(src, dst) == 0;
});
KT_DECL_TEST(MmCopyMemoryNull,
[]() -> bool { return MmCopyMemory(nullptr, nullptr, 10) == nullptr; });
KT_DECL_TEST(MmFillMemoryPattern, []() -> bool {
char buffer[16] = {0};
MmFillMemory(buffer, 16, 0xAA);
for (int i = 0; i < 16; i++) {
if ((unsigned char) buffer[i] != 0xAA) return NO;
}
return YES;
});
KT_DECL_TEST(MmFillMemoryNull, []() -> bool { return MmFillMemory(nullptr, 10, 0xFF) == nullptr; });
KT_DECL_TEST(MmCmpMemoryEqual, []() -> bool {
char buf1[8] = {1, 2, 3, 4, 5, 6, 7, 8};
char buf2[8] = {1, 2, 3, 4, 5, 6, 7, 8};
return MmCmpMemory(buf1, buf2, 8) == 0;
});
KT_DECL_TEST(MmCmpMemoryNotEqual, []() -> bool {
char buf1[8] = {1, 2, 3, 4, 5, 6, 7, 8};
char buf2[8] = {1, 2, 3, 9, 5, 6, 7, 8};
return MmCmpMemory(buf1, buf2, 8) != 0;
});
/// \note String operations tests
KT_DECL_TEST(MmStrLenNonEmpty, []() -> bool {
const char* str = "Hello";
return MmStrLen(str) == 5;
});
KT_DECL_TEST(MmStrLenEmpty, []() -> bool {
const char* str = "";
return MmStrLen(str) == 0;
});
KT_DECL_TEST(MmStrLenNull, []() -> bool { return MmStrLen(nullptr) < 0; });
KT_DECL_TEST(MmStrCmpEqual, []() -> bool { return MmStrCmp("test", "test") == 0; });
KT_DECL_TEST(MmStrCmpNotEqual, []() -> bool { return MmStrCmp("test", "fest") != 0; });
/// \note Heap flags tests
KT_DECL_TEST(MmSetGetHeapFlags, []() -> bool {
auto heap = MmCreateHeap(1024, 0x01);
if (!heap) return NO;
MmSetHeapFlags(heap, 0x02);
UInt32 flags = MmGetHeapFlags(heap);
MmDestroyHeap(heap);
return flags == 0x02;
});
KT_DECL_TEST(MmFillCRC32HeapValid, []() -> bool {
auto heap = MmCreateHeap(1024, 0);
if (!heap) return NO;
UInt32 crc = MmFillCRC32Heap(heap);
MmDestroyHeap(heap);
return crc != 0;
});
/// \brief Run memory tests.
IMPORT_C SInt32 KT_TEST_MAIN() {
KT_RUN_TEST(MmCreateHeapSuccess);
KT_RUN_TEST(MmCreateHeapZeroSize);
KT_RUN_TEST(MmDestroyHeapValid);
KT_RUN_TEST(MmDestroyHeapNull);
KT_RUN_TEST(MmCopyMemoryNonOverlapping);
KT_RUN_TEST(MmCopyMemoryNull);
KT_RUN_TEST(MmFillMemoryPattern);
KT_RUN_TEST(MmFillMemoryNull);
KT_RUN_TEST(MmCmpMemoryEqual);
KT_RUN_TEST(MmCmpMemoryNotEqual);
KT_RUN_TEST(MmStrLenNonEmpty);
KT_RUN_TEST(MmStrLenEmpty);
KT_RUN_TEST(MmStrLenNull);
KT_RUN_TEST(MmStrCmpEqual);
KT_RUN_TEST(MmStrCmpNotEqual);
KT_RUN_TEST(MmSetGetHeapFlags);
KT_RUN_TEST(MmFillCRC32HeapValid);
return KT_TEST_SUCCESS;
}
|