From 82cd30a14568814dfa70d383cf43c8d0d0bd6b16 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Mon, 30 Oct 2023 13:06:45 +0100 Subject: [PATCH] Add test for RTTI --- .gitignore | 2 + tests/internal/Makefile | 13 +++-- tests/internal/build.bat | 7 ++- tests/internal/test_rtti.odin | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 tests/internal/test_rtti.odin diff --git a/.gitignore b/.gitignore index 3528af624..59b5adb6d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,8 @@ bld/ ![Cc]ore/[Ll]og/ tests/documentation/verify/ tests/documentation/all.odin-doc +tests/internal/test_map +tests/internal/test_rtti # Visual Studio 2015 cache/options directory .vs/ # Visual Studio Code options directory diff --git a/tests/internal/Makefile b/tests/internal/Makefile index 7328968f0..898ba0517 100644 --- a/tests/internal/Makefile +++ b/tests/internal/Makefile @@ -1,6 +1,9 @@ -ODIN=../../odin - -all: map_test - -map_test: +ODIN=../../odin + +all: rtti_test map_test + +rtti_test: + $(ODIN) run test_rtti.odin -file -vet -strict-style -o:minimal + +map_test: $(ODIN) run test_map.odin -file -vet -strict-style -o:minimal \ No newline at end of file diff --git a/tests/internal/build.bat b/tests/internal/build.bat index 995086523..7e8ae84f2 100644 --- a/tests/internal/build.bat +++ b/tests/internal/build.bat @@ -1,4 +1,5 @@ -@echo off -set PATH_TO_ODIN==..\..\odin -%PATH_TO_ODIN% run test_map.odin -file -vet -strict-style -o:minimal || exit /b +@echo off +set PATH_TO_ODIN==..\..\odin +%PATH_TO_ODIN% run test_rtti.odin -file -vet -strict-style -o:minimal || exit /b +%PATH_TO_ODIN% run test_map.odin -file -vet -strict-style -o:minimal || exit /b rem -define:SEED=42 \ No newline at end of file diff --git a/tests/internal/test_rtti.odin b/tests/internal/test_rtti.odin new file mode 100644 index 000000000..9c88cc1b3 --- /dev/null +++ b/tests/internal/test_rtti.odin @@ -0,0 +1,101 @@ +package test_internal_rtti + +import "core:fmt" +import "core:mem" +import "core:os" +import "core:testing" + + +Buggy_Struct :: struct { + a: int, + b: bool, + c: [3]^string, +} +#assert(size_of(Buggy_Struct) == 40) + +g_buggy: Buggy_Struct = {} + +EXPECTED_REPR := "%!s(Buggy_Struct=Buggy_Struct{a = 0, b = false, c = [0x0, 0x0, 0x0]})" + +@test +rtti_test :: proc(t: ^testing.T) { + l_buggy: Buggy_Struct = {} + + g_b := ([^]u8)(&g_buggy)[:size_of(Buggy_Struct)] + l_b := ([^]u8)(&l_buggy)[:size_of(Buggy_Struct)] + { + checksum := 0 + for v, i in g_b { + checksum += (i+1) * int(v) + } + expect(t, checksum == 0, fmt.tprintf("Expected g_b to be zero-initialized, got %v", g_b)) + } + { + checksum := 0 + for v, i in l_b { + checksum += (i+1) * int(v) + } + expect(t, checksum == 0, fmt.tprintf("Expected l_b to be zero-initialized, got %v", l_b)) + } + + expect(t, size_of(Buggy_Struct) == 40, fmt.tprintf("Expected size_of(Buggy_Struct) == 40, got %v", size_of(Buggy_Struct))) + expect(t, size_of(g_buggy) == 40, fmt.tprintf("Expected size_of(g_buggy) == 40, got %v", size_of(g_buggy))) + expect(t, size_of(l_buggy) == 40, fmt.tprintf("Expected size_of(l_buggy) == 40, got %v", size_of(l_buggy))) + + g_s := fmt.tprintf("%s", g_buggy) + l_s := fmt.tprintf("%s", l_buggy) + expect(t, g_s == EXPECTED_REPR, fmt.tprintf("Expected fmt.tprintf(\"%%s\", g_s)) to return \"%v\", got \"%v\"", EXPECTED_REPR, g_s)) + expect(t, l_s == EXPECTED_REPR, fmt.tprintf("Expected fmt.tprintf(\"%%s\", l_s)) to return \"%v\", got \"%v\"", EXPECTED_REPR, l_s)) +} + +// -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- + +main :: proc() { + t := testing.T{} + + rtti_test(&t) + + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + if TEST_fail > 0 { + os.exit(1) + } +} + +mem_track_test :: proc(t: ^testing.T, test: proc(t: ^testing.T)) { + track: mem.Tracking_Allocator + mem.tracking_allocator_init(&track, context.allocator) + context.allocator = mem.tracking_allocator(&track) + + test(t) + + expect(t, len(track.allocation_map) == 0, "Expected no leaks.") + expect(t, len(track.bad_free_array) == 0, "Expected no leaks.") + + for _, leak in track.allocation_map { + fmt.printf("%v leaked %v bytes\n", leak.location, leak.size) + } + for bad_free in track.bad_free_array { + fmt.printf("%v allocation %p was freed badly\n", bad_free.location, bad_free.memory) + } +} + +TEST_count := 0 +TEST_fail := 0 + +when ODIN_TEST { + expect :: testing.expect + log :: testing.log +} else { + expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) { + TEST_count += 1 + if !condition { + TEST_fail += 1 + fmt.printf("[%v] %v\n", loc, message) + return + } + } + log :: proc(t: ^testing.T, v: any, loc := #caller_location) { + fmt.printf("[%v] ", loc) + fmt.printf("log: %v\n", v) + } +} \ No newline at end of file