Browse Source

Add test for RTTI

Jeroen van Rijn 1 year ago
parent
commit
82cd30a145
4 changed files with 115 additions and 8 deletions
  1. 2 0
      .gitignore
  2. 8 5
      tests/internal/Makefile
  3. 4 3
      tests/internal/build.bat
  4. 101 0
      tests/internal/test_rtti.odin

+ 2 - 0
.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

+ 8 - 5
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

+ 4 - 3
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

+ 101 - 0
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)
+	}
+}