@@ -2,19 +2,15 @@ module Test.Data.Array.ST (testArrayST) where
2
2
3
3
import Prelude
4
4
5
- import Control.Monad.ST (ST)
6
5
import Control.Monad.ST as ST
7
- import Data.Array.ST (STArray, withArray)
6
+ import Data.Array.ST (withArray)
8
7
import Data.Array.ST as STA
9
8
import Data.Foldable (all)
10
9
import Data.Maybe (Maybe(..), isNothing)
11
10
import Effect (Effect)
12
11
import Effect.Console (log)
13
12
import Test.Assert (assert)
14
13
15
- run :: forall a. (forall r. ST r (STArray r a)) -> Array a
16
- run act = ST.run (act >>= STA.unsafeFreeze)
17
-
18
14
testArrayST :: Effect Unit
19
15
testArrayST = do
20
16
@@ -34,11 +30,11 @@ testArrayST = do
34
30
35
31
log "empty should produce an empty array"
36
32
37
- assert $ run STA.empty == nil
33
+ assert $ STA. run STA.empty == nil
38
34
39
35
log "thaw should produce an STArray from a standard array"
40
36
41
- assert $ run (STA.thaw [1, 2, 3]) == [1, 2, 3]
37
+ assert $ STA. run (STA.thaw [1, 2, 3]) == [1, 2, 3]
42
38
43
39
log "freeze should produce a standard array from an STArray"
44
40
@@ -48,17 +44,17 @@ testArrayST = do
48
44
49
45
log "unsafeThaw should produce an STArray from a standard array"
50
46
51
- assert $ run (STA.unsafeThaw [1, 2, 3]) == [1, 2, 3]
47
+ assert $ STA. run (STA.unsafeThaw [1, 2, 3]) == [1, 2, 3]
52
48
53
49
log "push should append a value to the end of the array"
54
50
55
- assert $ run (do
51
+ assert $ STA. run (do
56
52
arr <- STA.empty
57
53
void $ STA.push 1 arr
58
54
void $ STA.push 2 arr
59
55
pure arr) == [1, 2]
60
56
61
- assert $ run (do
57
+ assert $ STA. run (do
62
58
arr <- STA.thaw [1, 2, 3]
63
59
void $ STA.push 4 arr
64
60
pure arr) == [1, 2, 3, 4]
@@ -71,12 +67,12 @@ testArrayST = do
71
67
72
68
log "pushAll should append multiple values to the end of the array"
73
69
74
- assert $ run (do
70
+ assert $ STA. run (do
75
71
arr <- STA.empty
76
72
void $ STA.pushAll [1, 2] arr
77
73
pure arr) == [1, 2]
78
74
79
- assert $ run (do
75
+ assert $ STA. run (do
80
76
arr <- STA.thaw [1, 2, 3]
81
77
void $ STA.pushAll [4, 5, 6] arr
82
78
pure arr) == [1, 2, 3, 4, 5, 6]
@@ -137,36 +133,36 @@ testArrayST = do
137
133
138
134
log "poke should replace the value at the specified index"
139
135
140
- assert $ run (do
136
+ assert $ STA. run (do
141
137
arr <- STA.thaw [1]
142
138
void $ STA.poke 0 10 arr
143
139
pure arr) == [10]
144
140
145
141
log "poke should do nothing when attempting to modify a value outside the array bounds"
146
142
147
- assert $ run (do
143
+ assert $ STA. run (do
148
144
arr <- STA.thaw [1]
149
145
void $ STA.poke 1 2 arr
150
146
pure arr) == [1]
151
147
152
148
log "sort should reorder a list into ascending order based on the result of compare"
153
- assert $ run (
149
+ assert $ STA. run (
154
150
STA.sort =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
155
151
) == [1, 2, 3, 4, 5, 6]
156
152
157
153
log "sortBy should reorder a list into ascending order based on the result of a comparison function"
158
- assert $ run (
154
+ assert $ STA. run (
159
155
STA.sortBy (flip compare) =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
160
156
) == [6, 5, 4, 3, 2, 1]
161
157
162
158
log "sortWith should reorder a list into ascending order based on the result of compare over a projection"
163
- assert $ run (
159
+ assert $ STA. run (
164
160
STA.sortWith identity =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
165
161
) == [1, 2, 3, 4, 5, 6]
166
162
167
163
log "splice should be able to delete multiple items at a specified index"
168
164
169
- assert $ run (do
165
+ assert $ STA. run (do
170
166
arr <- STA.thaw [1, 2, 3, 4, 5]
171
167
void $ STA.splice 1 3 [] arr
172
168
pure arr) == [1, 5]
@@ -179,14 +175,14 @@ testArrayST = do
179
175
180
176
log "splice should be able to insert multiple items at a specified index"
181
177
182
- assert $ run (do
178
+ assert $ STA. run (do
183
179
arr <- STA.thaw [1, 2, 3, 4, 5]
184
180
void $ STA.splice 1 0 [0, 100] arr
185
181
pure arr) == [1, 0, 100, 2, 3, 4, 5]
186
182
187
183
log "splice should be able to delete and insert at the same time"
188
184
189
- assert $ run (do
185
+ assert $ STA. run (do
190
186
arr <- STA.thaw [1, 2, 3, 4, 5]
191
187
void $ STA.splice 1 2 [0, 100] arr
192
188
pure arr) == [1, 0, 100, 4, 5]
0 commit comments