From 55d21f4c2fc1872f1e7a491b6a5c645f5559f9d8 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 12 May 2024 23:38:17 +0200 Subject: [PATCH 1/3] Test string compare To avoid regression of the bug fixed in f8581537e496e9854a40f07a87543fc1357404fb. --- tests/internal/Makefile | 3 + tests/internal/build.bat | 8 ++- tests/internal/test_string_compare.odin | 91 +++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 tests/internal/test_string_compare.odin diff --git a/tests/internal/Makefile b/tests/internal/Makefile index c5c612cdd..83080140d 100644 --- a/tests/internal/Makefile +++ b/tests/internal/Makefile @@ -16,3 +16,6 @@ pow_test: asan_test: $(ODIN) run test_asan.odin -file -sanitize:address -debug + +string_compare_test: + $(ODIN) run test_string_compare.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 da4fe890d..29b3e36c3 100644 --- a/tests/internal/build.bat +++ b/tests/internal/build.bat @@ -1,8 +1,10 @@ @echo off set PATH_TO_ODIN==..\..\odin rem %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 +%PATH_TO_ODIN% run test_map.odin -file -vet -strict-style -o:minimal || exit /b rem -define:SEED=42 -%PATH_TO_ODIN% run test_pow.odin -file -vet -strict-style -o:minimal || exit /b +%PATH_TO_ODIN% run test_pow.odin -file -vet -strict-style -o:minimal || exit /b -%PATH_TO_ODIN% run test_128.odin -file -vet -strict-style -o:minimal || exit /b +%PATH_TO_ODIN% run test_128.odin -file -vet -strict-style -o:minimal || exit /b + +%PATH_TO_ODIN% run test_string_compare.odin -file -vet -strict-style -o:minimal || exit /b \ No newline at end of file diff --git a/tests/internal/test_string_compare.odin b/tests/internal/test_string_compare.odin new file mode 100644 index 000000000..a8ced485d --- /dev/null +++ b/tests/internal/test_string_compare.odin @@ -0,0 +1,91 @@ +package test_internal_string_compare + +import "core:fmt" +import "core:os" +import "core:testing" + +Op :: enum { Eq, Lt, Gt } + +Test :: struct { + a: cstring, + b: cstring, + res: [Op]bool, +} + +CASES := []Test{ + {"hellope", "hellope", {.Eq=true, .Lt=false, .Gt=false}}, + {"Hellope", "hellope", {.Eq=false, .Lt=true, .Gt=false}}, // H < h + {"Hellope!", "Hellope", {.Eq=false, .Lt=false, .Gt=true }}, +} + +@test +string_compare :: proc(t: ^testing.T) { + for v in CASES { + s_a := string(v.a) + s_b := string(v.b) + + for res, op in v.res { + switch op { + case .Eq: + expect(t, (v.a == v.b) == res, fmt.tprintf("Expected cstring(\"%v\") == cstring(\"%v\") to be %v", v.a, v.b, res)) + expect(t, (s_a == s_b) == res, fmt.tprintf("Expected string(\"%v\") == string(\"%v\") to be %v", v.a, v.b, res)) + + // If a == b then a != b + expect(t, (v.a != v.b) == !res, fmt.tprintf("Expected cstring(\"%v\") != cstring(\"%v\") to be %v", v.a, v.b, !res)) + expect(t, (s_a != s_b) == !res, fmt.tprintf("Expected string(\"%v\") != string(\"%v\") to be %v", v.a, v.b, !res)) + + case .Lt: + expect(t, (v.a < v.b) == res, fmt.tprintf("Expected cstring(\"%v\") < cstring(\"%v\") to be %v", v.a, v.b, res)) + expect(t, (s_a < s_b) == res, fmt.tprintf("Expected string(\"%v\") < string(\"%v\") to be %v", v.a, v.b, res)) + + // .Lt | .Eq == .LtEq + lteq := v.res[.Eq] | res + expect(t, (v.a <= v.b) == lteq, fmt.tprintf("Expected cstring(\"%v\") <= cstring(\"%v\") to be %v", v.a, v.b, lteq)) + expect(t, (s_a <= s_b) == lteq, fmt.tprintf("Expected string(\"%v\") <= string(\"%v\") to be %v", v.a, v.b, lteq)) + + case .Gt: + expect(t, (v.a > v.b) == res, fmt.tprintf("Expected cstring(\"%v\") > cstring(\"%v\") to be %v", v.a, v.b, res)) + expect(t, (s_a > s_b) == res, fmt.tprintf("Expected string(\"%v\") > string(\"%v\") to be %v", v.a, v.b, res)) + + // .Gt | .Eq == .GtEq + gteq := v.res[.Eq] | res + expect(t, (v.a >= v.b) == gteq, fmt.tprintf("Expected cstring(\"%v\") >= cstring(\"%v\") to be %v", v.a, v.b, gteq)) + expect(t, (s_a >= s_b) == gteq, fmt.tprintf("Expected string(\"%v\") >= string(\"%v\") to be %v", v.a, v.b, gteq)) + } + } + } +} + +// -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- + +main :: proc() { + t := testing.T{} + + string_compare(&t) + + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + if TEST_fail > 0 { + os.exit(1) + } +} + +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 From 41b8f06f51fd8cf67696a95446c0e3273d6c7eeb Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 12 May 2024 23:43:05 +0200 Subject: [PATCH 2/3] Add 1 more each for < and > --- tests/internal/test_string_compare.odin | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/internal/test_string_compare.odin b/tests/internal/test_string_compare.odin index a8ced485d..ff65b41c2 100644 --- a/tests/internal/test_string_compare.odin +++ b/tests/internal/test_string_compare.odin @@ -15,7 +15,9 @@ Test :: struct { CASES := []Test{ {"hellope", "hellope", {.Eq=true, .Lt=false, .Gt=false}}, {"Hellope", "hellope", {.Eq=false, .Lt=true, .Gt=false}}, // H < h + {"Hell", "Hellope", {.Eq=false, .Lt=true, .Gt=false}}, {"Hellope!", "Hellope", {.Eq=false, .Lt=false, .Gt=true }}, + {"Hellopf", "Hellope", {.Eq=false, .Lt=false, .Gt=true }}, } @test From 23545c3f37833e77cd82ed9dd6c7a0e06278deda Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 12 May 2024 23:47:42 +0200 Subject: [PATCH 3/3] Enable in Makefile --- tests/internal/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/internal/Makefile b/tests/internal/Makefile index 83080140d..d17b9d012 100644 --- a/tests/internal/Makefile +++ b/tests/internal/Makefile @@ -1,6 +1,6 @@ ODIN=../../odin -all: rtti_test map_test pow_test 128_test asan_test +all: rtti_test map_test pow_test 128_test asan_test string_compare_test rtti_test: $(ODIN) run test_rtti.odin -file -vet -strict-style -o:minimal