1
use conjure_cp::pipeline::{Config, Pipeline, TraceConfig};
2
use conjure_cp::settings::{self, Channelling, Heuristic, Parser};
3
use conjure_cp::solutions::SolutionOptions;
4
use conjure_cp_rules as _;
5

            
6
3
fn random_sequence(seed: u64) -> [usize; 3] {
7
3
    settings::with_saved_settings(|| {
8
3
        settings::set_heuristic_seed(seed);
9
9
        std::array::from_fn(|_| settings::next_heuristic_random_index(997))
10
3
    })
11
3
}
12

            
13
#[test]
14
1
fn interactive_answers_continue_from_rewriting_into_solver_loading() {
15
1
    let pipeline = Pipeline::new(Config {
16
1
        heuristic: Heuristic::Interactive,
17
1
        responses: vec![1, 2, 3],
18
1
        ..Default::default()
19
1
    })
20
1
    .unwrap();
21
1
    let solver_input = tempfile::NamedTempFile::new().unwrap();
22

            
23
1
    pipeline.with_settings(|| {
24
        // The first answer selects a set representation; the second selects all BOUND integers.
25
1
        let model = pipeline
26
1
            .compile(
27
1
                "find s : set (size 1) of int(1..3)\nfind x : int(1..3)",
28
1
                None,
29
1
            )
30
1
            .unwrap();
31
1
        let solutions = pipeline
32
1
            .solve(
33
1
                model,
34
1
                &SolutionOptions {
35
1
                    solver_input_file: Some(solver_input.path().to_path_buf()),
36
1
                    ..Default::default()
37
1
                },
38
1
            )
39
1
            .unwrap();
40
1
        assert_eq!(solutions.len(), 1);
41
1
        assert_eq!(
42
1
            settings::next_heuristic_interactive_index(&["a", "b", "c"]),
43
1
            2
44
        );
45
1
    });
46

            
47
1
    let input = std::fs::read_to_string(solver_input.path()).unwrap();
48
1
    assert!(
49
5
        input.lines().any(|line| line == "BOUND x {1..3}"),
50
        "the second answer should select BOUND for x:\n{input}"
51
    );
52
1
}
53

            
54
#[test]
55
1
fn nested_pipelines_restore_each_stream_and_the_caller_after_panic() {
56
1
    let first = Pipeline::new(Config {
57
1
        heuristic: Heuristic::Random,
58
1
        seed: 11,
59
1
        responses: vec![1, 2, 3],
60
1
        channelling: Channelling::Uniform,
61
1
        ..Default::default()
62
1
    })
63
1
    .unwrap();
64
1
    let second = Pipeline::new(Config {
65
1
        parser: Parser::ViaConjure,
66
1
        heuristic: Heuristic::Compact,
67
1
        seed: 29,
68
1
        responses: vec![2, 1],
69
1
        ..Default::default()
70
1
    })
71
1
    .unwrap();
72
1
    let first_random = random_sequence(11);
73
1
    let second_random = random_sequence(29);
74
1
    let caller_random = random_sequence(101);
75
1
    let options = ["a", "b", "c"];
76

            
77
1
    settings::with_saved_settings(|| {
78
1
        settings::set_current_parser(Parser::TreeSitter);
79
1
        settings::set_heuristic(Heuristic::First);
80
1
        settings::set_channelling(Channelling::Yes);
81
1
        settings::set_heuristic_seed(101);
82
1
        settings::set_heuristic_responses(vec![3]);
83

            
84
1
        first.with_settings(|| {
85
1
            assert_eq!(settings::heuristic(), Heuristic::Random);
86
1
            assert_eq!(settings::channelling(), Channelling::Uniform);
87
1
            assert_eq!(settings::next_heuristic_random_index(997), first_random[0]);
88
1
            assert_eq!(settings::next_heuristic_interactive_index(&options), 0);
89

            
90
1
            second.with_settings(|| {
91
1
                assert_eq!(settings::current_parser(), Parser::ViaConjure);
92
1
                assert_eq!(settings::heuristic(), Heuristic::Compact);
93
1
                assert_eq!(settings::next_heuristic_random_index(997), second_random[0]);
94
1
                assert_eq!(settings::next_heuristic_interactive_index(&options), 1);
95

            
96
1
                let interrupted = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
97
1
                    first.with_settings(|| {
98
1
                        assert_eq!(settings::current_parser(), Parser::TreeSitter);
99
1
                        assert_eq!(settings::heuristic(), Heuristic::Random);
100
1
                        assert_eq!(settings::channelling(), Channelling::Uniform);
101
1
                        assert_eq!(settings::next_heuristic_random_index(997), first_random[0]);
102
1
                        assert_eq!(settings::next_heuristic_interactive_index(&options), 0);
103
1
                        panic!("interrupted nested pipeline");
104
                    });
105
                }));
106
1
                assert!(interrupted.is_err());
107
1
                assert_eq!(settings::current_parser(), Parser::ViaConjure);
108
1
                assert_eq!(settings::heuristic(), Heuristic::Compact);
109
1
                assert_eq!(settings::channelling(), Channelling::No);
110
1
                assert_eq!(settings::next_heuristic_random_index(997), second_random[1]);
111
1
                assert_eq!(settings::next_heuristic_interactive_index(&options), 0);
112
1
            });
113

            
114
1
            assert_eq!(settings::current_parser(), Parser::TreeSitter);
115
1
            assert_eq!(settings::heuristic(), Heuristic::Random);
116
1
            assert_eq!(settings::channelling(), Channelling::Uniform);
117
1
            assert_eq!(settings::next_heuristic_random_index(997), first_random[1]);
118
1
            assert_eq!(settings::next_heuristic_interactive_index(&options), 1);
119
1
            first.with_settings(|| {
120
1
                assert_eq!(settings::next_heuristic_random_index(997), first_random[2]);
121
1
                assert_eq!(settings::next_heuristic_interactive_index(&options), 2);
122
1
            });
123
1
        });
124

            
125
1
        assert_eq!(settings::current_parser(), Parser::TreeSitter);
126
1
        assert_eq!(settings::heuristic(), Heuristic::First);
127
1
        assert_eq!(settings::channelling(), Channelling::Yes);
128
1
        assert_eq!(settings::next_heuristic_random_index(997), caller_random[0]);
129
1
        assert_eq!(settings::next_heuristic_interactive_index(&options), 2);
130
1
    });
131
1
}
132

            
133
#[test]
134
1
fn solver_search_restores_rewrite_tracing_for_successive_operations() {
135
1
    let pipeline = Pipeline::new(Config {
136
1
        trace: TraceConfig {
137
1
            rules: true,
138
1
            ..Default::default()
139
1
        },
140
1
        ..Default::default()
141
1
    })
142
1
    .unwrap();
143

            
144
1
    pipeline.with_settings(|| {
145
1
        let first = pipeline.compile("find x : bool", None).unwrap();
146
1
        assert!(settings::rule_trace_enabled());
147
1
        assert_eq!(
148
1
            pipeline
149
1
                .solve(first, &SolutionOptions::default())
150
1
                .unwrap()
151
1
                .len(),
152
1
            1
153
        );
154
1
        assert!(settings::rule_trace_enabled());
155

            
156
1
        let second = pipeline
157
1
            .compile("find x : int(1..3)\nminimising x", None)
158
1
            .unwrap();
159
1
        assert!(settings::rule_trace_enabled());
160
1
        let error = pipeline
161
1
            .solve(
162
1
                second,
163
1
                &SolutionOptions {
164
1
                    number_of_solutions: 0,
165
1
                    ..Default::default()
166
1
                },
167
1
            )
168
1
            .unwrap_err();
169
1
        assert!(error.to_string().contains("optimisation problems"));
170
1
        assert!(settings::rule_trace_enabled());
171
1
    });
172
1
}