1
// There are three letter x, y, and z, which are each integers between 1 and 3 inclusive, where x + y = z
2

            
3
use std::collections::HashMap;
4
use std::sync::Mutex;
5

            
6
use minion_sys::ast::{Constant, Constraint, Model, Var, VarDomain, VarName};
7
use minion_sys::error::MinionError;
8
use minion_sys::get_from_table;
9

            
10
#[test]
11
#[allow(clippy::panic_in_result_fn)]
12
1
fn test_table_constraint() -> Result<(), MinionError> {
13
1
    let mut model = Model::new();
14

            
15
    // Declares variables (x, y, z), explicitly setting the integers to be between 1 and 3
16
1
    model
17
1
        .named_variables
18
1
        .add_var(String::from("x"), VarDomain::Discrete(1, 3));
19
1
    model
20
1
        .named_variables
21
1
        .add_var(String::from("y"), VarDomain::Discrete(1, 3));
22
1
    model
23
1
        .named_variables
24
1
        .add_var(String::from("z"), VarDomain::Discrete(1, 3));
25

            
26
    // Defines the table data (a list of tuples)
27
1
    let table_data = vec![
28
1
        vec![
29
1
            Constant::Integer(1),
30
1
            Constant::Integer(1),
31
1
            Constant::Integer(2),
32
        ],
33
1
        vec![
34
1
            Constant::Integer(1),
35
1
            Constant::Integer(2),
36
1
            Constant::Integer(3),
37
        ],
38
1
        vec![
39
1
            Constant::Integer(2),
40
1
            Constant::Integer(1),
41
1
            Constant::Integer(3),
42
        ],
43
    ];
44
    // Builds the Table constraint
45
1
    let vars = vec![
46
1
        Var::NameRef(String::from("x")),
47
1
        Var::NameRef(String::from("y")),
48
1
        Var::NameRef(String::from("z")),
49
    ];
50

            
51
1
    model.constraints.push(Constraint::Table(vars, table_data));
52

            
53
    // Runs the solver via the Minion interface
54
1
    minion_sys::run_minion(model, callback)?;
55

            
56
    // Asserts that we found exactly 3 solutions (one for each row in the table)
57
1
    let guard = SOLS_COUNTER.lock().unwrap();
58
1
    assert_eq!(*guard, 3);
59
1
    assert_ne!(get_from_table("Nodes".into()), None);
60
1
    Ok(())
61
1
}
62

            
63
// Global thread safe counter to store the number of solutions found
64
static SOLS_COUNTER: Mutex<u32> = Mutex::new(0);
65
// Callback function increments the counter for every solution
66
3
fn callback(_: HashMap<VarName, Constant>) -> bool {
67
    #[allow(clippy::unwrap_used)]
68
3
    let mut guard = SOLS_COUNTER.lock().unwrap();
69
3
    *guard += 1;
70
3
    true
71
3
}