Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit e9d2073

Browse files
committed
Update tests for 0.7
1 parent 7a570d3 commit e9d2073

File tree

3 files changed

+105
-104
lines changed

3 files changed

+105
-104
lines changed

tests/Data/Map.purs

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ module Tests.Data.Map where
33
import Prelude
44

55
import Control.Alt ((<|>))
6-
import Data.Array (groupBy, map, length, nubBy, sortBy)
6+
import Data.List (List(..), groupBy, length, nubBy, sortBy, singleton, toList)
77
import Data.Foldable (foldl, for_)
88
import Data.Function (on)
99
import Data.Maybe (Maybe(..), fromMaybe)
10-
import Data.Int (fromNumber)
1110
import Data.Tuple (Tuple(..), fst)
12-
import Debug.Trace
11+
import Control.Monad.Eff.Console (log)
12+
import Test.Data.List
1313
import Test.QuickCheck ((<?>), quickCheck, quickCheck')
1414
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
15+
import Test.QuickCheck.Gen (Gen(..))
1516
import qualified Data.Map as M
1617

1718
instance arbMap :: (Eq k, Ord k, Arbitrary k, Arbitrary v) => Arbitrary (M.Map k v) where
@@ -32,33 +33,32 @@ instance showSmallKey :: Show SmallKey where
3233
show J = "J"
3334

3435
instance eqSmallKey :: Eq SmallKey where
35-
(==) A A = true
36-
(==) B B = true
37-
(==) C C = true
38-
(==) D D = true
39-
(==) E E = true
40-
(==) F F = true
41-
(==) G G = true
42-
(==) H H = true
43-
(==) I I = true
44-
(==) J J = true
45-
(==) _ _ = false
46-
(/=) x y = not (x == y)
47-
48-
smallKeyToNumber :: SmallKey -> Number
49-
smallKeyToNumber A = 0
50-
smallKeyToNumber B = 1
51-
smallKeyToNumber C = 2
52-
smallKeyToNumber D = 3
53-
smallKeyToNumber E = 4
54-
smallKeyToNumber F = 5
55-
smallKeyToNumber G = 6
56-
smallKeyToNumber H = 7
57-
smallKeyToNumber I = 8
58-
smallKeyToNumber J = 9
36+
eq A A = true
37+
eq B B = true
38+
eq C C = true
39+
eq D D = true
40+
eq E E = true
41+
eq F F = true
42+
eq G G = true
43+
eq H H = true
44+
eq I I = true
45+
eq J J = true
46+
eq _ _ = false
47+
48+
smallKeyToInt :: SmallKey -> Int
49+
smallKeyToInt A = 0
50+
smallKeyToInt B = 1
51+
smallKeyToInt C = 2
52+
smallKeyToInt D = 3
53+
smallKeyToInt E = 4
54+
smallKeyToInt F = 5
55+
smallKeyToInt G = 6
56+
smallKeyToInt H = 7
57+
smallKeyToInt I = 8
58+
smallKeyToInt J = 9
5959

6060
instance ordSmallKey :: Ord SmallKey where
61-
compare = compare `on` smallKeyToNumber
61+
compare = compare `on` smallKeyToInt
6262

6363
instance arbSmallKey :: Arbitrary SmallKey where
6464
arbitrary = do
@@ -93,7 +93,7 @@ instance arbInstruction :: (Arbitrary k, Arbitrary v) => Arbitrary (Instruction
9393
k <- arbitrary
9494
return (Delete k)
9595

96-
runInstructions :: forall k v. (Ord k) => [Instruction k v] -> M.Map k v -> M.Map k v
96+
runInstructions :: forall k v. (Ord k) => List (Instruction k v) -> M.Map k v -> M.Map k v
9797
runInstructions instrs t0 = foldl step t0 instrs
9898
where
9999
step tree (Insert k v) = M.insert k v tree
@@ -102,98 +102,98 @@ runInstructions instrs t0 = foldl step t0 instrs
102102
smallKey :: SmallKey -> SmallKey
103103
smallKey k = k
104104

105-
number :: Number -> Number
105+
number :: Int -> Int
106106
number n = n
107107

108108
mapTests = do
109109

110110
-- Data.Map
111111

112-
trace "Test inserting into empty tree"
112+
log "Test inserting into empty tree"
113113
quickCheck $ \k v -> M.lookup (smallKey k) (M.insert k v M.empty) == Just (number v)
114114
<?> ("k: " ++ show k ++ ", v: " ++ show v)
115115

116-
trace "Test delete after inserting"
116+
log "Test delete after inserting"
117117
quickCheck $ \k v -> M.isEmpty (M.delete (smallKey k) (M.insert k (number v) M.empty))
118118
<?> ("k: " ++ show k ++ ", v: " ++ show v)
119119

120-
trace "Insert two, lookup first"
120+
log "Insert two, lookup first"
121121
quickCheck $ \k1 v1 k2 v2 -> k1 == k2 || M.lookup k1 (M.insert (smallKey k2) (number v2) (M.insert (smallKey k1) (number v1) M.empty)) == Just v1
122122
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
123123

124-
trace "Insert two, lookup second"
124+
log "Insert two, lookup second"
125125
quickCheck $ \k1 v1 k2 v2 -> M.lookup k2 (M.insert (smallKey k2) (number v2) (M.insert (smallKey k1) (number v1) M.empty)) == Just v2
126126
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
127127

128-
trace "Insert two, delete one"
128+
log "Insert two, delete one"
129129
quickCheck $ \k1 v1 k2 v2 -> k1 == k2 || M.lookup k2 (M.delete k1 (M.insert (smallKey k2) (number v2) (M.insert (smallKey k1) (number v1) M.empty))) == Just v2
130130
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
131131

132-
trace "Check balance property"
133-
quickCheck' (fromNumber 5000) $ \instrs ->
132+
log "Check balance property"
133+
quickCheck' 5000 $ \instrs ->
134134
let
135-
tree :: M.Map SmallKey Number
135+
tree :: M.Map SmallKey Int
136136
tree = runInstructions instrs M.empty
137137
in M.checkValid tree <?> ("Map not balanced:\n " ++ show tree ++ "\nGenerated by:\n " ++ show instrs)
138138

139-
trace "Lookup from empty"
140-
quickCheck $ \k -> M.lookup k (M.empty :: M.Map SmallKey Number) == Nothing
139+
log "Lookup from empty"
140+
quickCheck $ \k -> M.lookup k (M.empty :: M.Map SmallKey Int) == Nothing
141141

142-
trace "Lookup from singleton"
143-
quickCheck $ \k v -> M.lookup (k :: SmallKey) (M.singleton k (v :: Number)) == Just v
142+
log "Lookup from singleton"
143+
quickCheck $ \k v -> M.lookup (k :: SmallKey) (M.singleton k (v :: Int)) == Just v
144144

145-
trace "Random lookup"
146-
quickCheck' (fromNumber 5000) $ \instrs k v ->
145+
log "Random lookup"
146+
quickCheck' 5000 $ \instrs k v ->
147147
let
148-
tree :: M.Map SmallKey Number
148+
tree :: M.Map SmallKey Int
149149
tree = M.insert k v (runInstructions instrs M.empty)
150150
in M.lookup k tree == Just v <?> ("instrs:\n " ++ show instrs ++ "\nk:\n " ++ show k ++ "\nv:\n " ++ show v)
151151

152-
trace "Singleton to list"
153-
quickCheck $ \k v -> M.toList (M.singleton k v :: M.Map SmallKey Number) == [Tuple k v]
152+
log "Singleton to list"
153+
quickCheck $ \k v -> M.toList (M.singleton k v :: M.Map SmallKey Int) == singleton (Tuple k v)
154154

155-
trace "toList . fromList = id"
155+
log "toList . fromList = id"
156156
quickCheck $ \arr -> let f x = M.toList (M.fromList x)
157-
in f (f arr) == f (arr :: [Tuple SmallKey Number]) <?> show arr
157+
in f (f arr) == f (arr :: List (Tuple SmallKey Int)) <?> show arr
158158

159-
trace "fromList . toList = id"
159+
log "fromList . toList = id"
160160
quickCheck $ \m -> let f m = M.fromList (M.toList m) in
161-
M.toList (f m) == M.toList (m :: M.Map SmallKey Number) <?> show m
161+
M.toList (f m) == M.toList (m :: M.Map SmallKey Int) <?> show m
162162

163-
trace "fromListWith const = fromList"
163+
log "fromListWith const = fromList"
164164
quickCheck $ \arr -> M.fromListWith const arr ==
165-
M.fromList (arr :: [Tuple SmallKey Number]) <?> show arr
165+
M.fromList (arr :: List (Tuple SmallKey Int)) <?> show arr
166166

167-
trace "fromListWith (<>) = fromList . collapse with (<>) . group on fst"
167+
log "fromListWith (<>) = fromList . collapse with (<>) . group on fst"
168168
quickCheck $ \arr ->
169169
let combine (Tuple s a) (Tuple t b) = (Tuple s $ b <> a)
170-
foldl1 g (x : xs) = foldl g x xs
170+
foldl1 g (Cons x xs) = foldl g x xs
171171
f = M.fromList <<< (<$>) (foldl1 combine) <<<
172172
groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
173-
M.fromListWith (<>) arr == f (arr :: [Tuple String String]) <?> show arr
173+
M.fromListWith (<>) arr == f (arr :: List (Tuple String String)) <?> show arr
174174

175-
trace "Lookup from union"
175+
log "Lookup from union"
176176
quickCheck $ \m1 m2 k -> M.lookup (smallKey k) (M.union m1 m2) == (case M.lookup k m1 of
177177
Nothing -> M.lookup k m2
178178
Just v -> Just (number v)) <?> ("m1: " ++ show m1 ++ ", m2: " ++ show m2 ++ ", k: " ++ show k ++ ", v1: " ++ show (M.lookup k m1) ++ ", v2: " ++ show (M.lookup k m2) ++ ", union: " ++ show (M.union m1 m2))
179179

180-
trace "Union is idempotent"
181-
quickCheck $ \m1 m2 -> (m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.Map SmallKey Number))
180+
log "Union is idempotent"
181+
quickCheck $ \m1 m2 -> (m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.Map SmallKey Int))
182182

183-
trace "Union prefers left"
184-
quickCheck $ \m1 m2 k -> M.lookup k (M.union m1 (m2 :: M.Map SmallKey Number)) == (M.lookup k m1 <|> M.lookup k m2)
183+
log "Union prefers left"
184+
quickCheck $ \m1 m2 k -> M.lookup k (M.union m1 (m2 :: M.Map SmallKey Int)) == (M.lookup k m1 <|> M.lookup k m2)
185185

186-
trace "unionWith"
186+
log "unionWith"
187187
for_ [Tuple (+) 0, Tuple (*) 1] $ \(Tuple op ident) ->
188188
quickCheck $ \m1 m2 k ->
189-
let u = M.unionWith op m1 m2 :: M.Map SmallKey Number
189+
let u = M.unionWith op m1 m2 :: M.Map SmallKey Int
190190
in case M.lookup k u of
191191
Nothing -> not (M.member k m1 || M.member k m2)
192192
Just v -> v == op (fromMaybe ident (M.lookup k m1)) (fromMaybe ident (M.lookup k m2))
193193

194-
trace "unionWith argument order"
194+
log "unionWith argument order"
195195
quickCheck $ \m1 m2 k ->
196-
let u = M.unionWith (-) m1 m2 :: M.Map SmallKey Number
196+
let u = M.unionWith (-) m1 m2 :: M.Map SmallKey Int
197197
in1 = M.member k m1
198198
v1 = M.lookup k m1
199199
in2 = M.member k m2
@@ -204,7 +204,7 @@ mapTests = do
204204
Just v -> Just v == v2
205205
Nothing -> not (in1 || in2)
206206

207-
trace "size"
207+
log "size"
208208
quickCheck $ \xs ->
209209
let xs' = nubBy ((==) `on` fst) xs
210-
in M.size (M.fromList xs') == length (xs' :: [Tuple SmallKey Number])
210+
in M.size (M.fromList xs') == length (xs' :: List (Tuple SmallKey Int))

tests/Data/StrMap.purs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ module Tests.Data.StrMap where
22

33
import Prelude
44

5-
import Data.Array (groupBy, map, sortBy)
5+
import Data.List (List(..), groupBy, sortBy, singleton, toList, zipWith)
66
import Data.Foldable (foldl)
77
import Data.Function (on)
88
import Data.Maybe (Maybe(..))
9-
import Data.Int (fromNumber)
10-
import Data.Tuple (Tuple(..), fst, zip)
11-
import Debug.Trace
9+
import Data.Tuple (Tuple(..), fst)
10+
import Control.Monad.Eff.Console (log)
11+
import Test.Data.List
1212
import Test.QuickCheck ((<?>), quickCheck, quickCheck')
1313
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
14+
import Test.QuickCheck.Gen (Gen(..))
1415
import qualified Data.String as S
1516
import qualified Data.StrMap as M
1617

@@ -34,79 +35,79 @@ instance arbInstruction :: (Arbitrary v) => Arbitrary (Instruction String v) whe
3435
false -> do
3536
return (Delete k)
3637

37-
runInstructions :: forall v. [Instruction String v] -> M.StrMap v -> M.StrMap v
38+
runInstructions :: forall v. List (Instruction String v) -> M.StrMap v -> M.StrMap v
3839
runInstructions instrs t0 = foldl step t0 instrs
3940
where
4041
step tree (Insert k v) = M.insert k v tree
4142
step tree (Delete k) = M.delete k tree
4243

43-
number :: Number -> Number
44+
number :: Int -> Int
4445
number n = n
4546

4647
strMapTests = do
47-
trace "Test inserting into empty tree"
48+
log "Test inserting into empty tree"
4849
quickCheck $ \k v -> M.lookup k (M.insert k v M.empty) == Just (number v)
4950
<?> ("k: " ++ show k ++ ", v: " ++ show v)
5051

51-
trace "Test delete after inserting"
52+
log "Test delete after inserting"
5253
quickCheck $ \k v -> M.isEmpty (M.delete k (M.insert k (number v) M.empty))
5354
<?> ("k: " ++ show k ++ ", v: " ++ show v)
5455

55-
trace "Insert two, lookup first"
56+
log "Insert two, lookup first"
5657
quickCheck $ \k1 v1 k2 v2 -> k1 == k2 || M.lookup k1 (M.insert k2 (number v2) (M.insert k1 (number v1) M.empty)) == Just v1
5758
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
5859

59-
trace "Insert two, lookup second"
60+
log "Insert two, lookup second"
6061
quickCheck $ \k1 v1 k2 v2 -> M.lookup k2 (M.insert k2 (number v2) (M.insert k1 (number v1) M.empty)) == Just v2
6162
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
6263

63-
trace "Insert two, delete one"
64+
log "Insert two, delete one"
6465
quickCheck $ \k1 v1 k2 v2 -> k1 == k2 || M.lookup k2 (M.delete k1 (M.insert k2 (number v2) (M.insert k1 (number v1) M.empty))) == Just v2
6566
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
6667

67-
trace "Lookup from empty"
68-
quickCheck $ \k -> M.lookup k (M.empty :: M.StrMap Number) == Nothing
68+
log "Lookup from empty"
69+
quickCheck $ \k -> M.lookup k (M.empty :: M.StrMap Int) == Nothing
6970

70-
trace "Lookup from singleton"
71-
quickCheck $ \k v -> M.lookup k (M.singleton k (v :: Number)) == Just v
71+
log "Lookup from singleton"
72+
quickCheck $ \k v -> M.lookup k (M.singleton k (v :: Int)) == Just v
7273

73-
trace "Random lookup"
74-
quickCheck' (fromNumber 5000) $ \instrs k v ->
74+
log "Random lookup"
75+
quickCheck' 5000 $ \instrs k v ->
7576
let
76-
tree :: M.StrMap Number
77+
tree :: M.StrMap Int
7778
tree = M.insert k v (runInstructions instrs M.empty)
7879
in M.lookup k tree == Just v <?> ("instrs:\n " ++ show instrs ++ "\nk:\n " ++ show k ++ "\nv:\n " ++ show v)
7980

80-
trace "Singleton to list"
81-
quickCheck $ \k v -> M.toList (M.singleton k v :: M.StrMap Number) == [Tuple k v]
81+
log "Singleton to list"
82+
quickCheck $ \k v -> M.toList (M.singleton k v :: M.StrMap Int) == singleton (Tuple k v)
8283

83-
trace "toList . fromList = id"
84+
log "toList . fromList = id"
8485
quickCheck $ \arr -> let f x = M.toList (M.fromList x)
85-
in f (f arr) == f (arr :: [Tuple String Number]) <?> show arr
86+
in f (f arr) == f (arr :: List (Tuple String Int)) <?> show arr
8687

87-
trace "fromList . toList = id"
88+
log "fromList . toList = id"
8889
quickCheck $ \m -> let f m = M.fromList (M.toList m) in
89-
M.toList (f m) == M.toList (m :: M.StrMap Number) <?> show m
90+
M.toList (f m) == M.toList (m :: M.StrMap Int) <?> show m
9091

91-
trace "fromListWith const = fromList"
92+
log "fromListWith const = fromList"
9293
quickCheck $ \arr -> M.fromListWith const arr ==
93-
M.fromList (arr :: [Tuple String Number]) <?> show arr
94+
M.fromList (arr :: List (Tuple String Int)) <?> show arr
9495

95-
trace "fromListWith (<>) = fromList . collapse with (<>) . group on fst"
96+
log "fromListWith (<>) = fromList . collapse with (<>) . group on fst"
9697
quickCheck $ \arr ->
9798
let combine (Tuple s a) (Tuple t b) = (Tuple s $ b <> a)
98-
foldl1 g (x : xs) = foldl g x xs
99+
foldl1 g (Cons x xs) = foldl g x xs
99100
f = M.fromList <<< (<$>) (foldl1 combine) <<<
100101
groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
101-
M.fromListWith (<>) arr == f (arr :: [Tuple String String]) <?> show arr
102+
M.fromListWith (<>) arr == f (arr :: List (Tuple String String)) <?> show arr
102103

103-
trace "Lookup from union"
104+
log "Lookup from union"
104105
quickCheck $ \m1 m2 k -> M.lookup k (M.union m1 m2) == (case M.lookup k m1 of
105106
Nothing -> M.lookup k m2
106107
Just v -> Just (number v)) <?> ("m1: " ++ show m1 ++ ", m2: " ++ show m2 ++ ", k: " ++ show k ++ ", v1: " ++ show (M.lookup k m1) ++ ", v2: " ++ show (M.lookup k m2) ++ ", union: " ++ show (M.union m1 m2))
107108

108-
trace "Union is idempotent"
109-
quickCheck $ \m1 m2 -> (m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.StrMap Number)) <?> (show (M.size (m1 `M.union` m2)) ++ " != " ++ show (M.size ((m1 `M.union` m2) `M.union` m2)))
109+
log "Union is idempotent"
110+
quickCheck $ \m1 m2 -> (m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.StrMap Int)) <?> (show (M.size (m1 `M.union` m2)) ++ " != " ++ show (M.size ((m1 `M.union` m2) `M.union` m2)))
110111

111-
trace "toList = zip keys values"
112-
quickCheck $ \m -> M.toList m == zip (M.keys m) (M.values m :: Array Number)
112+
log "toList = zip keys values"
113+
quickCheck $ \m -> M.toList m == zipWith Tuple (toList $ M.keys m) (M.values m :: List Int)

tests/Tests.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module Tests where
22

33
import Prelude
44

5-
import Debug.Trace
5+
import Control.Monad.Eff.Console (log)
66
import Test.QuickCheck
77

88
import Tests.Data.Map (mapTests)
99
import Tests.Data.StrMap (strMapTests)
1010

1111
main = do
12-
trace "Running Map tests"
12+
log "Running Map tests"
1313
mapTests
1414

15-
trace "Running StrMap tests"
15+
log "Running StrMap tests"
1616
strMapTests

0 commit comments

Comments
 (0)