|
| 1 | +use easy_smt::{ContextBuilder, Response}; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn test_real_numbers() { |
| 5 | + let mut ctx = ContextBuilder::new() |
| 6 | + .solver("z3") |
| 7 | + .solver_args(["-smt2", "-in"]) |
| 8 | + .build() |
| 9 | + .unwrap(); |
| 10 | + |
| 11 | + let x = ctx.declare_const("x", ctx.real_sort()).unwrap(); |
| 12 | + // x == 2.0 |
| 13 | + ctx.assert(ctx.eq(x, ctx.decimal(2.0))).unwrap(); |
| 14 | + assert_eq!(ctx.check().unwrap(), Response::Sat); |
| 15 | + let solution = ctx.get_value(vec![x]).unwrap(); |
| 16 | + let sol = ctx.get_f64(solution[0].1).unwrap(); |
| 17 | + // Z3 returns `2.0` |
| 18 | + assert!(sol == 2.0, "Expected solution to be 2.5, got {}", sol); |
| 19 | + |
| 20 | + let y = ctx.declare_const("y", ctx.real_sort()).unwrap(); |
| 21 | + // y == -2.0 |
| 22 | + ctx.assert(ctx.eq(y, ctx.decimal(-2.0))).unwrap(); |
| 23 | + assert_eq!(ctx.check().unwrap(), Response::Sat); |
| 24 | + let solution = ctx.get_value(vec![y]).unwrap(); |
| 25 | + let sol = ctx.get_f64(solution[0].1).unwrap(); |
| 26 | + // Z3 returns `- 2.0` |
| 27 | + assert!(sol == -2.0, "Expected solution to be 2.5, got {}", sol); |
| 28 | + |
| 29 | + let z = ctx.declare_const("z", ctx.real_sort()).unwrap(); |
| 30 | + // z == 2.5 / 1.0 |
| 31 | + ctx.assert(ctx.eq(z, ctx.rdiv(ctx.decimal(2.5), ctx.decimal(1.0)))) |
| 32 | + .unwrap(); |
| 33 | + assert_eq!(ctx.check().unwrap(), Response::Sat); |
| 34 | + let solution = ctx.get_value(vec![z]).unwrap(); |
| 35 | + let sol = ctx.get_f64(solution[0].1).unwrap(); |
| 36 | + // Z3 returns `(/ 5.0 2.0)` |
| 37 | + assert!(sol == 2.5, "Expected solution to be 2.5, got {}", sol); |
| 38 | + |
| 39 | + let a = ctx.declare_const("a", ctx.real_sort()).unwrap(); |
| 40 | + // a == 2.5 / -1.0 |
| 41 | + ctx.assert(ctx.eq(a, ctx.rdiv(ctx.decimal(2.5), ctx.decimal(-1.0)))) |
| 42 | + .unwrap(); |
| 43 | + assert_eq!(ctx.check().unwrap(), Response::Sat); |
| 44 | + let solution = ctx.get_value(vec![a]).unwrap(); |
| 45 | + let sol = ctx.get_f64(solution[0].1).unwrap(); |
| 46 | + // Z3 returns `(- (/ 5.0 2.0))` |
| 47 | + assert!(sol == -2.5, "Expected solution to be -2.5, got {}", sol); |
| 48 | +} |
0 commit comments