LUMIERA.clone/tests/lib/diff/diff-index-table-test.cpp
Ichthyostega 3caa5cfb03 Tests: reorganise test folders
This resolves a long-standing issue:
we could not use proper include paths for test-helper code that was defined alongside the tests.

Since some test-helper code must now be relocated into the »Vault«-Layer,
I took the opportunity to finally resolve this annoyance. The solution is two-fold
 * introduce a dedicated test-support library
 * remove the quirks that were necessary to support some plain-C tests
Together, this allows to raise the include-directory by one level,
and then to adjust all folder names so that they match the namespace
2026-03-07 00:56:19 +01:00

205 lines
4.9 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
DiffIndexTable(Test) - simple sequence lookup table
Copyright (C)
2015, Hermann Vosseler <Ichthyostega@web.de>
  **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 diff-index-table-test.cpp
** unit test \ref DiffIndexTable_test
*/
#include "test/run.hpp"
#include "test/test-helper.hpp"
#include "lib/diff/index-table.hpp"
#include <utility>
#include <string>
#include <vector>
using std::string;
using std::vector;
using std::swap;
namespace lib {
namespace diff{
namespace test{
using lumiera::error::LUMIERA_ERROR_LOGIC;
namespace {//Test fixture....
using DataSeq = vector<string>;
using Index = IndexTable<string>;
#define TOK(id) id(STRINGIFY(id))
string TOK(a1), TOK(a2), TOK(a3), TOK(a4), TOK(a5);
string TOK(b1), TOK(b2), TOK(b3), TOK(b4);
}//(End)Test fixture
/*****************************************************************************//**
* @test Cover a simple lookup table helper, used to support diff generation.
* The IndexTable takes a snapshot of generic sequence data on construction.
* After that, it is immutable and supports lookup by element as key and
* membership check.
*
* @see IndexTable
* @see DiffListApplication_test
*/
class DiffIndexTable_test : public Test
{
virtual void
run (Arg)
{
simpleUsage();
verifySnapshot();
sequenceIteration();
duplicateDetection();
copy_and_move();
}
void
simpleUsage()
{
DataSeq data({a5,a2,a1,a4,a3});
Index idx(data);
CHECK (5 == idx.size());
CHECK (idx.contains(a1));
CHECK (idx.contains(a2));
CHECK (idx.contains(a3));
CHECK (idx.contains(a4));
CHECK (idx.contains(a5));
CHECK (!idx.contains(b1));
CHECK (!idx.contains(b2));
CHECK (a5 == idx.getElement(0));
CHECK (a2 == idx.getElement(1));
CHECK (a1 == idx.getElement(2));
CHECK (a4 == idx.getElement(3));
CHECK (a3 == idx.getElement(4));
CHECK (0 == idx.pos(a5));
CHECK (1 == idx.pos(a2));
CHECK (2 == idx.pos(a1));
CHECK (3 == idx.pos(a4));
CHECK (4 == idx.pos(a3));
}
void
verifySnapshot()
{
DataSeq data({a5,a2,a1,a4,a3});
Index idx(data);
data.clear();
data.push_back(b1);
CHECK (5 == idx.size());
CHECK (idx.contains(a1));
CHECK (idx.contains(a2));
CHECK (idx.contains(a3));
CHECK (idx.contains(a4));
CHECK (idx.contains(a5));
CHECK (!idx.contains(b1));
CHECK (!idx.contains(b2));
CHECK (0 == idx.pos(a5));
CHECK (1 == idx.pos(a2));
CHECK (2 == idx.pos(a1));
CHECK (3 == idx.pos(a4));
CHECK (4 == idx.pos(a3));
}
void
sequenceIteration()
{
DataSeq data({a5,a2,a1,a4,a3});
size_t i = 0;
for (auto elm : Index(data))
{
CHECK (elm == data[i++]);
}
}
void
duplicateDetection()
{
DataSeq data({a5,a2,a1,a4,a2,a3});
VERIFY_ERROR(LOGIC, Index idx(data));
}
void
copy_and_move()
{
DataSeq seqA({a5,a4,a1,a2,a3});
DataSeq seqB({b4,b3,b2,b1});
Index idxA(seqA);
Index idxB(seqB);
CHECK (5 == idxA.size());
CHECK (4 == idxB.size());
CHECK ( idxA.contains(a1));
CHECK (!idxA.contains(b1));
CHECK (!idxB.contains(a1));
CHECK ( idxB.contains(b1));
swap (idxA, idxB);
CHECK (!idxA.contains(a1));
CHECK ( idxA.contains(b1));
CHECK ( idxB.contains(a1));
CHECK (!idxB.contains(b1));
idxB = idxA;
CHECK (4 == idxA.size());
CHECK (4 == idxB.size());
CHECK (!idxA.contains(a1));
CHECK ( idxA.contains(b1));
CHECK (!idxB.contains(a1));
CHECK ( idxB.contains(b1));
}
};
/** Register this test class... */
LAUNCHER (DiffIndexTable_test, "unit common");
}}} // namespace lib::diff::test