repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
opencode/test-c-skills/src/test-platform.c
Raw/*
* test-platform.c - Tests for c-platform skill examples
*
* Extracts all code examples from c-platform/SKILL.md and creates
* compilable test functions.
*/
#include "base.h"
// ============================================================================
// Context Cracking - Compiler Detection (from c-platform SKILL.md)
// ============================================================================
#if defined(__clang__)
#define COMPILER_CLANG 1
#define COMPILER_MSVC 0
#define COMPILER_GCC 0
#elif defined(_MSC_VER)
#define COMPILER_CLANG 0
#define COMPILER_MSVC 1
#define COMPILER_GCC 0
#elif defined(__GNUC__)
#define COMPILER_CLANG 0
#define COMPILER_MSVC 0
#define COMPILER_GCC 1
#else
#define COMPILER_CLANG 0
#define COMPILER_MSVC 0
#define COMPILER_GCC 0
#endif
// ============================================================================
// Context Cracking - OS Detection (from c-platform SKILL.md)
// ============================================================================
#if defined(_WIN32)
#define OS_WINDOWS 1
#define OS_LINUX 0
#define OS_MAC 0
#elif defined(__gnu_linux__) || defined(__linux__)
#define OS_WINDOWS 0
#define OS_LINUX 1
#define OS_MAC 0
#elif defined(__APPLE__) && defined(__MACH__)
#define OS_WINDOWS 0
#define OS_LINUX 0
#define OS_MAC 1
#else
#define OS_WINDOWS 0
#define OS_LINUX 0
#define OS_MAC 0
#endif
// ============================================================================
// Context Cracking - Architecture Detection (from c-platform SKILL.md)
// ============================================================================
#if defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64)
#define ARCH_X64 1
#define ARCH_X86 0
#define ARCH_ARM64 0
#elif defined(__i386__) || defined(_M_IX86)
#define ARCH_X64 0
#define ARCH_X86 1
#define ARCH_ARM64 0
#elif defined(__aarch64__) || defined(_M_ARM64)
#define ARCH_X64 0
#define ARCH_X86 0
#define ARCH_ARM64 1
#else
#define ARCH_X64 0
#define ARCH_X86 0
#define ARCH_ARM64 0
#endif
// ============================================================================
// Compiler Intrinsics - Inline Control (from c-platform SKILL.md)
// ============================================================================
#if COMPILER_MSVC
#define PLATFORM_force_inline __forceinline
#define PLATFORM_no_inline __declspec(noinline)
#elif COMPILER_CLANG || COMPILER_GCC
#define PLATFORM_force_inline __attribute__((always_inline)) static inline
#define PLATFORM_no_inline __attribute__((noinline))
#endif
// ============================================================================
// Compiler Intrinsics - Branch Prediction (from c-platform SKILL.md)
// ============================================================================
#if COMPILER_CLANG || COMPILER_GCC
#define PLATFORM_Expect(expr, val) __builtin_expect((expr), (val))
#else
#define PLATFORM_Expect(expr, val) (expr)
#endif
#define PLATFORM_Likely(expr) PLATFORM_Expect(expr, 1)
#define PLATFORM_Unlikely(expr) PLATFORM_Expect(expr, 0)
// ============================================================================
// Compiler Intrinsics - Atomics (from c-platform SKILL.md)
// ============================================================================
#if COMPILER_MSVC
#define PLATFORM_ins_atomic_u32_eval(x) (*(volatile U32 *)(x))
#define PLATFORM_ins_atomic_u32_inc(x) InterlockedIncrement((LONG *)(x))
#define PLATFORM_ins_atomic_u32_eval_assign(x, val) InterlockedExchange((LONG *)(x), (val))
#define PLATFORM_ins_atomic_u64_cas(x, cmp, val) InterlockedCompareExchange64((__int64 *)(x), (val), (cmp))
#elif COMPILER_CLANG || COMPILER_GCC
#define PLATFORM_ins_atomic_u32_eval(x) __atomic_load_n((U32 *)(x), __ATOMIC_SEQ_CST)
#define PLATFORM_ins_atomic_u32_inc(x) __atomic_fetch_add((U32 *)(x), 1, __ATOMIC_SEQ_CST)
#define PLATFORM_ins_atomic_u32_eval_assign(x, val) __atomic_exchange_n((U32 *)(x), (val), __ATOMIC_SEQ_CST)
#define PLATFORM_ins_atomic_u64_cas(x, cmp, val) __sync_val_compare_and_swap((U64 *)(x), (cmp), (val))
#endif
// ============================================================================
// Compiler Intrinsics - Thread-Local Storage (from c-platform SKILL.md)
// ============================================================================
#if COMPILER_MSVC
#define PLATFORM_thread_static __declspec(thread)
#elif COMPILER_CLANG || COMPILER_GCC
#define PLATFORM_thread_static __thread
#endif
// ============================================================================
// Compiler Intrinsics - Read-Only Data Section (from c-platform SKILL.md)
// ============================================================================
#if COMPILER_MSVC
#define PLATFORM_read_only __declspec(allocate(".rdata$"))
#elif COMPILER_CLANG || COMPILER_GCC
#define PLATFORM_read_only __attribute__((section(".rodata")))
#endif
// ============================================================================
// Endianness (from c-platform SKILL.md)
// ============================================================================
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define ARCH_LITTLE_ENDIAN 1
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define ARCH_LITTLE_ENDIAN 0
#else
/* Runtime detection or assume little endian */
#define ARCH_LITTLE_ENDIAN 1
#endif
// ============================================================================
// Byte Swapping Functions (from c-platform SKILL.md)
// ============================================================================
internal U16
bswap_u16(U16 x)
{
return (U16)((x >> 8) | (U16)(x << 8));
}
internal U32
bswap_u32(U32 x)
{
#if COMPILER_CLANG || COMPILER_GCC
return __builtin_bswap32(x);
#else
/* Portable fallback */
return ((x & 0x000000FFU) << 24) |
((x & 0x0000FF00U) << 8) |
((x & 0x00FF0000U) >> 8) |
((x & 0xFF000000U) >> 24);
#endif
}
internal U64
bswap_u64(U64 x)
{
#if COMPILER_CLANG || COMPILER_GCC
return __builtin_bswap64(x);
#else
/* Portable fallback */
return ((x & 0x00000000000000FFULL) << 56) |
((x & 0x000000000000FF00ULL) << 40) |
((x & 0x0000000000FF0000ULL) << 24) |
((x & 0x00000000FF000000ULL) << 8) |
((x & 0x000000FF00000000ULL) >> 8) |
((x & 0x0000FF0000000000ULL) >> 24) |
((x & 0x00FF000000000000ULL) >> 40) |
((x & 0xFF00000000000000ULL) >> 56);
#endif
}
// ============================================================================
// Opaque Handle (from c-platform SKILL.md)
// Using custom name to avoid conflict with base.h's OS_Handle
// ============================================================================
typedef struct OS_Handle_Skill OS_Handle_Skill;
struct OS_Handle_Skill
{
U64 u64[1]; // Wraps native handle (HANDLE on Windows, int on Linux)
};
internal OS_Handle_Skill
os_handle_zero_skill(void)
{
OS_Handle_Skill h = { 0 };
return h;
}
internal B32
os_handle_match_skill(OS_Handle_Skill a, OS_Handle_Skill b)
{
return a.u64[0] == b.u64[0];
}
// ============================================================================
// OS_SystemInfo (from c-platform SKILL.md)
// Using custom name to avoid conflict with base.h's OS_SystemInfo
// ============================================================================
typedef struct OS_SystemInfo_Skill OS_SystemInfo_Skill;
struct OS_SystemInfo_Skill
{
U64 page_size;
U64 large_page_size;
U64 core_count;
U64 memory_size;
};
// ============================================================================
// Big-Endian Conversion Macros (from c-platform SKILL.md)
// ============================================================================
#if ARCH_LITTLE_ENDIAN
#define from_be_u16(x) bswap_u16(x)
#define from_be_u32(x) bswap_u32(x)
#define from_be_u64(x) bswap_u64(x)
#define to_be_u16(x) bswap_u16(x)
#define to_be_u32(x) bswap_u32(x)
#define to_be_u64(x) bswap_u64(x)
#else
#define from_be_u16(x) (x)
#define from_be_u32(x) (x)
#define from_be_u64(x) (x)
#define to_be_u16(x) (x)
#define to_be_u32(x) (x)
#define to_be_u64(x) (x)
#endif
// ============================================================================
// Test Variables (Global Scope)
// ============================================================================
PLATFORM_thread_static U64 thread_local_counter = 0;
/* Global read-only table for testing */
#if COMPILER_CLANG || COMPILER_GCC
static const U8 test_rodata_hex_table[17] __attribute__((section(".rodata"))) = "0123456789ABCDEF";
#endif
// ============================================================================
// Helper Functions (File Scope)
// ============================================================================
/* Test PLATFORM_force_inline */
PLATFORM_force_inline int test_add_ints(int a, int b) {
return a + b;
}
/* Test PLATFORM_no_inline */
PLATFORM_no_inline int test_subtract_ints(int a, int b) {
return a - b;
}
/* Thread-local variable for testing (global scope required) */
PLATFORM_thread_static int test_thread_local_value = 0;
// ============================================================================
// Test Functions
// ============================================================================
internal void
test_context_cracking(void)
{
/* Test compiler detection macros are set */
int compiler_detected = 0;
#if COMPILER_CLANG
compiler_detected = 1;
(void)compiler_detected;
#endif
#if COMPILER_MSVC
compiler_detected = 1;
(void)compiler_detected;
#endif
#if COMPILER_GCC
compiler_detected = 1;
(void)compiler_detected;
#endif
Assert(compiler_detected);
/* Test OS detection macros are set */
int os_detected = 0;
#if OS_WINDOWS
os_detected = 1;
(void)os_detected;
#endif
#if OS_LINUX
os_detected = 1;
(void)os_detected;
#endif
#if OS_MAC
os_detected = 1;
(void)os_detected;
#endif
Assert(os_detected);
/* Test architecture detection macros are set */
int arch_detected = 0;
#if ARCH_X64
arch_detected = 1;
(void)arch_detected;
#endif
#if ARCH_X86
arch_detected = 1;
(void)arch_detected;
#endif
#if ARCH_ARM64
arch_detected = 1;
(void)arch_detected;
#endif
Assert(arch_detected);
LOG_INFO("Context cracking: COMPILER_CLANG=%d COMPILER_MSVC=%d COMPILER_GCC=%d",
COMPILER_CLANG, COMPILER_MSVC, COMPILER_GCC);
LOG_INFO("Context cracking: OS_WINDOWS=%d OS_LINUX=%d OS_MAC=%d",
OS_WINDOWS, OS_LINUX, OS_MAC);
LOG_INFO("Context cracking: ARCH_X64=%d ARCH_X86=%d ARCH_ARM64=%d",
ARCH_X64, ARCH_X86, ARCH_ARM64);
}
internal void
test_os_verification(void)
{
/* Verify OS_WINDOWS or OS_LINUX is set (not both) */
U32 os_count = OS_WINDOWS + OS_LINUX + OS_MAC;
if (os_count == 1) {
LOG_INFO("OS verification: Exactly one OS detected (pass)");
} else {
LOG_ERROR("OS verification: Expected 1 OS, got %u (fail)", os_count);
Assert(0); /* Fail the test */
}
#if OS_WINDOWS
LOG_INFO("OS verification: Running on Windows");
Assert(OS_WINDOWS == 1);
Assert(OS_LINUX == 0);
#elif OS_LINUX
LOG_INFO("OS verification: Running on Linux");
Assert(OS_WINDOWS == 0);
Assert(OS_LINUX == 1);
#elif OS_MAC
LOG_INFO("OS verification: Running on macOS");
Assert(OS_WINDOWS == 0);
Assert(OS_LINUX == 0);
#endif
}
internal void
test_compiler_verification(void)
{
/* Verify COMPILER_* is set correctly */
U32 compiler_count = COMPILER_CLANG + COMPILER_MSVC + COMPILER_GCC;
if (compiler_count == 1) {
LOG_INFO("Compiler verification: Exactly one compiler detected (pass)");
} else {
LOG_ERROR("Compiler verification: Expected 1 compiler, got %u (fail)", compiler_count);
Assert(0);
}
#if COMPILER_CLANG
LOG_INFO("Compiler verification: Compiled with Clang");
Assert(COMPILER_CLANG == 1);
Assert(COMPILER_MSVC == 0);
Assert(COMPILER_GCC == 0);
#elif COMPILER_MSVC
LOG_INFO("Compiler verification: Compiled with MSVC");
Assert(COMPILER_CLANG == 0);
Assert(COMPILER_MSVC == 1);
Assert(COMPILER_GCC == 0);
#elif COMPILER_GCC
LOG_INFO("Compiler verification: Compiled with GCC");
Assert(COMPILER_CLANG == 0);
Assert(COMPILER_MSVC == 0);
Assert(COMPILER_GCC == 1);
#endif
}
internal void
test_intrinsics_inline(void)
{
/* Test PLATFORM_force_inline compiles */
int result = test_add_ints(5, 3);
Assert(result == 8);
/* Test PLATFORM_no_inline compiles */
result = test_subtract_ints(10, 4);
Assert(result == 6);
LOG_INFO("Intrinsics inline: PLATFORM_force_inline and PLATFORM_no_inline compile correctly");
}
internal void
test_branch_prediction(void)
{
/* Test branch prediction macros compile */
int x = 0;
if (PLATFORM_Likely(x == 0)) {
/* This should be predicted as likely */
}
if (PLATFORM_Unlikely(x != 0)) {
/* This should be predicted as unlikely */
} else {
/* Expected path */
}
/* Test with actual values */
int likely_count = 0;
int unlikely_count = 0;
for (int i = 0; i < 1000; i++) {
if (PLATFORM_Likely(i < 500)) {
likely_count++;
}
if (PLATFORM_Unlikely(i >= 900)) {
unlikely_count++;
}
}
Assert(likely_count == 500);
Assert(unlikely_count == 100);
LOG_INFO("Branch prediction: Likely=%d Unlikely=%d", likely_count, unlikely_count);
}
internal void
test_atomics(void)
{
#if COMPILER_MSVC || COMPILER_CLANG || COMPILER_GCC
/* Test atomic operations compile and work */
volatile U32 atomic_val = 0;
/* Test atomic load */
U32 loaded = PLATFORM_ins_atomic_u32_eval(&atomic_val);
Assert(loaded == 0);
/* Test atomic increment */
U32 prev = PLATFORM_ins_atomic_u32_inc(&atomic_val);
Assert(prev == 0); /* Returns previous value */
Assert(atomic_val == 1);
/* Test atomic exchange */
U32 old = PLATFORM_ins_atomic_u32_eval_assign(&atomic_val, 42);
Assert(old == 1);
Assert(atomic_val == 42);
/* Test 64-bit atomic CAS if available */
volatile U64 atomic64_val = 0;
#if COMPILER_CLANG || COMPILER_GCC
U64 cas_result = PLATFORM_ins_atomic_u64_cas(&atomic64_val, 0, 123);
Assert(cas_result == 0); /* Returns original value */
Assert(atomic64_val == 123);
/* CAS with non-matching expected value */
cas_result = PLATFORM_ins_atomic_u64_cas(&atomic64_val, 0, 456);
Assert(cas_result == 123); /* Returns current value, no swap */
Assert(atomic64_val == 123);
#endif
LOG_INFO("Atomics: u32_inc=%u u32_assign=%u", prev, old);
#else
LOG_INFO("Atomics: Not available for this compiler");
#endif
}
internal void
test_thread_static(void)
{
/* Test thread_local storage compiles - use global variable */
test_thread_local_value = 123;
Assert(test_thread_local_value == 123);
/* Test the global thread_local_counter from earlier */
thread_local_counter = 999;
Assert(thread_local_counter == 999);
LOG_INFO("Thread static: thread_local_value=%d global_counter=%lu",
test_thread_local_value, (unsigned long)thread_local_counter);
}
internal void
test_read_only_section(void)
{
/* Test read_only section attribute compiles - use global from earlier */
/* The global test_rodata_hex_table is in .rodata section */
/* Also test that const works */
const U8 hex_table[17] = "0123456789ABCDEF";
/* Verify content */
Assert(hex_table[0] == '0');
Assert(hex_table[9] == '9');
Assert(hex_table[15] == 'F');
LOG_INFO("Read only section: hex_table='%s' global_rodata='%s'",
(char*)hex_table, (char*)test_rodata_hex_table);
}
internal void
test_endianness(void)
{
/* Test ARCH_LITTLE_ENDIAN is defined */
LOG_INFO("Endianness: ARCH_LITTLE_ENDIAN=%d", ARCH_LITTLE_ENDIAN);
#if ARCH_LITTLE_ENDIAN
/* On little-endian, bswap should change the value */
U16 v16 = 0x1234;
U32 v32 = 0x12345678;
U64 v64 = 0x123456789ABCDEF0ULL;
U16 swapped16 = bswap_u16(v16);
U32 swapped32 = bswap_u32(v32);
U64 swapped64 = bswap_u64(v64);
Assert(swapped16 == 0x3412);
Assert(swapped32 == 0x78563412);
Assert(swapped64 == 0xF0DEBC9A78563412ULL);
/* Test big-endian conversion macros */
U16 from_be16 = from_be_u16(v16);
U32 from_be32 = from_be_u32(v32);
U64 from_be64 = from_be_u64(v64);
U16 to_be16 = to_be_u16(v16);
U32 to_be32 = to_be_u32(v32);
U64 to_be64 = to_be_u64(v64);
Assert(from_be16 == swapped16);
Assert(from_be32 == swapped32);
Assert(from_be64 == swapped64);
Assert(to_be16 == swapped16);
Assert(to_be32 == swapped32);
Assert(to_be64 == swapped64);
LOG_INFO("Byte swapping: 0x%04X -> 0x%04X", v16, swapped16);
LOG_INFO("Byte swapping: 0x%08X -> 0x%08X", v32, swapped32);
LOG_INFO("Byte swapping: 0x%016llX -> 0x%016llX",
(unsigned long long)v64, (unsigned long long)swapped64);
#else
/* On big-endian, bswap should return original values */
Assert(bswap_u16(0x1234) == 0x1234);
Assert(bswap_u32(0x12345678) == 0x12345678);
Assert(bswap_u64(0x123456789ABCDEF0ULL) == 0x123456789ABCDEF0ULL);
#endif
}
internal void
test_os_handle(void)
{
/* Test OS_Handle_Skill struct from c-platform skill */
OS_Handle_Skill handle_a = os_handle_zero_skill();
OS_Handle_Skill handle_b = os_handle_zero_skill();
/* Test matching zero handles */
Assert(os_handle_match_skill(handle_a, handle_b) == true);
/* Test non-matching handles */
handle_a.u64[0] = 42;
Assert(os_handle_match_skill(handle_a, handle_b) == false);
/* Test matching non-zero handles */
handle_b.u64[0] = 42;
Assert(os_handle_match_skill(handle_a, handle_b) == true);
LOG_INFO("OS Handle: os_handle_zero_skill and os_handle_match_skill work correctly");
}
internal void
test_os_system_info(void)
{
/* Test OS_SystemInfo_Skill struct from c-platform skill */
OS_SystemInfo_Skill info = {0};
info.page_size = 4096;
info.large_page_size = MB(2);
info.core_count = 4;
info.memory_size = GB(8);
Assert(info.page_size == 4096);
Assert(info.large_page_size == MB(2));
Assert(info.core_count == 4);
Assert(info.memory_size == GB(8));
LOG_INFO("OS System Info: page_size=%lu large_page_size=%lu core_count=%lu memory_size=%lu",
(unsigned long)info.page_size,
(unsigned long)info.large_page_size,
(unsigned long)info.core_count,
(unsigned long)info.memory_size);
}
internal void
test_platform_usage_example(void)
{
/* Test the memory allocation example from the skill */
void *mem = NULL;
#if OS_WINDOWS
/* Windows memory allocation would go here */
/* result = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); */
mem = (void *)0x1; /* Placeholder - just test the macro works */
#elif OS_LINUX
/* Linux memory allocation would go here */
/* result = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); */
mem = (void *)0x1; /* Placeholder - just test the macro works */
#endif
Assert(mem != NULL);
LOG_INFO("Platform usage: OS-specific code compiles correctly");
}
// ============================================================================
// Main Test Runner
// ============================================================================
int
main(int argc, char **argv)
{
(void)argc;
(void)argv;
LOG_INFO("=== c-platform Skill Tests ===");
test_context_cracking();
test_os_verification();
test_compiler_verification();
test_intrinsics_inline();
test_branch_prediction();
test_atomics();
test_thread_static();
test_read_only_section();
test_endianness();
test_os_handle();
test_os_system_info();
test_platform_usage_example();
LOG_INFO("=== All tests passed! ===");
return 0;
}