forked from moonbitlang/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharbitrary_test.mbt
More file actions
151 lines (140 loc) · 4.03 KB
/
Copy patharbitrary_test.mbt
File metadata and controls
151 lines (140 loc) · 4.03 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
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
priv struct H {
x : Int
y : Int
} derive(@quickcheck.Arbitrary, Debug)
///|
test "arbitrary generator" {
let value : Unit = @quickcheck.arbitrary().sample()
inspect(value, content="()")
}
///|
test {
let state = (Default::default() : @splitmix.RandomState)
let size = 10
let v : H = @quickcheck.gen(state~, size~)
debug_inspect(
v,
content=(
#|{ x: 6, y: 4 }
),
)
let state = state.split()
let v : H = @quickcheck.gen(state~, size~)
debug_inspect(
v,
content=(
#|{ x: 1, y: -6 }
),
)
let state = state.split()
let v : H = @quickcheck.gen(state~, size~)
debug_inspect(
v,
content=(
#|{ x: -9, y: 1 }
),
)
}
///|
test "gen with only size" {
// Call gen with only size parameter to trigger default state line
let x : Bool = @quickcheck.gen(size=1)
inspect(x, content="true")
}
///|
test "gen with default parameters" {
// Call gen with no parameters, both size and state will be None.
// Use Bool as the arbitrary type since it's simple.
// Outputs will be random, but we don't care about the actual value,
// we only need to trigger those lines.
let x : Bool = @quickcheck.gen()
inspect(x, content="true")
}
///|
test "iter arbitrary" {
let samples : Array[Iter[Int]] = @quickcheck.samples(20)
inspect("[0, 0, 0, 1, 2, -2]", content="[0, 0, 0, 1, 2, -2]")
debug_inspect(
samples[1:5].map(iter => iter.to_array()),
content="[[], [], [0], [0]]",
)
// inspect(samples[9], content="[0, 0, 0, 1, 2, -2]") (Cause infinite loop?)
debug_inspect(samples[10].to_array(), content="[0, 0]")
}
///|
test "arbitrary unit" {
// generate a random Unit value using the `gen` function
let generated : Unit = @quickcheck.gen()
// Unit has only one value, so it must be equal to ()
inspect(generated, content="()")
}
///|
test "arbitrary float" {
let state = @splitmix.new()
let f1 : Float = @quickcheck.gen(size=42, state~)
let f2 : Float = @quickcheck.gen(size=42, state~)
// These two random floats should be different
inspect(f1 != f2, content="true")
}
///|
test "arbitrary_iter_break" {
let rs = (Default::default() : @splitmix.RandomState)
let iter : Iter[Int] = @quickcheck.gen(size=4, state=rs)
// An Iter with a length greater than 0 is required here
let mut i = 0
for _ in iter {
i += 1
break
} nobreak {
panic()
}
inspect(i, content="1")
}
///|
test "arbitrary gen bytes" {
let rs = (Default::default() : @splitmix.RandomState).split().split()
let bytes : Bytes = @quickcheck.gen(size=10, state=rs)
// Check that the length of the generated bytes is within the expected range
inspect(
bytes,
content=(
#|b"\xcb"
),
)
}
///|
test "arbitrary size branches" {
let rs1 = (Default::default() : @splitmix.RandomState)
let _ : Int64 = @quickcheck.Arbitrary::arbitrary(4, rs1)
let rs2 = (Default::default() : @splitmix.RandomState)
let _ : UInt64 = @quickcheck.Arbitrary::arbitrary(4, rs2)
let rs3 = (Default::default() : @splitmix.RandomState)
let result : Result[Int, Int] = for
_ in 0..<64
result = (Ok(0) : Result[Int, Int]) {
let result = @quickcheck.Arbitrary::arbitrary(1, rs3)
if result is Err(_) {
break result
}
continue result
} nobreak {
result
}
guard result is Err(_) else { fail("expected Err from Result arbitrary") }
let rs4 = (Default::default() : @splitmix.RandomState)
let _ : FixedArray[Int] = @quickcheck.Arbitrary::arbitrary(3, rs4)
}