aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2023-09-19 15:13:05 +0100
committergingerBill <bill@gingerbill.org>2023-09-19 15:13:05 +0100
commit6257d0e1a9d9107043e13af410228a298d14abcb (patch)
tree35677554f24da2d66c8fb0c96893a2fc04189b00 /tests
parentb2f1c58321a21c3376e6ba329bdf928da5fabc94 (diff)
parentecde06e3a31179bd8f86383fd65cfbce31ab6d9a (diff)
Merge branch 'master' into windows-llvm-11.1.0windows-llvm-11.1.0
Diffstat (limited to 'tests')
-rw-r--r--tests/core/Makefile6
-rw-r--r--tests/core/fmt/test_core_fmt.odin59
2 files changed, 64 insertions, 1 deletions
diff --git a/tests/core/Makefile b/tests/core/Makefile
index 77d4b85a0..919262f85 100644
--- a/tests/core/Makefile
+++ b/tests/core/Makefile
@@ -2,7 +2,8 @@ ODIN=../../odin
PYTHON=$(shell which python3)
all: download_test_assets image_test compress_test strings_test hash_test crypto_test noise_test encoding_test \
- math_test linalg_glsl_math_test filepath_test reflect_test os_exit_test i18n_test match_test c_libc_test net_test
+ math_test linalg_glsl_math_test filepath_test reflect_test os_exit_test i18n_test match_test c_libc_test net_test \
+ fmt_test
download_test_assets:
$(PYTHON) download_assets.py
@@ -57,3 +58,6 @@ c_libc_test:
net_test:
$(ODIN) run net -out:test_core_net
+
+fmt_test:
+ $(ODIN) run fmt -out:test_core_fmt
diff --git a/tests/core/fmt/test_core_fmt.odin b/tests/core/fmt/test_core_fmt.odin
new file mode 100644
index 000000000..4459af609
--- /dev/null
+++ b/tests/core/fmt/test_core_fmt.odin
@@ -0,0 +1,59 @@
+package test_core_fmt
+
+import "core:fmt"
+import "core:os"
+import "core:testing"
+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)
+
+ fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
+ if TEST_fail > 0 {
+ os.exit(1)
+ }
+}
+
+test_fmt_memory :: proc(t: ^testing.T) {
+ 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)
+ }
+
+ check(t, "5b", "%m", 5)
+ check(t, "5B", "%M", 5)
+ check(t, "-5B", "%M", -5)
+ check(t, "3.00kib", "%m", mem.Kilobyte * 3)
+ check(t, "3kib", "%.0m", mem.Kilobyte * 3)
+ check(t, "3KiB", "%.0M", mem.Kilobyte * 3)
+ check(t, "3.000 mib", "%#.3m", mem.Megabyte * 3)
+ check(t, "3.50 gib", "%#m", u32(mem.Gigabyte * 3.5))
+ check(t, "01tib", "%5.0m", mem.Terabyte)
+ check(t, "-1tib", "%5.0m", -mem.Terabyte)
+ check(t, "2.50 pib", "%#5.m", uint(mem.Petabyte * 2.5))
+ check(t, "1.00 EiB", "%#M", mem.Exabyte)
+ check(t, "255 B", "%#M", u8(255))
+ check(t, "0b", "%m", u8(0))
+}