mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-03 14:18:47 +00:00
Merge branch 'master' into fix-fmt-compquat-sign
This commit is contained in:
@@ -2,48 +2,9 @@ package test_core_fmt
|
||||
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:testing"
|
||||
import "core:math"
|
||||
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_fmt_memory(&t)
|
||||
test_fmt_doc_examples(&t)
|
||||
test_fmt_options(&t)
|
||||
|
||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
||||
if TEST_fail > 0 {
|
||||
os.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
check :: proc(t: ^testing.T, exp: string, format: string, args: ..any, loc := #caller_location) {
|
||||
got := fmt.tprintf(format, ..args)
|
||||
expect(t, got == exp, fmt.tprintf("(%q, %v): %q != %q", format, args, got, exp), loc)
|
||||
}
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
test_fmt_memory :: proc(t: ^testing.T) {
|
||||
@@ -141,7 +102,7 @@ test_fmt_doc_examples :: proc(t: ^testing.T) {
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_fmt_options :: proc(t: ^testing.T) {
|
||||
test_fmt_escaping_prefixes :: proc(t: ^testing.T) {
|
||||
// Escaping
|
||||
check(t, "% { } 0 { } } {", "%% {{ }} {} {{ }} }} {{", 0 )
|
||||
|
||||
@@ -152,7 +113,10 @@ test_fmt_options :: proc(t: ^testing.T) {
|
||||
check(t, "+3", "%+i", 3 )
|
||||
check(t, "0b11", "%#b", 3 )
|
||||
check(t, "0xA", "%#X", 10 )
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_fmt_indexing :: proc(t: ^testing.T) {
|
||||
// Specific index formatting
|
||||
check(t, "1 2 3", "%i %i %i", 1, 2, 3)
|
||||
check(t, "1 2 3", "%[0]i %[1]i %[2]i", 1, 2, 3)
|
||||
@@ -161,7 +125,10 @@ test_fmt_options :: proc(t: ^testing.T) {
|
||||
check(t, "1 2 3", "%i %[1]i %i", 1, 2, 3)
|
||||
check(t, "1 3 2", "%i %[2]i %i", 1, 2, 3)
|
||||
check(t, "1 1 1", "%[0]i %[0]i %[0]i", 1)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_fmt_width_precision :: proc(t: ^testing.T) {
|
||||
// Width
|
||||
check(t, "3.140", "%f", 3.14)
|
||||
check(t, "3.140", "%4f", 3.14)
|
||||
@@ -199,7 +166,10 @@ test_fmt_options :: proc(t: ^testing.T) {
|
||||
check(t, "3.140", "%*[1].*[2][0]f", 3.14, 5, 3)
|
||||
check(t, "3.140", "%*[2].*[1]f", 3.14, 3, 5)
|
||||
check(t, "3.140", "%5.*[1]f", 3.14, 3)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_fmt_arg_errors :: proc(t: ^testing.T) {
|
||||
// Error checking
|
||||
check(t, "%!(MISSING ARGUMENT)%!(NO VERB)", "%" )
|
||||
|
||||
@@ -222,7 +192,10 @@ test_fmt_options :: proc(t: ^testing.T) {
|
||||
check(t, "%!(BAD ARGUMENT NUMBER)%!(NO VERB)%!(EXTRA 0)", "%[1]", 0)
|
||||
|
||||
check(t, "3.1%!(EXTRA 3.14)", "%.1f", 3.14, 3.14)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_fmt_python_syntax :: proc(t: ^testing.T) {
|
||||
// Python-like syntax
|
||||
check(t, "1 2 3", "{} {} {}", 1, 2, 3)
|
||||
check(t, "3 2 1", "{2} {1} {0}", 1, 2, 3)
|
||||
@@ -247,3 +220,9 @@ test_fmt_options :: proc(t: ^testing.T) {
|
||||
check(t, "%!(MISSING CLOSE BRACE)%!(EXTRA 1)", "{", 1)
|
||||
check(t, "%!(MISSING CLOSE BRACE)%!(EXTRA 1)", "{0", 1 )
|
||||
}
|
||||
|
||||
@(private)
|
||||
check :: proc(t: ^testing.T, exp: string, format: string, args: ..any) {
|
||||
got := fmt.tprintf(format, ..args)
|
||||
testing.expectf(t, got == exp, "(%q, %v): %q != %q", format, args, got, exp)
|
||||
}
|
||||
Reference in New Issue
Block a user