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
3
fn test_table_constraint() -> Result<(), MinionError> {
13
3
    let mut model = Model::new();
14

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

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

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

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

            
56
    // Asserts that we found exactly 3 solutions (one for each row in the table)
57
3
    let guard = SOLS_COUNTER.lock().unwrap();
58
3
    assert_eq!(*guard, 3);
59
3
    assert_ne!(get_from_table("Nodes".into()), None);
60
3
    Ok(())
61
3
}
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
9
fn callback(_: HashMap<VarName, Constant>) -> bool {
67
    #[allow(clippy::unwrap_used)]
68
9
    let mut guard = SOLS_COUNTER.lock().unwrap();
69
9
    *guard += 1;
70
9
    true
71
9
}