1
// There are three letter x, y, and z, which are each integers between 0 and 1 inclusive, where its binary and accepts every binary triple except all zeros and all ones
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_negative_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 0 and 1
16
1
    model
17
1
        .named_variables
18
1
        .add_var(String::from("x"), VarDomain::Discrete(0, 1));
19
1
    model
20
1
        .named_variables
21
1
        .add_var(String::from("y"), VarDomain::Discrete(0, 1));
22
1
    model
23
1
        .named_variables
24
1
        .add_var(String::from("z"), VarDomain::Discrete(0, 1));
25

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

            
46
1
    model
47
1
        .constraints
48
1
        .push(Constraint::NegativeTable(vars, forbidden_table_data));
49

            
50
    // Runs the solver via the Minion interface
51
1
    minion_sys::run_minion(model, callback)?;
52

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

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