mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-06 10:44:06 +00:00
Port tests\core\strings
This commit is contained in:
committed by
Feoramund
parent
9ba02e888d
commit
a406ff7063
@@ -100,7 +100,7 @@ slice_test:
|
||||
$(ODIN) test slice $(COMMON) -out:test_core_slice
|
||||
|
||||
strings_test:
|
||||
$(ODIN) run strings $(COMMON) -out:test_core_strings
|
||||
$(ODIN) test strings $(COMMON) -out:test_core_strings
|
||||
|
||||
thread_test:
|
||||
$(ODIN) run thread $(COMMON) -out:test_core_thread
|
||||
|
||||
@@ -107,7 +107,7 @@ echo ---
|
||||
echo ---
|
||||
echo Running core:strings tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run strings %COMMON% -out:test_core_strings.exe || exit /b
|
||||
%PATH_TO_ODIN% test strings %COMMON% -out:test_core_strings.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:thread tests
|
||||
|
||||
@@ -2,81 +2,42 @@ package test_core_strings
|
||||
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "base:runtime"
|
||||
import "core:mem"
|
||||
|
||||
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_index_any_small_string_not_found(&t)
|
||||
test_index_any_larger_string_not_found(&t)
|
||||
test_index_any_small_string_found(&t)
|
||||
test_index_any_larger_string_found(&t)
|
||||
test_cut(&t)
|
||||
test_case_conversion(&t)
|
||||
|
||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
||||
if TEST_fail > 0 {
|
||||
os.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@test
|
||||
test_index_any_small_string_not_found :: proc(t: ^testing.T) {
|
||||
index := strings.index_any(".", "/:\"")
|
||||
expect(t, index == -1, "index_any should be negative")
|
||||
testing.expect(t, index == -1, "index_any should be negative")
|
||||
}
|
||||
|
||||
@test
|
||||
test_index_any_larger_string_not_found :: proc(t: ^testing.T) {
|
||||
index := strings.index_any("aaaaaaaa.aaaaaaaa", "/:\"")
|
||||
expect(t, index == -1, "index_any should be negative")
|
||||
testing.expect(t, index == -1, "index_any should be negative")
|
||||
}
|
||||
|
||||
@test
|
||||
test_index_any_small_string_found :: proc(t: ^testing.T) {
|
||||
index := strings.index_any(".", "/:.\"")
|
||||
expect(t, index == 0, "index_any should be 0")
|
||||
testing.expect(t, index == 0, "index_any should be 0")
|
||||
}
|
||||
|
||||
@test
|
||||
test_index_any_larger_string_found :: proc(t: ^testing.T) {
|
||||
index := strings.index_any("aaaaaaaa:aaaaaaaa", "/:\"")
|
||||
expect(t, index == 8, "index_any should be 8")
|
||||
testing.expect(t, index == 8, "index_any should be 8")
|
||||
}
|
||||
|
||||
@test
|
||||
test_last_index_any_small_string_found :: proc(t: ^testing.T) {
|
||||
index := strings.last_index_any(".", "/:.\"")
|
||||
expect(t, index == 0, "last_index_any should be 0")
|
||||
testing.expect(t, index == 0, "last_index_any should be 0")
|
||||
}
|
||||
|
||||
@test
|
||||
test_last_index_any_small_string_not_found :: proc(t: ^testing.T) {
|
||||
index := strings.last_index_any(".", "/:\"")
|
||||
expect(t, index == -1, "last_index_any should be -1")
|
||||
testing.expect(t, index == -1, "last_index_any should be -1")
|
||||
}
|
||||
|
||||
Cut_Test :: struct {
|
||||
@@ -100,9 +61,12 @@ test_cut :: proc(t: ^testing.T) {
|
||||
res := strings.cut(test.input, test.offset, test.length)
|
||||
defer delete(res)
|
||||
|
||||
msg := fmt.tprintf("cut(\"%v\", %v, %v) expected to return \"%v\", got \"%v\"",
|
||||
test.input, test.offset, test.length, test.output, res)
|
||||
expect(t, res == test.output, msg)
|
||||
testing.expectf(
|
||||
t,
|
||||
res == test.output,
|
||||
"cut(\"%v\", %v, %v) expected to return \"%v\", got \"%v\"",
|
||||
test.input, test.offset, test.length, test.output, res,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +82,7 @@ Case_Kind :: enum {
|
||||
Ada_Case,
|
||||
}
|
||||
|
||||
Case_Proc :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error)
|
||||
Case_Proc :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error)
|
||||
|
||||
test_cases := [Case_Kind]struct{s: string, p: Case_Proc}{
|
||||
.Lower_Space_Case = {"hellope world", to_lower_space_case},
|
||||
@@ -132,33 +96,31 @@ test_cases := [Case_Kind]struct{s: string, p: Case_Proc}{
|
||||
.Ada_Case = {"Hellope_World", to_ada_case},
|
||||
}
|
||||
|
||||
to_lower_space_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) {
|
||||
to_lower_space_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) {
|
||||
return strings.to_delimiter_case(r, ' ', false, allocator)
|
||||
}
|
||||
to_upper_space_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) {
|
||||
to_upper_space_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) {
|
||||
return strings.to_delimiter_case(r, ' ', true, allocator)
|
||||
}
|
||||
|
||||
// NOTE: we have these wrappers as having #optional_allocator_error changes the type to not be equivalent
|
||||
to_snake_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_snake_case(r, allocator) }
|
||||
to_upper_snake_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_upper_snake_case(r, allocator) }
|
||||
to_kebab_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_kebab_case(r, allocator) }
|
||||
to_upper_kebab_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_upper_kebab_case(r, allocator) }
|
||||
to_camel_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_camel_case(r, allocator) }
|
||||
to_pascal_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_pascal_case(r, allocator) }
|
||||
to_ada_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_ada_case(r, allocator) }
|
||||
to_snake_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_snake_case(r, allocator) }
|
||||
to_upper_snake_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_upper_snake_case(r, allocator) }
|
||||
to_kebab_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_kebab_case(r, allocator) }
|
||||
to_upper_kebab_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_upper_kebab_case(r, allocator) }
|
||||
to_camel_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_camel_case(r, allocator) }
|
||||
to_pascal_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_pascal_case(r, allocator) }
|
||||
to_ada_case :: proc(r: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { return strings.to_ada_case(r, allocator) }
|
||||
|
||||
@test
|
||||
test_case_conversion :: proc(t: ^testing.T) {
|
||||
for entry in test_cases {
|
||||
for test_case, case_kind in test_cases {
|
||||
result, err := entry.p(test_case.s, context.allocator)
|
||||
msg := fmt.tprintf("ERROR: We got the allocation error '{}'\n", err)
|
||||
expect(t, err == nil, msg)
|
||||
testing.expectf(t, err == nil, "ERROR: We got the allocation error '{}'\n", err)
|
||||
defer delete(result)
|
||||
|
||||
msg = fmt.tprintf("ERROR: Input `{}` to converter {} does not match `{}`, got `{}`.\n", test_case.s, case_kind, entry.s, result)
|
||||
expect(t, result == entry.s, msg)
|
||||
testing.expectf(t, result == entry.s, "ERROR: Input `{}` to converter {} does not match `{}`, got `{}`.\n", test_case.s, case_kind, entry.s, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user