/* IndexIter(Test) - verify index access packaged as iterator handle Copyright (C) 2024, Hermann Vosseler   **Lumiera** is free software; you can redistribute it and/or modify it   under the terms of the GNU General Public License as published by the   Free Software Foundation; either version 2 of the License, or (at your   option) any later version. See the file COPYING for further details. * *****************************************************************/ /** @file index-iter-test.cpp ** unit test \ref IndexIter_test */ #include "lib/test/run.hpp" #include "lib/index-iter.hpp" #include "lib/test/test-helper.hpp" #include "lib/iter-explorer.hpp" #include "lib/format-util.hpp" #include "lib/util.hpp" #include #include namespace lib { namespace test{ using ::Test; using util::join; using util::isnil; using std::vector; using std::shared_ptr; using std::make_shared; using LERR_(ITER_EXHAUST); using LERR_(INDEX_BOUNDS); namespace { // test fixture const uint NUM_ELMS = 10; using Numz = vector; using Iter = IndexIter; using CIter = IndexIter; using SMIter = IndexIter>; inline Numz makeNumz() { Numz numz; for (uint i=0; i (makeNumz()); Numz & numz{*smartNumz}; Numz const& const_numz{numz}; uint i = 0; for (Iter iter{numz}; iter; ++iter, ++i ) { CHECK (iter); CHECK (iter != Iter()); CHECK (*iter == i); --(*iter); CHECK (*iter == i-1); } i = 0; for (CIter iter{const_numz}; iter; ++iter, ++i ) { CHECK (iter); CHECK (iter != CIter()); CHECK (*iter == i-1); // Note: the preceding loop has indeed modified the contents // ++(*iter); // but this doesn't compile, because the CIter yields a _const_ } verifyComparisons (CIter{numz}); CHECK (1 == smartNumz.use_count()); { SMIter smIter{smartNumz}; CIter cIter{*smartNumz}; CHECK (*cIter == uint(-1)); for (i=0; smIter; ++smIter, ++i) { CHECK (smIter); CHECK (smIter != SMIter()); CHECK (*smIter == i-1); ++(*smIter); CHECK (*smIter == i); } CHECK (isnil (smIter)); CHECK (smIter == SMIter()); cIter.setIDX(5); smIter.setIDX(5); CHECK (*smIter == *cIter); verifyComparisons (smIter); CHECK (5 == *cIter); // shared data modified CHECK (2 == smartNumz.use_count()); } CHECK (1 == smartNumz.use_count()); } /** @test verify equality handling and NIL detection * for the given iterator/wrapper handed in. * @note the argument is not altered; rather we create * several copies, to iterate and compare those */ template void verifyComparisons (IT const& ii) { IT i1(ii); IT i2(ii); IT iN; CHECK ( isnil (iN)); CHECK (!isnil (i1)); CHECK (!isnil (i2)); CHECK (i1 == i2); CHECK (i2 == i1); CHECK (i1 != iN); CHECK (iN != i1); CHECK (i2 != iN); CHECK (iN != i2); ++i1; CHECK (i1 != i2); CHECK (i1 != iN); ++i2; CHECK (i1 == i2); CHECK (i1 != iN); CHECK (i2 != iN); while (++i1) { } CHECK (isnil(i1)); CHECK (i1 != i2); CHECK (i1 == iN); while (++i2) { } CHECK (isnil(i2)); CHECK (i2 == i1); CHECK (i2 == iN); } }; LAUNCHER (IndexIter_test, "unit common"); }} // namespace lib::test