Raw ↗
+ +
9a12521 fix(ci): resolve windows strcasecmp linkage and macos test failures
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange Library
6 * test/openavx_platform_test.c - Cross-Platform Portability Layer Verification Suite
7 */
8  
9 #include "avx_platform.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14 #include <math.h>
15  
16 #define ASSERT_TRUE(cond, msg) do { \
17 if (!(cond)) { \
18 fprintf(stderr, "[FAILED] %s (line %d): %s\n", __FILE__, __LINE__, msg); \
19 exit(1); \
20 } \
21 } while(0)
22  
23 /* Worker thread context for mutex & condvar test */
24 typedef struct {
25 avx_mutex_t lock;
26 avx_cond_t cond;
27 bool ready;
28 int counter;
29 } thread_test_ctx_t;
30  
31 static void* thread_worker(void* arg) {
32 thread_test_ctx_t* ctx = (thread_test_ctx_t*)arg;
33  
34 /* Sleep briefly to verify asynchronous scheduling */
35 avx_sleep_ms(15);
36  
37 avx_mutex_lock(&ctx->lock);
38 ctx->counter += 42;
39 ctx->ready = true;
40 avx_cond_signal(&ctx->cond);
41 avx_mutex_unlock(&ctx->lock);
42  
43 return NULL;
44 }
45  
46 int main(void) {
47 printf("=== OpenAVX Platform Abstraction Tests ===\n\n");
48  
49 /* 1. Platform & Architecture Identification */
50 printf("[*] 1. Verifying Platform & Architecture Detection...\n");
51 printf(" [+] Target OS: %s\n", AVX_PLATFORM_NAME);
52 printf(" [+] Target Architecture: %s\n", AVX_ARCH_NAME);
53 printf(" [+] Path Separator: '%c'\n", AVX_PATH_SEP_CHAR);
54  
55 #if defined(AVX_OS_LINUX)
56 ASSERT_TRUE(strcmp(AVX_PLATFORM_NAME, "Linux") == 0, "AVX_OS_LINUX defined but name mismatch");
57 #elif defined(AVX_OS_MACOS)
58 ASSERT_TRUE(strcmp(AVX_PLATFORM_NAME, "macOS") == 0, "AVX_OS_MACOS defined but name mismatch");
59 #elif defined(AVX_OS_WINDOWS)
60 ASSERT_TRUE(strcmp(AVX_PLATFORM_NAME, "Windows") == 0, "AVX_OS_WINDOWS defined but name mismatch");
61 #elif defined(AVX_OS_BSD)
62 ASSERT_TRUE(strcmp(AVX_PLATFORM_NAME, "BSD") == 0, "AVX_OS_BSD defined but name mismatch");
63 #endif
64  
65 /* 2. Sockets Subsystem */
66 printf("[*] 2. Testing Cross-Platform Sockets Subsystem...\n");
67 bool sock_init = avx_platform_sockets_init();
68 ASSERT_TRUE(sock_init, "avx_platform_sockets_init failed");
69  
70 avx_socket_t srv = socket(AF_INET, SOCK_STREAM, 0);
71 ASSERT_TRUE(srv != AVX_INVALID_SOCKET, "Server socket creation failed");
72  
73 int opt = 1;
74 setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
75 avx_socket_set_nosigpipe(srv);
76  
77 struct sockaddr_in srv_addr;
78 memset(&srv_addr, 0, sizeof(srv_addr));
79 srv_addr.sin_family = AF_INET;
80 srv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
81 srv_addr.sin_port = 0; /* ephemeral port */
82  
83 int b_res = bind(srv, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
84 ASSERT_TRUE(b_res == 0, "bind() failed");
85  
86 avx_socklen_t srv_len = sizeof(srv_addr);
87 getsockname(srv, (struct sockaddr*)&srv_addr, &srv_len);
88 uint16_t port = ntohs(srv_addr.sin_port);
89  
90 listen(srv, 1);
91 printf(" [+] Server bound to 127.0.0.1:%u\n", port);
92  
93 /* Connect client socket */
94 avx_socket_t cli = socket(AF_INET, SOCK_STREAM, 0);
95 ASSERT_TRUE(cli != AVX_INVALID_SOCKET, "Client socket creation failed");
96 avx_socket_set_nosigpipe(cli);
97  
98 struct sockaddr_in cli_addr;
99 memset(&cli_addr, 0, sizeof(cli_addr));
100 cli_addr.sin_family = AF_INET;
101 cli_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
102 cli_addr.sin_port = htons(port);
103  
104 int c_res = connect(cli, (struct sockaddr*)&cli_addr, sizeof(cli_addr));
105 ASSERT_TRUE(c_res == 0, "connect() failed");
106  
107 /* Accept connection */
108 avx_socket_t conn = accept(srv, NULL, NULL);
109 ASSERT_TRUE(conn != AVX_INVALID_SOCKET, "accept() failed");
110 avx_socket_set_nosigpipe(conn);
111  
112 /* Test TCP Keepalive and TCP_NODELAY */
113 int ka_res = avx_socket_set_keepalive(conn, 2, 1, 3);
114 ASSERT_TRUE(ka_res == 0, "avx_socket_set_keepalive() failed");
115 int nd_res = avx_socket_set_tcp_nodelay(conn, true);
116 ASSERT_TRUE(nd_res == 0, "avx_socket_set_tcp_nodelay() failed");
117  
118 int opt_val = 0;
119 avx_socklen_t opt_len = sizeof(opt_val);
120 ASSERT_TRUE(getsockopt(conn, SOL_SOCKET, SO_KEEPALIVE, (char*)&opt_val, &opt_len) == 0 && opt_val != 0,
121 "SO_KEEPALIVE not enabled on socket");
122  
123 opt_val = 0;
124 opt_len = sizeof(opt_val);
125 ASSERT_TRUE(getsockopt(conn, IPPROTO_TCP, TCP_NODELAY, (char*)&opt_val, &opt_len) == 0 && opt_val != 0,
126 "TCP_NODELAY not enabled on socket");
127 printf(" [+] Socket TCP Keepalive and TCP_NODELAY verified.\n");
128  
129 /* Test non-blocking mode */
130 int nb_res = avx_socket_set_nonblocking(conn, true);
131 ASSERT_TRUE(nb_res == 0, "avx_socket_set_nonblocking() failed");
132  
133 /* Test poll abstraction */
134 avx_pollfd_t pfd;
135 pfd.fd = conn;
136 pfd.events = POLLIN;
137 pfd.revents = 0;
138 int pr = avx_poll(&pfd, 1, 10);
139 ASSERT_TRUE(pr == 0, "avx_poll() should have timed out with 0 events");
140  
141 /* Send data with MSG_NOSIGNAL */
142 const char msg[] = "OPENAVX_PLATFORM_PING";
143 ssize_t sent = send(cli, msg, sizeof(msg), MSG_NOSIGNAL);
144 ASSERT_TRUE(sent == (ssize_t)sizeof(msg), "send() with MSG_NOSIGNAL failed");
145  
146 /* Poll again: should now be readable */
147 pr = avx_poll(&pfd, 1, 100);
148 ASSERT_TRUE(pr > 0 && (pfd.revents & POLLIN), "avx_poll() should signal POLLIN");
149  
150 char buf[64] = {0};
151 ssize_t recvd = recv(conn, buf, sizeof(buf), 0);
152 ASSERT_TRUE(recvd == (ssize_t)sizeof(msg), "recv() failed");
153 ASSERT_TRUE(strcmp(buf, msg) == 0, "Payload data mismatch");
154 printf(" [+] Socket loopback exchange succeeded with MSG_NOSIGNAL!\n");
155  
156 avx_socket_close(conn);
157 avx_socket_close(cli);
158 avx_socket_close(srv);
159 avx_platform_sockets_cleanup();
160  
161 /* 3. Threading & Synchronization */
162 printf("[*] 3. Testing Threading & Synchronization Primitives...\n");
163 thread_test_ctx_t tctx;
164 memset(&tctx, 0, sizeof(tctx));
165 avx_mutex_init(&tctx.lock);
166 avx_cond_init(&tctx.cond);
167  
168 /* Test timedwait timeout */
169 avx_mutex_lock(&tctx.lock);
170 int tw_res = avx_cond_timedwait(&tctx.cond, &tctx.lock, 10);
171 ASSERT_TRUE(tw_res != 0, "timedwait should have timed out");
172 avx_mutex_unlock(&tctx.lock);
173  
174 /* Launch worker thread */
175 avx_thread_t th;
176 int cr_res = avx_thread_create(&th, thread_worker, &tctx);
177 ASSERT_TRUE(cr_res == 0, "avx_thread_create() failed");
178  
179 avx_mutex_lock(&tctx.lock);
180 while (!tctx.ready) {
181 avx_cond_wait(&tctx.cond, &tctx.lock);
182 }
183 ASSERT_TRUE(tctx.counter == 42, "Worker thread computation mismatch");
184 avx_mutex_unlock(&tctx.lock);
185  
186 avx_thread_join(th);
187 avx_mutex_destroy(&tctx.lock);
188 avx_cond_destroy(&tctx.cond);
189 printf(" [+] Mutex, Condvar, and Thread Create/Join verified!\n");
190  
191 /* 4. High-Resolution Clocks & Timers */
192 printf("[*] 4. Testing High-Resolution Clocks & Timers...\n");
193 int64_t t0 = avx_time_ms();
194 int64_t t0_100ns = avx_time_100ns();
195 ASSERT_TRUE(t0 > 0, "avx_time_ms returned <= 0");
196 ASSERT_TRUE(t0_100ns > 0, "avx_time_100ns returned <= 0");
197  
198 avx_sleep_ms(30);
199  
200 int64_t t1 = avx_time_ms();
201 int64_t elapsed = t1 - t0;
202 ASSERT_TRUE(elapsed >= 25 && elapsed <= 500, "avx_sleep_ms(30) elapsed outside tolerance");
203 printf(" [+] avx_sleep_ms(30) measured %lld ms (within tolerance)\n", (long long)elapsed);
204  
205 /* 5. Dynamic Library Loading */
206 printf("[*] 5. Testing Dynamic Library Loading...\n");
207 #if defined(AVX_OS_WINDOWS)
208 void* handle = avx_dlopen("kernel32.dll");
209 ASSERT_TRUE(handle != NULL, "Failed to open kernel32.dll");
210 void* sym = avx_dlsym(handle, "GetTickCount64");
211 ASSERT_TRUE(sym != NULL, "Failed to resolve GetTickCount64");
212 avx_dlclose(handle);
213 #elif defined(AVX_OS_MACOS)
214 void* handle = avx_dlopen("libSystem.B.dylib");
215 if (!handle) handle = avx_dlopen("/usr/lib/libSystem.B.dylib");
216 ASSERT_TRUE(handle != NULL, "Failed to open libSystem.B.dylib");
217 void* sym = avx_dlsym(handle, "sin");
218 ASSERT_TRUE(sym != NULL, "Failed to resolve sin");
219 avx_dlclose(handle);
220 #else
221 void* handle = avx_dlopen("libm.so.6");
222 if (!handle) handle = avx_dlopen("libm.so");
223 ASSERT_TRUE(handle != NULL, "Failed to open libm.so");
224 void* sym = avx_dlsym(handle, "sin");
225 ASSERT_TRUE(sym != NULL, "Failed to resolve sin");
226 double (*my_sin)(double) = (double (*)(double))sym;
227 ASSERT_TRUE(fabs(my_sin(0.0)) < 0.0001, "Resolved sin(0) != 0");
228 avx_dlclose(handle);
229 #endif
230 printf(" [+] Dynamic library loading and symbol resolution verified!\n");
231  
232 /* 6. Cache-line / DMA-safe 64-Byte Aligned Allocation */
233 printf("[*] 6. Testing 64-Byte Cache-Line Aligned Memory Allocation...\n");
234 void* aligned_ptr = avx_aligned_alloc(64, 4096);
235 ASSERT_TRUE(aligned_ptr != NULL, "avx_aligned_alloc(64, 4096) returned NULL");
236 ASSERT_TRUE(((uintptr_t)aligned_ptr % 64) == 0, "Allocated pointer is not 64-byte aligned");
237 memset(aligned_ptr, 0x5A, 4096);
238 ASSERT_TRUE(((uint8_t*)aligned_ptr)[0] == 0x5A && ((uint8_t*)aligned_ptr)[4095] == 0x5A, "Aligned memory read/write check failed");
239 avx_aligned_free(aligned_ptr);
240 printf(" [+] 64-byte aligned allocation and deallocation verified!\n");
241  
242 printf("\n[PASS] openavx_platform_test\n");
243  
244 return 0;
245 }