Add initial test suite for core:strconv

This commit is contained in:
Feoramund
2024-06-04 13:39:31 -04:00
parent d33668fa91
commit 7d670f6562
3 changed files with 88 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ all_bsd: download_test_assets \
reflect_test \
runtime_test \
slice_test \
strconv_test \
strings_test \
thread_test \
time_test
@@ -98,6 +99,9 @@ runtime_test:
slice_test:
$(ODIN) test slice $(COMMON) -out:test_core_slice
strconv_test:
$(ODIN) test strconv $(COMMON) -out:test_core_strconv
strings_test:
$(ODIN) test strings $(COMMON) -out:test_core_strings

View File

@@ -103,6 +103,11 @@ echo Running core:slice tests
echo ---
%PATH_TO_ODIN% test slice %COMMON% -out:test_core_slice.exe || exit /b
echo ---
echo Running core:strconv tests
echo ---
%PATH_TO_ODIN% test strconv %COMMON% -out:test_core_strconv.exe || exit /b
echo ---
echo Running core:strings tests
echo ---

View File

@@ -0,0 +1,79 @@
package test_core_strconv
import "core:math"
import "core:strconv"
import "core:testing"
@(test)
test_infinity :: proc(t: ^testing.T) {
pos_inf := math.inf_f64(+1)
neg_inf := math.inf_f64(-1)
n: int
s := "infinity"
for i in 1 ..< len(s) + 1 {
ss := s[:i]
f, ok := strconv.parse_f64(ss, &n)
if i == 3 { // "inf"
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, true)
} else if i == 8 { // "infinity"
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 8)
testing.expect_value(t, ok, true)
} else { // invalid substring
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
}
}
s = "+infinity"
for i in 1 ..< len(s) + 1 {
ss := s[:i]
f, ok := strconv.parse_f64(ss, &n)
if i == 4 { // "+inf"
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 4)
testing.expect_value(t, ok, true)
} else if i == 9 { // "+infinity"
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 9)
testing.expect_value(t, ok, true)
} else { // invalid substring
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
}
}
s = "-infinity"
for i in 1 ..< len(s) + 1 {
ss := s[:i]
f, ok := strconv.parse_f64(ss, &n)
if i == 4 { // "-inf"
testing.expect_value(t, f, neg_inf)
testing.expect_value(t, n, 4)
testing.expect_value(t, ok, true)
} else if i == 9 { // "-infinity"
testing.expect_value(t, f, neg_inf)
testing.expect_value(t, n, 9)
testing.expect_value(t, ok, true)
} else { // invalid substring
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
}
}
// Make sure odd casing works.
batch := [?]string {"INFiniTY", "iNfInItY", "InFiNiTy"}
for ss in batch {
f, ok := strconv.parse_f64(ss, &n)
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 8)
testing.expect_value(t, ok, true)
}
}