-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonoidSpec.scala
323 lines (284 loc) · 9.44 KB
/
MonoidSpec.scala
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
package just.fp
import hedgehog._
import hedgehog.runner._
/**
* @author Kevin Lee
* @since 2019-09-18
*/
object MonoidSpec extends Properties {
import just.fp.syntax._
override def tests: List[Test] = List(
property("testOptionMonoidLaw", OptionMonoidLaws.laws)
, example(
"test OptionMonoid.isZero zero case"
, testMonoidFunction("isZero", none[Int])(_.isZero(_))
)
, property(
"test OptionMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genIntFromMinToMax.map(_.some))(!_.isZero(_))
)
, example(
"test OptionMonoid.nonZero zero case"
, testMonoidFunction("nonZero", none[Int])(!_.nonZero(_))
)
, property(
"test OptionMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genIntFromMinToMax.map(_.some))(_.nonZero(_))
)
, property("testListMonoidLaw", ListMonoidLaws.laws)
, example(
"test ListMonoid.isZero zero case"
, testMonoidFunction("isZero", List.empty[Int])(_.isZero(_))
)
, property(
"test ListMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genList(Gens.genIntFromMinToMax, 1, 10))(!_.isZero(_))
)
, example(
"test ListMonoid.nonZero zero case"
, testMonoidFunction("nonZero", List.empty[Int])(!_.nonZero(_))
)
, property(
"test ListMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genList(Gens.genIntFromMinToMax, 1, 10))(_.nonZero(_))
)
, property("testVectorMonoidLaw", VectorMonoidLaws.laws)
, example(
"test VectorMonoid.isZero zero case"
, testMonoidFunction("isZero", Vector.empty[Int])(_.isZero(_))
)
, property(
"test VectorMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genVector(Gens.genIntFromMinToMax, 1, 10))(!_.isZero(_))
)
, example(
"test VectorMonoid.nonZero zero case"
, testMonoidFunction("nonZero", Vector.empty[Int])(!_.nonZero(_))
)
, property(
"test VectorMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genVector(Gens.genIntFromMinToMax, 1, 10))(_.nonZero(_))
)
, property("testStringMonoidLaw", StringMonoidLaws.laws)
, example(
"test StringMonoid.isZero zero case"
, testMonoidFunction("isZero", "")(_.isZero(_))
)
, property(
"test StringMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gen.constant("a").flatMap(c => Gens.genUnicodeString.map(c + _)))(!_.isZero(_))
)
, example(
"test StringMonoid.nonZero zero case"
, testMonoidFunction("nonZero", "")(!_.nonZero(_))
)
, property(
"test StringMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gen.constant("a").flatMap(c => Gens.genUnicodeString.map(c + _)))(_.nonZero(_))
)
, property("testByteMonoidLaw", ByteMonoidLaws.laws)
, example(
"test ByteMonoid.isZero zero case"
, testMonoidFunction("isZero", 0.toByte)(_.isZero(_))
)
, property(
"test ByteMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genByte(1, Byte.MaxValue))(!_.isZero(_))
)
, example(
"test ByteMonoid.nonZero zero case"
, testMonoidFunction("nonZero", 0.toByte)(!_.nonZero(_))
)
, property(
"test ByteMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genByte(1, Byte.MaxValue))(_.nonZero(_))
)
, property("testShortMonoidLaw", ShortMonoidLaws.laws)
, example(
"test ShortMonoid.isZero zero case"
, testMonoidFunction("isZero", 0.toShort)(_.isZero(_))
)
, property(
"test ShortMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genShort(1, Short.MaxValue))(!_.isZero(_))
)
, example(
"test ShortMonoid.nonZero zero case"
, testMonoidFunction("nonZero", 0.toShort)(!_.nonZero(_))
)
, property(
"test ShortMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genShort(1, Short.MaxValue))(_.nonZero(_))
)
, property("testCharMonoidLaw", CharMonoidLaws.laws)
, example(
"test CharMonoid.isZero zero case"
, testMonoidFunction("isZero", 0.toChar)(_.isZero(_))
)
, property(
"test CharMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genChar(1, Char.MaxValue))(!_.isZero(_))
)
, example(
"test CharMonoid.nonZero zero case"
, testMonoidFunction("nonZero", 0.toChar)(!_.nonZero(_))
)
, property(
"test CharMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genChar(1, Char.MaxValue))(_.nonZero(_))
)
, property("testIntMonoidLaw", IntMonoidLaws.laws)
, example(
"test IntMonoid.isZero zero case"
, testMonoidFunction("isZero", 0)(_.isZero(_))
)
, property(
"test IntMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genInt(1, Int.MaxValue))(!_.isZero(_))
)
, example(
"test IntMonoid.nonZero zero case"
, testMonoidFunction("nonZero", 0)(!_.nonZero(_))
)
, property(
"test IntMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genInt(1, Int.MaxValue))(_.nonZero(_))
)
, property("testLongMonoidLaw", LongMonoidLaws.laws)
, example(
"test LongMonoid.isZero zero case"
, testMonoidFunction("isZero", 0L)(_.isZero(_))
)
, property(
"test LongMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genLong(1L, Long.MaxValue))(!_.isZero(_))
)
, example(
"test LongMonoid.nonZero zero case"
, testMonoidFunction("nonZero", 0L)(!_.nonZero(_))
)
, property(
"test LongMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genLong(1L, Long.MaxValue))(_.nonZero(_))
)
, property("testBigIntMonoidLaw", BigIntMonoidLaws.laws)
, example(
"test BigIntMonoid.isZero zero case"
, testMonoidFunction("isZero", BigInt(0))(_.isZero(_))
)
, property(
"test BigIntMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genBigInt(1L, Long.MaxValue))(!_.isZero(_))
)
, example(
"test BigIntMonoid.nonZero zero case"
, testMonoidFunction("nonZero", BigInt(0))(!_.nonZero(_))
)
, property(
"test BigIntMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genBigInt(1L, Long.MaxValue))(_.nonZero(_))
)
, property("testBigDecimalMonoidLaw", BigDecimalMonoidLaws.laws)
, example(
"test BigDecimalMonoid.isZero zero case"
, testMonoidFunction("isZero", BigDecimal(0))(_.isZero(_))
)
, property(
"test BigDecimalMonoid.isZero non-zero case"
, propertyTestMonoidFunction("isZero", Gens.genBigDecimal(1F, Float.MaxValue, 1L, Long.MaxValue))(!_.isZero(_))
)
, example(
"test BigDecimalMonoid.nonZero zero case"
, testMonoidFunction("nonZero", BigDecimal(0))(!_.nonZero(_))
)
, property(
"test BigDecimalMonoid.nonZero non-zero case"
, propertyTestMonoidFunction("nonZero", Gens.genBigDecimal(1F, Float.MaxValue, 1L, Long.MaxValue))(_.nonZero(_))
)
)
def testMonoidFunction[A : Monoid : Equal](name: String, monoid: A)(f: (Monoid[A], A) => Boolean): Result = {
Result.diffNamed(s"=== $name Not true ===", Monoid[A], monoid)(f)
}
def propertyTestMonoidFunction[A : Monoid : Equal](name: String, genA: Gen[A])(f: (Monoid[A], A) => Boolean): Property = for {
monoid <- genA.log("monoid")
} yield {
Result.diffNamed(s"=== $name Not false ===", Monoid[A], monoid)(f)
}
object ListMonoidLaws {
def genList: Gen[List[Int]] = Gens.genList(Gens.genIntFromMinToMax, 0, 20)
def laws: Property =
Specs.monoidLaws.laws[List[Int]](
genList
)
}
object VectorMonoidLaws {
def genVector: Gen[Vector[Int]] = Gens.genVector(Gens.genIntFromMinToMax, 0, 20)
def laws: Property =
Specs.monoidLaws.laws[Vector[Int]](
genVector
)
}
object OptionMonoidLaws {
def genOption: Gen[Option[Int]] = Gens.genOption(Gens.genIntFromMinToMax)
def laws: Property =
Specs.monoidLaws.laws[Option[Int]](
genOption
)
}
object StringMonoidLaws {
def genString: Gen[String] = Gens.genUnicodeString
def laws: Property =
Specs.monoidLaws.laws[String](
genString
)
}
object ByteMonoidLaws {
def genByte: Gen[Byte] = Gens.genByteFromMinToMax
def laws: Property =
Specs.monoidLaws.laws[Byte](
genByte
)
}
object ShortMonoidLaws {
def genShort: Gen[Short] = Gens.genShortFromMinToMax
def laws: Property =
Specs.monoidLaws.laws[Short](
genShort
)
}
object CharMonoidLaws {
def genChar: Gen[Char] = Gens.genUnicodeChar
def laws: Property =
Specs.monoidLaws.laws[Char](
genChar
)
}
object IntMonoidLaws {
def genInt: Gen[Int] = Gens.genIntFromMinToMax
def laws: Property =
Specs.monoidLaws.laws[Int](
genInt
)
}
object LongMonoidLaws {
def genLong: Gen[Long] = Gens.genLongFromMinToMax
def laws: Property =
Specs.monoidLaws.laws[Long](
genLong
)
}
object BigIntMonoidLaws {
def genBigInt: Gen[BigInt] = Gens.genBigIntFromMinToMaxLong
def laws: Property =
Specs.monoidLaws.laws[BigInt](
genBigInt
)
}
object BigDecimalMonoidLaws {
def genBigDecimal: Gen[BigDecimal] = Gens.genBigDecimalFromMinToMaxFloatLong
def laws: Property =
Specs.monoidLaws.laws[BigDecimal](
genBigDecimal
)
}
}