Generated on for Gecode by doxygen 1.17.0
public-runner-smoke.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/test.hh"
35
36#include <cstdlib>
37#include <initializer_list>
38#include <iostream>
39#include <sstream>
40#include <string>
41#include <vector>
42
43namespace {
44
45 int passing_runs = 0;
46 int failing_runs = 0;
47
48 class PassingSmokeTest : public Test::Base {
49 public:
50 PassingSmokeTest(void)
51 : Test::Base("Smoke::A-Pass") {}
52
53 bool run(void) override {
54 passing_runs++;
55 return true;
56 }
57 } passing_smoke_test;
58
59 class FailingSmokeTest : public Test::Base {
60 public:
61 FailingSmokeTest(void)
62 : Test::Base("Smoke::B-Fail", Test::TestTags::sweep()) {}
63
64 bool run(void) override {
65 failing_runs++;
66 return false;
67 }
68 } failing_smoke_test;
69
70 unsigned int observed_seed = 0;
71 int replay_runs = 0;
72 class ReplaySmokeTest : public Test::Base {
73 public:
74 ReplaySmokeTest(void) : Test::Base("Smoke::Replay", Test::TestTags()) {}
75 bool run(void) override {
76 if (++replay_runs == 1) {
77 // Leave a state above the seed setter's modulus for the next iteration.
78 _rand.seed(44488);
79 (void) _rand.next();
80 return true;
81 }
82 observed_seed = _rand.seed();
83 (void) _rand(10);
84 return false;
85 }
86 } replay_smoke_test;
87
88 bool require(bool condition, const std::string& message) {
89 if (!condition) {
90 std::cerr << "public-runner-smoke: " << message << std::endl;
91 return false;
92 }
93 return true;
94 }
95
96 int run_and_capture(std::initializer_list<const char*> args, std::string& output) {
97 std::vector<char*> argv;
98 argv.reserve(args.size());
99 for (const char* arg : args) {
100 argv.push_back(const_cast<char*>(arg));
101 }
102
103 std::ostringstream captured;
104 std::streambuf* old = std::cout.rdbuf(captured.rdbuf());
105 const int rc = Test::run_registered_tests(static_cast<int>(argv.size()), argv.data());
106 std::cout.rdbuf(old);
107 output = captured.str();
108 return rc;
109 }
110
111}
112
113int
114main(void) {
115 std::string list_output;
116 if (!require(run_and_capture({"public-runner-smoke", "-list"}, list_output) == EXIT_SUCCESS,
117 "-list should succeed")) {
118 return EXIT_FAILURE;
119 }
120 const std::string pass_name = "Smoke::A-Pass";
121 const std::string fail_name = "Smoke::B-Fail";
122 const std::size_t pass_pos = list_output.find(pass_name);
123 const std::size_t fail_pos = list_output.find(fail_name);
124 if (!require(pass_pos != std::string::npos, "-list output missing pass test")) {
125 return EXIT_FAILURE;
126 }
127 if (!require(fail_pos != std::string::npos, "-list output missing fail test")) {
128 return EXIT_FAILURE;
129 }
130 if (!require(pass_pos < fail_pos, "-list output is not alphabetically ordered")) {
131 return EXIT_FAILURE;
132 }
133 if (!require(passing_runs == 0 && failing_runs == 0,
134 "-list should not execute any registered tests")) {
135 return EXIT_FAILURE;
136 }
137
138 std::string tagged_list_output;
139 if (!require(run_and_capture({"public-runner-smoke", "-list-with-tags"},
140 tagged_list_output) == EXIT_SUCCESS,
141 "-list-with-tags should succeed")) {
142 return EXIT_FAILURE;
143 }
144 if (!require(tagged_list_output.find(pass_name + " [standard]") != std::string::npos,
145 "default test should have the standard tag")) {
146 return EXIT_FAILURE;
147 }
148 if (!require(tagged_list_output.find("Smoke::B-Fail [sweep]") != std::string::npos,
149 "explicit test tag should be listed")) {
150 return EXIT_FAILURE;
151 }
152
153 std::string normal_output;
154 if (!require(run_and_capture({"public-runner-smoke", "-tag", "standard",
155 "-iter", "1", "-stop", "true"},
156 normal_output) == EXIT_SUCCESS,
157 "standard tag selection should succeed")) {
158 return EXIT_FAILURE;
159 }
160 if (!require(normal_output.find(pass_name) != std::string::npos &&
161 normal_output.find(fail_name) == std::string::npos,
162 "standard tag selection chose the wrong tests")) {
163 return EXIT_FAILURE;
164 }
165 if (!require(passing_runs == 1 && failing_runs == 0,
166 "standard tag selection run counts are wrong")) {
167 return EXIT_FAILURE;
168 }
169
170 std::string pass_output;
171 if (!require(run_and_capture({"public-runner-smoke", "-test", "Smoke::A-Pass", "-iter", "1", "-stop", "true"},
172 pass_output) == EXIT_SUCCESS,
173 "filtered passing run should succeed")) {
174 return EXIT_FAILURE;
175 }
176 if (!require(pass_output.find(pass_name) != std::string::npos,
177 "filtered passing run did not print the selected test")) {
178 return EXIT_FAILURE;
179 }
180 if (!require(pass_output.find(fail_name) == std::string::npos,
181 "filtered passing run executed an unselected test")) {
182 return EXIT_FAILURE;
183 }
184 if (!require(passing_runs == 2 && failing_runs == 0,
185 "filtered passing run counts are wrong")) {
186 return EXIT_FAILURE;
187 }
188
189 std::string fail_output;
190 if (!require(run_and_capture({"public-runner-smoke", "-test", "Smoke::B-Fail", "-iter", "1", "-stop", "true"},
191 fail_output) == EXIT_FAILURE,
192 "filtered failing run should fail")) {
193 return EXIT_FAILURE;
194 }
195 if (!require(fail_output.find(fail_name) != std::string::npos,
196 "filtered failing run did not print the selected test")) {
197 return EXIT_FAILURE;
198 }
199 if (!require(fail_output.find("Options: -replay ") != std::string::npos,
200 "filtered failing run did not preserve seed diagnostics")) {
201 return EXIT_FAILURE;
202 }
203 if (!require(fail_output.find("-test Smoke::B-Fail") != std::string::npos,
204 "filtered failing run did not preserve test diagnostics")) {
205 return EXIT_FAILURE;
206 }
207 if (!require(passing_runs == 2 && failing_runs == 1,
208 "filtered failing run counts are wrong")) {
209 return EXIT_FAILURE;
210 }
211
212 std::string replay_output;
213 if (!require(run_and_capture({"public-runner-smoke", "-test", "Smoke::Replay",
214 "-seed", "12345", "-iter", "2"},
215 replay_output) == EXIT_FAILURE && replay_runs == 2,
216 "seed-sensitive test should fail on its second iteration"))
217 return EXIT_FAILURE;
218 const std::string seed = std::to_string(observed_seed);
219 if (!require(replay_output.find("Options: -replay " + seed) != std::string::npos,
220 "failure should report the iteration's initial random state"))
221 return EXIT_FAILURE;
222 if (!require(run_and_capture({"public-runner-smoke", "-test", "Smoke::Replay",
223 "-replay", seed.c_str()}, replay_output) == EXIT_FAILURE &&
224 std::to_string(observed_seed) == seed,
225 "replay should restore the reported random state"))
226 return EXIT_FAILURE;
227
228 std::string combined_output;
229 if (!require(run_and_capture({"public-runner-smoke", "-tag", "standard",
230 "-tag", "sweep", "-iter", "1", "-stop", "true"},
231 combined_output) == EXIT_FAILURE,
232 "multiple tags should select their union")) {
233 return EXIT_FAILURE;
234 }
235 if (!require(combined_output.find(pass_name) != std::string::npos &&
236 combined_output.find(fail_name) != std::string::npos,
237 "multiple tag selection did not run both tags")) {
238 return EXIT_FAILURE;
239 }
240 if (!require(passing_runs == 3 && failing_runs == 2,
241 "multiple tag selection run counts are wrong")) {
242 return EXIT_FAILURE;
243 }
244
245 return EXIT_SUCCESS;
246}
247
248// STATISTICS: test-core
Base class for all tests to be run
Definition test.hh:186
static std::string str(bool b)
Map bool to string.
Definition test.hpp:121
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;})
int run_registered_tests(int argc, char *argv[])
Definition test.cpp:603
@ sweep
Really heavy sweep tests.
Definition test.hh:101
int main(void)