Explorar el Código

Add initial test suite for `core:strconv`

Feoramund hace 1 año
padre
commit
7d670f6562
Se han modificado 3 ficheros con 88 adiciones y 0 borrados
  1. 4 0
      tests/core/Makefile
  2. 5 0
      tests/core/build.bat
  3. 79 0
      tests/core/strconv/test_core_strconv.odin

+ 4 - 0
tests/core/Makefile

@@ -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
 

+ 5 - 0
tests/core/build.bat

@@ -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 ---

+ 79 - 0
tests/core/strconv/test_core_strconv.odin

@@ -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)
+	}
+}