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) Lumiera.org
5  2015, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/diff/index-table.hpp"
31 
32 #include <utility>
33 #include <string>
34 #include <vector>
35 
36 using std::string;
37 using std::vector;
38 using std::swap;
39 
40 
41 namespace lib {
42 namespace diff{
43 namespace test{
44 
45  using lumiera::error::LUMIERA_ERROR_LOGIC;
46 
47  namespace {//Test fixture....
48 
49  using DataSeq = vector<string>;
50  using Index = IndexTable<string>;
51 
52  #define TOK(id) id(STRINGIFY(id))
53 
54  string TOK(a1), TOK(a2), TOK(a3), TOK(a4), TOK(a5);
55  string TOK(b1), TOK(b2), TOK(b3), TOK(b4);
56 
57 
58  }//(End)Test fixture
59 
60 
61 
62 
63 
64 
65 
66 
67 
68  /*****************************************************************************/
77  class DiffIndexTable_test : public Test
78  {
79 
80  virtual void
81  run (Arg)
82  {
83  simpleUsage();
84  verifySnapshot();
85  sequenceIteration();
86  duplicateDetection();
87  copy_and_move();
88  }
89 
90 
91  void
92  simpleUsage()
93  {
94  DataSeq data({a5,a2,a1,a4,a3});
95  Index idx(data);
96 
97  CHECK (5 == idx.size());
98 
99  CHECK (idx.contains(a1));
100  CHECK (idx.contains(a2));
101  CHECK (idx.contains(a3));
102  CHECK (idx.contains(a4));
103  CHECK (idx.contains(a5));
104 
105  CHECK (!idx.contains(b1));
106  CHECK (!idx.contains(b2));
107 
108 
109  CHECK (a5 == idx.getElement(0));
110  CHECK (a2 == idx.getElement(1));
111  CHECK (a1 == idx.getElement(2));
112  CHECK (a4 == idx.getElement(3));
113  CHECK (a3 == idx.getElement(4));
114 
115 
116  CHECK (0 == idx.pos(a5));
117  CHECK (1 == idx.pos(a2));
118  CHECK (2 == idx.pos(a1));
119  CHECK (3 == idx.pos(a4));
120  CHECK (4 == idx.pos(a3));
121  }
122 
123 
124  void
125  verifySnapshot()
126  {
127  DataSeq data({a5,a2,a1,a4,a3});
128  Index idx(data);
129 
130  data.clear();
131  data.push_back(b1);
132 
133  CHECK (5 == idx.size());
134 
135  CHECK (idx.contains(a1));
136  CHECK (idx.contains(a2));
137  CHECK (idx.contains(a3));
138  CHECK (idx.contains(a4));
139  CHECK (idx.contains(a5));
140 
141  CHECK (!idx.contains(b1));
142  CHECK (!idx.contains(b2));
143 
144  CHECK (0 == idx.pos(a5));
145  CHECK (1 == idx.pos(a2));
146  CHECK (2 == idx.pos(a1));
147  CHECK (3 == idx.pos(a4));
148  CHECK (4 == idx.pos(a3));
149  }
150 
151 
152  void
153  sequenceIteration()
154  {
155  DataSeq data({a5,a2,a1,a4,a3});
156 
157  size_t i = 0;
158  for (auto elm : Index(data))
159  {
160  CHECK (elm == data[i++]);
161  }
162  }
163 
164 
165  void
166  duplicateDetection()
167  {
168  DataSeq data({a5,a2,a1,a4,a2,a3});
169 
170  VERIFY_ERROR(LOGIC, Index idx(data));
171  }
172 
173 
174  void
175  copy_and_move()
176  {
177  DataSeq seqA({a5,a4,a1,a2,a3});
178  DataSeq seqB({b4,b3,b2,b1});
179 
180  Index idxA(seqA);
181  Index idxB(seqB);
182  CHECK (5 == idxA.size());
183  CHECK (4 == idxB.size());
184 
185  CHECK ( idxA.contains(a1));
186  CHECK (!idxA.contains(b1));
187  CHECK (!idxB.contains(a1));
188  CHECK ( idxB.contains(b1));
189 
190  swap (idxA, idxB);
191 
192  CHECK (!idxA.contains(a1));
193  CHECK ( idxA.contains(b1));
194  CHECK ( idxB.contains(a1));
195  CHECK (!idxB.contains(b1));
196 
197  idxB = idxA;
198  CHECK (4 == idxA.size());
199  CHECK (4 == idxB.size());
200 
201  CHECK (!idxA.contains(a1));
202  CHECK ( idxA.contains(b1));
203  CHECK (!idxB.contains(a1));
204  CHECK ( idxB.contains(b1));
205  }
206  };
207 
208 
210  LAUNCHER (DiffIndexTable_test, "unit common");
211 
212 
213 
214 }}} // namespace lib::diff::test
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Implementation namespace for support and library code.
Simple test class runner.
data snapshot and lookup table
Definition: index-table.hpp:61
Generic lookup table for a sequence of unique values.
A collection of frequently used helper functions to support unit testing.