|
@@ -4,6 +4,56 @@ import "core:math"
|
|
|
import "core:strconv"
|
|
|
import "core:testing"
|
|
|
|
|
|
+@(test)
|
|
|
+test_float :: proc(t: ^testing.T) {
|
|
|
+ n: int
|
|
|
+ f: f64
|
|
|
+ ok: bool
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("1.2", &n)
|
|
|
+ testing.expect_value(t, f, 1.2)
|
|
|
+ testing.expect_value(t, n, 3)
|
|
|
+ testing.expect_value(t, ok, true)
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("1.2a", &n)
|
|
|
+ testing.expect_value(t, f, 1.2)
|
|
|
+ testing.expect_value(t, n, 3)
|
|
|
+ testing.expect_value(t, ok, false)
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("+", &n)
|
|
|
+ testing.expect_value(t, f, 0)
|
|
|
+ testing.expect_value(t, n, 0)
|
|
|
+ testing.expect_value(t, ok, false)
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("-", &n)
|
|
|
+ testing.expect_value(t, f, 0)
|
|
|
+ testing.expect_value(t, n, 0)
|
|
|
+ testing.expect_value(t, ok, false)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+@(test)
|
|
|
+test_nan :: proc(t: ^testing.T) {
|
|
|
+ n: int
|
|
|
+ f: f64
|
|
|
+ ok: bool
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("nan", &n)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.NaN)
|
|
|
+ testing.expect_value(t, n, 3)
|
|
|
+ testing.expect_value(t, ok, true)
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("nAN", &n)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.NaN)
|
|
|
+ testing.expect_value(t, n, 3)
|
|
|
+ testing.expect_value(t, ok, true)
|
|
|
+
|
|
|
+ f, ok = strconv.parse_f64("Nani", &n)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.NaN)
|
|
|
+ testing.expect_value(t, n, 3)
|
|
|
+ testing.expect_value(t, ok, false)
|
|
|
+}
|
|
|
+
|
|
|
@(test)
|
|
|
test_infinity :: proc(t: ^testing.T) {
|
|
|
pos_inf := math.inf_f64(+1)
|
|
@@ -19,14 +69,17 @@ test_infinity :: proc(t: ^testing.T) {
|
|
|
testing.expect_value(t, f, pos_inf)
|
|
|
testing.expect_value(t, n, 3)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
|
|
|
} else if i == 8 { // "infinity"
|
|
|
testing.expect_value(t, f, pos_inf)
|
|
|
testing.expect_value(t, n, 8)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
|
|
|
} else { // invalid substring
|
|
|
testing.expect_value(t, f, 0)
|
|
|
testing.expect_value(t, n, 0)
|
|
|
testing.expect_value(t, ok, false)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Zero)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -38,14 +91,17 @@ test_infinity :: proc(t: ^testing.T) {
|
|
|
testing.expect_value(t, f, pos_inf)
|
|
|
testing.expect_value(t, n, 4)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
|
|
|
} else if i == 9 { // "+infinity"
|
|
|
testing.expect_value(t, f, pos_inf)
|
|
|
testing.expect_value(t, n, 9)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
|
|
|
} else { // invalid substring
|
|
|
testing.expect_value(t, f, 0)
|
|
|
testing.expect_value(t, n, 0)
|
|
|
testing.expect_value(t, ok, false)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Zero)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -57,14 +113,17 @@ test_infinity :: proc(t: ^testing.T) {
|
|
|
testing.expect_value(t, f, neg_inf)
|
|
|
testing.expect_value(t, n, 4)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Neg_Inf)
|
|
|
} else if i == 9 { // "-infinity"
|
|
|
testing.expect_value(t, f, neg_inf)
|
|
|
testing.expect_value(t, n, 9)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Neg_Inf)
|
|
|
} else { // invalid substring
|
|
|
testing.expect_value(t, f, 0)
|
|
|
testing.expect_value(t, n, 0)
|
|
|
testing.expect_value(t, ok, false)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Zero)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -75,5 +134,6 @@ test_infinity :: proc(t: ^testing.T) {
|
|
|
testing.expect_value(t, f, pos_inf)
|
|
|
testing.expect_value(t, n, 8)
|
|
|
testing.expect_value(t, ok, true)
|
|
|
+ testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
|
|
|
}
|
|
|
}
|