Strip old test runner back out of internal, issues and vendor

This commit is contained in:
Jeroen van Rijn
2024-06-02 21:15:25 +02:00
parent 9d8d864400
commit 60d0c03134
16 changed files with 282 additions and 582 deletions
+8 -9
View File
@@ -1,23 +1,22 @@
ODIN=../../odin
COMMON=-file -vet -strict-style -o:minimal
all: all_bsd asan_test
all_bsd: rtti_test map_test pow_test 128_test string_compare_test
all: asan_test rtti_test map_test pow_test 128_test string_compare_test
rtti_test:
$(ODIN) run test_rtti.odin -file -vet -strict-style -o:minimal
$(ODIN) test test_rtti.odin $(COMMON)
map_test:
$(ODIN) run test_map.odin -file -vet -strict-style -o:minimal
$(ODIN) test test_map.odin $(COMMON)
pow_test:
$(ODIN) run test_pow.odin -file -vet -strict-style -o:minimal
$(ODIN) test test_pow.odin $(COMMON)
128_test:
$(ODIN) run test_128.odin -file -vet -strict-style -o:minimal
$(ODIN) test test_128.odin $(COMMON)
asan_test:
$(ODIN) run test_asan.odin -file -sanitize:address -debug
$(ODIN) test test_asan.odin $(COMMON) -sanitize:address -debug
string_compare_test:
$(ODIN) run test_string_compare.odin -file -vet -strict-style -o:minimal
$(ODIN) test test_string_compare.odin $(COMMON)
+7 -8
View File
@@ -1,10 +1,9 @@
@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
rem -define:SEED=42
%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_string_compare.odin -file -vet -strict-style -o:minimal || exit /b
set COMMON=-file -vet -strict-style -o:minimal
%PATH_TO_ODIN% test test_rtti.odin %COMMON% || exit /b
%PATH_TO_ODIN% test test_map.odin %COMMON% || exit /b
%PATH_TO_ODIN% test test_pow.odin %COMMON% || exit /b
%PATH_TO_ODIN% test test_asan.odin %COMMON% || exit /b
%PATH_TO_ODIN% test test_128.odin %COMMON% || exit /b
%PATH_TO_ODIN% test test_string_compare.odin %COMMON% || exit /b
+8 -42
View File
@@ -1,41 +1,7 @@
package test_128
import "core:fmt"
import "core:os"
import "core:testing"
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)
}
}
main :: proc() {
t := testing.T{}
test_128_align(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
os.exit(1)
}
}
@test
test_128_align :: proc(t: ^testing.T) {
Danger_Struct :: struct {
@@ -45,15 +11,15 @@ test_128_align :: proc(t: ^testing.T) {
list := [?]Danger_Struct{{0, 0}, {1, 0}, {2, 0}, {3, 0}}
expect(t, list[0].x == 0, fmt.tprintf("[0].x (%v) != 0", list[0].x))
expect(t, list[0].y == 0, fmt.tprintf("[0].y (%v) != 0", list[0].y))
testing.expectf(t, list[0].x == 0, "[0].x (%v) != 0", list[0].x)
testing.expectf(t, list[0].y == 0, "[0].y (%v) != 0", list[0].y)
expect(t, list[1].x == 1, fmt.tprintf("[1].x (%v) != 1", list[1].x))
expect(t, list[1].y == 0, fmt.tprintf("[1].y (%v) != 0", list[1].y))
testing.expectf(t, list[1].x == 1, "[1].x (%v) != 1", list[1].x)
testing.expectf(t, list[1].y == 0, "[1].y (%v) != 0", list[1].y)
expect(t, list[2].x == 2, fmt.tprintf("[2].x (%v) != 2", list[2].x))
expect(t, list[2].y == 0, fmt.tprintf("[2].y (%v) != 0", list[2].y))
testing.expectf(t, list[2].x == 2, "[2].x (%v) != 2", list[2].x)
testing.expectf(t, list[2].y == 0, "[2].y (%v) != 0", list[2].y)
expect(t, list[3].x == 3, fmt.tprintf("[3].x (%v) != 3", list[3].x))
expect(t, list[3].y == 0, fmt.tprintf("[3].y (%v) != 0", list[3].y))
testing.expectf(t, list[3].x == 3, "[3].x (%v) != 3", list[3].x)
testing.expectf(t, list[3].y == 0, "[3].y (%v) != 0", list[3].y)
}
+5 -40
View File
@@ -1,42 +1,7 @@
// Intended to contain code that would trigger asan easily if the abi was set up badly.
package test_asan
import "core:fmt"
import "core:testing"
import "core:os"
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)
}
}
main :: proc() {
t := testing.T{}
test_12_bytes(&t)
test_12_bytes_two(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
os.exit(1)
}
}
@(test)
test_12_bytes :: proc(t: ^testing.T) {
@@ -45,9 +10,9 @@ test_12_bytes :: proc(t: ^testing.T) {
}
a, b, ok := internal()
expect(t, a == max(f32), fmt.tprintf("a (%v) != max(f32)", a))
expect(t, b == 0, fmt.tprintf("b (%v) != 0", b))
expect(t, ok, fmt.tprintf("ok (%v) != true", ok))
testing.expectf(t, a == max(f32), "a (%v) != max(f32)", a)
testing.expectf(t, b == 0, "b (%v) != 0", b)
testing.expectf(t, ok, "ok (%v) != true", ok)
}
@(test)
@@ -57,6 +22,6 @@ test_12_bytes_two :: proc(t: ^testing.T) {
}
a, b := internal()
expect(t, a == 100., fmt.tprintf("a (%v) != 100.", a))
expect(t, b == max(int), fmt.tprintf("b (%v) != max(int)", b))
testing.expectf(t, a == 100., "a (%v) != 100.", a)
testing.expectf(t, b == max(int), "b (%v) != max(int)", b)
}
+46 -116
View File
@@ -1,26 +1,22 @@
package test_internal_map
import "core:fmt"
import "core:log"
import "base:intrinsics"
import "core:math/rand"
import "core:mem"
import "core:os"
import "core:testing"
seed: u64
ENTRY_COUNTS := []int{11, 101, 1_001, 10_001, 100_001, 1_000_001}
@test
map_insert_random_key_value :: proc(t: ^testing.T) {
seed_incr := u64(0)
for entries in ENTRY_COUNTS {
fmt.printf("[map_insert_random_key_value] Testing %v entries.\n", entries)
log.infof("Testing %v entries", entries)
m: map[i64]i64
defer delete(m)
unique_keys := 0
r := rand.create(seed + seed_incr)
r := rand.create(t.seed + seed_incr)
for _ in 0..<entries {
k := rand.int63(&r)
v := rand.int63(&r)
@@ -36,11 +32,11 @@ map_insert_random_key_value :: proc(t: ^testing.T) {
key_count += 1
}
expect(t, key_count == unique_keys, fmt.tprintf("Expected key_count to equal %v, got %v", unique_keys, key_count))
expect(t, len(m) == unique_keys, fmt.tprintf("Expected len(map) to equal %v, got %v", unique_keys, len(m)))
testing.expectf(t, key_count == unique_keys, "Expected key_count to equal %v, got %v", unique_keys, key_count)
testing.expectf(t, len(m) == unique_keys, "Expected len(map) to equal %v, got %v", unique_keys, len(m))
// Reset randomizer and verify
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
num_fails := 0
for _ in 0..<entries {
@@ -51,10 +47,10 @@ map_insert_random_key_value :: proc(t: ^testing.T) {
if !cond {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k]))
testing.expectf(t, false, "Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k])
}
}
seed_incr += 1
@@ -65,12 +61,12 @@ map_insert_random_key_value :: proc(t: ^testing.T) {
map_update_random_key_value :: proc(t: ^testing.T) {
seed_incr := u64(0)
for entries in ENTRY_COUNTS {
fmt.printf("[map_update_random_key_value] Testing %v entries.\n", entries)
log.infof("Testing %v entries", entries)
m: map[i64]i64
defer delete(m)
unique_keys := 0
r := rand.create(seed + seed_incr)
r := rand.create(t.seed + seed_incr)
for _ in 0..<entries {
k := rand.int63(&r)
v := rand.int63(&r)
@@ -86,13 +82,13 @@ map_update_random_key_value :: proc(t: ^testing.T) {
key_count += 1
}
expect(t, key_count == unique_keys, fmt.tprintf("Expected key_count to equal %v, got %v", unique_keys, key_count))
expect(t, len(m) == unique_keys, fmt.tprintf("Expected len(map) to equal %v, got %v", unique_keys, len(m)))
testing.expectf(t, key_count == unique_keys, "Expected key_count to equal %v, got %v", unique_keys, key_count)
testing.expectf(t, len(m) == unique_keys, "Expected len(map) to equal %v, got %v", unique_keys, len(m))
half_entries := entries / 2
// Reset randomizer and update half the entries
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
for _ in 0..<half_entries {
k := rand.int63(&r)
v := rand.int63(&r)
@@ -101,7 +97,7 @@ map_update_random_key_value :: proc(t: ^testing.T) {
}
// Reset randomizer and verify
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
num_fails := 0
for i in 0..<entries {
@@ -113,10 +109,10 @@ map_update_random_key_value :: proc(t: ^testing.T) {
if !cond {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k]))
testing.expectf(t, false, "Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k])
}
}
seed_incr += 1
@@ -127,12 +123,12 @@ map_update_random_key_value :: proc(t: ^testing.T) {
map_delete_random_key_value :: proc(t: ^testing.T) {
seed_incr := u64(0)
for entries in ENTRY_COUNTS {
fmt.printf("[map_delete_random_key_value] Testing %v entries.\n", entries)
log.infof("Testing %v entries", entries)
m: map[i64]i64
defer delete(m)
unique_keys := 0
r := rand.create(seed + seed_incr)
r := rand.create(t.seed + seed_incr)
for _ in 0..<entries {
k := rand.int63(&r)
v := rand.int63(&r)
@@ -148,13 +144,13 @@ map_delete_random_key_value :: proc(t: ^testing.T) {
key_count += 1
}
expect(t, key_count == unique_keys, fmt.tprintf("Expected key_count to equal %v, got %v", unique_keys, key_count))
expect(t, len(m) == unique_keys, fmt.tprintf("Expected len(map) to equal %v, got %v", unique_keys, len(m)))
testing.expectf(t, key_count == unique_keys, "Expected key_count to equal %v, got %v", unique_keys, key_count)
testing.expectf(t, len(m) == unique_keys, "Expected len(map) to equal %v, got %v", unique_keys, len(m))
half_entries := entries / 2
// Reset randomizer and delete half the entries
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
for _ in 0..<half_entries {
k := rand.int63(&r)
_ = rand.int63(&r)
@@ -163,7 +159,7 @@ map_delete_random_key_value :: proc(t: ^testing.T) {
}
// Reset randomizer and verify
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
num_fails := 0
for i in 0..<entries {
@@ -174,26 +170,26 @@ map_delete_random_key_value :: proc(t: ^testing.T) {
if k in m {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Unexpected key present. Expected m[%v] to have been deleted, got %v", k, m[k]))
testing.expectf(t, false, "Unexpected key present. Expected m[%v] to have been deleted, got %v", k, m[k])
}
} else {
if k not_in m {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Expected key not present. Expected m[%v] = %v", k, v))
testing.expectf(t, false, "Expected key not present. Expected m[%v] = %v", k, v)
} else if m[k] != v {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k]))
testing.expectf(t, false, "Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k])
}
}
}
@@ -205,12 +201,12 @@ map_delete_random_key_value :: proc(t: ^testing.T) {
set_insert_random_key_value :: proc(t: ^testing.T) {
seed_incr := u64(0)
for entries in ENTRY_COUNTS {
fmt.printf("[set_insert_random_key_value] Testing %v entries.\n", entries)
log.infof("Testing %v entries", entries)
m: map[i64]struct{}
defer delete(m)
unique_keys := 0
r := rand.create(seed + seed_incr)
r := rand.create(t.seed + seed_incr)
for _ in 0..<entries {
k := rand.int63(&r)
if k not_in m {
@@ -224,11 +220,11 @@ set_insert_random_key_value :: proc(t: ^testing.T) {
key_count += 1
}
expect(t, key_count == unique_keys, fmt.tprintf("Expected key_count to equal %v, got %v", unique_keys, key_count))
expect(t, len(m) == unique_keys, fmt.tprintf("Expected len(map) to equal %v, got %v", unique_keys, len(m)))
testing.expectf(t, key_count == unique_keys, "Expected key_count to equal %v, got %v", unique_keys, key_count)
testing.expectf(t, len(m) == unique_keys, "Expected len(map) to equal %v, got %v", unique_keys, len(m))
// Reset randomizer and verify
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
num_fails := 0
for _ in 0..<entries {
@@ -238,10 +234,10 @@ set_insert_random_key_value :: proc(t: ^testing.T) {
if !cond {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Unexpected value. Expected m[%v] to exist", k))
testing.expectf(t, false, "Unexpected value. Expected m[%v] to exist", k)
}
}
seed_incr += 1
@@ -252,12 +248,12 @@ set_insert_random_key_value :: proc(t: ^testing.T) {
set_delete_random_key_value :: proc(t: ^testing.T) {
seed_incr := u64(0)
for entries in ENTRY_COUNTS {
fmt.printf("[set_delete_random_key_value] Testing %v entries.\n", entries)
log.infof("Testing %v entries", entries)
m: map[i64]struct{}
defer delete(m)
unique_keys := 0
r := rand.create(seed + seed_incr)
r := rand.create(t.seed + seed_incr)
for _ in 0..<entries {
k := rand.int63(&r)
@@ -272,20 +268,20 @@ set_delete_random_key_value :: proc(t: ^testing.T) {
key_count += 1
}
expect(t, key_count == unique_keys, fmt.tprintf("Expected key_count to equal %v, got %v", unique_keys, key_count))
expect(t, len(m) == unique_keys, fmt.tprintf("Expected len(map) to equal %v, got %v", unique_keys, len(m)))
testing.expectf(t, key_count == unique_keys, "Expected key_count to equal %v, got %v", unique_keys, key_count)
testing.expectf(t, len(m) == unique_keys, "Expected len(map) to equal %v, got %v", unique_keys, len(m))
half_entries := entries / 2
// Reset randomizer and delete half the entries
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
for _ in 0..<half_entries {
k := rand.int63(&r)
delete_key(&m, k)
}
// Reset randomizer and verify
r = rand.create(seed + seed_incr)
r = rand.create(t.seed + seed_incr)
num_fails := 0
for i in 0..<entries {
@@ -295,88 +291,22 @@ set_delete_random_key_value :: proc(t: ^testing.T) {
if k in m {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Unexpected key present. Expected m[%v] to have been deleted", k))
testing.expectf(t, false, "Unexpected key present. Expected m[%v] to have been deleted", k)
}
} else {
if k not_in m {
num_fails += 1
if num_fails > 5 {
fmt.println("... and more")
log.info("... and more")
break
}
expect(t, false, fmt.tprintf("Expected key not present. Expected m[%v] to exist", k))
testing.expectf(t, false, "Expected key not present. Expected m[%v] to exist", k)
}
}
}
seed_incr += 1
}
}
// -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
main :: proc() {
t := testing.T{}
// Allow tests to be repeatable
SEED :: #config(SEED, -1)
when SEED > 0 {
seed = u64(SEED)
} else {
seed = u64(intrinsics.read_cycle_counter())
}
fmt.println("Initialized seed to", seed)
mem_track_test(&t, map_insert_random_key_value)
mem_track_test(&t, map_update_random_key_value)
mem_track_test(&t, map_delete_random_key_value)
mem_track_test(&t, set_insert_random_key_value)
mem_track_test(&t, set_delete_random_key_value)
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)
}
}
}
+6 -42
View File
@@ -1,8 +1,7 @@
package test_internal_math_pow
import "core:fmt"
@(require) import "core:log"
import "core:math"
import "core:os"
import "core:testing"
@test
@@ -19,14 +18,14 @@ pow_test :: proc(t: ^testing.T) {
// pow2_f64 returns the same float on all platforms because it isn't this stupid
_v1 = 0h00000000_00000000
}
expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f64(%d) == math.pow(2, %d) (= %16x), got %16x", exp, exp, _v1, _v2))
testing.expectf(t, _v1 == _v2, "Expected math.pow2_f64(%d) == math.pow(2, %d) (= %16x), got %16x", exp, exp, _v1, _v2)
}
{
v1 := math.pow(2, f32(exp))
v2 := math.pow2_f32(exp)
_v1 := transmute(u32)v1
_v2 := transmute(u32)v2
expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f32(%d) == math.pow(2, %d) (= %08x), got %08x", exp, exp, _v1, _v2))
testing.expectf(t, _v1 == _v2, "Expected math.pow2_f32(%d) == math.pow(2, %d) (= %08x), got %08x", exp, exp, _v1, _v2)
}
{
v1 := math.pow(2, f16(exp))
@@ -36,46 +35,11 @@ pow_test :: proc(t: ^testing.T) {
when ODIN_OS == .Darwin && ODIN_ARCH == .arm64 {
if exp == -25 {
testing.logf(t, "skipping known test failure on darwin+arm64, Expected math.pow2_f16(-25) == math.pow(2, -25) (= 0000), got 0001")
log.info("skipping known test failure on darwin+arm64, Expected math.pow2_f16(-25) == math.pow(2, -25) (= 0000), got 0001")
_v2 = 0
}
}
expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f16(%d) == math.pow(2, %d) (= %04x), got %04x", exp, exp, _v1, _v2))
testing.expectf(t, _v1 == _v2, "Expected math.pow2_f16(%d) == math.pow(2, %d) (= %04x), got %04x", exp, exp, _v1, _v2)
}
}
}
// -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
main :: proc() {
t := testing.T{}
pow_test(&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)
}
}
}
+7 -62
View File
@@ -1,11 +1,8 @@
package test_internal_rtti
import "core:fmt"
import "core:mem"
import "core:os"
import "core:testing"
Buggy_Struct :: struct {
a: int,
b: bool,
@@ -28,74 +25,22 @@ rtti_test :: proc(t: ^testing.T) {
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))
testing.expectf(t, checksum == 0, "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))
testing.expectf(t, checksum == 0, "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)))
testing.expectf(t, size_of(Buggy_Struct) == 40, "Expected size_of(Buggy_Struct) == 40, got %v", size_of(Buggy_Struct))
testing.expectf(t, size_of(g_buggy) == 40, "Expected size_of(g_buggy) == 40, got %v", size_of(g_buggy))
testing.expectf(t, size_of(l_buggy) == 40, "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{}
mem_track_test(&t, rtti_test)
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)
}
testing.expectf(t, g_s == EXPECTED_REPR, "Expected fmt.tprintf(\"%%s\", g_s)) to return \"%v\", got \"%v\"", EXPECTED_REPR, g_s)
testing.expectf(t, l_s == EXPECTED_REPR, "Expected fmt.tprintf(\"%%s\", l_s)) to return \"%v\", got \"%v\"", EXPECTED_REPR, l_s)
}
+12 -48
View File
@@ -1,7 +1,5 @@
package test_internal_string_compare
import "core:fmt"
import "core:os"
import "core:testing"
Op :: enum { Eq, Lt, Gt }
@@ -29,65 +27,31 @@ string_compare :: proc(t: ^testing.T) {
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))
testing.expectf(t, (v.a == v.b) == res, "Expected cstring(\"%v\") == cstring(\"%v\") to be %v", v.a, v.b, res)
testing.expectf(t, (s_a == s_b) == res, "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))
testing.expectf(t, (v.a != v.b) == !res, "Expected cstring(\"%v\") != cstring(\"%v\") to be %v", v.a, v.b, !res)
testing.expectf(t, (s_a != s_b) == !res, "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))
testing.expectf(t, (v.a < v.b) == res, "Expected cstring(\"%v\") < cstring(\"%v\") to be %v", v.a, v.b, res)
testing.expectf(t, (s_a < s_b) == res, "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))
testing.expectf(t, (v.a <= v.b) == lteq, "Expected cstring(\"%v\") <= cstring(\"%v\") to be %v", v.a, v.b, lteq)
testing.expectf(t, (s_a <= s_b) == lteq, "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))
testing.expectf(t, (v.a > v.b) == res, "Expected cstring(\"%v\") > cstring(\"%v\") to be %v", v.a, v.b, res)
testing.expectf(t, (s_a > s_b) == res, "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))
testing.expectf(t, (v.a >= v.b) == gteq, "Expected cstring(\"%v\") >= cstring(\"%v\") to be %v", v.a, v.b, gteq)
testing.expectf(t, (s_a >= s_b) == gteq, "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)
}
}