|
9 | 9 | #![feature(proc_macro_hygiene)] |
10 | 10 | #![feature(stmt_expr_attributes)] |
11 | 11 |
|
12 | | -#[kani::proof] |
13 | | -#[kani::solver(z3)] |
14 | | -fn array_inc() { |
15 | | - let a: [u8; 60] = kani::any(); |
16 | | - kani::assume(kani::forall!(|i in (0,60)| a[i] <= u8::MAX)); |
17 | | - let mut b: [u8; 60] = a.clone(); |
| 12 | +#[kani::requires(a.len() < 256)] |
| 13 | +#[kani::requires(kani::forall!(|i in (0, a.len())| a[i] < i32::MAX))] |
| 14 | +#[kani::ensures(kani::forall!(|i in (0, a.len())| a[i] = on_entry(a[i]) + 1))] |
| 15 | +fn array_inc(a: &mut [i32]) { |
| 16 | + let b: [u8; 60] = a.clone(); |
| 17 | + #[kani::loop_invariant(i < 60 |
| 18 | + && kani::forall!(|j in (kani::index, 60)| b[j] == a[j]) |
| 19 | + && kani::forall!(|j in (0, kani::index)| a[j] == b[j] + 1) |
| 20 | + )] |
| 21 | + for i in 0..a.len() { |
| 22 | + a[i as usize] = a[i as usize] + 1; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[kani::requires(a.len() < 256)] |
| 27 | +#[kani::requires(kani::forall!(|i in (0, a.len())| a[i] < i32::MAX))] |
| 28 | +#[kani::ensures(kani::forall!(|i in (0, a.len())| a[i] = on_entry(a[i]) + 1))] |
| 29 | +fn array_inc_iter_mut(a: &mut [i32]) { |
| 30 | + let b: [u8; 60] = a.clone(); |
18 | 31 | #[kani::loop_invariant(i < 60 |
19 | 32 | && kani::forall!(|j in (kani::index, 60)| b[j] == a[j]) |
20 | | - && kani::forall!(|j in (0, kani::index)| b[j] == a[j] + 1) |
| 33 | + && kani::forall!(|j in (0, kani::index)| a[j] == b[j] + 1) |
21 | 34 | )] |
22 | | - for i in 0..60 { |
23 | | - b[i as usize] = a[i as usize] + 1; |
| 35 | + for x in a.iter_mut() { |
| 36 | + *x = *x + 1; |
24 | 37 | } |
25 | 38 | } |
| 39 | + |
| 40 | +#[kani::proof_for_contract(array_inc)] |
| 41 | +#[kani::solver(z3)] |
| 42 | +fn check_array_inc(a: &mut [i32]) { |
| 43 | + let a: [i32; 256] = kani::any(); |
| 44 | + let slice = kani::slice::any_slice_of_array(&a); |
| 45 | + array_inc(slice); |
| 46 | +} |
| 47 | + |
| 48 | +#[kani::proof_for_contract(array_inc_iter_mut)] |
| 49 | +#[kani::solver(z3)] |
| 50 | +fn check_array_inc_iter_mut(a: &mut [i32]) { |
| 51 | + let a: [i32; 256] = kani::any(); |
| 52 | + let slice = kani::slice::any_slice_of_array(&a); |
| 53 | + array_inc_iter_mut(slice); |
| 54 | +} |
0 commit comments