|
| 1 | +#![feature(rustc_private)] |
| 2 | +#[macro_use] |
| 3 | +mod common; |
| 4 | +use common::*; |
| 5 | + |
| 6 | +test_verify_with_pervasive! { |
| 7 | + #[test] basic_correctness_expr code! { |
| 8 | + #[spec] |
| 9 | + fn arith_sum_nat(i: nat) -> nat { |
| 10 | + decreases(i); |
| 11 | + |
| 12 | + if i == 0 { 0 } else { i + arith_sum_nat(i - 1) } |
| 13 | + } |
| 14 | + } => Ok(()) |
| 15 | +} |
| 16 | + |
| 17 | +test_verify_with_pervasive! { |
| 18 | + #[test] basic_correctness_stmt code! { |
| 19 | + #[proof] |
| 20 | + fn count_down_stmt(i:nat) { |
| 21 | + decreases(i); |
| 22 | + |
| 23 | + if i != 0 { |
| 24 | + count_down_stmt(i - 1); |
| 25 | + } |
| 26 | + } |
| 27 | + } => Ok(()) |
| 28 | +} |
| 29 | + |
| 30 | +test_verify_with_pervasive! { |
| 31 | + // Basic test of mutually recursive expressions |
| 32 | + #[test] mutually_recursive_expressions code! { |
| 33 | + #[spec] |
| 34 | + fn count_down_a(i:nat) -> nat { |
| 35 | + decreases(i); |
| 36 | + |
| 37 | + if i == 0 { 0 } else { 1 + count_down_b(i - 1) } |
| 38 | + } |
| 39 | + |
| 40 | + #[spec] |
| 41 | + fn count_down_b(i:nat) -> nat { |
| 42 | + decreases(i); |
| 43 | + |
| 44 | + if i == 0 { 0 } else { 1 + count_down_a(i - 1) } |
| 45 | + } |
| 46 | + |
| 47 | + #[proof] |
| 48 | + fn count_down_properties() { |
| 49 | + assert(count_down_b(0) == 0); |
| 50 | + assert(count_down_a(1) == 1); |
| 51 | + } |
| 52 | + } => Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +test_verify_with_pervasive! { |
| 56 | + // Test that fuel only provides one definition unfolding |
| 57 | + #[test] mutually_recursive_expressions_insufficient_fuel code! { |
| 58 | + #[spec] |
| 59 | + fn count_down_a(i:nat) -> nat { |
| 60 | + decreases(i); |
| 61 | + |
| 62 | + if i == 0 { 0 } else { 1 + count_down_b(i - 1) } |
| 63 | + } |
| 64 | + |
| 65 | + #[spec] |
| 66 | + fn count_down_b(i:nat) -> nat { |
| 67 | + decreases(i); |
| 68 | + |
| 69 | + if i == 0 { 0 } else { 1 + count_down_a(i - 1) } |
| 70 | + } |
| 71 | + |
| 72 | + #[proof] |
| 73 | + fn count_down_properties() { |
| 74 | + assert(count_down_a(1) == 1); // FAILS |
| 75 | + } |
| 76 | + } => Err(err) => assert_one_fails(err) |
| 77 | +} |
| 78 | + |
| 79 | +test_verify_with_pervasive! { |
| 80 | + // Basic test of mutually recursive statements |
| 81 | + #[test] mutually_recursive_statements code! { |
| 82 | + #[proof] |
| 83 | + fn count_down_a_stmt(i:nat) { |
| 84 | + decreases(i); |
| 85 | + |
| 86 | + if i != 0 { |
| 87 | + count_down_b_stmt(i - 1); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + #[proof] |
| 92 | + fn count_down_b_stmt(i:nat) { |
| 93 | + decreases(i); |
| 94 | + |
| 95 | + if i != 0 { |
| 96 | + count_down_a_stmt(i - 1); |
| 97 | + } |
| 98 | + } |
| 99 | + } => Ok(()) |
| 100 | +} |
| 101 | + |
| 102 | +test_verify_with_pervasive! { |
| 103 | + // Expression that fails to decrease |
| 104 | + #[test] expr_decrease_fail_1 code! { |
| 105 | + #[spec] |
| 106 | + fn arith_sum_int(i: int) -> int { |
| 107 | + decreases(i); |
| 108 | + |
| 109 | + if i <= 0 { 0 } else { i + arith_sum_int(i + 1) } // FAILS |
| 110 | + } |
| 111 | + } => Err(err) => assert_one_fails(err) |
| 112 | +} |
| 113 | + |
| 114 | +test_verify_with_pervasive! { |
| 115 | + // Statement that fails to decrease |
| 116 | + #[test] stmt_decrease_fail code! { |
| 117 | + #[proof] |
| 118 | + fn count_down_stmt(i:nat) { |
| 119 | + decreases(i); |
| 120 | + |
| 121 | + if i != 0 { |
| 122 | + count_down_stmt(i + 1); // FAILS |
| 123 | + } |
| 124 | + } |
| 125 | + } => Err(err) => assert_one_fails(err) |
| 126 | +} |
| 127 | + |
| 128 | +test_verify_with_pervasive! { |
| 129 | + // Expression that decreases, but not based on the decreases clause provided |
| 130 | + #[test] expr_wrong_decreases code! { |
| 131 | + #[spec] |
| 132 | + fn arith_sum_int(i: int) -> int { |
| 133 | + decreases(100 - i); |
| 134 | + |
| 135 | + if i <= 0 { 0 } else { i + arith_sum_int(i - 1) } // FAILS |
| 136 | + } |
| 137 | + } => Err(err) => assert_one_fails(err) |
| 138 | +} |
| 139 | + |
| 140 | +test_verify_with_pervasive! { |
| 141 | + // Expression that decreases, but not based on the decreases clause provided |
| 142 | + #[test] expr_wrong_decreases_2 code! { |
| 143 | + #[spec] |
| 144 | + fn arith_sum_int(x:nat, i: int) -> int { |
| 145 | + decreases(x); |
| 146 | + |
| 147 | + if i <= 0 { 0 } else { i + arith_sum_int(x, i - 1) } // FAILS |
| 148 | + } |
| 149 | + } => Err(err) => assert_one_fails(err) |
| 150 | +} |
| 151 | + |
| 152 | +test_verify_with_pervasive! { |
| 153 | + // Expression that decreases, but not based on the decreases clause provided |
| 154 | + #[test] expr_wrong_decreases_3 code! { |
| 155 | + #[spec] |
| 156 | + fn arith_sum_int(i: int) -> int { |
| 157 | + decreases(5); |
| 158 | + |
| 159 | + if i <= 0 { 0 } else { i + arith_sum_int(4) } // FAILS |
| 160 | + } |
| 161 | + } => Err(err) => assert_one_fails(err) |
| 162 | +} |
| 163 | + |
| 164 | +test_verify_with_pervasive! { |
| 165 | + // Expression that doesn't decrease due to extra clause |
| 166 | + #[test] expr_decrease_fail_2 code! { |
| 167 | + #[spec] |
| 168 | + fn arith_sum_int(x:nat, y:nat, i: int) -> int { |
| 169 | + decreases(i); |
| 170 | + |
| 171 | + if i <= 0 && x < y { 0 } else { i + arith_sum_int(x, y, i - 1) } // FAILS |
| 172 | + } |
| 173 | + } => Err(err) => assert_one_fails(err) |
| 174 | +} |
| 175 | + |
| 176 | +test_verify_with_pervasive! { |
| 177 | + // Expression that fails to decrease |
| 178 | + #[test] expr_decrease_fail_3 code! { |
| 179 | + #[spec] |
| 180 | + fn arith_sum_int(i: int) -> int { |
| 181 | + decreases(i); |
| 182 | + |
| 183 | + if i <= 0 { 0 } else { i + arith_sum_int(i) } // FAILS |
| 184 | + } |
| 185 | + } => Err(err) => assert_one_fails(err) |
| 186 | +} |
| 187 | + |
| 188 | +test_verify_with_pervasive! { |
| 189 | + // Mutually recursive expressions fail to decrease |
| 190 | + #[test] mutual_expr_decrease_fail code! { |
| 191 | + #[spec] |
| 192 | + fn count_down_a(i:nat) -> nat { |
| 193 | + decreases(i); |
| 194 | + |
| 195 | + if i == 0 { 0 } else { 1 + count_down_b(i - 1) } // FAILS |
| 196 | + } |
| 197 | + |
| 198 | + #[spec] |
| 199 | + fn count_down_b(i:nat) -> nat { |
| 200 | + decreases(5 - i); |
| 201 | + |
| 202 | + if i >= 5 { 0 } else { 1 + count_down_a(i + 1) } // FAILS |
| 203 | + } |
| 204 | + } => Err(err) => assert_two_fails(err) |
| 205 | +} |
| 206 | + |
| 207 | +test_verify_with_pervasive! { |
| 208 | + // Mutually recursive statements fail to decrease |
| 209 | + #[test] mutual_stmt_decrease_fail code! { |
| 210 | + #[proof] |
| 211 | + fn count_down_a_stmt(i:nat) { |
| 212 | + decreases(i); |
| 213 | + |
| 214 | + if i != 0 { |
| 215 | + count_down_b_stmt(i + 1); // FAILS |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + #[proof] |
| 220 | + fn count_down_b_stmt(i:nat) { |
| 221 | + decreases(i); |
| 222 | + |
| 223 | + if i != 0 { |
| 224 | + count_down_a_stmt(i + 1); // FAILS |
| 225 | + } |
| 226 | + } |
| 227 | + } => Err(err) => assert_two_fails(err) |
| 228 | +} |
| 229 | + |
| 230 | +// TODO: Expression that fails to decrease in a function returning unit |
| 231 | +/* |
| 232 | +test_verify_with_pervasive! { |
| 233 | + #[test] unit_expr_decrease_fail code! { |
| 234 | + #[spec] |
| 235 | + fn count_down_tricky(i:nat) { |
| 236 | + decreases(i); |
| 237 | +
|
| 238 | + if i != 0 { |
| 239 | + count_down_tricky(i + 1) |
| 240 | + } |
| 241 | + } |
| 242 | + } => Err(err) => assert_one_fails(err) |
| 243 | +} |
| 244 | +*/ |
0 commit comments