/
GridTests.elm
126 lines (105 loc) · 4.2 KB
/
GridTests.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module GridTests exposing (..)
import Expect
import Fuzz exposing (Fuzzer)
import Grid exposing (Grid)
import SlowGrid
import Test exposing (..)
getTest : Test
getTest =
let
overInUnderFuzzer =
Fuzz.intRange -1 1
in
describe "get"
[ fuzz2 overInUnderFuzzer overInUnderFuzzer "only returns values in bounds" <|
\row column ->
let
grid =
Grid.initialize { rows = 1, columns = 1 } (always ())
in
if row == 0 && column == 0 then
Expect.equal (Just ()) (Grid.get { row = row, column = column } grid)
else
Expect.equal Nothing (Grid.get { row = row, column = column } grid)
]
getAndSetTest : Test
getAndSetTest =
fuzz coordsFuzzer "setting a value and then getting the same value should work" <|
\coords ->
blank10x10
|> Grid.set coords 1
|> Grid.get coords
|> Expect.equal (Just 1)
windowsTest : Test
windowsTest =
describe "windows"
[ test "creates a moving window over the grid data" <|
\_ ->
Grid.fromRowsAndColumns
[ [ 1, 2 ]
, [ 3, 4 ]
]
|> Grid.windows { width = 2, height = 2 }
|> Expect.equal
(Grid.fromRowsAndColumns
[ [ Grid.fromRowsAndColumns [ [ 1, 2 ], [ 3, 4 ] ]
, Grid.fromRowsAndColumns [ [ 2, 1 ], [ 4, 3 ] ]
]
, [ Grid.fromRowsAndColumns [ [ 3, 4 ], [ 1, 2 ] ]
, Grid.fromRowsAndColumns [ [ 4, 3 ], [ 2, 1 ] ]
]
]
)
]
{- Tests for compatibility with the older, slower, but known-good version. -}
compatibilityTest : Test
compatibilityTest =
describe "compatibility with the array-of-arrays implementation"
[ fuzz2 (Fuzz.intRange 0 2) (Fuzz.intRange 0 2) "initialize" <|
\rows columns ->
let
expected =
SlowGrid.toArrays (SlowGrid.initialize { rows = rows, columns = columns } identity)
actual =
Grid.toArrays (Grid.initialize { rows = rows, columns = columns } identity)
in
Expect.equal expected actual
, fuzz2 (Fuzz.intRange 0 2) (Fuzz.intRange 0 2) "rotate" <|
\rows columns ->
let
expected =
SlowGrid.initialize { rows = rows, columns = columns } identity
|> SlowGrid.rotate
|> SlowGrid.toArrays
actual =
Grid.initialize { rows = rows, columns = columns } identity
|> Grid.rotate
|> Grid.toArrays
in
Expect.equal expected actual
, fuzz2 (Fuzz.intRange 0 4) (Fuzz.intRange 0 4) "windows" <|
\rows columns ->
let
windowSize =
{ width = columns // 2, height = rows // 2 }
expected =
SlowGrid.initialize { rows = rows, columns = columns } identity
|> SlowGrid.windows windowSize
|> SlowGrid.map SlowGrid.toArrays
|> SlowGrid.toArrays
actual =
Grid.initialize { rows = rows, columns = columns } identity
|> Grid.windows windowSize
|> Grid.map Grid.toArrays
|> Grid.toArrays
in
Expect.equal expected actual
]
blank10x10 : Grid Int
blank10x10 =
Grid.initialize { rows = 10, columns = 10 } (always 0)
coordsFuzzer : Fuzzer { row : Int, column : Int }
coordsFuzzer =
Fuzz.map2 (\row column -> { row = row, column = column })
(Fuzz.intRange 0 9)
(Fuzz.intRange 0 9)