Generated on for Gecode by doxygen 1.15.0
flatzinc.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.dev>
5 *
6 * Copyright:
7 * Guido Tack, 2014
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"
35
36#include <memory>
37
38namespace Test { namespace FlatZinc {
39
40 namespace {
41
43 class TupleSetAutoRepresentation : public Base {
44 public:
45 TupleSetAutoRepresentation(void)
46 : Base("FlatZinc::TupleSet::AutoRepresentation") {}
47
48 virtual bool run(void) {
49 using namespace Gecode;
50
51 const int n = 4096;
52 IntArgs tuples(2*n);
53 for (int i=0; i<n; i++) {
54 tuples[2*i] = i;
55 tuples[2*i+1] = n+i;
56 }
57
59 TupleSet ts = space.arg2tupleset(tuples,2);
60 return ts.finalized() &&
61 (ts.representation() == EPK_DENSE_COMPRESSED) &&
62 (ts.tuples() == n);
63 }
64 };
65
66 TupleSetAutoRepresentation tuple_set_auto_representation;
67
69 class GistStatisticsMode : public Base {
70 private:
71 static Gecode::ScriptMode mode(std::vector<std::string> args) {
72 Gecode::FlatZinc::FlatZincOptions opt("Gecode/FlatZinc");
73 std::string cmd("fzn-gecode");
74 int argc = static_cast<int>(args.size()) + 1;
75 std::vector<char*> argv(argc);
76 argv[0] = const_cast<char*>(cmd.data());
77 for (int i=1; i<argc; i++)
78 argv[i] = const_cast<char*>(args[i-1].data());
79 opt.parse(argc,argv.data());
80 return opt.mode();
81 }
82 public:
83 GistStatisticsMode(void)
84 : Base("FlatZinc::Options::GistStatisticsMode") {}
85
86 virtual bool run(void) {
87 return
88 (mode({"-s"}) == Gecode::SM_STAT) &&
89 (mode({"-mode", "gist", "-s"}) == Gecode::SM_GIST) &&
90 (mode({"-s", "-mode", "gist"}) == Gecode::SM_GIST);
91 }
92 };
93
94 GistStatisticsMode gist_statistics_mode;
95
96#ifndef GECODE_HAS_GIST
98 FlatZincErrorTest gist_unavailable(
99 "Options::GistUnavailable",
100 "var 1..1: x :: output_var;\nsolve satisfy;\n",
101 {"-mode", "gist", "-s"},
102 "Gist mode is unavailable in this build");
103#endif
104
105 }
106
107 FlatZincTest::FlatZincTest(const std::string& name, const std::string& source,
108 const std::string& expected, bool allSolutions,
109 std::vector<std::string> cmdlineOpt,
110 OutputCheck check, BeforeRun before)
111 : Base("FlatZinc::"+name), _name(name), _source(source), _expected(expected),
112 _allSolutions(allSolutions), _cmdlineOpt(cmdlineOpt),
113 _check(check), _before(before) {}
114
116 const std::string& source,
117 std::vector<std::string> cmdlineOpt,
118 std::string expectedMessage)
119 : FlatZincTest(name, source, "", false, cmdlineOpt),
120 _expectedMessage(expectedMessage) {}
121
122 bool
124 using namespace Gecode;
125 Support::Timer t_total;
126 t_total.start();
127 Gecode::FlatZinc::FlatZincOptions fznopt("Gecode/FlatZinc");
128 if (!_cmdlineOpt.empty()) {
129 std::string cmd("fzn-gecode");
130 int argc = static_cast<int>(_cmdlineOpt.size()) + 1;
131 std::vector<char*> argv(argc);
132 argv[0] = const_cast<char*>(cmd.data());
133 for (int i = 1; i < argc; ++i) {
134 argv[i] = const_cast<char*>(_cmdlineOpt[i-1].data());
135 }
136 fznopt.parse(argc, argv.data());
137 }
140 try {
141 if (_before) {
142 _before();
143 }
144 std::stringstream ss(_source);
145 std::unique_ptr<Gecode::FlatZinc::FlatZincSpace> fg(
147
148 if (fg) {
149 fg->createBranchers(p, fg->solveAnnotations(), fznopt,
150 false, olog);
151 fg->shrinkArrays(p);
152 std::ostringstream os;
153 fg->run(os, p, fznopt, t_total);
154
155 const std::string output = os.str();
156 fg.reset();
157 if (_check ? _check(output) : (_expected == output)) {
158 return true;
159 }
160 if (opt.log)
161 olog << "FlatZinc produced the following output:\n" << output << "\n";
162 return false;
163 } else {
164 if (opt.log)
165 olog << "Could not parse input\n";
166 return false;
167 }
168 } catch (Gecode::FlatZinc::Error& e) {
169 if (opt.log)
170 olog << ind(2) << "FlatZinc error : " << e.toString() << std::endl;
171 return false;
172 }
173 return true;
174 }
175
176 bool
178 using namespace Gecode;
179 Support::Timer t_total;
180 t_total.start();
181 Gecode::FlatZinc::FlatZincOptions fznopt("Gecode/FlatZinc");
182 if (!_cmdlineOpt.empty()) {
183 std::string cmd("fzn-gecode");
184 int argc = static_cast<int>(_cmdlineOpt.size()) + 1;
185 std::vector<char*> argv(argc);
186 argv[0] = const_cast<char*>(cmd.data());
187 for (int i = 1; i < argc; ++i) {
188 argv[i] = const_cast<char*>(_cmdlineOpt[i-1].data());
189 }
190 fznopt.parse(argc, argv.data());
191 }
193 std::ostringstream os;
194 try {
195 std::stringstream ss(_source);
196 std::unique_ptr<Gecode::FlatZinc::FlatZincSpace> fg(
198 if (fg) {
199 fg->createBranchers(p, fg->solveAnnotations(), fznopt,
200 false, olog);
201 fg->shrinkArrays(p);
202 fg->run(os, p, fznopt, t_total);
203 }
204 return false;
205 } catch (Gecode::FlatZinc::Error& e) {
206 const std::string message = e.toString();
207 if (opt.log)
208 olog << ind(2) << "Expected FlatZinc error : "
209 << message << std::endl;
210 return (os.str().find("----------") == std::string::npos) &&
211 (_expectedMessage.empty() ||
212 (message.find(_expectedMessage) != std::string::npos));
213 } catch (Gecode::Exception& e) {
214 const std::string message = e.what();
215 if (opt.log)
216 olog << ind(2) << "Expected Gecode exception : "
217 << message << std::endl;
218 return (os.str().find("----------") == std::string::npos) &&
219 (_expectedMessage.empty() ||
220 (message.find(_expectedMessage) != std::string::npos));
221 }
222 return false;
223 }
224
225}}
226
227// STATISTICS: test-flatzinc
Exception: Base-class for exceptions
Definition exception.hpp:42
Exception class for FlatZinc errors
Definition flatzinc.hh:727
const std::string & toString(void) const
Definition flatzinc.hh:735
Options for running FlatZinc models
Definition flatzinc.hh:226
bool allSolutions(void) const
Script mode to run.
Definition flatzinc.hh:344
void parse(int &argc, char *argv[])
Script mode to run.
Definition flatzinc.hh:324
A space that can be initialized with a FlatZinc model.
Definition flatzinc.hh:424
TupleSet arg2tupleset(const IntArgs &a, int noOfVars)
Convert a to TupleSet.
Output support class for FlatZinc interpreter.
Definition flatzinc.hh:107
Passing integer arguments.
Definition int.hh:652
void start(void)
Start timer.
Definition timer.hpp:58
Class representing a set of tuples.
Definition int.hh:2382
int tuples(void) const
Number of tuples.
bool finalized(void) const
Is tuple set successfully finalized.
ExtensionalPropKind representation(void) const
Return materialized tuple-set representation.
Base class for all tests to be run
Definition test.hh:121
Base(std::string s)
Create and register test with name s.
Definition test.cpp:60
const std::string & name(void) const
Return name of test.
Definition test.hpp:51
virtual bool run(void)
Perform test.
Definition flatzinc.cpp:177
FlatZincErrorTest(const std::string &name, const std::string &source, std::vector< std::string > cmdlineOpt={}, std::string expectedMessage="")
Construct and register test.
Definition flatzinc.cpp:115
virtual bool run(void)
Perform test.
Definition flatzinc.cpp:123
FlatZincTest(const std::string &name, const std::string &source, const std::string &expected, bool allSolutions=false, std::vector< std::string > cmdlineOpt={}, OutputCheck check=OutputCheck(), BeforeRun before=BeforeRun())
Construct and register test.
Definition flatzinc.cpp:107
std::function< void(void)> BeforeRun
Definition flatzinc.hh:58
std::vector< std::string > _cmdlineOpt
Definition flatzinc.hh:63
std::function< bool(const std::string &)> OutputCheck
Definition flatzinc.hh:57
Simple class for describing indentation.
Definition test.hh:67
ScriptMode
Different modes for executing scripts.
Definition driver.hh:94
@ SM_STAT
Print statistics for script.
Definition driver.hh:97
@ SM_GIST
Run script in Gist.
Definition driver.hh:98
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.
Gecode toplevel namespace
Tests for FlatZinc.
Definition flatzinc.cpp:38
General test support.
Definition afc.cpp:39
std::ostringstream olog
Stream used for logging.
Definition test.cpp:54
Options opt
The options.
Definition test.cpp:95