Generated on for Gecode by doxygen 1.17.0
blackbox.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Jip J. Dekker <jip.dekker@monash.edu>
5 *
6 * Contributing authors:
7 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
8 *
9 * Copyright:
10 * Jip J. Dekker, 2026
11 *
12 * This file is part of Gecode, the generic constraint
13 * development environment:
14 * http://www.gecode.dev
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining
17 * a copy of this software and associated documentation files (the
18 * "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 */
36
37#include "test/flatzinc.hh"
38
40
41#include <cstdint>
42#include <cerrno>
43#include <cstdio>
44#include <cstdlib>
45#include <exception>
46#include <fstream>
47#include <set>
48
49#ifdef GECODE_HAS_THREADS
50#include <thread>
51#endif
52
53#if defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
54#include <signal.h>
55#include <unistd.h>
56#endif
57
58namespace Test { namespace FlatZinc {
59
60 namespace {
61 const char* blackbox_decl =
62 "predicate gecode_blackbox("
63 "array[int] of var int: int_input, "
64 "array[int] of var float: float_input, "
65 "array[int] of var int: int_output, "
66 "array[int] of var float: float_output);\n";
67
68 const char* blackbox_bounds_decl =
69 "predicate gecode_blackbox_bounds("
70 "array[int] of var int: int_input, "
71 "array[int] of var float: float_input, "
72 "array[int] of int: flat_reason);\n";
73
74 std::string
75 fzn_string(const std::string& value) {
76 std::string quoted("\"");
77 for (char c : value) {
78 if ((c == '\\') || (c == '\"')) {
79 quoted += '\\';
80 }
81 quoted += c;
82 }
83 return quoted + "\"";
84 }
85
86 std::string
87 fixture_annotation(const char* mode, const std::string& fixture,
88 const std::vector<std::string>& args) {
89 std::string annotation("blackbox_");
90 annotation += mode;
91 annotation += "(" + fzn_string(fixture);
92 if (!args.empty()) {
93 annotation += ", [";
94 for (size_t i = 0; i < args.size(); ++i) {
95 if (i != 0) {
96 annotation += ", ";
97 }
98 annotation += fzn_string(args[i]);
99 }
100 annotation += "]";
101 }
102 return annotation + ")";
103 }
104
105 std::string
106 fixture_log(const std::string& name) {
107 const char* base = std::getenv("GECODE_TEST_BLACKBOX_LOG");
108 return (base == nullptr) ? std::string() : std::string(base) + "." + name;
109 }
110
111 void
112 reset_log(const std::string& log) {
113 std::remove(log.c_str());
114 }
115
116 std::vector<std::pair<std::string, std::string> >
117 read_log(const std::string& log) {
118 std::vector<std::pair<std::string, std::string> > entries;
119 std::ifstream in(log.c_str());
120 std::string event;
121 std::string id;
122 while (in >> event >> id) {
123 entries.push_back(std::make_pair(event, id));
124 }
125 return entries;
126 }
127
128 bool
129 concurrent_log(const std::string& log) {
130 const std::vector<std::pair<std::string, std::string> > entries =
131 read_log(log);
132 std::set<std::string> starts;
133 std::set<std::string> ready;
134 if ((entries.size() != 4) || (entries[0].first != "start") ||
135 (entries[1].first != "start") || (entries[2].first != "ready") ||
136 (entries[3].first != "ready")) {
137 return false;
138 }
139 for (const auto& entry : entries) {
140 (entry.first == "start" ? starts : ready).insert(entry.second);
141 }
142 return (starts.size() == 2) && (ready == starts);
143 }
144
145 bool
146 dll_parallel_lifecycle(const std::string& log) {
147 const std::vector<std::pair<std::string, std::string> > entries =
148 read_log(log);
149 std::set<std::string> clones;
150 std::set<std::string> starts;
151 std::set<std::string> ready;
152 std::set<std::string> freed;
153 std::vector<std::string> calls;
154 std::string prototype;
155 unsigned int inits = 0;
156 unsigned int clone_count = 0;
157 unsigned int start_count = 0;
158 unsigned int ready_count = 0;
159 unsigned int free_count = 0;
160 bool frees_after_calls = true;
161 for (const auto& entry : entries) {
162 if (entry.first == "init") {
163 ++inits;
164 prototype = entry.second;
165 } else if (entry.first == "clone") {
166 ++clone_count;
167 clones.insert(entry.second);
168 } else if (entry.first == "start") {
169 ++start_count;
170 starts.insert(entry.second);
171 calls.push_back(entry.first);
172 } else if (entry.first == "ready") {
173 ++ready_count;
174 ready.insert(entry.second);
175 calls.push_back(entry.first);
176 } else if (entry.first == "free") {
177 ++free_count;
178 freed.insert(entry.second);
179 frees_after_calls = frees_after_calls && (calls.size() == 4);
180 } else {
181 return false;
182 }
183 }
184 std::set<std::string> instances(clones);
185 instances.insert(prototype);
186 return (entries.size() == 10) && (entries[0].first == "init") &&
187 (inits == 1) && (clone_count == 2) && (start_count == 2) &&
188 (ready_count == 2) && (free_count == 3) && (clones.size() == 2) &&
189 (clones.find(prototype) == clones.end()) && (starts == clones) &&
190 (ready == clones) && (freed == instances) && frees_after_calls &&
191 (calls == std::vector<std::string>{"start", "start", "ready", "ready"});
192 }
193
194 bool
195 dll_model_lifecycle(const std::string& log) {
196 const std::vector<std::pair<std::string, std::string> > entries =
197 read_log(log);
198#ifdef GECODE_HAS_THREADS
199 if (entries.size() != 8) {
200 return false;
201 }
202 for (size_t i = 0; i < entries.size(); i += 4) {
203 if ((entries[i].first != "init") ||
204 (entries[i+1].first != "clone") ||
205 (entries[i+2] != std::make_pair(std::string("free"),
206 entries[i+1].second)) ||
207 (entries[i+3] != std::make_pair(std::string("free"),
208 entries[i].second))) {
209 return false;
210 }
211 }
212#else
213 if (entries.size() != 4) {
214 return false;
215 }
216 for (size_t i = 0; i < entries.size(); i += 2) {
217 if ((entries[i].first != "init") ||
218 (entries[i+1] != std::make_pair(std::string("free"),
219 entries[i].second))) {
220 return false;
221 }
222 }
223#endif
224 return true;
225 }
226
227#if defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
228 bool
229 descendant_gone(const std::string& output, const std::string& log) {
230 const std::vector<std::pair<std::string, std::string> > entries =
231 read_log(log);
232 reset_log(log);
233 if (output.find("y = 1;") == std::string::npos) {
234 return false;
235 }
236 for (const auto& entry : entries) {
237 if (entry.first == "descendant") {
238 const long pid = std::strtol(entry.second.c_str(), nullptr, 10);
239 if (pid <= 0) {
240 return false;
241 }
242 for (unsigned int i = 0; i < 500; ++i) {
243 if ((kill(static_cast<pid_t>(pid), 0) == -1) && (errno == ESRCH)) {
244 return true;
245 }
246 usleep(10000);
247 }
248 return false;
249 }
250 }
251 return false;
252 }
253#endif
254 }
255
256 namespace Blackbox {
257 class NativeProtocol : public Base {
258 public:
260 : Base("FlatZinc::blackbox::native_protocol",
261 TestTags::check()) {}
262 virtual bool run(void) {
263 std::vector<int64_t> int_input{-2};
264 std::vector<double> float_input{1.25};
265 std::vector<int64_t> int_output(1);
266 std::vector<double> float_output(1);
268 int_input, float_input, int_output, float_output
269 };
270 try {
272 "-2;1.25\n") {
273 return false;
274 }
276 } catch (...) {
277 return false;
278 }
279 return (int_output[0] == 7) && (float_output[0] == 2.5);
280 }
281 };
282
283#ifdef GECODE_HAS_THREADS
284 template<class T>
285 bool
286 parallel_runs(T& black_box) {
287 bool echoed[2] = {false, false};
288 std::exception_ptr exception[2];
289 auto run = [&black_box, &echoed, &exception](int i) {
290 try {
291 const int64_t input = (i == 0) ? -1 : i + 1;
292 std::vector<int64_t> int_out(1);
293 std::vector<double> float_out;
294 black_box.run({input}, {}, int_out, float_out);
295 echoed[i] = (int_out[0] == input);
296 } catch (...) {
297 exception[i] = std::current_exception();
298 }
299 };
300 std::thread first;
301 std::thread second;
302 auto join = [&first, &second](void) {
303 if (first.joinable()) {
304 first.join();
305 }
306 if (second.joinable()) {
307 second.join();
308 }
309 };
310 try {
311 first = std::thread(run, 0);
312 second = std::thread(run, 1);
313 } catch (...) {
314 join();
315 return false;
316 }
317 join();
318 return (exception[0] == nullptr) && (exception[1] == nullptr) &&
319 echoed[0] && echoed[1];
320 }
321
322#if defined(_WIN32) || defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
323 class NativeExecParallelSessions : public Base {
324 private:
325 std::string executable;
326 std::string log;
327 public:
328 NativeExecParallelSessions(const std::string& executable0,
329 const std::string& log0)
330 : Base("FlatZinc::blackbox::native_exec_parallel_sessions"),
331 executable(executable0), log(log0) {}
332 virtual bool run(void) {
333 reset_log(log);
334 bool ok = false;
335 try {
336 Gecode::FlatZinc::BlackBoxExec black_box(executable,
337 {"normal", log});
338 ok = parallel_runs(black_box) && concurrent_log(log);
339 } catch (...) {}
340 reset_log(log);
341 return ok;
342 }
343 };
344#endif
345
347 private:
348 std::string library;
349 std::string log;
350 public:
351 NativeDllParallelCalls(const std::string& library0,
352 const std::string& log0)
353 : Base("FlatZinc::blackbox::native_dll_parallel_calls"),
354 library(library0), log(log0) {}
355 virtual bool run(void) {
356 reset_log(log);
357 bool ok = false;
358 try {
359 {
360 Gecode::FlatZinc::BlackBoxLibrary black_box(library,
361 {"normal", log});
362 ok = parallel_runs(black_box);
363 }
364 ok = ok && dll_parallel_lifecycle(log);
365 } catch (...) {}
366 reset_log(log);
367 return ok;
368 }
369 };
370#endif
371
372 class Create {
373 public:
375 Create(void) {
376 (void) new NativeProtocol;
377 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::malformed_annotation",
378 std::string(blackbox_decl) +
379 "var 0..1: y;\n"
380 "constraint gecode_blackbox([], [], [y], []) :: "
381 "blackbox_exec([]);\n"
382 "solve satisfy;\n", {},
383 "expected a target string and an argument array");
384
385#if defined(_WIN32) || defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
386 const char* exec = std::getenv("GECODE_TEST_BLACKBOX_EXEC");
387 if (exec != nullptr) {
388 const std::string executable(exec);
389 (void) new FlatZincTest(TestTags::check(),"blackbox::constant_value",
390 std::string(blackbox_decl) +
391 "var 7..7: y :: output_var;\n"
392 "constraint gecode_blackbox([], [], [y], []) :: " +
393 fixture_annotation("exec", executable, {"value7"}) + ";\n"
394 "solve satisfy;\n",
395 "y = 7;\n----------\n");
396
397 (void) new FlatZincTest(TestTags::check(),"blackbox::constant_value_unsat",
398 std::string(blackbox_decl) +
399 "var 8..8: y;\n"
400 "constraint gecode_blackbox([], [], [y], []) :: " +
401 fixture_annotation("exec", executable, {"value7"}) + ";\n"
402 "solve satisfy;\n",
403 "=====UNSATISFIABLE=====\n");
404
405 (void) new FlatZincTest(TestTags::check(),"blackbox::reason_independent_bounds",
406 std::string(blackbox_bounds_decl) +
407 "var 5..5: x :: output_var;\n"
408 "constraint gecode_blackbox_bounds([x], [], [1,0,0]) :: " +
409 fixture_annotation("exec", executable, {"bounds2"}) + ";\n"
410 "solve satisfy;\n",
411 "x = 5;\n----------\n");
412
413 (void) new FlatZincTest(TestTags::check(),"blackbox::reason_independent_bounds_unsat",
414 std::string(blackbox_bounds_decl) +
415 "var 6..6: x;\n"
416 "constraint gecode_blackbox_bounds([x], [], [1,0,0]) :: " +
417 fixture_annotation("exec", executable, {"bounds2"}) + ";\n"
418 "solve satisfy;\n",
419 "=====UNSATISFIABLE=====\n");
420
421 (void) new FlatZincTest(TestTags::check(),"blackbox::reason_dependent_bounds",
422 std::string(blackbox_bounds_decl) +
423 "var 5..5: x :: output_var;\n"
424 "constraint gecode_blackbox_bounds([x], [], [1,1,1,1,0]) :: " +
425 fixture_annotation("exec", executable, {"bounds2"}) + ";\n"
426 "solve satisfy;\n",
427 "x = 5;\n----------\n");
428
429 (void) new FlatZincTest(TestTags::check(),"blackbox::bounds_rescheduled_after_branch",
430 std::string(blackbox_bounds_decl) +
431 "var 0..1: x :: output_var;\n"
432 "var 0..5: y :: output_var;\n"
433 "constraint gecode_blackbox_bounds([x,y], [], "
434 "[1,0,0,2,1,1,1,1,1,2]) :: " +
435 fixture_annotation("exec", executable, {"dependent_bounds"}) +
436 ";\nsolve :: int_search([x], input_order, indomain_min, complete) "
437 "satisfy;\n",
438 "x = 0;\ny = 0;\n----------\n"
439 "x = 1;\ny = 5;\n----------\n==========\n",
440 false, {"-a"});
441
442#ifdef GECODE_HAS_FLOAT_VARS
443 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::missing_bounds_reason_entry",
444 std::string(blackbox_bounds_decl) +
445 "var 0..10: x;\n"
446 "var 0.0..10.0: y;\n"
447 "constraint gecode_blackbox_bounds([x], [y], [1,0,0]) :: " +
448 fixture_annotation("exec", executable, {"mixed"}) + ";\n"
449 "solve satisfy;\n", {}, "missing explained variable entry");
450#endif
451
452 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::duplicate_bounds_reason_entry",
453 std::string(blackbox_bounds_decl) +
454 "var 0..10: x;\n"
455 "var 0..10: y;\n"
456 "constraint gecode_blackbox_bounds([x,y], [], [1,0,0,1,0,0]) :: " +
457 fixture_annotation("exec", executable, {"bounds4"}) + ";\n"
458 "solve satisfy;\n", {}, "duplicate explained variable index");
459
460 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::invalid_bounds_reason_code",
461 std::string(blackbox_bounds_decl) +
462 "var 0..10: x;\n"
463 "constraint gecode_blackbox_bounds([x], [], [1,1,1,0,0]) :: " +
464 fixture_annotation("exec", executable, {"bounds2"}) + ";\n"
465 "solve satisfy;\n", {}, "dependency bound code is out of range");
466
467#ifdef GECODE_HAS_FLOAT_VARS
468 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::invalid_float_output",
469 std::string(blackbox_decl) +
470 "var 0.0..10.0: y;\n"
471 "constraint gecode_blackbox([], [], [], [y]) :: " +
472 fixture_annotation("exec", executable, {"nan"}) + ";\n"
473 "solve satisfy;\n", {}, "Failed to read output float 0");
474#endif
475
476 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::malformed_exec_parallel",
477 std::string(blackbox_decl) +
478 "var 0..1: x :: output_var;\n"
479 "var 0..1: y :: output_var;\n"
480 "constraint gecode_blackbox([x], [], [y], []) :: " +
481 fixture_annotation("exec", executable, {"malformed"}) + ";\n"
482 "solve :: int_search([x], first_fail, indomain_min, complete) "
483 "satisfy;\n",
484 {"-p", "2"}, "Failed to read output integer 0");
485
486 for (int kind = 1; kind <= 5; ++kind) {
487 const char* expected = nullptr;
488 switch (kind) {
489 case 1:
490 expected = "Failed to read output integer 0";
491 break;
492 case 2:
493 expected = "provided an incomplete response";
494 break;
495 case 3:
496 expected = "response contains NUL data";
497 break;
498 case 4:
499 expected = "response exceeds the size limit";
500 break;
501 default:
502 expected = "integer 0 is outside Gecode's integer range";
503 break;
504 }
506 "blackbox::native_exec_fault_" + std::to_string(kind),
507 std::string(blackbox_decl) +
508 "var " + std::to_string(kind) + ".." + std::to_string(kind) +
509 ": x;\nvar 0..1: y;\n"
510 "constraint gecode_blackbox([x], [], [y], []) :: " +
511 fixture_annotation("exec", executable, {"fault"}) + ";\n"
512 "solve satisfy;\n", {}, expected);
513 }
514
515#ifdef GECODE_HAS_THREADS
516 const std::string exec_log = fixture_log("exec_parallel");
517 if (!exec_log.empty()) {
518 (void) new NativeExecParallelSessions(executable, exec_log);
519 }
520#endif
521
522#if defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
523 const std::string descendant_log = fixture_log("exec_descendant");
524 if (!descendant_log.empty()) {
525 (void) new FlatZincTest(TestTags::check(),"blackbox::native_exec_descendant_cleanup",
526 std::string(blackbox_decl) +
527 "var 1..1: y :: output_var;\n"
528 "constraint gecode_blackbox([], [], [y], []) :: " +
529 fixture_annotation("exec", executable,
530 {"descendant", descendant_log}) + ";\n"
531 "solve satisfy;\n",
532 "", false, {},
533 [descendant_log](const std::string& output) {
534 return descendant_gone(output, descendant_log);
535 },
536 [descendant_log](void) { reset_log(descendant_log); });
537 }
538#endif
539 }
540
541 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::missing_exec_parallel",
542 std::string(blackbox_decl) +
543 "var 0..1: x :: output_var;\n"
544 "var 0..1: y :: output_var;\n"
545 "constraint gecode_blackbox([x], [], [y], []) :: "
546 "blackbox_exec(\"gecode-blackbox-missing-program\");\n"
547 "solve :: int_search([x], first_fail, indomain_min, complete) "
548 "satisfy;\n",
549 {"-p", "2"}, "starting blackbox process failed");
550
551 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::missing_exec_root_status",
552 std::string(blackbox_decl) +
553 "var 0..0: x :: output_var;\n"
554 "var 0..1: y :: output_var;\n"
555 "constraint gecode_blackbox([x], [], [y], []) :: "
556 "blackbox_exec(\"gecode-blackbox-missing-program\");\n"
557 "solve satisfy;\n",
558 {"-p", "2"}, "starting blackbox process failed");
559#endif
560
561 const char* dll = std::getenv("GECODE_TEST_BLACKBOX_DLL");
562 if (dll != nullptr) {
563 const std::string library(dll);
564 const std::string dll_model_log = fixture_log("dll_model");
565 if (!dll_model_log.empty()) {
566 (void) new FlatZincTest(TestTags::check(),"blackbox::native_dll_per_constraint",
567 std::string(blackbox_decl) +
568 "var 1..1: a :: output_var;\n"
569 "var 1..1: b :: output_var;\n"
570 "constraint gecode_blackbox([], [], [a], []) :: " +
571 fixture_annotation("dll", library, {"normal", dll_model_log}) +
572 ";\nconstraint gecode_blackbox([], [], [b], []) :: " +
573 fixture_annotation("dll", library, {"normal", dll_model_log}) +
574 ";\nsolve satisfy;\n",
575 "a = 1;\nb = 1;\n----------\n", false, {},
576 [dll_model_log](const std::string& output) {
577 const bool ok = (output == "a = 1;\nb = 1;\n----------\n") &&
578 dll_model_lifecycle(dll_model_log);
579 reset_log(dll_model_log);
580 return ok;
581 },
582 [dll_model_log](void) { reset_log(dll_model_log); });
583 }
584
585#ifdef GECODE_HAS_THREADS
586 const std::string dll_log = fixture_log("dll_parallel");
587 if (!dll_log.empty()) {
588 (void) new NativeDllParallelCalls(library, dll_log);
589 }
590#endif
591
592#ifdef GECODE_HAS_FLOAT_VARS
593 (void) new FlatZincErrorTest(TestTags::check(),"blackbox::native_dll_nonfinite",
594 std::string(blackbox_decl) +
595 "var 0.0..1.0: y;\n"
596 "constraint gecode_blackbox([], [], [], [y]) :: " +
597 fixture_annotation("dll", library, {"nan"}) + ";\n"
598 "solve satisfy;\n", {},
599 "library output float 0 is not a finite value");
600#endif
601 }
602 }
603 };
604
606 }
607
608}}
609
610// STATISTICS: test-flatzinc
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1606
Persistent-process backend shared by equal executable configurations.
Dynamic-library backend owned by one blackbox constraint.
Base class for all tests to be run
Definition test.hh:186
Base(std::string s)
Create and register a standard test with name s.
Definition test.cpp:125
Create(void)
Perform creation and registration.
Definition blackbox.cpp:375
NativeDllParallelCalls(const std::string &library0, const std::string &log0)
Definition blackbox.cpp:351
virtual bool run(void)
Run test.
Definition blackbox.cpp:262
Base class for tests for FlatZinc
Definition flatzinc.hh:55
Set of test tags.
Definition test.hh:105
static TestTags check(void)
Integrity tests, also part of the standard suite.
Definition test.hpp:51
std::string encode_blackbox_request(const BlackBoxCall &call)
Encode one request for the executable backend's line protocol.
void decode_blackbox_response(const std::string &response, BlackBoxCall &call)
Decode and validate one response from the executable backend.
const double base
Base for geometric restart sequence.
Definition search.hh:134
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
bool parallel_runs(T &black_box)
Definition blackbox.cpp:286
Tests for FlatZinc.
Definition flatzinc.cpp:38
Case output("Data::Output", [](void) { Data d=read("n: int f: float w: word m[2][2]: int repeat n { k: int x[k]: int }", "2 20 a 1 2 3 4 1 5 2 6 7");std::ostringstream z;d.dzn(z);CHECK(z.str().find("f = 20.0;") !=std::string::npos);CHECK(z.str().find("m = [| 1, 2\n | 3, 4 |];") !=std::string::npos);CHECK(z.str().find("% x: array[int] of tuple(array[int] of int)\n" "x = [([5],), ([6, 7],)];") !=std::string::npos);CHECK(z.str().find("w = \"a\";") !=std::string::npos);std::ostringstream zq;read("var: int w: word", "3 a\033b").dzn(zq);CHECK(zq.str().find("'var' = 3;") !=std::string::npos);CHECK(zq.str().find("w = \"a\\x1bb\";") !=std::string::npos);Data e=read("e[2][0]: int repeat 2 { k: int repeat k { list y: int until 0 } }", "1 7 0 2 8 9 0 0");std::ostringstream ze;e.dzn(ze);CHECK(ze.str().find("e = array2d(1..2, 1..0, []);") !=std::string::npos);CHECK(ze.str().find("y = [([([7],)],), ([([8, 9],), ([],)],)];") !=std::string::npos);std::ostringstream z7;read("a[1][1][1][1][1][1][1]: int", "5").dzn(z7);CHECK(z7.str().find("a = [([([([([([([5],)],)],)],)],)],)];") !=std::string::npos);return true;})
Case instances("Read::Instances", [](void) { const char *spec="comment \"+++\"\n" "instances {\n" " sections skip other {\n" " \"instance {name:word}\" optional\n" " \"{n:int}\" last { x[n]: int }\n" " }\n" "}\n";const char *text="header text\ninstance a\n2\n1 2\n+++\n" "instance b\ndescription\n1\n7\n+++ end\n";Params by_name, by_number, bad;by_name.set("instance", "b");by_number.set("instance", "1");bad.set("instance", "3");CHECK(equal(read(spec, text, by_name).ints("x"), {7}));CHECK(equal(read(spec, text, by_number).ints("x"), {1, 2}));CHECK(read(spec, text, by_number).text("name")=="a");CHECK(fails([&](void) {(void) read(spec, text, bad);}, "there is no instance '3' (the file has 2 instances)"));CHECK(fails(spec, text, "the file has 2 instances; select one"));CHECK(equal(read(spec, "3\n4 5 6\n").ints("x"), {4, 5, 6}));Spec s=Spec::parse(spec, "s");Data c=s.catalog_text(text);CHECK(c.integer("instances")==2);CHECK(c.text("name", {1})=="b");Data c1=s.catalog_text("3\n4 5 6\n");CHECK((c1.integer("instances")==1) &&!c1.has("name"));CHECK(Spec::parse("a: int", "s").catalog_text("5").integer("instances")==1);CHECK(fails(spec, "1\n2\njunk\n", "expected a line matching \"{n:int}\""));CHECK(fails("instances { a: int }", "", "the file contains no instance"));CHECK(fails([](void) {(void) Spec::parse("instances { a: int } b: int", "s");}, "'instances' must be the last statement"));CHECK(fails([](void) {(void) Spec::parse("repeat 2 { instances { a: int } }", "s");}, "'instances' must be at the top level"));CHECK(fails("instances { if 0 > 1 { a: int } }", "5", "an instance must read some input"));return true;})
Gecode::IntArgs i({1, 2, 3, 4})
General test support.
Definition afc.cpp:41
@ check
Basic integrity tests.
Definition test.hh:99
Inputs and pre-sized output buffers for one backend call.