49#ifdef GECODE_HAS_THREADS
53#if defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
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";
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";
75 fzn_string(
const std::string& value) {
76 std::string quoted(
"\"");
77 for (
char c : value) {
78 if ((c ==
'\\') || (c ==
'\"')) {
87 fixture_annotation(
const char* mode,
const std::string& fixture,
88 const std::vector<std::string>& args) {
89 std::string annotation(
"blackbox_");
91 annotation +=
"(" + fzn_string(fixture);
94 for (
size_t i = 0;
i < args.
size(); ++
i) {
98 annotation += fzn_string(args[i]);
102 return annotation +
")";
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;
112 reset_log(
const std::string& log) {
113 std::remove(
log.c_str());
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());
122 while (in >> event >>
id) {
123 entries.push_back(std::make_pair(event,
id));
129 concurrent_log(
const std::string& log) {
130 const std::vector<std::pair<std::string, std::string> > entries =
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")) {
139 for (
const auto& entry : entries) {
140 (entry.first ==
"start" ? starts : ready).insert(entry.second);
142 return (starts.size() == 2) && (ready == starts);
146 dll_parallel_lifecycle(
const std::string& log) {
147 const std::vector<std::pair<std::string, std::string> > entries =
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") {
164 prototype = entry.second;
165 }
else if (entry.first ==
"clone") {
167 clones.insert(entry.second);
168 }
else if (entry.first ==
"start") {
170 starts.insert(entry.second);
171 calls.push_back(entry.first);
172 }
else if (entry.first ==
"ready") {
174 ready.insert(entry.second);
175 calls.push_back(entry.first);
176 }
else if (entry.first ==
"free") {
178 freed.insert(entry.second);
179 frees_after_calls = frees_after_calls && (calls.size() == 4);
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"});
195 dll_model_lifecycle(
const std::string& log) {
196 const std::vector<std::pair<std::string, std::string> > entries =
198#ifdef GECODE_HAS_THREADS
199 if (entries.size() != 8) {
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))) {
213 if (entries.size() != 4) {
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))) {
227#if defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
229 descendant_gone(
const std::string& output,
const std::string& log) {
230 const std::vector<std::pair<std::string, std::string> > entries =
233 if (
output.find(
"y = 1;") == std::string::npos) {
236 for (
const auto& entry : entries) {
237 if (entry.first ==
"descendant") {
238 const long pid = std::strtol(entry.second.c_str(),
nullptr, 10);
242 for (
unsigned int i = 0;
i < 500; ++
i) {
243 if ((kill(
static_cast<pid_t
>(pid), 0) == -1) && (errno == ESRCH)) {
260 :
Base(
"FlatZinc::blackbox::native_protocol",
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
279 return (int_output[0] == 7) && (float_output[0] == 2.5);
283#ifdef GECODE_HAS_THREADS
287 bool echoed[2] = {
false,
false};
288 std::exception_ptr exception[2];
289 auto run = [&black_box, &echoed, &exception](
int i) {
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);
297 exception[i] = std::current_exception();
302 auto join = [&first, &second](void) {
303 if (first.joinable()) {
306 if (second.joinable()) {
311 first = std::thread(run, 0);
312 second = std::thread(run, 1);
318 return (exception[0] ==
nullptr) && (exception[1] ==
nullptr) &&
319 echoed[0] && echoed[1];
322#if defined(_WIN32) || defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
323 class NativeExecParallelSessions :
public Base {
325 std::string executable;
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) {
338 ok = parallel_runs(black_box) && concurrent_log(log);
352 const std::string& log0)
353 :
Base(
"FlatZinc::blackbox::native_dll_parallel_calls"),
354 library(library0), log(log0) {}
364 ok = ok && dll_parallel_lifecycle(log);
378 std::string(blackbox_decl) +
380 "constraint gecode_blackbox([], [], [y], []) :: "
381 "blackbox_exec([]);\n"
382 "solve satisfy;\n", {},
383 "expected a target string and an argument array");
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);
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"
395 "y = 7;\n----------\n");
398 std::string(blackbox_decl) +
400 "constraint gecode_blackbox([], [], [y], []) :: " +
401 fixture_annotation(
"exec", executable, {
"value7"}) +
";\n"
403 "=====UNSATISFIABLE=====\n");
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"
411 "x = 5;\n----------\n");
414 std::string(blackbox_bounds_decl) +
416 "constraint gecode_blackbox_bounds([x], [], [1,0,0]) :: " +
417 fixture_annotation(
"exec", executable, {
"bounds2"}) +
";\n"
419 "=====UNSATISFIABLE=====\n");
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"
427 "x = 5;\n----------\n");
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) "
438 "x = 0;\ny = 0;\n----------\n"
439 "x = 1;\ny = 5;\n----------\n==========\n",
442#ifdef GECODE_HAS_FLOAT_VARS
444 std::string(blackbox_bounds_decl) +
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");
453 std::string(blackbox_bounds_decl) +
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");
461 std::string(blackbox_bounds_decl) +
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");
467#ifdef GECODE_HAS_FLOAT_VARS
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");
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) "
484 {
"-p",
"2"},
"Failed to read output integer 0");
486 for (
int kind = 1; kind <= 5; ++kind) {
487 const char* expected =
nullptr;
490 expected =
"Failed to read output integer 0";
493 expected =
"provided an incomplete response";
496 expected =
"response contains NUL data";
499 expected =
"response exceeds the size limit";
502 expected =
"integer 0 is outside Gecode's integer range";
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);
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);
522#if defined(GECODE_HAS_POSIX_BLACKBOX_EXEC)
523 const std::string descendant_log = fixture_log(
"exec_descendant");
524 if (!descendant_log.empty()) {
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"
533 [descendant_log](
const std::string& output) {
534 return descendant_gone(output, descendant_log);
536 [descendant_log](void) { reset_log(descendant_log); });
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) "
549 {
"-p",
"2"},
"starting blackbox process failed");
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"
558 {
"-p",
"2"},
"starting blackbox process failed");
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()) {
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);
582 [dll_model_log](void) { reset_log(dll_model_log); });
585#ifdef GECODE_HAS_THREADS
586 const std::string dll_log = fixture_log(
"dll_parallel");
587 if (!dll_log.empty()) {
592#ifdef GECODE_HAS_FLOAT_VARS
594 std::string(blackbox_decl) +
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");
int size(void) const
Return size of array (number of elements).
Persistent-process backend shared by equal executable configurations.
Dynamic-library backend owned by one blackbox constraint.
Base class for all tests to be run
Base(std::string s)
Create and register a standard test with name s.
Create(void)
Perform creation and registration.
virtual bool run(void)
Run test.
NativeDllParallelCalls(const std::string &library0, const std::string &log0)
virtual bool run(void)
Run test.
Base class for tests for FlatZinc
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.
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
bool parallel_runs(T &black_box)
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})
@ check
Basic integrity tests.
Inputs and pre-sized output buffers for one backend call.