|
| 1 | +use std::{sync::Once, vec}; |
| 2 | +use viper::*; |
| 3 | + |
| 4 | +static INIT: Once = Once::new(); |
| 5 | + |
| 6 | +lazy_static::lazy_static! { |
| 7 | + static ref VIPER: Viper = Viper::new_for_tests(); |
| 8 | +} |
| 9 | + |
| 10 | +/// Setup function that is only run once, even if called multiple times. |
| 11 | +fn setup() { |
| 12 | + INIT.call_once(|| { |
| 13 | + env_logger::init(); |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +fn verify_multiple_errors(backend: viper::VerificationBackend, args: Vec<String>) { |
| 18 | + setup(); |
| 19 | + |
| 20 | + let verification_context: VerificationContext = VIPER.attach_current_thread(); |
| 21 | + let ast = verification_context.new_ast_factory(); |
| 22 | + |
| 23 | + let pos_1 = ast.identifier_position(123, 0, "pos-id:123"); |
| 24 | + let pos_1b = ast.identifier_position(1230, 0, "pos-id:1230"); |
| 25 | + let assertion_1 = ast.assert( |
| 26 | + ast.eq_cmp_with_pos(ast.local_var("x", ast.int_type()), ast.int_lit(0), pos_1b), |
| 27 | + pos_1, |
| 28 | + ); |
| 29 | + |
| 30 | + let havoc = ast.local_var_assign( |
| 31 | + ast.local_var("x", ast.int_type()), |
| 32 | + ast.local_var("v", ast.int_type()), |
| 33 | + ); |
| 34 | + |
| 35 | + let pos_2 = ast.identifier_position(456, 0, "pos-id:456"); |
| 36 | + let pos_2b = ast.identifier_position(4560, 0, "pos-id:4560"); |
| 37 | + let assertion_2 = ast.assert( |
| 38 | + ast.eq_cmp_with_pos(ast.local_var("x", ast.int_type()), ast.int_lit(0), pos_2b), |
| 39 | + pos_2, |
| 40 | + ); |
| 41 | + |
| 42 | + let body = ast.seqn( |
| 43 | + &[assertion_1, havoc, assertion_2], |
| 44 | + &[ast.local_var_decl("x", ast.int_type()).into()], |
| 45 | + ); |
| 46 | + let method = ast.method( |
| 47 | + "foo", |
| 48 | + &[ast.local_var_decl("v", ast.int_type())], |
| 49 | + &[], |
| 50 | + &[], |
| 51 | + &[], |
| 52 | + Some(body), |
| 53 | + ); |
| 54 | + let program = ast.program(&[], &[], &[], &[], &[method]); |
| 55 | + |
| 56 | + let mut verifier = |
| 57 | + verification_context.new_verifier_with_default_smt_and_extra_args(backend, args); |
| 58 | + |
| 59 | + let verification_result = verifier.verify(program); |
| 60 | + |
| 61 | + if let VerificationResult::Failure(errors) = verification_result { |
| 62 | + assert_eq!(errors.len(), 2); |
| 63 | + assert_eq!( |
| 64 | + errors[0].full_id, |
| 65 | + "assert.failed:assertion.false".to_string() |
| 66 | + ); |
| 67 | + assert_eq!( |
| 68 | + errors[1].full_id, |
| 69 | + "assert.failed:assertion.false".to_string() |
| 70 | + ); |
| 71 | + assert_eq!(errors[0].offending_pos_id, Some("pos-id:123".to_string())); |
| 72 | + assert_eq!(errors[1].offending_pos_id, Some("pos-id:456".to_string())); |
| 73 | + } else { |
| 74 | + unreachable!("{:?}", verification_result); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn report_multiple_errors_with_silicon() { |
| 80 | + verify_multiple_errors( |
| 81 | + viper::VerificationBackend::Silicon, |
| 82 | + vec![ |
| 83 | + "--logLevel=INFO".to_string(), |
| 84 | + "--numberOfErrorsToReport=0".to_string(), |
| 85 | + ], |
| 86 | + ); |
| 87 | +} |
0 commit comments