-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathProperties1.hs
More file actions
259 lines (238 loc) · 7.16 KB
/
Copy pathProperties1.hs
File metadata and controls
259 lines (238 loc) · 7.16 KB
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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:context-level=0 #-}
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:defer-errors #-}
-- CSE is very unstable and produces different output, likely depending on the version of either
-- @unordered-containers@ or @hashable@.
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:max-cse-iterations=0 #-}
module AssocMap.Properties1 where
import Hedgehog (Property, forAll, property, (===))
import Hedgehog.Gen qualified as Gen
import PlutusTx.AssocMap qualified as AssocMap
import PlutusTx.Code (CompiledCode, unsafeApplyCode)
import PlutusTx.Data.AssocMap qualified as Data.AssocMap
import PlutusTx.IsData ()
import PlutusTx.Lift (liftCodeDef)
import PlutusTx.List qualified as PlutusTx
import PlutusTx.Prelude qualified as PlutusTx
import PlutusTx.TH (compile)
import PlutusTx.Test.Run.Code (evaluationResultMatchesHaskell)
import AssocMap.Semantics
lookupProgram :: CompiledCode (Integer -> AssocMap.Map Integer Integer -> Maybe Integer)
lookupProgram = $$(compile [||AssocMap.lookup||])
dataLookupProgram :: CompiledCode (Integer -> Data.AssocMap.Map Integer Integer -> Maybe Integer)
dataLookupProgram = $$(compile [||Data.AssocMap.lookup||])
memberProgram :: CompiledCode (Integer -> AssocMap.Map Integer Integer -> Bool)
memberProgram = $$(compile [||AssocMap.member||])
dataMemberProgram :: CompiledCode (Integer -> Data.AssocMap.Map Integer Integer -> Bool)
dataMemberProgram = $$(compile [||Data.AssocMap.member||])
insertProgram
:: CompiledCode
( Integer
-> Integer
-> AssocMap.Map Integer Integer
-> [(Integer, Integer)]
)
insertProgram =
$$( compile
[||
\k v m ->
PlutusTx.sort $ AssocMap.toList $ AssocMap.insert k v m
||]
)
dataInsertProgram
:: CompiledCode
( Integer
-> Integer
-> Data.AssocMap.Map Integer Integer
-> [(Integer, Integer)]
)
dataInsertProgram =
$$( compile
[||
\k v m ->
PlutusTx.sort $ Data.AssocMap.toSOPList $ Data.AssocMap.insert k v m
||]
)
deleteProgram
:: CompiledCode
( Integer
-> AssocMap.Map Integer Integer
-> [(Integer, Integer)]
)
deleteProgram =
$$( compile
[||
\k m ->
PlutusTx.sort $ AssocMap.toList $ AssocMap.delete k m
||]
)
dataDeleteProgram
:: CompiledCode
( Integer
-> Data.AssocMap.Map Integer Integer
-> [(Integer, Integer)]
)
dataDeleteProgram =
$$( compile
[||
\k m ->
PlutusTx.sort $ Data.AssocMap.toSOPList $ Data.AssocMap.delete k m
||]
)
allProgram
:: CompiledCode
( Integer
-> AssocMap.Map Integer Integer
-> Bool
)
allProgram =
$$(compile [||\num m -> AssocMap.all (\x -> x PlutusTx.< num) m||])
dataAllProgram
:: CompiledCode
( Integer
-> Data.AssocMap.Map Integer Integer
-> Bool
)
dataAllProgram =
$$(compile [||\num m -> Data.AssocMap.all (\x -> x PlutusTx.< num) m||])
dataAnyProgram
:: CompiledCode
( Integer
-> Data.AssocMap.Map Integer Integer
-> Bool
)
dataAnyProgram =
$$(compile [||\num m -> Data.AssocMap.any (\x -> x PlutusTx.< num) m||])
lookupSpec :: Property
lookupSpec = property $ do
assocMapS <- forAll genAssocMapS
key <- forAll $ Gen.integral rangeElem
let assocMap = semanticsToAssocMap assocMapS
dataAssocMap = semanticsToDataAssocMap assocMapS
expected = lookupS key assocMapS
evaluationResultMatchesHaskell
( lookupProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef assocMap
)
(===)
expected
evaluationResultMatchesHaskell
( dataLookupProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef dataAssocMap
)
(===)
expected
memberSpec :: Property
memberSpec = property $ do
assocMapS <- forAll genAssocMapS
key <- forAll $ Gen.integral rangeElem
let assocMap = semanticsToAssocMap assocMapS
dataAssocMap = semanticsToDataAssocMap assocMapS
expected = memberS key assocMapS
evaluationResultMatchesHaskell
( memberProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef assocMap
)
(===)
expected
evaluationResultMatchesHaskell
( dataMemberProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef dataAssocMap
)
(===)
expected
insertSpec :: Property
insertSpec = property $ do
assocMapS <- forAll genAssocMapS
key <- forAll $ Gen.integral rangeElem
value <- forAll $ Gen.integral rangeElem
let assocMap = semanticsToAssocMap assocMapS
dataAssocMap = semanticsToDataAssocMap assocMapS
expected = sortS $ insertS key value assocMapS
evaluationResultMatchesHaskell
( insertProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef value
`unsafeApplyCode` liftCodeDef assocMap
)
(===)
expected
evaluationResultMatchesHaskell
( dataInsertProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef value
`unsafeApplyCode` liftCodeDef dataAssocMap
)
(===)
expected
deleteSpec :: Property
deleteSpec = property $ do
assocMapS <- forAll genAssocMapS
key <- forAll $ Gen.integral rangeElem
let assocMap = semanticsToAssocMap assocMapS
dataAssocMap = semanticsToDataAssocMap assocMapS
expected = sortS $ deleteS key assocMapS
evaluationResultMatchesHaskell
( deleteProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef assocMap
)
(===)
expected
evaluationResultMatchesHaskell
( dataDeleteProgram
`unsafeApplyCode` liftCodeDef key
`unsafeApplyCode` liftCodeDef dataAssocMap
)
(===)
expected
allSpec :: Property
allSpec = property $ do
assocMapS <- forAll genAssocMapS
num <- forAll $ Gen.integral rangeElem
let assocMap = semanticsToAssocMap assocMapS
dataAssocMap = semanticsToDataAssocMap assocMapS
expected = allS (< num) assocMapS
evaluationResultMatchesHaskell
( allProgram
`unsafeApplyCode` liftCodeDef num
`unsafeApplyCode` liftCodeDef assocMap
)
(===)
expected
evaluationResultMatchesHaskell
( dataAllProgram
`unsafeApplyCode` liftCodeDef num
`unsafeApplyCode` liftCodeDef dataAssocMap
)
(===)
expected
anySpec :: Property
anySpec = property $ do
assocMapS <- forAll genAssocMapS
num <- forAll $ Gen.integral rangeElem
let dataAssocMap = semanticsToDataAssocMap assocMapS
expected = anyS (< num) assocMapS
evaluationResultMatchesHaskell
( dataAnyProgram
`unsafeApplyCode` liftCodeDef num
`unsafeApplyCode` liftCodeDef dataAssocMap
)
(===)
expected