-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_test_equality.pony
More file actions
362 lines (306 loc) · 11.2 KB
/
_test_equality.pony
File metadata and controls
362 lines (306 loc) · 11.2 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
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
use "collections"
use "pony_check"
use "pony_test"
class \nodoc\ iso _TestFieldEqualityReflexive is UnitTest
"""
Every FieldDataTypes variant produces a Field that is equal to itself.
Covers all 9 variants of the FieldDataTypes union to verify each match \exhaustive\
branch in Field.eq.
"""
fun name(): String => "Field/Equality/Reflexive"
fun apply(h: TestHelper) =>
let fields: Array[Field] val = [
Field("bytes", recover val [as U8: 1; 2; 3] end)
Field("b", true)
Field("f32", F32(1.5))
Field("f64", F64(2.5))
Field("i16", I16(16))
Field("i32", I32(32))
Field("i64", I64(64))
Field("none", None)
Field("str", "hello")
]
for f in fields.values() do
h.assert_true(f == f)
end
class \nodoc\ iso _TestFieldEqualityStructural is UnitTest
"""
Two independently constructed Fields with the same name and value are equal.
Covers all 9 variants of the FieldDataTypes union.
"""
fun name(): String => "Field/Equality/Structural"
fun apply(h: TestHelper) =>
h.assert_true(
Field("a", recover val [as U8: 1; 2] end)
== Field("a", recover val [as U8: 1; 2] end))
h.assert_true(Field("a", true) == Field("a", true))
h.assert_true(Field("a", F32(1.5)) == Field("a", F32(1.5)))
h.assert_true(Field("a", F64(2.5)) == Field("a", F64(2.5)))
h.assert_true(Field("a", I16(16)) == Field("a", I16(16)))
h.assert_true(Field("a", I32(32)) == Field("a", I32(32)))
h.assert_true(Field("a", I64(64)) == Field("a", I64(64)))
h.assert_true(Field("a", None) == Field("a", None))
h.assert_true(Field("a", "hello") == Field("a", "hello"))
class \nodoc\ iso _TestFieldEqualitySymmetric is UnitTest
"""
Field equality is symmetric: if a == b then b == a, and if a != b then
b != a. Tests across different value types.
"""
fun name(): String => "Field/Equality/Symmetric"
fun apply(h: TestHelper) =>
// Equal pairs: a == b and b == a
let f1 = Field("x", I32(42))
let f2 = Field("x", I32(42))
h.assert_true(f1.eq(f2) == f2.eq(f1))
// Unequal pairs: different types
let f3 = Field("x", I32(42))
let f4 = Field("x", "42")
h.assert_true(f3.eq(f4) == f4.eq(f3))
// Unequal pairs: different names
let f5 = Field("x", I32(42))
let f6 = Field("y", I32(42))
h.assert_true(f5.eq(f6) == f6.eq(f5))
// None vs non-None
let f7 = Field("x", None)
let f8 = Field("x", I32(0))
h.assert_true(f7.eq(f8) == f8.eq(f7))
// Array[U8] vs String
let f9 = Field("x", recover val [as U8: 1; 2] end)
let f10 = Field("x", "hello")
h.assert_true(f9.eq(f10) == f10.eq(f9))
class \nodoc\ iso _TestFieldInequality is UnitTest
fun name(): String => "Field/Inequality"
fun apply(h: TestHelper) =>
// Different names, same value
h.assert_false(Field("a", I32(42)) == Field("b", I32(42)))
// Same name, different values of same type
h.assert_false(Field("a", I32(42)) == Field("a", I32(43)))
h.assert_false(Field("a", "hello") == Field("a", "world"))
h.assert_false(Field("a", true) == Field("a", false))
// Same name, different value types
h.assert_false(Field("a", I32(42)) == Field("a", "42"))
h.assert_false(Field("a", I32(42)) == Field("a", I64(42)))
h.assert_false(Field("a", F32(1.0)) == Field("a", F64(1.0)))
h.assert_false(Field("a", I16(1)) == Field("a", I32(1)))
// None vs non-None
h.assert_false(Field("a", None) == Field("a", I32(0)))
h.assert_false(Field("a", I32(0)) == Field("a", None))
// Array[U8] vs different Array[U8]
h.assert_false(
Field("a", recover val [as U8: 1; 2] end)
== Field("a", recover val [as U8: 1; 3] end))
h.assert_false(
Field("a", recover val [as U8: 1; 2] end)
== Field("a", recover val [as U8: 1; 2; 3] end))
// Array[U8] vs String
h.assert_false(
Field("a", recover val [as U8: 1; 2] end)
== Field("a", "hello"))
class \nodoc\ iso _TestRowEquality is UnitTest
fun name(): String => "Row/Equality"
fun apply(h: TestHelper) =>
// Empty rows are equal
let empty1 = Row(recover val Array[Field] end)
let empty2 = Row(recover val Array[Field] end)
h.assert_true(empty1 == empty2)
// Reflexive
let r1 = Row(recover val
[Field("a", I32(1)); Field("b", "hello")]
end)
h.assert_true(r1 == r1)
// Structural equality: same content, independent construction
let r2 = Row(recover val
[Field("a", I32(1)); Field("b", "hello")]
end)
let r3 = Row(recover val
[Field("a", I32(1)); Field("b", "hello")]
end)
h.assert_true(r2 == r3)
class \nodoc\ iso _TestRowInequality is UnitTest
fun name(): String => "Row/Inequality"
fun apply(h: TestHelper) =>
// Different sizes
let r1 = Row(recover val [Field("a", I32(1))] end)
let r2 = Row(recover val
[Field("a", I32(1)); Field("b", I32(2))]
end)
h.assert_false(r1 == r2)
// Same size, different content
let r3 = Row(recover val [Field("a", I32(1))] end)
let r4 = Row(recover val [Field("a", I32(2))] end)
h.assert_false(r3 == r4)
class \nodoc\ iso _TestRowsEquality is UnitTest
fun name(): String => "Rows/Equality"
fun apply(h: TestHelper) =>
// Empty Rows are equal
let empty1 = Rows(recover val Array[Row] end)
let empty2 = Rows(recover val Array[Row] end)
h.assert_true(empty1 == empty2)
// Reflexive
let rs1 = Rows(recover val
[Row(recover val [Field("a", I32(1))] end)]
end)
h.assert_true(rs1 == rs1)
// Structural equality
let rs2 = Rows(recover val
[Row(recover val [Field("a", I32(1))] end)]
end)
let rs3 = Rows(recover val
[Row(recover val [Field("a", I32(1))] end)]
end)
h.assert_true(rs2 == rs3)
class \nodoc\ iso _TestRowsInequality is UnitTest
fun name(): String => "Rows/Inequality"
fun apply(h: TestHelper) =>
// Different sizes
let rs1 = Rows(recover val
[Row(recover val [Field("a", I32(1))] end)]
end)
let rs2 = Rows(recover val Array[Row] end)
h.assert_false(rs1 == rs2)
// Different content
let rs3 = Rows(recover val
[Row(recover val [Field("a", I32(1))] end)]
end)
let rs4 = Rows(recover val
[Row(recover val [Field("a", I32(2))] end)]
end)
h.assert_false(rs3 == rs4)
// -- Generators --
primitive \nodoc\ _FieldDataTypesGen
fun apply(): Generator[FieldDataTypes] =>
Generators.frequency[FieldDataTypes]([
(1, Generator[FieldDataTypes](object is GenObj[FieldDataTypes]
fun generate(rnd: Randomness): FieldDataTypes =>
let size = rnd.usize(0, 10)
recover val
let arr = Array[U8](size)
for _ in Range(0, size) do
arr.push(rnd.u8())
end
arr
end
end))
(1, Generators.bool().map[FieldDataTypes]({(v) => v }))
(1, Generators.i32().map[FieldDataTypes]({(v) => F32.from[I32](v) }))
(1, Generators.i64().map[FieldDataTypes]({(v) => F64.from[I64](v) }))
(1, Generators.i16().map[FieldDataTypes]({(v) => v }))
(1, Generators.i32().map[FieldDataTypes]({(v) => v }))
(1, Generators.i64().map[FieldDataTypes]({(v) => v }))
(1, Generators.unit[None](None).map[FieldDataTypes]({(v) => v }))
(1, Generators.ascii_printable(0, 20)
.map[FieldDataTypes]({(v) => v }))
])
primitive \nodoc\ _FieldGen
fun apply(): Generator[Field] =>
Generators.map2[String, FieldDataTypes, Field](
Generators.ascii_printable(1, 10),
_FieldDataTypesGen(),
{(name, value) => Field(name, value) })
primitive \nodoc\ _RowGen
fun apply(): Generator[Row] =>
Generator[Row](object is GenObj[Row]
fun generate(rnd: Randomness): Row =>
let size = rnd.usize(0, 5)
let fields = recover iso Array[Field](size) end
for i in Range(0, size) do
fields.push(Field("f" + i.string(),
_random_field_value(rnd)))
end
Row(consume fields)
fun _random_field_value(rnd: Randomness): FieldDataTypes =>
match rnd.usize(0, 8)
| 0 => rnd.bool()
| 1 => F32.from[I32](rnd.i32())
| 2 => F64.from[I64](rnd.i64())
| 3 => rnd.i16()
| 4 => rnd.i32()
| 5 => rnd.i64()
| 6 => None
| 7 =>
recover val
let arr = Array[U8](rnd.usize(0, 10))
for _ in Range(0, arr.space()) do
arr.push(rnd.u8())
end
arr
end
else
"str" + rnd.u32().string()
end
end)
primitive \nodoc\ _RowsGen
fun apply(): Generator[Rows] =>
Generator[Rows](object is GenObj[Rows]
fun generate(rnd: Randomness): Rows =>
let size = rnd.usize(0, 3)
let rows = recover iso Array[Row](size) end
for i in Range(0, size) do
let field_count = rnd.usize(0, 5)
let fields = recover iso Array[Field](field_count) end
for j in Range(0, field_count) do
fields.push(Field("f" + j.string(),
_random_field_value(rnd)))
end
rows.push(Row(consume fields))
end
Rows(consume rows)
fun _random_field_value(rnd: Randomness): FieldDataTypes =>
match rnd.usize(0, 8)
| 0 => rnd.bool()
| 1 => F32.from[I32](rnd.i32())
| 2 => F64.from[I64](rnd.i64())
| 3 => rnd.i16()
| 4 => rnd.i32()
| 5 => rnd.i64()
| 6 => None
| 7 =>
recover val
let arr = Array[U8](rnd.usize(0, 10))
for _ in Range(0, arr.space()) do
arr.push(rnd.u8())
end
arr
end
else
"str" + rnd.u32().string()
end
end)
// -- Property Tests --
class \nodoc\ iso _TestFieldReflexiveProperty is Property1[Field]
fun name(): String => "Field/Equality/Reflexive/Property"
fun gen(): Generator[Field] =>
_FieldGen()
fun ref property(arg1: Field, h: PropertyHelper) =>
h.assert_true(arg1 == arg1)
class \nodoc\ iso _TestFieldStructuralProperty is Property1[FieldDataTypes]
fun name(): String => "Field/Equality/Structural/Property"
fun gen(): Generator[FieldDataTypes] =>
_FieldDataTypesGen()
fun ref property(arg1: FieldDataTypes, h: PropertyHelper) =>
h.assert_true(Field("x", arg1) == Field("x", arg1))
class \nodoc\ iso _TestFieldSymmetricProperty
is Property2[FieldDataTypes, FieldDataTypes]
fun name(): String => "Field/Equality/Symmetric/Property"
fun gen1(): Generator[FieldDataTypes] =>
_FieldDataTypesGen()
fun gen2(): Generator[FieldDataTypes] =>
_FieldDataTypesGen()
fun ref property2(arg1: FieldDataTypes, arg2: FieldDataTypes,
h: PropertyHelper)
=>
let f1 = Field("x", arg1)
let f2 = Field("x", arg2)
h.assert_true(f1.eq(f2) == f2.eq(f1))
class \nodoc\ iso _TestRowReflexiveProperty is Property1[Row]
fun name(): String => "Row/Equality/Reflexive/Property"
fun gen(): Generator[Row] =>
_RowGen()
fun ref property(arg1: Row, h: PropertyHelper) =>
h.assert_true(arg1 == arg1)
class \nodoc\ iso _TestRowsReflexiveProperty is Property1[Rows]
fun name(): String => "Rows/Equality/Reflexive/Property"
fun gen(): Generator[Rows] =>
_RowsGen()
fun ref property(arg1: Rows, h: PropertyHelper) =>
h.assert_true(arg1 == arg1)