Files
wasm_transcript_browser/src/testing/testing_main.c
2025-04-04 08:25:47 +02:00

137 lines
4.2 KiB
C

#include "core/core.h"
#include "os/os.h"
#include "core/core.c"
#include "os/os.c"
#include "testing.gen.c"
fn void os_test(void) {
os_date_t local_time = os_local_time();
os_date_t universal_time = os_universal_time();
debugf("OS local_time = %S | universal_time = %S", os_format_date(tcx->temp, local_time), os_format_date(tcx->temp, universal_time));
s8_t exe_dir = os_exe_dir(tcx->temp);
assert(exe_dir.str[exe_dir.len - 1] == '/');
s8_t exe = os_exe(tcx->temp);
s8_t cwd = os_cwd(tcx->temp);
assert(cwd.str[cwd.len - 1] == '/');
assert(os_is_dir(exe_dir));
assert(os_is_file(exe));
assert(os_is_dir(cwd));
assert(os_exists(exe_dir));
assert(os_exists(exe));
assert(os_exists(cwd));
assert(os_is_abs(exe_dir));
assert(os_is_abs(exe));
assert(os_is_abs(cwd));
assert(!os_is_abs(s8("../path/")));
debugf("OS paths %S %S %S", exe_dir, exe, cwd);
s8_t file = os_read(tcx->temp, s8("../.gitignore"));
assert(file.str != 0);
assert(file.len != 0);
}
fn void test_s8(void) {
ma_arena_t *arena = ma_create(ma_default_reserve_size);
{
ma_temp_t temp = ma_begin_temp(arena);
sb8_t *sb = &(sb8_t){arena};
s8_t memes = s8("memes");
sb8_printf(sb, "%S", memes);
assert(sb->first == sb->last);
assert(sb->first->len == 5);
assert(s8_are_equal(sb->first->string, memes));
sb8_printf(sb, "%S", s8("things are going fine"));
s8_t string = sb8_merge(temp.arena, sb);
assert(s8_are_equal(string, s8("memesthings are going fine")));
ma_end_temp(temp);
}
{
s8_t str = s8("thing|another|");
sb8_t sb = s8_split(arena, str, s8("|"), s8_split_none);
assert(s8_are_equal(sb.first->string, s8("thing")));
assert(s8_are_equal(sb.first->next->string, s8("another")));
assert(sb.first->next->next == NULL);
}
{
s8_t str = s8("thing|another|");
sb8_t sb = s8_split(arena, str, s8("|"), s8_split_inclusive);
assert(s8_are_equal(sb.first->string, s8("thing")));
assert(s8_are_equal(sb.first->next->string, s8("|")));
assert(s8_are_equal(sb.first->next->next->string, s8("another")));
assert(s8_are_equal(sb.first->next->next->next->string, s8("|")));
assert(sb.first->next->next->next->next == NULL);
}
{
s8_t str = s8("aabaaBaa");
sb8_t sb = s8_split(arena, str, s8("b"), s8_split_inclusive | s8_split_ignore_case);
assert(s8_are_equal(sb.first->string, s8("aa")));
assert(s8_are_equal(sb.first->next->string, s8("b")));
assert(s8_are_equal(sb.first->next->next->string, s8("aa")));
assert(s8_are_equal(sb.first->next->next->next->string, s8("B")));
assert(s8_are_equal(sb.first->next->next->next->next->string, s8("aa")));
assert(sb.first->next->next->next->next->next == NULL);
}
{
s8_t str = s8("aabaaBaa");
sb8_t sb = s8_split(arena, str, s8("b"), s8_split_inclusive);
assert(s8_are_equal(sb.first->string, s8("aa")));
assert(s8_are_equal(sb.first->next->string, s8("b")));
assert(s8_are_equal(sb.first->next->next->string, s8("aaBaa")));
}
{
s8_t s = s8("0123456789");
assert(s8_are_equal(s8_slice(s, 0, 4), s8("0123")));
assert(s8_are_equal(s8_slice(s, -2, -1), s8("89")));
assert(s8_are_equal(s8_slice(s, -2, 10), s8("89")));
assert(s8_are_equal(s8_slice(s, 8, 10), s8("89")));
}
{
s8_t s = s8(" a \n");
s = s8_trim(s);
assert(s8_are_equal(s, s8("a")));
}
{
s8_t s = s8("C:/memes/the_thing.c");
s8_t ss = s8_get_name_no_ext(s);
assert(s8_are_equal(ss, s8("the_thing")));
}
{
s8_t s = s8_printf(arena, "%d%Sv%s", 32, s8("|"), ">");
assert(s8_are_equal(s, s8("32|v>")));
}
{
s8_t s0 = s8("0123456789");
s8_t s1 = s8_cut_start(&s0, 2);
assert(s8_are_equal(s0, s8("23456789")));
assert(s8_are_equal(s1, s8("01")));
}
ma_destroy(arena);
}
int main() {
core_init();
run_tests();
test_s8();
os_test();
return 0;
}