-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathadts_generics.rs
254 lines (219 loc) · 5.17 KB
/
adts_generics.rs
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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] test_box_unbox_struct verus_code! {
#[derive(Eq, PartialEq)]
struct Thing<A> {
a: A,
}
proof fn one(v: int) {
let t1 = Thing { a: v };
let t2 = Thing { a: v };
let a: int = t2.a;
}
fn two(v: Thing<u8>) {
assert(v.a >= 0);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_box_enum verus_code! {
#[derive(Eq, PartialEq)]
enum Thing<A> {
First(A),
}
fn one(v: int) {
let t1 = Thing::First(v);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_generic_adt_eq verus_code! {
#[derive(Eq, PartialEq)]
struct Thing<A> {
a: A,
}
fn one(v: u64) {
let t1 = Thing { a: v };
let t2 = Thing { a: v };
let a1: u64 = t1.a;
let a2: u64 = t2.a;
assert(a1 == a2);
assert(a1 != a2); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_generic_adt_u8 verus_code! {
#[derive(Eq, PartialEq)]
struct Thing<A> {
a: A,
}
fn two(v: Thing<u8>) {
assert(v.a >= 1); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_refinements1 verus_code! {
struct X {
u: u64,
}
fn aa<A>(a: A) -> A {
a
}
#[verifier(opaque)]
spec fn id<A>(a: A) -> A {
a
}
fn f() -> X {
let x = X { u: 3 };
let y = aa(x);
assert(id(y.u) >= 0);
assert(y.u >= 0);
y
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_refinements1_fail verus_code! {
struct X {
u: u64,
}
fn aa<A>(a: A) -> A {
a
}
#[verifier(opaque)]
spec fn id<A>(a: A) -> A {
a
}
fn f() -> X {
let x = X { u: 3 };
let y = aa(x);
assert(id(y.u) >= 1); // FAILS
assert(y.u >= 0);
y
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_refinements2 verus_code! {
struct P<A> {
a: A,
}
#[verifier(opaque)]
spec fn id<A>(a: A) -> A {
a
}
fn fp(p: P<u64>) {
assert(p.a >= 0);
let p2: P<u8> = P { a: 2 };
assert(id(p).a >= 0);
assert(id(p2).a >= 0);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_refinements2_fails verus_code! {
struct P<A> {
a: A,
}
#[verifier(opaque)] /* vattr */
spec fn id<A>(a: A) -> A {
a
}
fn fp(p: P<u64>) {
assert(p.a >= 0);
let p2: P<u8> = P { a: 2 };
assert(id(p).a >= 0);
assert(id(p2).a >= 1); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_out_of_order verus_code! {
struct XY {
tz: TZ,
}
struct TZ {
p: (u64, u64),
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_triggers verus_code! {
struct S {
i: int,
}
#[verifier(opaque)]
spec fn fi(i: int) -> bool {
true
}
#[verifier(opaque)]
spec fn fs(s: S) -> bool {
true
}
fn test_struct_field_trigger(s: S)
requires
forall|s: S| fi(s.i),
{
assert(fi(s.i));
}
fn test_struct_constructor_arg_trigger()
requires
forall|i: int| fs(S {i: i}),
{
assert(fs(S {i: 5}));
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_erase1 verus_code! {
struct S1<A, B>(
#[verifier::spec] A,
#[verifier::exec] B,
);
fn test() {
let x = S1::<bool, _>(true, false);
assert(x.0);
assert(!x.1);
let S1(y, z) = x;
assert(y);
assert(!z);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_erase1_fail verus_code! {
struct S1<A, B>(
#[verifier::spec] A,
#[verifier::exec] B,
);
fn test() {
let x = S1::<bool, _>(true, false);
assert(x.0);
assert(!x.1);
let S1(y, z) = x;
assert(y);
assert(z); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_erase2 verus_code! {
struct S1<A, B> {
#[verifier::spec] a: A,
#[verifier::exec] b: B,
}
fn test() {
let x = S1::<bool, _> { a: true, b: false };
assert(x.a);
assert(!x.b);
let S1 { a: y, b: z } = x;
assert(y);
assert(!z);
}
} => Ok(())
}