-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContainersSpec.purs
440 lines (383 loc) · 15.3 KB
/
ContainersSpec.purs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
module Web3Spec.Encoding.ContainersSpec (spec, BMPString(..)) where
import Prelude
import Control.Monad.Gen (chooseInt, frequency, oneOf, suchThat)
import Data.Array (filter, foldMap, take, (..))
import Data.Array.NonEmpty (NonEmptyArray, fromArray)
import Data.Array.NonEmpty as NEA
import Data.Either (Either(..))
import Data.Enum (toEnumWithDefaults)
import Data.Foldable (for_)
import Data.Int (toNumber)
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype, un)
import Data.NonEmpty (NonEmpty(..))
import Data.Reflectable (reifyType)
import Data.String (CodePoint, fromCodePointArray)
import Data.Tuple (Tuple(..))
import Effect.Class (liftEffect)
import Effect.Class.Console as Console
import Network.Ethereum.Core.HexString as Hex
import Network.Ethereum.Core.Signatures as Address
import Network.Ethereum.Web3.Solidity (class ABIDecode, class ABIEncode, class EncodingType, Tuple2(..), Tuple3(..), Tuple4(..), Tuple5(..), abiDecode, abiEncode)
import Network.Ethereum.Web3.Solidity.Bytes as BytesN
import Network.Ethereum.Web3.Solidity.Int as IntN
import Network.Ethereum.Web3.Solidity.UInt as UIntN
import Network.Ethereum.Web3.Solidity.Vector as Vector
import Parsing (ParseError)
import Partial.Unsafe (unsafeCrashWith)
import Test.QuickCheck (class Arbitrary, arbitrary, quickCheck, quickCheckGen, quickCheckGen', (===))
import Test.QuickCheck.Gen (Gen, arrayOf)
import Test.Spec (Spec, describe, it)
spec :: Spec Unit
spec =
describe "encoding-spec for containers" do
typePropertyTests
arrayTypePropertyTests
vecTypePropertyTests
nestedTypePropertyTests
tupleTests
typePropertyTests :: Spec Unit
typePropertyTests =
describe "Type property tests" do
it "can encode/decode a string" $ liftEffect $ do
Console.log "wtf"
quickCheck \(x :: BMPString) ->
let
y = un BMPString x
in
(encodeDecode y) === Right y
it "can encode/decode bytestring" $ liftEffect $ do
quickCheckGen $ do
n <- chooseInt 1 100
x <- Hex.toBuffer <$> Hex.generator n
pure $ encodeDecode x === Right x
it "can encode/decode bool" $ liftEffect $ do
quickCheck \(x :: Boolean) -> encodeDecode x === Right x
it "can encode/decode address" $ liftEffect $ do
quickCheckGen $ do
x <- Address.generator
pure $ encodeDecode x === Right x
it "can encode/decode intN" $ liftEffect $ do
for_ intSizes $ \n -> quickCheckGen $ do
reifyType n \p -> do
x <- IntN.generator p
pure $ encodeDecode x === Right x
it "can encode/decode uintN" $ liftEffect $ do
for_ intSizes $ \n -> quickCheckGen $ do
reifyType n \p -> do
x <- UIntN.generator p
pure $ encodeDecode x === Right x
it "can encode/decode bytesN" $ liftEffect $ do
for_ bytesSizes $ \n -> quickCheckGen $ do
reifyType n \p -> do
x <- BytesN.generator p
pure $ encodeDecode x === Right x
it "can encode/decode string" $ liftEffect $ do
quickCheck \(x :: BMPString) ->
let
y = un BMPString x
in
encodeDecode y === Right y
arrayTypePropertyTests :: Spec Unit
arrayTypePropertyTests = do
describe "Array type property tests" do
it "Can encode/decode intN[]" $ liftEffect do
for_ intSizes $ \n -> quickCheckGen $ do
reifyType n \p -> do
x <- arrayOf (IntN.generator p)
pure $ encodeDecode x === Right x
it "Can encode/decode uintN[]" $ liftEffect do
for_ intSizes $ \n -> quickCheckGen $ do
reifyType n \p -> do
x <- arrayOf (UIntN.generator p)
pure $ encodeDecode x === Right x
it "Can encode/decode bytesN[]" $ liftEffect do
for_ bytesSizes $ \n -> quickCheckGen $ do
reifyType n \p -> do
x <- arrayOf (BytesN.generator p)
pure $ encodeDecode x === Right x
it "Can encode/decode address[]" $ liftEffect do
quickCheckGen $ do
x <- Address.generator
pure $ encodeDecode x === Right x
it "Can encode/decode string[]" $ liftEffect do
quickCheck $ \(x :: Array BMPString) ->
let
y = map (un BMPString) x
in
encodeDecode y === Right y
vecTypePropertyTests :: Spec Unit
vecTypePropertyTests = do
describe "Vector type property tests" do
it "Can encode/decode intN[k]" $ liftEffect do
for_ intSizes $ \n ->
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk ->
reifyType n \pn -> do
x <- Vector.generator pk (IntN.generator pn)
pure $ encodeDecode x === Right x
it "Can encode/decode uintN[k]" $ liftEffect do
for_ intSizes $ \n ->
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk ->
reifyType n \pn -> do
x <- Vector.generator pk (UIntN.generator pn)
pure $ encodeDecode x === Right x
it "Can encode/decode bytesN[k]" $ liftEffect do
for_ bytesSizes $ \n ->
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk ->
reifyType n \pn -> do
x <- Vector.generator pk (BytesN.generator pn)
pure $ encodeDecode x === Right x
it "Can encode/decode address[k]" $ liftEffect do
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk -> do
x <- Vector.generator pk Address.generator
pure $ encodeDecode x === Right x
it "Can encode/decode string[k]" $ liftEffect do
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk -> do
_x <- Vector.generator pk (arbitrary :: Gen BMPString)
let x = un BMPString <$> _x
pure $ encodeDecode x === Right x
nestedTypePropertyTests :: Spec Unit
nestedTypePropertyTests = do
describe "Nested type property tests for vector, vector" do
it "Can encode/decode bytesN[k1][k2]" $ liftEffect do
for_ bytesSizes $ \n -> do
quickCheckGen $ do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
reifyType k1 \pk1 ->
reifyType k2 \pk2 ->
reifyType n \pn -> do
x <- Vector.generator pk2 (Vector.generator pk1 (BytesN.generator pn))
pure $ encodeDecode x === Right x
it "Can encode/decode string[k1][k2]" $ liftEffect do
quickCheckGen $ do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
reifyType k1 \pk1 ->
reifyType k2 \pk2 -> do
_x <- Vector.generator pk2 (Vector.generator pk1 (arbitrary :: Gen BMPString))
let x = map (un BMPString) <$> _x
pure $ encodeDecode x === Right x
describe "Nested type property tests for array, vector" do
it "Can encode/decode bytesN[k][]" $ liftEffect do
for_ bytesSizes $ \n -> do
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk ->
reifyType n \pn -> do
x <- arrayOf (Vector.generator pk (BytesN.generator pn))
pure $ encodeDecode x === Right x
it "Can encode/decode string[k][]" $ liftEffect do
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk -> do
_x <- arrayOf (Vector.generator pk (arbitrary :: Gen BMPString))
let x = map (un BMPString) <$> _x
pure $ encodeDecode x === Right x
describe "Nested type property tests for vector, array" do
it "Can encode/decode uintN[][k]" $ liftEffect do
for_ intSizes $ \n -> do
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk ->
reifyType n \pn -> do
x <- (Vector.generator pk (arrayOf $ UIntN.generator pn))
pure $ encodeDecode x === Right x
it "Can encode/decode string[][k]" $ liftEffect do
quickCheckGen $ do
k <- chooseInt 1 10
reifyType k \pk -> do
_x <- (Vector.generator pk (arrayOf (arbitrary :: Gen BMPString)))
let x = map (un BMPString) <$> _x
pure $ encodeDecode x === Right x
describe "Nested type property tests for array, array" do
it "Can encode/decode intN[][]" $ liftEffect do
for_ intSizes $ \n -> do
quickCheckGen $
reifyType n \pn -> do
x <- (arrayOf (arrayOf $ IntN.generator pn))
pure $ encodeDecode x === Right x
it "Can encode/decode string[][]" $ liftEffect do
quickCheck \(x :: Array (Array BMPString)) ->
let
y = map (map (un BMPString)) x
in
encodeDecode y === Right y
tupleTests :: Spec Unit
tupleTests = do
describe "Basic static sized Tuple Tests" $ do
it "Can encode/decode (intN, address, bool, uintN, bytesN)" $ liftEffect do
quickCheckGen $ do
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
k <- oneOf (pure <$> bytesSizes)
reifyType n \pn ->
reifyType m \pm ->
reifyType k \pk -> do
int <- IntN.generator pn
addr <- Address.generator
bool <- arbitrary :: Gen Boolean
uint <- UIntN.generator pm
bytes <- BytesN.generator pk
let x = Tuple5 int addr bool uint bytes
pure $ encodeDecode x === Right x
it "Can encode/decode (address[k], bool, intN[k], uint)" $ liftEffect do
quickCheckGen' 1 $ do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
reifyType k1 \pk1 ->
reifyType k2 \pk2 ->
reifyType n \pn -> do
reifyType m \pm -> do
addrs <- arrayOf (Vector.generator pk1 Address.generator)
bool <- arbitrary @Boolean
ints <- Vector.generator pk2 (IntN.generator pn)
uint <- (UIntN.generator pm)
let x = Tuple4 addrs bool ints uint
pure $ encodeDecode x === Right x
describe "Basic dynamic sized Tuple Tests" $ do
it "Can encode/decode (intN[], bytes, address[][k], string[k][], bool)" $ liftEffect do
quickCheckGen $ do
n <- oneOf (pure <$> intSizes)
m <- chooseInt 1 10
k <- chooseInt 1 10
reifyType n \pn ->
reifyType m \pm ->
reifyType k \pk -> do
ints <- arrayOf (IntN.generator pn)
bytes <- Hex.toBuffer <$> (chooseInt 1 100 >>= Hex.generator)
addrs <- Vector.generator pm (arrayOf Address.generator)
strings <- arrayOf (Vector.generator pk (arbitrary @BMPString))
bool <- arbitrary :: Gen Boolean
let x = Tuple5 ints bytes addrs (map (un BMPString) <$> strings) bool
pure $ encodeDecode x === Right x
it "Can encode/decode (address[k], bool, intN[k], uint)" $ liftEffect do
quickCheckGen' 5 $ do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
reifyType k1 \pk1 ->
reifyType k2 \pk2 ->
reifyType n \pn -> do
reifyType m \pm -> do
addrs <- arrayOf (Vector.generator pk1 Address.generator)
bool <- arbitrary @Boolean
ints <- Vector.generator pk2 (IntN.generator pn)
uint <- (UIntN.generator pm)
let x = Tuple4 addrs bool ints uint
pure $ encodeDecode x === Right x
it "Can encode/decode arrays of tuples" $ liftEffect do
quickCheckGen' 5 $ do
k1 <- chooseInt 1 3
reifyType k1 \pk1 ->
do
let
tupleGen = do
addrs <- arrayOf (Vector.generator pk1 Address.generator)
bool <- arbitrary @Boolean
pure $ Tuple2 addrs bool
as <- take 2 <$> arrayOf tupleGen
pure $ encodeDecode as === Right as
-- this test is admittedly pretty ad hoc
it "Can encode/decode nested tuples" $ liftEffect do
quickCheckGen' 5 do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
reifyType k1 \pk1 ->
reifyType k2 \pk2 ->
reifyType n \pn ->
reifyType m \pm -> do
let
mkTuple4 = do
addrs <- arrayOf (Vector.generator pk1 Address.generator)
bool <- arbitrary @Boolean
ints <- Vector.generator pk2 (IntN.generator pn)
uint <- (UIntN.generator pm)
pure $ Tuple4 addrs bool ints uint
_n <- oneOf (pure <$> intSizes)
_m <- chooseInt 1 10
_k <- chooseInt 1 10
reifyType _n \_pn ->
reifyType _m \_pm ->
reifyType _k \_pk -> do
let
mkTuple5 = do
ints <- arrayOf (IntN.generator _pn)
bytes <- Hex.toBuffer <$> (chooseInt 1 100 >>= Hex.generator)
addrs <- Vector.generator _pm (arrayOf Address.generator)
strings <- map (map (un BMPString)) <$>
arrayOf (Vector.generator _pk (arbitrary @BMPString))
bool <- arbitrary :: Gen Boolean
pure $ Tuple5 ints bytes addrs strings bool
mkTuple2 = do
strings <- map (un BMPString) <$>
arrayOf (arbitrary @BMPString)
addrs <- Vector.generator pk2 (arrayOf Address.generator)
pure $ Tuple2 strings addrs
t <- Tuple3 <$> mkTuple5 <*> mkTuple4 <*> mkTuple2
pure $ encodeDecode t === Right t
--------------------------------------------------------------------------------
newtype BMPString = BMPString String
derive newtype instance Eq BMPString
derive newtype instance Show BMPString
derive instance Newtype BMPString _
data UnicodeChar = Normal CodePoint | Surrogates CodePoint CodePoint
instance Arbitrary BMPString where
arbitrary = BMPString <$> do
ucs <- arrayOf arbitrary
pure $ fromCodePointArray $ foldMap f ucs
where
f uc = case uc of
Normal a -> [ a ]
Surrogates a b -> [ a, b ]
instance Arbitrary UnicodeChar where
arbitrary = frequency $ NonEmpty (Tuple (1.0 - p) normalGen) [ Tuple p surrogatesGen ]
where
hiLB = 0xD800
hiUB = 0xDBFF
loLB = 0xDC00
loUB = 0xDFFF
maxCP = 65535
toCP = toEnumWithDefaults bottom top
-- must have a high surrogate followed by a low surrogate
surrogatesGen = Surrogates <$> (toCP <$> chooseInt hiLB hiUB) <*> (toCP <$> chooseInt loLB loUB)
normalGen = Normal <<< toCP <$> do
chooseInt 0 maxCP `suchThat` \n ->
(n < hiLB || n > hiUB) && (n < loLB || n > loUB)
-- probability that you pick a surrogate from all possible codepoints
p = toNumber ((hiUB - hiLB + 1) + (loUB - loLB + 1)) / toNumber (maxCP + 1)
encodeDecode
:: forall a
. Show a
=> Eq a
=> EncodingType a
=> ABIEncode a
=> ABIDecode a
=> a
-> Either ParseError a
encodeDecode x =
let
a = abiEncode x
in
abiDecode a
intSizes :: NonEmptyArray Int
intSizes = case fromArray $ filter (\x -> x `mod` 8 == 0) (8 .. 256) of
Nothing -> unsafeCrashWith "intSizes: impossible"
Just x -> x
bytesSizes :: NonEmptyArray Int
bytesSizes = 1 NEA... 32