rtcore/rtcore_test.c
Kevin Trogant 523eb30aa7 Add rttest.h
Simple library for writing tests.
2026-05-05 11:04:09 +02:00

83 lines
1.7 KiB
C

#define RT_CORE_IMPLEMENTATION
#include "rtcore.h"
#define RT_TEST_IMPLEMENTATION
#include "rttest.h"
#include <stdio.h>
THREAD_FN(TestThread)
{
int *p = param;
#ifndef RTC_NO_ATOMICS
printf("Thread got param %d\n", AtomicLoad(p));
AtomicStore(p, 42);
#else
printf("Thread go param %d\n", *p);
*p = 42;
#endif
return 0;
}
internal
TEST(AllocTest)
{
NoSuite;
char space[260];
arena a = {.begin = space, .end = space + CountOf(space)};
int *t = Alloc(&a, int);
AssertNotNull(testing, t);
*t = 42;
int *t2 = Alloc(&a, int);
AssertNotEqual(testing, t, t2);
*t2 = *t;
}
internal
TEST(ReadFileTest)
{
NoSuite;
char space[260];
arena a = {.begin = space, .end = space + CountOf(space)};
s8 data = ReadEntireFileS8(S8("somedata.txt"), &a);
AssertTrue(testing, S8Equals(data, S8("1234567890\n")));
}
internal
TEST(ThreadTest)
{
NoSuite;
int p = 32;
thread *t = StartThread(TestThread, &p);
JoinThread(t);
#ifndef RTC_NO_ATOMICS
AssertEqual(testing, AtomicLoad(&p), 42);
#else
AssertEqual(testing, p, 42);
#endif
}
internal
TEST(ParseIntTest)
{
NoSuite;
AssertEqual(testing, S8ParseI32(S8("123"), 10).i, 123);
AssertEqual(testing, S8ParseI32(S8("-124"), 10).i, -124);
AssertEqual(testing, S8ParseI64(S8("9223372036854775807"), 10).i, 9223372036854775807);
AssertFalse(testing, S8ParseI32(S8("Not a number"), 10).ok);
}
int
main(int argc, char **argv)
{
test_driver *testing = CreateTestDriver(4096);
if (!testing)
return 1;
RegisterStandaloneTest(testing, AllocTest);
RegisterStandaloneTest(testing, ReadFileTest);
RegisterStandaloneTest(testing, ThreadTest);
RegisterStandaloneTest(testing, ParseIntTest);
return ExecuteTests(testing, argc, argv) ? 0 : 1;
}