Lumiera  0.pre.03
»edit your freedom«
diff-index-table-test.cpp
Go to the documentation of this file.
1 /*
2  DiffIndexTable(Test) - simple sequence lookup table
3 
4  Copyright (C)
5  2015, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/diff/index-table.hpp"
22 
23 #include <utility>
24 #include <string>
25 #include <vector>
26 
27 using std::string;
28 using std::vector;
29 using std::swap;
30 
31 
32 namespace lib {
33 namespace diff{
34 namespace test{
35 
36  using lumiera::error::LUMIERA_ERROR_LOGIC;
37 
38  namespace {//Test fixture....
39 
40  using DataSeq = vector<string>;
41  using Index = IndexTable<string>;
42 
43  #define TOK(id) id(STRINGIFY(id))
44 
45  string TOK(a1), TOK(a2), TOK(a3), TOK(a4), TOK(a5);
46  string TOK(b1), TOK(b2), TOK(b3), TOK(b4);
47 
48 
49  }//(End)Test fixture
50 
51 
52 
53 
54 
55 
56 
57 
58 
59  /*****************************************************************************/
68  class DiffIndexTable_test : public Test
69  {
70 
71  virtual void
72  run (Arg)
73  {
74  simpleUsage();
75  verifySnapshot();
76  sequenceIteration();
77  duplicateDetection();
78  copy_and_move();
79  }
80 
81 
82  void
83  simpleUsage()
84  {
85  DataSeq data({a5,a2,a1,a4,a3});
86  Index idx(data);
87 
88  CHECK (5 == idx.size());
89 
90  CHECK (idx.contains(a1));
91  CHECK (idx.contains(a2));
92  CHECK (idx.contains(a3));
93  CHECK (idx.contains(a4));
94  CHECK (idx.contains(a5));
95 
96  CHECK (!idx.contains(b1));
97  CHECK (!idx.contains(b2));
98 
99 
100  CHECK (a5 == idx.getElement(0));
101  CHECK (a2 == idx.getElement(1));
102  CHECK (a1 == idx.getElement(2));
103  CHECK (a4 == idx.getElement(3));
104  CHECK (a3 == idx.getElement(4));
105 
106 
107  CHECK (0 == idx.pos(a5));
108  CHECK (1 == idx.pos(a2));
109  CHECK (2 == idx.pos(a1));
110  CHECK (3 == idx.pos(a4));
111  CHECK (4 == idx.pos(a3));
112  }
113 
114 
115  void
116  verifySnapshot()
117  {
118  DataSeq data({a5,a2,a1,a4,a3});
119  Index idx(data);
120 
121  data.clear();
122  data.push_back(b1);
123 
124  CHECK (5 == idx.size());
125 
126  CHECK (idx.contains(a1));
127  CHECK (idx.contains(a2));
128  CHECK (idx.contains(a3));
129  CHECK (idx.contains(a4));
130  CHECK (idx.contains(a5));
131 
132  CHECK (!idx.contains(b1));
133  CHECK (!idx.contains(b2));
134 
135  CHECK (0 == idx.pos(a5));
136  CHECK (1 == idx.pos(a2));
137  CHECK (2 == idx.pos(a1));
138  CHECK (3 == idx.pos(a4));
139  CHECK (4 == idx.pos(a3));
140  }
141 
142 
143  void
144  sequenceIteration()
145  {
146  DataSeq data({a5,a2,a1,a4,a3});
147 
148  size_t i = 0;
149  for (auto elm : Index(data))
150  {
151  CHECK (elm == data[i++]);
152  }
153  }
154 
155 
156  void
157  duplicateDetection()
158  {
159  DataSeq data({a5,a2,a1,a4,a2,a3});
160 
161  VERIFY_ERROR(LOGIC, Index idx(data));
162  }
163 
164 
165  void
166  copy_and_move()
167  {
168  DataSeq seqA({a5,a4,a1,a2,a3});
169  DataSeq seqB({b4,b3,b2,b1});
170 
171  Index idxA(seqA);
172  Index idxB(seqB);
173  CHECK (5 == idxA.size());
174  CHECK (4 == idxB.size());
175 
176  CHECK ( idxA.contains(a1));
177  CHECK (!idxA.contains(b1));
178  CHECK (!idxB.contains(a1));
179  CHECK ( idxB.contains(b1));
180 
181  swap (idxA, idxB);
182 
183  CHECK (!idxA.contains(a1));
184  CHECK ( idxA.contains(b1));
185  CHECK ( idxB.contains(a1));
186  CHECK (!idxB.contains(b1));
187 
188  idxB = idxA;
189  CHECK (4 == idxA.size());
190  CHECK (4 == idxB.size());
191 
192  CHECK (!idxA.contains(a1));
193  CHECK ( idxA.contains(b1));
194  CHECK (!idxB.contains(a1));
195  CHECK ( idxB.contains(b1));
196  }
197  };
198 
199 
201  LAUNCHER (DiffIndexTable_test, "unit common");
202 
203 
204 
205 }}} // namespace lib::diff::test
Definition: run.hpp:40
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Implementation namespace for support and library code.
Simplistic test class runner.
data snapshot and lookup table
Definition: index-table.hpp:52
Generic lookup table for a sequence of unique values.
A collection of frequently used helper functions to support unit testing.