Skip to content

Commit 43b8641

Browse files
berdariogaryb
authored andcommitted
NaN shouldn't compare EQ to itself (#91)
* NaN shouldn't compare EQ to itself * Add a bunch of tests for Ord * Styling
1 parent 371e981 commit 43b8641

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/Data/Ord/Unsafe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports.unsafeCompareImpl = function (lt) {
77
return function (gt) {
88
return function (x) {
99
return function (y) {
10-
return x < y ? lt : x > y ? gt : eq;
10+
return x < y ? lt : x === y ? eq : gt;
1111
};
1212
};
1313
};

test/Test/Main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// module Test.Main
44

5-
exports.mainImpl = function(showNumber) {
5+
exports.testNumberShow = function(showNumber) {
66
return function() {
77
function testAll(cases) {
88
cases.forEach(function(c) {
@@ -45,3 +45,8 @@ exports.mainImpl = function(showNumber) {
4545
};
4646
};
4747

48+
exports.throwErr = function(msg) {
49+
return function() {
50+
throw new Error(msg);
51+
}
52+
}

test/Test/Main.purs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,69 @@ import Prelude
55
type AlmostEff = Unit -> Unit
66

77
main :: AlmostEff
8-
main = mainImpl show
8+
main = do
9+
testNumberShow show
10+
testOrderings
11+
12+
foreign import testNumberShow :: (Number -> String) -> AlmostEff
13+
foreign import throwErr :: String -> AlmostEff
14+
15+
16+
assert :: String -> Boolean -> AlmostEff
17+
assert msg condition = if condition then const unit else throwErr msg
18+
19+
testOrd :: forall a. (Ord a, Show a) => a -> a -> Ordering -> AlmostEff
20+
testOrd x y ord =
21+
assert
22+
("(compare " <> show x <> " " <> show y <> " ) is not equal to " <> show ord)
23+
$ (compare x y) == ord
24+
25+
nan :: Number
26+
nan = 0.0/0.0
27+
28+
-- Unfortunately, NaN inhabits our Int
29+
intNan :: Int
30+
intNan = mod 1 0
31+
32+
plusInfinity :: Number
33+
plusInfinity = 1.0/0.0
34+
35+
minusInfinity :: Number
36+
minusInfinity = -1.0/0.0
37+
38+
testOrderings :: AlmostEff
39+
testOrderings = do
40+
assert "NaN shouldn't be equal to itself" $ nan /= nan
41+
assert "NaN shouldn't be equal to itself" $ (compare nan nan) /= EQ
42+
testOrd 1.0 2.0 LT
43+
testOrd 2.0 1.0 GT
44+
testOrd 1.0 (-2.0) GT
45+
testOrd (-2.0) 1.0 LT
46+
testOrd minusInfinity plusInfinity LT
47+
testOrd minusInfinity 0.0 LT
48+
testOrd plusInfinity 0.0 GT
49+
testOrd plusInfinity minusInfinity GT
50+
testOrd 1.0 nan GT
51+
testOrd nan 1.0 GT
52+
testOrd nan plusInfinity GT
53+
testOrd plusInfinity nan GT
54+
assert "1 > NaN should be false" $ (1.0 > nan) == false
55+
assert "1 < NaN should be false" $ (1.0 < nan) == false
56+
assert "NaN > 1 should be false" $ (nan > 1.0) == false
57+
assert "NaN < 1 should be false" $ (nan < 1.0) == false
58+
assert "NaN == 1 should be false" $ nan /= 1.0
59+
testOrd intNan 2147483647 GT
60+
testOrd 'a' 'b' LT
61+
testOrd 'b' 'A' GT
62+
testOrd "10" "0" GT
63+
testOrd "10" "2" LT
64+
testOrd true true EQ
65+
testOrd false false EQ
66+
testOrd false true LT
67+
testOrd true false GT
68+
testOrd ([] :: Array Int) [] EQ
69+
testOrd [1, 0] [1] GT
70+
testOrd [1] [1, 0] LT
71+
testOrd [1, 1] [1, 0] GT
72+
testOrd [1, -1] [1, 0] LT
973

10-
foreign import mainImpl :: (Number -> String) -> AlmostEff

0 commit comments

Comments
 (0)