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
|
/// \file thread.test.cc
/// \brief Threading 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 Thread procedure for testing
static SInt32 test_thread_proc(SInt32 argc, Char** argv) {
return 0;
}
static SInt32 test_thread_proc_with_arg(SInt32 argc, Char** argv) {
if (argc > 0 && argv) {
return 1;
}
return 0;
}
/// \note Thread creation tests
KT_DECL_TEST(ThrCreateThreadValid, []() -> bool {
auto thread = ThrCreateThread("test_thread", test_thread_proc, 0, 0);
if (!thread) return NO;
ThrJoinThread(thread);
return YES;
});
KT_DECL_TEST(ThrCreateThreadNull, []() -> bool {
auto thread = ThrCreateThread(nullptr, nullptr, 0, 0);
return thread == nullptr;
});
KT_DECL_TEST(ThrCreateThreadWithName, []() -> bool {
auto thread = ThrCreateThread("named_thread", test_thread_proc, 0, 0);
if (!thread) return NO;
ThrJoinThread(thread);
return YES;
});
/// \note Thread lifecycle tests
KT_DECL_TEST(ThrJoinThreadValid, []() -> bool {
auto thread = ThrCreateThread("join_test", test_thread_proc, 0, 0);
if (!thread) return NO;
SInt32 result = ThrJoinThread(thread);
return result == 0;
});
KT_DECL_TEST(ThrDetachThreadValid, []() -> bool {
auto thread = ThrCreateThread("detach_test", test_thread_proc, 0, 0);
if (!thread) return NO;
SInt32 result = ThrDetachThread(thread);
return result == 0;
});
KT_DECL_TEST(ThrYieldThreadValid, []() -> bool {
auto thread = ThrCreateThread("yield_test", test_thread_proc, 0, 0);
if (!thread) return NO;
SInt32 result = ThrYieldThread(thread);
ThrJoinThread(thread);
return result == 0;
});
/// \note Thread exit tests
KT_DECL_TEST(ThrExitThreadValid, []() -> bool {
auto thread = ThrCreateThread("exit_test", test_thread_proc, 0, 0);
if (!thread) return NO;
SInt32 result = ThrExitThread(thread, 0);
return result == 0;
});
KT_DECL_TEST(ThrExitThreadWithCode, []() -> bool {
auto thread = ThrCreateThread("exit_code_test", test_thread_proc, 0, 0);
if (!thread) return NO;
SInt32 result = ThrExitThread(thread, 42);
return result == 0;
});
/// \note Thread argument tests
KT_DECL_TEST(ThrCreateThreadWithArgs, []() -> bool {
auto thread = ThrCreateThread("args_test", test_thread_proc_with_arg, 1, 0);
if (!thread) return NO;
ThrJoinThread(thread);
return YES;
});
KT_DECL_TEST(ThrCreateThreadMultiple, []() -> bool {
auto thread1 = ThrCreateThread("multi_test_1", test_thread_proc, 0, 0);
auto thread2 = ThrCreateThread("multi_test_2", test_thread_proc, 0, 0);
if (!thread1 || !thread2) return NO;
ThrJoinThread(thread1);
ThrJoinThread(thread2);
return YES;
});
/// \note Current thread tests
KT_DECL_TEST(ThrExitCurrentThreadValid, []() -> bool {
// Note: Can't directly test this as it would exit the test thread
// This is a placeholder for integration testing
return YES;
});
KT_DECL_TEST(ThrExitMainThreadValid, []() -> bool {
// Note: Can't directly test this as it would exit the main thread
// This is a placeholder for integration testing
return YES;
});
/// \brief Run threading tests.
SInt32 KT_TEST_MAIN() {
KT_RUN_TEST(ThrCreateThreadValid);
KT_RUN_TEST(ThrCreateThreadNull);
KT_RUN_TEST(ThrCreateThreadWithName);
KT_RUN_TEST(ThrJoinThreadValid);
KT_RUN_TEST(ThrDetachThreadValid);
KT_RUN_TEST(ThrYieldThreadValid);
KT_RUN_TEST(ThrExitThreadValid);
KT_RUN_TEST(ThrExitThreadWithCode);
KT_RUN_TEST(ThrCreateThreadWithArgs);
KT_RUN_TEST(ThrCreateThreadMultiple);
KT_RUN_TEST(ThrExitCurrentThreadValid);
KT_RUN_TEST(ThrExitMainThreadValid);
return KT_TEST_SUCCESS;
}
|