Generated on for Gecode by doxygen 1.17.0
native-registry.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 *
6 * Copyright:
7 * Mikael Zayenz Lagerkvist, 2026
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include "test/flatzinc.hh"
36
37#include <memory>
38
39namespace Test { namespace FlatZinc {
40 namespace {
41 // Annotations preserve both consistency and algorithm flags.
42 class AlgorithmAnnotations : public Base {
43 public:
44 AlgorithmAnnotations(void)
45 : Base("FlatZinc::NativeRegistry::AlgorithmAnnotations") {}
46 virtual bool run(void) {
47 const char* names[] = {"gecode_basic_propagation",
48 "gecode_advanced_propagation", "gecode_full_propagation"};
49 const Gecode::IntPropLevel levels[] = {
51 };
52 for (int i=0; i<3; i++) {
53 Gecode::FlatZinc::FlatZincSpace space;
54 Gecode::FlatZinc::AST::Array ann;
55 ann.a.push_back(new Gecode::FlatZinc::AST::Atom(names[i]));
56 ann.a.push_back(new Gecode::FlatZinc::AST::Atom("domain"));
57 if (space.ann2ipl(&ann) != (Gecode::IPL_DOM | levels[i]))
58 return false;
59 }
60 return true;
61 }
62 } algorithm_annotations;
63
65 class CumulativeAlgorithmAnnotations : public Base {
66 public:
67 CumulativeAlgorithmAnnotations(void)
68 : Base("FlatZinc::NativeRegistry::CumulativeAlgorithmAnnotations",
69 TestTags::check()) {}
70 virtual bool run(void) {
71 const char* levels[] = {"gecode_basic_propagation",
72 "gecode_advanced_propagation", "gecode_full_propagation"};
73 for (int i=0; i<3; i++) {
74 std::stringstream source;
75 source << "var 1..4:a; var 0..6:b; var 2..6:c; var 0..1:e; "
76 "constraint gecode_cumulatives([a,b,c,0,e],"
77 "[3,1,1,2,4],[3,3,1,2,1],3) :: " << levels[i]
78 << "; solve satisfy;";
79 Gecode::FlatZinc::Printer printer;
80 std::unique_ptr<Gecode::FlatZinc::FlatZincSpace> space(
81 Gecode::FlatZinc::parse(source, printer, olog));
82 if (!space)
83 return false;
84 const bool failed = space->status() == Gecode::SS_FAILED;
85 if (failed != (i != 0)) {
86 olog << "Unexpected root propagation for " << levels[i]
87 << std::endl;
88 return false;
89 }
90 }
91 return true;
92 }
93 } cumulative_algorithm_annotations;
94
95 class Create {
96 public:
97 Create(void) {
98 const std::string sat = "----------\n";
99 const std::string unsat = "=====UNSATISFIABLE=====\n";
100 // Explicit ends retain native scheduling semantics, including shared
101 // starts, empty tasks, and negative resource usage.
102 (void) new FlatZincTest(TestTags::check(),
103 "NativeRegistry::UnaryFlexSharedStart",
104 "var 0..1:s; var 1..2:e; "
105 "constraint int_plus(s,1,e); "
106 "constraint gecode_unary_flex([s,s],[1,1],[e,e]); solve satisfy;", unsat);
107 (void) new FlatZincTest(TestTags::check(),
108 "NativeRegistry::CumulativeFlexSharedStart",
109 "var 0..1:s; var 1..2:e; "
110 "constraint int_plus(s,1,e); "
111 "constraint gecode_cumulative_flex([s,s],[1,1],[e,e],[1,1],1); "
112 "solve satisfy;", unsat);
113 (void) new FlatZincTest(TestTags::check(),
114 "NativeRegistry::CumulativeFlexZeroDuration",
115 "constraint gecode_cumulative_flex([0,1],[2,0],[2,1],[1,1],1); "
116 "solve satisfy;", unsat);
117 (void) new FlatZincTest(TestTags::check(),
118 "NativeRegistry::CumulativesFlexNegativeUsage",
119 "constraint gecode_cumulatives_flex([0,0],[2,2],[2,2],"
120 "[3,-2],[0,0],[1],true); solve satisfy;", sat);
121 (void) new FlatZincTest(TestTags::check(),
122 "NativeRegistry::CumulativesFlexLowerBound",
123 "constraint gecode_cumulatives_flex([0,0],[2,2],[2,2],"
124 "[3,-2],[0,0],[2],false); solve satisfy;", unsat);
125 (void) new FlatZincTest(TestTags::check(),
126 "NativeRegistry::CumulativeSingletonZeroDuration",
127 "var 0..1:s; var 2..3:r; "
128 "constraint gecode_cumulatives([s],[0],[r],1); solve satisfy;", sat);
129 (void) new FlatZincTest(TestTags::check(),
130 "NativeRegistry::CumulativeSingletonPositiveDuration",
131 "var 0..1:s; var 2..3:r; "
132 "constraint gecode_cumulatives([s],[1],[r],1); solve satisfy;", unsat);
133 (void) new FlatZincTest(TestTags::check(),
134 "NativeRegistry::CumulativeSingletonVariableDuration",
135 "var 0..1:s; var 0..1:d :: output_var; var 2..3:r; "
136 "constraint gecode_cumulatives([s],[d],[r],1); solve satisfy;",
137 "d = 0;\n----------\n");
138 (void) new FlatZincTest("NativeRegistry::DistanceShared",
139 "var 0..2:x; constraint gecode_inter_distance([x,x],1); solve satisfy;", unsat);
140 (void) new FlatZincTest("NativeRegistry::DistanceNegativeEmpty",
141 "constraint gecode_inter_distance_fzn([],-1); solve satisfy;", unsat);
142 (void) new FlatZincTest("NativeRegistry::DistanceNegativeSingleton",
143 "constraint gecode_inter_distance_fzn([0],-1); solve satisfy;", unsat);
144 (void) new FlatZincTest("NativeRegistry::NotAllEqual",
145 "constraint gecode_not_all_equal_int([1,1,2]); solve satisfy;", sat);
146 (void) new FlatZincTest("NativeRegistry::CardinalityHoles",
147 "var {0,3}: count; constraint gecode_global_cardinality_closed([1,1],[1],[count]); solve satisfy;", unsat);
148 (void) new FlatZincTest("NativeRegistry::SortOrientation",
149 "constraint gecode_sort_permutation([3,1,2],[1,2,3],[2,3,1],1); solve satisfy;", unsat);
150 (void) new FlatZincTest("NativeRegistry::CountMatchesUnbounded",
151 "var int:x :: output_var; var int:y :: output_var; "
152 "constraint gecode_count_matches_fzn([x],[3],1,1); "
153 "constraint gecode_count_matches_fzn([y],[-3],1,1); solve satisfy;",
154 "x = 3;\ny = -3;\n----------\n");
155 (void) new FlatZincTest("NativeRegistry::CountMatchesRelations",
156 "var int:a :: output_var; var 3..4:b :: output_var; "
157 "var 3..4:c :: output_var; var int:d :: output_var; "
158 "var int:e :: output_var; "
159 "constraint gecode_count_matches_fzn([a],[3],2,0); "
160 "constraint gecode_count_matches_fzn([b],[3],3,0); "
161 "constraint gecode_count_matches_fzn([c],[3],4,1); "
162 "constraint gecode_count_matches_fzn([d],[3],5,1); "
163 "constraint gecode_count_matches_fzn([e],[3],6,0); solve satisfy;",
164 "a = 3;\nb = 4;\nc = 4;\nd = 3;\ne = 3;\n----------\n");
165 (void) new FlatZincTest("NativeRegistry::CountMatchesLimits",
166 "var -10..2147483646:x :: output_var; "
167 "var -2147483646..10:y :: output_var; var -10..10:z; "
168 "constraint gecode_count_matches_fzn([x],[2147483646],1,1); "
169 "constraint gecode_count_matches_fzn([y],[-2147483646],1,1); "
170 "constraint gecode_count_matches_fzn([z,z],[-2147483646,2147483646],1,0); solve satisfy;",
171 "x = 2147483646;\ny = -2147483646;\n----------\n");
172 (void) new FlatZincTest("NativeRegistry::ArgMinShared",
173 "var 1..2:x; constraint gecode_arg_min_int([x,x],1,x,false); solve satisfy;", sat);
174 (void) new FlatZincTest("NativeRegistry::InvalidModulusReification",
175 "constraint gecode_product_mod_var_fzn_reif([3],0,3,false); solve satisfy;", sat);
176 (void) new FlatZincTest("NativeRegistry::MinimumDistanceRepeatedSite",
177 "constraint gecode_minimum_distance_fzn([0,0,2],3,[0,2,4,2,0,3,4,3,0],1); solve satisfy;", unsat);
178 (void) new FlatZincTest("NativeRegistry::NondeterministicPrefix",
179 "constraint gecode_regular_nfa_int_fzn([0,1],3,0,2,[{1,2},{},{},{3},{},{}],1,{3}); solve satisfy;", sat);
180 (void) new FlatZincErrorTest("NativeRegistry::SortSize",
181 "constraint gecode_sort_permutation([1,2],[1],[1,2],1); solve satisfy;",
182 {}, "sort arrays must have equal lengths");
183#ifdef GECODE_HAS_SET_VARS
184 (void) new FlatZincTest("NativeRegistry::SetSequenceEmpty",
185 "constraint gecode_array_set_seq([]); solve satisfy;", sat);
186 (void) new FlatZincTest("NativeRegistry::SetSequenceEmptyUnion",
187 "constraint gecode_array_set_seq_union([],{1}); solve satisfy;", unsat);
188 (void) new FlatZincTest("NativeRegistry::AtmostOneSingletonCardinality",
189 "constraint gecode_set_atmost_one([{1}],2); solve satisfy;", unsat);
190 (void) new FlatZincTest("NativeRegistry::AtmostOneUnitCardinality",
191 "constraint gecode_set_atmost_one([{1},{1}],1); solve satisfy;", sat);
192 (void) new FlatZincErrorTest("NativeRegistry::SetMatrixSize",
193 "constraint gecode_set_element2d([{1}],0,0,0,1,{1}); solve satisfy;",
194 {}, "invalid set matrix dimensions");
195 (void) new FlatZincTest("NativeRegistry::EmptySetMinReification",
196 "constraint gecode_set_min_reif({},1,false); solve satisfy;", sat);
197#endif
198 }
199 };
200 Create c;
201 }
202}}
203
204// STATISTICS: test-flatzinc
std::vector< Node * > a
Definition ast.hh:233
IntPropLevel ann2ipl(AST::Node *ann)
Convert ann to integer propagation level.
static TestTags check(void)
Integrity tests, also part of the standard suite.
Definition test.hpp:51
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:1274
@ IPL_BASIC
Use basic propagation algorithm.
Definition int.hh:1281
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:1279
@ IPL_FULL
Use full propagation.
Definition int.hh:1284
@ IPL_ADVANCED
Use advanced propagation algorithm.
Definition int.hh:1282
SpaceStatus status(StatusStatistics &stat)
Query space status.
Definition core.cpp:281
@ SS_FAILED
Space is failed
Definition core.hpp:1727
GECODE_FLATZINC_EXPORT FlatZincSpace * parse(const std::string &fileName, Printer &p, std::ostream &err=std::cerr, FlatZincSpace *fzs=nullptr, Rnd &rnd=defrnd)
Parse FlatZinc file fileName into fzs and return it.
Tests for FlatZinc.
Definition flatzinc.cpp:38
Gecode::IntArgs i({1, 2, 3, 4})
General test support.
Definition afc.cpp:41
std::ostringstream olog
Stream used for logging.
Definition test.cpp:54
@ check
Basic integrity tests.
Definition test.hh:99