From 7b27027cd8d4b1ec0ede300b29926867f402f229 Mon Sep 17 00:00:00 2001 From: Yuito Murase Date: Mon, 4 Mar 2024 12:22:54 +0900 Subject: [PATCH] GH-142: Migrate test cases of Float --- __test__/satysrc/generic.saty | 51 -------------- test/float.test.satyg | 124 ++++++++++++++++++++++++++++++++++ test/main.test.saty | 2 + 3 files changed, 126 insertions(+), 51 deletions(-) create mode 100644 test/float.test.satyg diff --git a/__test__/satysrc/generic.saty b/__test__/satysrc/generic.saty index 38da84d..d59abf4 100644 --- a/__test__/satysrc/generic.saty +++ b/__test__/satysrc/generic.saty @@ -37,57 +37,6 @@ @import: ../../src/tree-map @import: ../../src/big-int -let () = Debug.log `==== Float ====` in -let p f = f |> String.of-float |> Debug.log in -let () = p Float.pi in -let () = p Float.e in -let l = [Float.inf; Float.ninf; Float.nan; 0.0; Float.neg 0.0; 1.0] in -let _ = l |> List.iter (fun a -> - l |> List.iter (fun b -> - Debug.log (String.of-float a ^ #` == `# ^ String.of-float b ^ #` = `# ^ String.of-bool Float.(a == b)) - ) -) in -let () = p (1.0 /. 0.0) in -let () = p (1.0 /. Float.neg 0.0) in -let () = p (0.0 /. 0.0) in -let () = p (Float.exp Float.pi) in -let () = p (Float.log Float.pi) in -let () = p (Float.pi |> Float.pow Float.pi) in -let () = p (2.1 |> Float.pow 54.0) in -let () = p (Float.sqrt 2.0) in -let () = p (Float.log10 Float.pi) in -let dbl-min = Stream.nat |> Stream.take 1022 |> Fn.flip Stream.fold 1.0 (fun f _ -> f /. 2.0) in -let () = p dbl-min in -let half-dbl-min = dbl-min /. 2.0 in -let () = p half-dbl-min in -let l = l |> List.append [dbl-min; half-dbl-min] in -let p-bool b = b |> String.of-bool |> Debug.log in -let () = l |> List.iter (fun f -> p-bool (Float.is-zero f)) in -let () = l |> List.iter (fun f -> p-bool (Float.is-inf f)) in -let () = l |> List.iter (fun f -> p-bool (Float.is-nan f)) in -let () = l |> List.iter (fun f -> p-bool (Float.is-normal f)) in -let () = l |> List.iter (fun f -> p-bool (Float.is-subnormal f)) in -let l = [2.3; 3.8; 5.5; Float.neg 2.3; Float.neg 3.8; Float.neg 5.5; 6.0] in -let () = l |> List.iter (fun a -> (p (Float.round a))) in -let () = l |> List.iter (fun a -> (p (Float.floor a))) in -let () = l |> List.iter (fun a -> (p (Float.ceil a))) in -let () = l |> List.iter (fun a -> (p (Float.truncate a))) in -% virtually testing exp2i -let () = [-1073; -1074; -1075; 1022; 1023; 1024] |> List.iter (fun a -> (p (1.0 |> Float.ldexp a))) in -let p-opt o = - let s = - match o with - | None -> `None` - | Some(i) -> `Some(` ^ Float.to-string i ^ `)` - in - Debug.log s -in -let () = p-opt(`123.45` |> Float.of-string-opt) in -let () = p-opt(`-123.45` |> Float.of-string-opt) in -let () = p-opt(`123.0` |> Float.of-string-opt) in -let () = p-opt(`0.111` |> Float.of-string-opt) in -let () = p-opt(`0.1a11` |> Float.of-string-opt) in - let () = Debug.log `==== Bool ====` in let p b = b |> Bool.to-string |> Debug.log in let p-opt o = diff --git a/test/float.test.satyg b/test/float.test.satyg new file mode 100644 index 0000000..f551cc9 --- /dev/null +++ b/test/float.test.satyg @@ -0,0 +1,124 @@ +@require: test/test +@require: test/expect + +@import: ../src/bool +@import: ../src/eq +@import: ../src/float +@import: ../src/stream +@import: ../src/string + +let float-test-cases = open Test in + let streq = Eq.equal String.eq in + let strleq = Eq.equal (List.eq String.eq) in + let boolleq = Eq.equal (List.eq Bool.eq) in + let flleq = Eq.equal (List.eq Float.eq) in + let floleq = Eq.equal (List.eq (Option.eq Float.eq)) in + % smallest normal value + let dbl-min = Stream.nat + |> Stream.take 1022 + |> Fn.flip Stream.fold 1.0 (fun f _ -> f /. 2.0) in + % subnormal value + let half-dbl-min = dbl-min /. 2.0 in + describe `Float module` [ + it `provide constants` (fun () -> ( + (String.of-float Float.pi + |> streq `3.14159265359`) + && (String.of-float Float.e + |> streq `2.71828182846`) + |> Expect.is-true + )); + it `check equality of special values` (fun () -> ( + Float.( + inf == inf + && ninf == ninf + && not (nan == nan) + && not (nan == Float.neg nan) + && 0.0 == 0.0 + && 0.0 == Float.neg 0.0 + ) |> Expect.is-true + )); + it `calculate division with zero` (fun () -> ( + Float.( + 1.0 /. 0.0 == inf + && 1.0 /. (neg 0.0) == ninf + && (0.0 /. 0.0 |> is-nan) + ) |> Expect.is-true + )); + it `calculuate exp, log, pow, sqrt, log10` (fun () -> ( + (Float.exp Float.pi + |> String.of-float + |> streq `23.1406926328`) + && (Float.log Float.pi + |> String.of-float + |> streq `1.14472988585`) + && (Float.pi |> Float.pow Float.pi + |> String.of-float + |> streq `36.4621596072`) + && (2.1 |> Float.pow 54.0 + |> String.of-float + |> streq `2.51097226443e+17`) + && (Float.sqrt 2.0 + |> String.of-float + |> streq `1.41421356237`) + && (Float.log10 Float.pi + |> String.of-float + |> streq `0.497149872694`) + |> Expect.is-true + )); + it `prnit smallest normal value and subnormal value` (fun () -> ( + (dbl-min |> String.of-float |> streq `2.22507385851e-308`) + && (half-dbl-min |> String.of-float |> streq `1.11253692925e-308`) + |> Expect.is-true + )); + it `check props of special values` (fun () -> ( + let props f = [ + Float.is-zero f; + Float.is-inf f; + Float.is-nan f; + Float.is-normal f; + Float.is-subnormal f; + ] in + (props Float.inf |> boolleq [false; true; false; false; false]) + && (props Float.ninf |> boolleq [false; true; false; false; false]) + && (props Float.nan |> boolleq [false; false; true; false; false]) + && (props 0.0 |> boolleq [true; false; false; false; false]) + && (props (Float.neg 0.0) |> boolleq [true; false; false; false; false]) + && (props 1.0 |> boolleq [false; false; false; true; false]) + && (props dbl-min |> boolleq [false; false; false; true; false]) + && (props half-dbl-min |> boolleq [false; false; false; false; true ]) + |> Expect.is-true + )); + it `round, floor, ceil, truncate` (fun () -> ( + open Float in + let l = [2.3; 3.8; 5.5; neg 2.3; neg 3.8; neg 5.5; 6.0] in + (l |> List.map round + |> flleq [2.; 4.; 6.; neg 2.; neg 4.; neg 6.; 6.]) + && (l |> List.map floor + |> flleq [2.; 3.; 5.; neg 3.; neg 4.; neg 6.; 6.]) + && (l |> List.map ceil + |> flleq [3.; 4.; 6.; neg 2.; neg 3.; neg 5.; 6.]) + && (l |> List.map truncate + |> flleq [2.; 3.; 5.; neg 2.; neg 3.; neg 5.; 6.]) + |> Expect.is-true + )); + it `virtually test exp2i` (fun () -> ( + [-1073; -1074; -1075; 1022; 1023; 1024] + |> List.map (fun a -> 1.0 |> Float.ldexp a) + |> List.map String.of-float + |> strleq [ + `9.88131291682e-324`; + `4.94065645841e-324`; + `0.`; + `4.49423283716e+307`; + `8.98846567431e+307`; + `inf`; + ] + |> Expect.is-true + )); + it `parse float` (fun () -> ( + [`123.45`; `-123.45`; `123.0`; `0.111`; `0.1a11`] + |> List.map Float.of-string-opt + |> floleq Option.([some 123.45; some (Float.neg 123.45); some 123.0; some 0.111; none]) + |> Expect.is-true + )) + ] diff --git a/test/main.test.saty b/test/main.test.saty index 5ce72e1..033ee64 100644 --- a/test/main.test.saty +++ b/test/main.test.saty @@ -8,6 +8,7 @@ @import: array.test @import: color.test +@import: float.test @import: int.test @import: lexer.test @import: list.test @@ -22,6 +23,7 @@ open Test in describe `base` [ array-test-cases; color-test-cases; + float-test-cases; int-test-cases; lexer-test-cases; list-test-cases;