25. Testing propagators¶
Gecode’s test library checks a propagator against a specification of its
constraint. It tests propagation on complete assignments and partially
assigned variables, as well as cloning, scheduling, and subsumption. These
checks exercise the obligations discussed in
Propagator obligations. This chapter uses the less propagator
from Getting started to show how to write and run such a test.
The installed test library provides a test runner and support for testing integer, set, and float constraints. This chapter uses the integer test class.
25.1. Correctness and consistency¶
The test needs a specification independent of the propagator. For a constraint over integer variables, this is often a predicate that decides whether a complete assignment satisfies the constraint. Expressing the constraint directly in this predicate reduces the risk of reproducing errors in the propagation algorithm.
As discussed in Propagator obligations, a propagator must be correct and checking: it must preserve all solutions and detect failure for every complete assignment that violates the constraint. A test can also check whether the propagator achieves a particular consistency level. Correctness alone does not require the propagator to remove every unsupported value.
Testing a small finite domain can cover every complete assignment in that domain. It does not prove correctness for arbitrary integers, nor does it cover every possible partial domain or execution order. The test library also uses random domain changes to exercise propagation and reports a seed for reproducing failures.
25.2. Testing the less propagator¶
The class LessTest in Program 25.1 derives from
Test::Int::Test. Its constructor specifies the test’s tags and name,
followed by the number of variables and their common domain bounds. Here,
the two variables range from -3 to 3. Of the 49 complete assignments,
21 satisfy the constraint and 28 violate it. The argument false disables
reification tests, and IPL_DOM selects a domain consistency check.
#include "less.cpp"
#include <test/int.hh>
namespace {
class LessTest final : public ::Test::Int::Test {
public:
LessTest(void)
: ::Test::Int::Test(
::Test::TestTags(::Test::TestTag::check, ::Test::TestTag::standard),
"Less", 2, -3, 3, false, Gecode::IPL_DOM) {}
bool solution(const ::Test::Int::Assignment& x) const override {
return x[0] < x[1];
}
void post(Gecode::Space& home, Gecode::IntVarArray& x) override {
less(home, x[0], x[1]);
}
} less_test;
}
int
main(int argc, char* argv[]) {
return Test::run_registered_tests(argc, argv);
}
Download: less-test.cpp
The member function solution() specifies the constraint by comparing the
two assigned values. The member function post() posts the constraint on
the variables supplied by the test library, using the constraint post function
less(). The test library compares the results of propagation with
solution().
The test program defines main() and calls
Test::run_registered_tests() to run its registered tests.
The global object less_test registers the test during initialization and
remains alive throughout the call to run_registered_tests(). The integer
test class prefixes the name with Int::, so the runner lists this test as
Int::Less. Calls to run_registered_tests() share the registry and must
not overlap.
25.3. What the test library exercises¶
Using solution() and post(), the test library performs the following
checks with different variable domains, assigning values both before and
after posting the constraint.
Check |
Purpose |
|---|---|
Complete satisfying and violating assignments |
Detects rejection of solutions and acceptance of non-solutions. |
Assignment before and after posting, including partial assignment |
Exercises both posting-time behavior and later propagation. |
Random bounds changes and removal of domain values |
Exercises subscriptions and propagation as domains change. |
The selected consistency check |
Detects values or bounds that should have been pruned at the selected consistency level. |
Cloning and fixpoint checks |
Checks propagation in cloned spaces and looks for further pruning after a reported fixpoint. |
Disabling and re-enabling propagation |
Exercises |
Subsumption on successful complete assignments |
Checks that the propagator is subsumed when all variables are assigned and the constraint is satisfied. |
Search compared with the assignment predicate |
Detects a disagreement between the solutions reached by search and the independent specification. |
By default, the test library enumerates complete assignments in the declared domain. Domain pruning and some fixpoint checks are randomized. Increasing the number of iterations therefore exercises additional cases even when all complete assignments are enumerated.
25.4. Testing open-list constraints¶
Test::Int::OpenTest extends the integer test class for constraints
that follow an open variable list. Implement
solution() for the complete relation and the post() overload taking
an OpenIntVarList and an IntVarArray. The constructor specifies the
number of list items and auxiliary variables. The first entries in the
assignment and variable array are the list items; the remaining entries are
the auxiliary variables. All use the supplied common domain. Any additional
restrictions posted by the test must also appear in solution().
The harness runs the ordinary closed-list tests and bounded randomized histories that post, grow, prune, clone, and close the list. One additional position remains possible during these histories until closure. This exercises propagation while the list can still grow, including independence of cloned spaces. It supplements the closed-list checks without exhaustively covering all growth histories.
25.5. Checking consistency¶
The last constructor argument initializes the propagation level ipl stored
by the test class. Passing IPL_DOM also initializes the consistency check
to CTL_DOMAIN. A test can pass ipl to its constraint post function
when that function accepts a propagation level. The less() function has
no such argument, so the choice affects only the test.
The less propagator is domain consistent. After propagation succeeds,
every remaining value of x0 has support at x1.max(), and every
remaining value of x1 has support at x0.min(). Its two bound updates
therefore remove every unsupported value, even when the domains contain holes.
The propagation level and the checked consistency are separate fields in the
test class. The field contest selects the check: CTL_DOMAIN for domain
consistency, CTL_BOUNDS_D for bounds(D) consistency, CTL_BOUNDS_Z for
bounds(Z) consistency, or CTL_NONE for no consistency check. A test can
set this field in its constructor to match the consistency level of the
propagator.
25.6. Building a standalone test¶
Gecode must be built and installed with BUILD_TESTING=ON. The test
component also requires the integer and search modules.
Program 25.2 shows how to request the test component and
link the libraries needed for an integer propagator test.
cmake_minimum_required(VERSION 3.21)
project(less_test LANGUAGES CXX)
find_package(Gecode CONFIG REQUIRED COMPONENTS test)
add_executable(less-test less-test.cpp)
target_compile_features(less-test PRIVATE cxx_std_17)
target_link_libraries(less-test PRIVATE Gecode::gecodetestint)
Download: CMakeLists.txt
Place less.cpp, the test from
Program 25.1, and CMakeLists.txt in one directory. Then
configure, build, and run the test:
cmake -S . -B build -DCMAKE_PREFIX_PATH=/path/to/gecode
cmake --build build
./build/less-test -iter 1
Gecode_ROOT or Gecode_DIR can be used instead of CMAKE_PREFIX_PATH
to locate the installation. Linking against the imported target
Gecode::gecodetestint also links the test runner and the required Gecode
libraries.
25.7. Selecting tests and reproducing failures¶
The test has both the check and standard tags. Tags select groups of
tests; membership in one group does not imply membership in another. A constructor
without a tag argument assigns only standard.
The runner accepts one or more tag selections:
./build/less-test -tag check
./build/less-test -tag standard
./build/less-test -tag check -tag standard
./build/less-test -tag all
Repeated -tag options form a union. A -test name filter and a tag
filter must both match. With no -tag option, the runner applies no tag
filter, so even tests without tags can run. -tag all selects tests with
at least one known tag. -list-tags prints the known tags, while
-list-with-tags prints every registered test and its tags. These
listing commands ignore name, tag, and starting-point filters.
On failure, the runner prints the test name and a -replay command with
the seed for the failing iteration. Use that command to repeat the test.
For example, for Int::Less with seed 12345, enable logging with:
./build/less-test -test Int::Less -replay 12345 -log
The option -replay runs the named test once on a single thread. Unlike
-seed, it uses the supplied seed directly for that test. Preserve any
-fixprob option in the reported command as well. The option -log
prints the test’s buffered log on failure and requires a single thread. The
log includes the assignment that caused the failure. For example, replacing
x[0] < x[1] in the specification with x[0] <= x[1] makes equal
assignments valid according to the specification, while the propagator
rejects them. An assignment such as {-3,-3} exposes this disagreement.
25.8. Extending the test¶
The number of complete assignments grows exponentially with the number of
variables. For constraints with many arguments, use small representative
domains or override assignment() to supply a suitable assignment
generator. Include domains with holes when these affect the propagation
algorithm. A failing random case can be retained by recording its seed or
writing a separate test for that case.
A test for a reified constraint passes true to the base class constructor
and implements the post() overload that receives a Reify object. The
rms mask limits the test to supported reification modes. See
Reification and rewriting for the propagator side of reification.
Further tests may be needed for shared variables, invalid arguments, and arithmetic near the integer limits. Memory safety and performance also require checks beyond those provided by the integer test class.