diff --git a/bower.json b/bower.json index 96f254e..5c4b0ab 100644 --- a/bower.json +++ b/bower.json @@ -22,6 +22,6 @@ }, "devDependencies": { "purescript-console": "^4.2.0", - "purescript-test-unit": "^15.0.0" + "purescript-assert": "^4.1.0" } } diff --git a/test/Main.purs b/test/Main.purs index c593e6c..ae50d27 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -10,10 +10,8 @@ import Data.Number.Approximate (Fraction(..), Tolerance(..), eqRelative, eqAbsolute, (≅), (≇)) import Effect (Effect) - -import Test.Unit (suite, test) -import Test.Unit.Assert (assert, assertFalse, equal) -import Test.Unit.Main (runTest) +import Effect.Console (log) +import Test.Assert (assertTrue', assertFalse', assertEqual) -- | Comparison up to 10% relative error. @@ -28,182 +26,232 @@ eqAbsolute' = eqAbsolute (Tolerance 0.1) infix 1 eqAbsolute' as =~= -main ∷ Effect Unit -main = runTest do - - - suite "Data.Number.fromString" do - test "valid number string" do - assert "integer strings are coerced" $ - fromMaybe false $ map (_ == 123.0) $ fromString "123" - - assert "decimals are coerced" $ - fromMaybe false $ map (_ == 12.34) $ fromString "12.34" +main :: Effect Unit +main = do - assert "exponents are coerced" $ - fromMaybe false $ map (_ == 1e4) $ fromString "1e4" - assert "decimals exponents are coerced" $ - fromMaybe false $ map (_ == 1.2e4) $ fromString "1.2e4" + log "Data.Number.fromString" + log "\tvalid number string" + assertTrue' "integer strings are coerced" $ + fromMaybe false $ map (_ == 123.0) $ fromString "123" - test "invalid number string" do - assert "invalid strings are not coerced" $ - Nothing == fromString "bad string" + assertTrue' "decimals are coerced" $ + fromMaybe false $ map (_ == 12.34) $ fromString "12.34" - test "too large numbers" do - assert "too large numbers are not coerced" $ - Nothing == fromString "1e1000" + assertTrue' "exponents are coerced" $ + fromMaybe false $ map (_ == 1e4) $ fromString "1e4" + assertTrue' "decimals exponents are coerced" $ + fromMaybe false $ map (_ == 1.2e4) $ fromString "1.2e4" - suite "Data.Number.isNaN" do - test "Check for NaN" do - assert "NaN is not a number" $ isNaN nan - assertFalse "infinity is a number" $ isNaN infinity - assertFalse "1.0 is a number" $ isNaN 1.0 + log "\tinvalid number string" + assertTrue' "invalid strings are not coerced" $ + Nothing == fromString "bad string" + log "\ttoo large numbers" + assertTrue' "too large numbers are not coerced" $ + Nothing == fromString "1e1000" - suite "Data.Number.isFinite" do - test "Check for infinity" do - assert "1.0e100 is a finite number" $ isFinite 1.0e100 - assertFalse "detect positive infinity" $ isFinite infinity - assertFalse "detect negative infinity" $ isFinite (-infinity) - assertFalse "detect NaN" $ isFinite nan + log "Data.Number.isNaN" + log "\tCheck for NaN" + assertTrue' "NaN is not a number" $ isNaN nan + assertFalse' "infinity is a number" $ isNaN infinity + assertFalse' "1.0 is a number" $ isNaN 1.0 + log "Data.Number.isFinite" + log "\tCheck for infinity" + assertTrue' "1.0e100 is a finite number" $ isFinite 1.0e100 + assertFalse' "detect positive infinity" $ isFinite infinity + assertFalse' "detect negative infinity" $ isFinite (-infinity) + assertFalse' "detect NaN" $ isFinite nan let pi = 3.14159 - suite "Data.Format.toStringWith" do - - test "precision" do - equal "3.14" (toStringWith (precision 3) pi) - equal "3.1416" (toStringWith (precision 5) pi) - equal "3" (toStringWith (precision 1) pi) - equal "3" (toStringWith (precision (-3)) pi) - equal "3.14" (toStringWith (precision 3) pi) - equal "1.2e+3" (toStringWith (precision 2) 1234.5) - - test "fixed" do - equal "3.14" (toStringWith (fixed 2) pi) - equal "3.1416" (toStringWith (fixed 4) pi) - equal "3" (toStringWith (precision 0) pi) - equal "3" (toStringWith (precision (-3)) pi) - equal "1234.5" (toStringWith (fixed 1) 1234.5) - - test "exponential" do - equal "3e+0" (toStringWith (exponential 0) pi) - equal "3.14e+0" (toStringWith (exponential 2) pi) - equal "3.14e+2" (toStringWith (exponential 2) (100.0 * pi)) - equal "1.2e+3" (toStringWith (exponential 1) 1234.5) - - suite "Data.Format.toString" do - - test "toString" do - equal "3.14159" (toString pi) - equal "10" (toString 10.0) - - suite "Data.Number.Approximate.eqRelative" do - test "eqRelative" do - assert "should return true for differences smaller 10%" $ - 10.0 ~= 10.9 - - assert "should return true for differences smaller 10%" $ - 10.0 ~= 9.2 - - assertFalse "should return false for differences larger than 10%" $ - 10.0 ~= 11.1 - - assertFalse "should return false for differences larger than 10%" $ - 10.0 ~= 9.01 - - test "eqRelative (large numbers)" do - assert "should return true for differences smaller 10%" $ - 100000000000.0 ~= 109000000000.0 - - assert "should return true for differences smaller 10%" $ - 100000000000.0 ~= 92000000000.0 - - assertFalse "should return false for differences larger than 10%" $ - 100000000000.0 ~= 111000000000.0 - - assertFalse "should return false for differences larger than 10%" $ - 100000000000.0 ~= 90000000000.0 - - test "eqRelative (small numbers)" do - assert "should return true for differences smaller 10%" $ - 0.000000000001 ~= 0.00000000000109 - - assert "should return true for differences smaller 10%" $ - 0.000000000001 ~= 0.00000000000092 - - assertFalse "should return false for differences larger than 10%" $ - 0.000000000001 ~= 0.00000000000111 - - assertFalse "should return false for differences larger than 10%" $ - 0.000000000001 ~= 0.0000000000009 - - test "eqRelative (negative numbers)" do - assert "should return true for differences smaller 10%" $ - -10.0 ~= -10.9 - - assert "should return true for differences smaller 10%" $ - -10.0 ~= -9.2 - - assertFalse "should return false for differences larger than 10%" $ - -10.0 ~= -11.1 - - assertFalse "should return false for differences larger than 10%" $ - -10.0 ~= -9.01 - - test "eqRelative (close or equal to 0.0)" do - assert "should compare against the fraction if left argument is zero" $ - 0.0 ~= 0.0001 + log "Data.Format.toStringWith" + + log "\tprecision" + assertEqual + { expected: "3.14" + , actual: toStringWith (precision 3) pi + } + assertEqual + { expected: "3.1416" + , actual: toStringWith (precision 5) pi + } + assertEqual + { expected: "3" + , actual: toStringWith (precision 1) pi + } + assertEqual + { expected: "3" + , actual: toStringWith (precision (-3)) pi + } + assertEqual + { expected: "3.14" + , actual: toStringWith (precision 3) pi + } + assertEqual + { expected: "1.2e+3" + , actual: toStringWith (precision 2) 1234.5 + } + + log "\tfixed" + assertEqual + { expected: "3.14" + , actual: toStringWith (fixed 2) pi + } + assertEqual + { expected: "3.1416" + , actual: toStringWith (fixed 4) pi + } + assertEqual + { expected: "3" + , actual: toStringWith (precision 0) pi + } + assertEqual + { expected: "3" + , actual: toStringWith (precision (-3)) pi + } + assertEqual + { expected: "1234.5" + , actual: toStringWith (fixed 1) 1234.5 + } + + log "\texponential" + assertEqual + { expected: "3e+0" + , actual: toStringWith (exponential 0) pi + } + assertEqual + { expected: "3.14e+0" + , actual: toStringWith (exponential 2) pi + } + assertEqual + { expected: "3.14e+2" + , actual: toStringWith (exponential 2) (100.0 * pi) + } + assertEqual + { expected: "1.2e+3" + , actual: toStringWith (exponential 1) 1234.5 + } + + log "Data.Format.toString" + + log "\ttoString" + assertEqual + { expected: "3.14159" + , actual: toString pi + } + + assertEqual + { expected: "10" + , actual: toString 10.0 + } + + log "Data.Number.Approximate.eqRelative" + log "\teqRelative" + assertTrue' "should return true for differences smaller 10%" $ + 10.0 ~= 10.9 + + assertTrue' "should return true for differences smaller 10%" $ + 10.0 ~= 9.2 + + assertFalse' "should return false for differences larger than 10%" $ + 10.0 ~= 11.1 + + assertFalse' "should return false for differences larger than 10%" $ + 10.0 ~= 9.01 + + log "\teqRelative (large numbers)" + assertTrue' "should return true for differences smaller 10%" $ + 100000000000.0 ~= 109000000000.0 + + assertTrue' "should return true for differences smaller 10%" $ + 100000000000.0 ~= 92000000000.0 + + assertFalse' "should return false for differences larger than 10%" $ + 100000000000.0 ~= 111000000000.0 + + assertFalse' "should return false for differences larger than 10%" $ + 100000000000.0 ~= 90000000000.0 + + log "\teqRelative (small numbers)" + assertTrue' "should return true for differences smaller 10%" $ + 0.000000000001 ~= 0.00000000000109 + + assertTrue' "should return true for differences smaller 10%" $ + 0.000000000001 ~= 0.00000000000092 + + assertFalse' "should return false for differences larger than 10%" $ + 0.000000000001 ~= 0.00000000000111 + + -- assertFalse + assertFalse' "should return false for differences larger than 10%" $ + 0.000000000001 ~= 0.0000000000009 + + log "\teqRelative (negative numbers)" + assertTrue' "should return true for differences smaller 10%" $ + -10.0 ~= -10.9 + + assertTrue' "should return true for differences smaller 10%" $ + -10.0 ~= -9.2 + + assertFalse' "should return false for differences larger than 10%" $ + -10.0 ~= -11.1 + + assertFalse' "should return false for differences larger than 10%" $ + -10.0 ~= -9.01 + + log "\teqRelative (close or equal to 0.0)" + assertTrue' "should compare against the fraction if left argument is zero" $ + 0.0 ~= 0.0001 - assert "should compare against the fraction if right argument is zero" $ - 0.0001 ~= 0.0 + assertTrue' "should compare against the fraction if right argument is zero" $ + 0.0001 ~= 0.0 - assert "should succeed if both arguments are zero" $ - 0.0 ~= 0.0 + assertTrue' "should succeed if both arguments are zero" $ + 0.0 ~= 0.0 - assertFalse "should fail if argument is larger than fraction" $ - 0.0 ~= 0.11 + assertFalse' "should fail if argument is larger than fraction" $ + 0.0 ~= 0.11 - assertFalse "should fail if other argument is not exactly zero" $ - 1.0e-100 ~= 0.1 + assertFalse' "should fail if other argument is not exactly zero" $ + 1.0e-100 ~= 0.1 - test "eqRelative (fraction = 0.0)" do - assert "should succeed if numbers are exactly equal" $ - eqRelative (Fraction 0.0) 3.14 3.14 + log "\teqRelative (fraction = 0.0)" + assertTrue' "should succeed if numbers are exactly equal" $ + eqRelative (Fraction 0.0) 3.14 3.14 - assertFalse "should fail if numbers are not exactly equal" $ - eqRelative (Fraction 0.0) 3.14 3.14000000000001 + assertFalse' "should fail if numbers are not exactly equal" $ + eqRelative (Fraction 0.0) 3.14 3.14000000000001 - test "eqRelative (fraction > 1.0)" do - assert "should work for 'fractions' larger than one" $ - eqRelative (Fraction 3.0) 10.0 29.5 + log "\teqRelative (fraction > 1.0)" + assertTrue' "should work for 'fractions' larger than one" $ + eqRelative (Fraction 3.0) 10.0 29.5 - suite "Data.Number.Approximate.eqApproximate" do - test "0.1 + 0.2 ≅ 0.3" do - assert "0.1 + 0.2 should be approximately equal to 0.3" $ - 0.1 + 0.2 ≅ 0.3 + log "Data.Number.Approximate.eqApproximate" + log "\t0.1 + 0.2 ≅ 0.3" + assertTrue' "0.1 + 0.2 should be approximately equal to 0.3" $ + 0.1 + 0.2 ≅ 0.3 - assert "0.1 + 0.200001 should not be approximately equal to 0.3" $ - 0.1 + 0.200001 ≇ 0.3 + assertTrue' "0.1 + 0.200001 should not be approximately equal to 0.3" $ + 0.1 + 0.200001 ≇ 0.3 - suite "Data.Number.Approximate.eqAbsolute" do - test "eqAbsolute" do - assert "should succeed for differences smaller than the tolerance" $ - 10.0 =~= 10.09 + log "Data.Number.Approximate.eqAbsolute" + log "\teqAbsolute" + assertTrue' "should succeed for differences smaller than the tolerance" $ + 10.0 =~= 10.09 - assert "should succeed for differences smaller than the tolerance" $ - 9.91 ~= 10.00 + assertTrue' "should succeed for differences smaller than the tolerance" $ + 9.91 ~= 10.00 - assertFalse "should fail for differences larger than the tolerance" $ - 10.0 =~= 10.11 + assertFalse' "should fail for differences larger than the tolerance" $ + 10.0 =~= 10.11 - assertFalse "should fail for differences larger than the tolerance" $ - 9.89 =~= 10.0 + assertFalse' "should fail for differences larger than the tolerance" $ + 9.89 =~= 10.0 - test "eqAbsolute (compare against 0)" do - assert "should succeed for numbers smaller than the tolerance" $ - 0.0 ~= -0.09 + log "\teqAbsolute (compare against 0)" + assertTrue' "should succeed for numbers smaller than the tolerance" $ + 0.0 ~= -0.09