-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshrinking_test.mbt
More file actions
166 lines (160 loc) · 4.45 KB
/
shrinking_test.mbt
File metadata and controls
166 lines (160 loc) · 4.45 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
///|
// Minimal dependency model for shrinking:
// `ShrinkNew` produces a symbolic reference and `ShrinkUse` consumes it. This
// mirrors the file-handle dependency from the blogs without file-system noise,
// making the property focused on whether the shrinker rejects candidates with
// dangling symbolic references.
enum ShrinkCommand {
ShrinkNew
ShrinkUse(@quickcheck_statemachine.Ref[Int])
} derive(Eq, Debug)
///|
enum ShrinkConcreteCommand {
ShrinkConcreteNew
ShrinkConcreteUse(Int)
} derive(Eq, Debug)
///|
enum ShrinkResponse {
ShrinkNewed(@quickcheck_statemachine.Var)
ShrinkUsed
} derive(Eq, Debug)
///|
enum ShrinkConcreteResponse {
ShrinkConcreteNewed(Int)
ShrinkConcreteUsed
} derive(Eq, Debug)
///|
fn shrink_ref_known(
model : Array[@quickcheck_statemachine.Var],
handle : @quickcheck_statemachine.Var,
) -> Bool {
for item in model {
if item == handle {
return true
}
}
false
}
///|
fn shrink_spec() -> @quickcheck_statemachine.StateMachine[
Array[@quickcheck_statemachine.Var],
Int,
ShrinkCommand,
ShrinkConcreteCommand,
ShrinkResponse,
ShrinkConcreteResponse,
Int,
Unit,
] {
@quickcheck_statemachine.StateMachine::new(
init_symbolic_model=() => [],
init_concrete_model=() => 0,
init_system=() => (),
generator=(model, size, rng) => {
ignore(model)
ignore(size)
ignore(rng)
None
},
mock=(model, command, gen_sym) => {
ignore(model)
match command {
ShrinkNew => {
let (handle, next) = gen_sym.fresh()
(ShrinkNewed(handle), next)
}
ShrinkUse(_) => (ShrinkUsed, gen_sym)
}
},
response_vars=response => {
match response {
ShrinkNewed(handle) => [handle]
ShrinkUsed => []
}
},
transition_symbolic=(model, command, response) => {
ignore(command)
match response {
ShrinkNewed(handle) => {
let next = model.copy()
next.push(handle)
next
}
ShrinkUsed => model
}
},
transition_concrete=(model, command, response) => {
ignore(command)
ignore(response)
model
},
precondition=(model, command) => {
let ok = match command {
ShrinkNew => true
ShrinkUse(Symbolic(handle)) => shrink_ref_known(model, handle)
ShrinkUse(Concrete(_)) => true
}
@quickcheck_statemachine.Logic::boolean(ok)
},
remap_command=(command, scope) => {
match command {
ShrinkNew => Some(ShrinkNew)
ShrinkUse(Symbolic(handle)) =>
match scope.get(handle) {
Some(next) => Some(ShrinkUse(Symbolic(next)))
None => None
}
ShrinkUse(Concrete(value)) => Some(ShrinkUse(Concrete(value)))
}
},
reify_command=(command, environment) => {
match command {
ShrinkNew => Ok(ShrinkConcreteNew)
ShrinkUse(Concrete(value)) => Ok(ShrinkConcreteUse(value))
ShrinkUse(Symbolic(handle)) =>
match environment.lookup(handle) {
Ok(value) => Ok(ShrinkConcreteUse(value))
Err(error) => Err(error)
}
}
},
run_command=(command, system) => {
ignore(system)
match command {
ShrinkConcreteNew => ShrinkConcreteNewed(0)
ShrinkConcreteUse(_) => ShrinkConcreteUsed
}
},
bind_response=(symbolic, concrete, environment) => {
match (symbolic, concrete) {
(ShrinkNewed(handle), ShrinkConcreteNewed(value)) =>
Ok(environment.insert(handle, value))
_ => Ok(environment)
}
},
)
}
///|
// Dependency-aware shrink regression:
// Removing the producer command would leave `ShrinkUse(Var 0)` without a
// binding. The property checks that the shrinker either keeps the producer or
// rejects the candidate, which is the essential "remove dependent commands
// together" behavior required for stateful programs.
test "shrinking rejects candidates with dangling symbolic refs" {
let v0 = @quickcheck_statemachine.Var::{ id: 0 }
let commands = @quickcheck_statemachine.Commands::{
commands: [
{ command: ShrinkNew, response: ShrinkNewed(v0), vars: [v0] },
{ command: ShrinkUse(Symbolic(v0)), response: ShrinkUsed, vars: [] },
],
}
let candidates = @quickcheck_statemachine.shrink_commands_once(
shrink_spec(),
commands,
)
for candidate in candidates {
if candidate.length() == 1 {
assert_true(candidate.commands[0].command is ShrinkNew)
}
}
}