Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIOVOC_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 
13 namespace Teuchos {
14 
15 
16 TEUCHOS_UNIT_TEST( OrdinalIndex, defaultConstruct )
17 {
19  ECHO(SIOVOCB::OrdinalIndex idx);
20  TEST_EQUALITY_CONST(idx.idx, -1); // Depends on implementation choice!
21 }
22 
23 
24 TEUCHOS_UNIT_TEST( OrdinalIndex, construct )
25 {
27  ECHO(SIOVOCB::OrdinalIndex idx(5));
28  TEST_EQUALITY_CONST(idx.idx, 5);
29 }
30 
31 
32 TEUCHOS_UNIT_TEST( KeyObjectPair, defaultConstruct )
33 {
35  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop);
36  TEST_EQUALITY_CONST(kop.first, "");
37  TEST_EQUALITY_CONST(kop.second.idx, -1);
38  TEST_EQUALITY_CONST(kop.isActive(), true);
39 }
40 
41 
42 TEUCHOS_UNIT_TEST( KeyObjectPair, construct_default_isActive )
43 {
45  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop("key_name1", 7));
46  TEST_EQUALITY_CONST(kop.first, "key_name1");
47  TEST_EQUALITY_CONST(kop.second.idx, 7);
48  TEST_EQUALITY_CONST(kop.isActive(), true);
49 }
50 
51 
52 TEUCHOS_UNIT_TEST( KeyObjectPair, makeInvalid )
53 {
55  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop =
56  SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex>::makeInvalid());
57  TEST_EQUALITY_CONST(kop.first, "");
58  TEST_EQUALITY_CONST(kop.second.idx, -1);
59  TEST_EQUALITY_CONST(kop.isActive(), false);
60 }
61 
62 
63 TEUCHOS_UNIT_TEST( KeyObjectPair, construct_set_isActive )
64 {
66  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop("key_name2", 8, false));
67  TEST_EQUALITY_CONST(kop.first, "key_name2");
68  TEST_EQUALITY_CONST(kop.second.idx, 8);
69  TEST_EQUALITY_CONST(kop.isActive(), false);
70 }
71 
72 
73 TEUCHOS_UNIT_TEST( KeyObjectPair, copyConstruct )
74 {
76  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop1("key_name", 3, false));
77  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop2(kop1));
78  TEST_EQUALITY_CONST(kop2.first, "key_name");
79  TEST_EQUALITY_CONST(kop2.second.idx, 3);
80  TEST_EQUALITY_CONST(kop2.isActive(), false);
81 }
82 
83 
84 TEUCHOS_UNIT_TEST( KeyObjectPair, assign )
85 {
87  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop1("key_name", 3, false));
88  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop2);
89  TEST_EQUALITY_CONST(kop2.isActive(), true);
90  ECHO(kop2 = kop1);
91  TEST_EQUALITY_CONST(kop2.first, "key_name");
92  TEST_EQUALITY_CONST(kop2.second.idx, 3);
93  TEST_EQUALITY_CONST(kop2.isActive(), false);
94 }
95 
96 
98 {
102 }
103 
104 
106 {
107 
108  //typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB; // unused
109  //typedef SIOVOCB::Ordinal Ordinal;
111 
112  ECHO(const Ordinal my_int_1_idx1 = oc.setObj("my_int_1", 3));
113  TEST_EQUALITY_CONST(my_int_1_idx1, 0);
116  ECHO(const Ordinal my_int_1_idx2 = oc.getObjOrdinalIndex(("my_int_1")));
117  TEST_EQUALITY(my_int_1_idx2, my_int_1_idx2);
118  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr(my_int_1_idx1), 3);
119  TEST_EQUALITY_CONST(*oc.getObjPtr(my_int_1_idx1), 3);
120  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_1"), 3);
121  TEST_EQUALITY_CONST(*oc.getObjPtr("my_int_1"), 3);
122 
123  ECHO(const Ordinal my_int_2_idx1 = oc.setObj("my_int_2", 4));
124  TEST_EQUALITY_CONST(my_int_2_idx1, 1);
127  ECHO(const Ordinal my_int_2_idx2 = oc.getObjOrdinalIndex(("my_int_2")));
128  TEST_EQUALITY(my_int_2_idx2, my_int_2_idx2);
129  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr(my_int_2_idx1), 4);
130  TEST_EQUALITY_CONST(*oc.getObjPtr(my_int_2_idx1), 4);
131  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_2"), 4);
132  TEST_EQUALITY_CONST(*oc.getObjPtr("my_int_2"), 4);
133 
134 }
135 
136 
138 {
139  // Test test makes sure that objects keep the same address when adding new
140  // objects.
141  //typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB; // unused
142  //typedef SIOVOCB::Ordinal Ordinal;
144  ECHO(const Ordinal my_int_1_idx = oc.setObj("my_int_1", 3));
145  ECHO(const int &my_int_1 = *oc.getObjPtr(my_int_1_idx));
146  ECHO(oc.setObj("my_int_2", 4));
147  TEST_EQUALITY_CONST(*oc.getObjPtr(my_int_1_idx), 3);
148  TEST_EQUALITY(&my_int_1, oc.getObjPtr(my_int_1_idx).get());
149  TEST_EQUALITY_CONST(my_int_1, 3);
150 }
151 
152 
154 {
155 
157  //typedef SIOVOCB::Ordinal Ordinal;
159  ECHO(oc.setObj("my_int_1", 3));
160  TEST_EQUALITY_CONST(oc.getObjOrdinalIndex("my_int_1"), 0);
161  ECHO(oc.setObj("my_int_2", 3));
162  TEST_EQUALITY_CONST(oc.getObjOrdinalIndex("my_int_2"), 1);
163  TEST_EQUALITY_CONST(oc.getObjOrdinalIndex("does_not_exist"), SIOVOCB::getInvalidOrdinal());
164 }
165 
166 
168 {
169 
170  //typedef StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal;
172 
173  ECHO(const Ordinal my_int_1_idx1 = oc.setObj("my_int_1", 3));
174  TEST_EQUALITY_CONST(my_int_1_idx1, 0);
175  ECHO(const Ordinal my_int_1_idx2 = oc.getObjOrdinalIndex(("my_int_1")));
176  TEST_EQUALITY_CONST(my_int_1_idx2, my_int_1_idx1);
177  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_1"), 3);
178 
179  ECHO(const Ordinal my_int_1_idx3 = oc.setObj("my_int_1", 4));
180  TEST_EQUALITY_CONST(my_int_1_idx3, 0);
181  ECHO(const Ordinal my_int_1_idx4 = oc.getObjOrdinalIndex(("my_int_1")));
182  TEST_EQUALITY_CONST(my_int_1_idx3, my_int_1_idx4);
183  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_1"), 4);
184 
185 }
186 
187 
189 {
192  ECHO(oc.setObj("c", 1));
193  ECHO(oc.setObj("a", 2));
194  ECHO(oc.setObj("b", 3));
195  ECHO(Iterator itr = oc.nonconstBegin());
196  TEST_EQUALITY_CONST(itr->first, "c");
197  TEST_EQUALITY_CONST(itr->second, 1);
198  ECHO(itr->second = 5);
199  TEST_EQUALITY_CONST(itr->second, 5);
200  ECHO(++itr);
201  TEST_EQUALITY_CONST(itr->first, "a");
202  TEST_EQUALITY_CONST(itr->second, 2);
203  ECHO(itr->second = 6);
204  TEST_EQUALITY_CONST(itr->second, 6);
205  ECHO(++itr);
206  TEST_EQUALITY_CONST(itr->first, "b");
207  TEST_EQUALITY_CONST(itr->second, 3);
208  ECHO(itr->second = 7);
209  TEST_EQUALITY_CONST(itr->second, 7);
210  ECHO(++itr);
211  TEST_ITER_EQUALITY(itr, oc.nonconstEnd());
212 }
213 
214 
216 {
219  ECHO(oc.setObj("c", 1));
220  ECHO(oc.setObj("a", 2));
221  ECHO(oc.setObj("b", 3));
222  ECHO(ConstIterator itr = oc.begin());
223  TEST_EQUALITY_CONST(itr->first, "c");
224  TEST_EQUALITY_CONST(itr->second, 1);
225  ECHO(++itr);
226  TEST_EQUALITY_CONST(itr->first, "a");
227  TEST_EQUALITY_CONST(itr->second, 2);
228  ECHO(++itr);
229  TEST_EQUALITY_CONST(itr->first, "b");
230  TEST_EQUALITY_CONST(itr->second, 3);
231  ECHO(++itr);
232  TEST_ITER_EQUALITY(itr, oc.end());
233 }
234 
235 
237 {
239  typedef SIOVOCB::OrdinalIndex OI;
242  ECHO(oc.setObj("c", 1));
243  ECHO(oc.setObj("a", 2));
244  ECHO(oc.setObj("b", 3));
247  ECHO(const OI &b = *oc.getObjPtr("b"));
248  ECHO(oc.removeObj(0));
251  TEST_EQUALITY(&b, oc.getObjPtr("b").get());
252  ECHO(ConstIterator itr = oc.begin());
253  TEST_EQUALITY_CONST(itr->first, "a");
254  TEST_EQUALITY_CONST(itr->second.idx, 2);
255  TEST_EQUALITY_CONST(itr->isActive(), true);
256  ECHO(++itr);
257  TEST_EQUALITY_CONST(itr->first, "b");
258  TEST_EQUALITY_CONST(itr->second.idx, 3);
259  TEST_EQUALITY_CONST(itr->isActive(), true);
260  ECHO(++itr);
261  TEST_ITER_EQUALITY(itr, oc.end());
262 }
263 
264 
266 {
268  typedef SIOVOCB::OrdinalIndex OI;
271  ECHO(oc.setObj("c", 1));
272  ECHO(oc.setObj("a", 2));
273  ECHO(oc.setObj("b", 3));
276  ECHO(const OI &b = *oc.getObjPtr("b"));
277  ECHO(oc.removeObj("c"));
280  TEST_EQUALITY(&b, oc.getObjPtr("b").get());
281  ECHO(ConstIterator itr = oc.begin());
282  TEST_EQUALITY_CONST(itr->first, "a");
283  TEST_EQUALITY_CONST(itr->second.idx, 2);
284  TEST_EQUALITY_CONST(itr->isActive(), true);
285  ECHO(++itr);
286  TEST_EQUALITY_CONST(itr->first, "b");
287  TEST_EQUALITY_CONST(itr->second.idx, 3);
288  TEST_EQUALITY_CONST(itr->isActive(), true);
289  ECHO(++itr);
290  TEST_ITER_EQUALITY(itr, oc.end());
291 }
292 
293 
295 {
297  typedef SIOVOCB::OrdinalIndex OI;
300  ECHO(oc.setObj("c", 1));
301  ECHO(oc.setObj("a", 2));
302  ECHO(oc.setObj("b", 3));
305  ECHO(const OI &c = *oc.getObjPtr("c"));
306  ECHO(oc.removeObj(1));
307  TEST_EQUALITY(&c, oc.getObjPtr("c").get());
308  ECHO(ConstIterator itr = oc.begin());
311  TEST_EQUALITY_CONST(itr->first, "c");
312  TEST_EQUALITY_CONST(itr->second.idx, 1);
313  TEST_EQUALITY_CONST(itr->isActive(), true);
314  ECHO(++itr);
315  TEST_EQUALITY_CONST(itr->first, "b");
316  TEST_EQUALITY_CONST(itr->second.idx, 3);
317  TEST_EQUALITY_CONST(itr->isActive(), true);
318  ECHO(++itr);
319  TEST_ITER_EQUALITY(itr, oc.end());
320 }
321 
322 
324 {
326  typedef SIOVOCB::OrdinalIndex OI;
329  ECHO(oc.setObj("c", 1));
330  ECHO(oc.setObj("a", 2));
331  ECHO(oc.setObj("b", 3));
334  ECHO(const OI &c = *oc.getObjPtr("c"));
335  ECHO(oc.removeObj("a"));
336  TEST_EQUALITY(&c, oc.getObjPtr("c").get());
339  ECHO(ConstIterator itr = oc.begin());
340  TEST_EQUALITY_CONST(itr->first, "c");
341  TEST_EQUALITY_CONST(itr->second.idx, 1);
342  TEST_EQUALITY_CONST(itr->isActive(), true);
343  ECHO(++itr);
344  TEST_EQUALITY_CONST(itr->first, "b");
345  TEST_EQUALITY_CONST(itr->second.idx, 3);
346  TEST_EQUALITY_CONST(itr->isActive(), true);
347  ECHO(++itr);
348  TEST_ITER_EQUALITY(itr, oc.end());
349 }
350 
351 
353 {
355  typedef SIOVOCB::OrdinalIndex OI;
358  ECHO(oc.setObj("c", 1));
359  ECHO(oc.setObj("a", 2));
360  ECHO(oc.setObj("b", 3));
363  ECHO(const OI &a = *oc.getObjPtr("a"));
364  ECHO(oc.removeObj(2));
367  TEST_EQUALITY(&a, oc.getObjPtr("a").get());
368  ECHO(ConstIterator itr = oc.begin());
369  TEST_EQUALITY_CONST(itr->first, "c");
370  TEST_EQUALITY_CONST(itr->second.idx, 1);
371  TEST_EQUALITY_CONST(itr->isActive(), true);
372  ECHO(++itr);
373  TEST_EQUALITY_CONST(itr->first, "a");
374  TEST_EQUALITY_CONST(itr->second.idx, 2);
375  TEST_EQUALITY_CONST(itr->isActive(), true);
376  ECHO(++itr);
377  TEST_ITER_EQUALITY(itr, oc.end());
378 }
379 
380 
382 {
384  typedef SIOVOCB::OrdinalIndex OI;
387  ECHO(oc.setObj("c", 1));
388  ECHO(oc.setObj("a", 2));
389  ECHO(oc.setObj("b", 3));
392  ECHO(const OI &a = *oc.getObjPtr("a"));
393  ECHO(oc.removeObj("b"));
396  TEST_EQUALITY(&a, oc.getObjPtr("a").get());
397  ECHO(ConstIterator itr = oc.begin());
398  TEST_EQUALITY_CONST(itr->first, "c");
399  TEST_EQUALITY_CONST(itr->second.idx, 1);
400  TEST_EQUALITY_CONST(itr->isActive(), true);
401  ECHO(++itr);
402  TEST_EQUALITY_CONST(itr->first, "a");
403  TEST_EQUALITY_CONST(itr->second.idx, 2);
404  TEST_EQUALITY_CONST(itr->isActive(), true);
405  ECHO(++itr);
406  TEST_ITER_EQUALITY(itr, oc.end());
407 }
408 
409 
411 {
413  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
415  ECHO(oc.setObj("a", 4));
417  TEST_THROW(oc.getNonconstObjPtr(-1), SIOVOCB::InvalidOrdinalIndexError);
418  TEST_THROW(oc.getNonconstObjPtr(1), SIOVOCB::InvalidOrdinalIndexError);
419  ECHO(oc.removeObj(0));
420  TEST_THROW(oc.getNonconstObjPtr(0), SIOVOCB::InvalidOrdinalIndexError);
421 }
422 
423 
425 {
427  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
429  ECHO(oc.setObj("a", 4));
430  TEST_EQUALITY_CONST(*oc.getObjPtr(0), 4);
431  TEST_THROW(oc.getObjPtr(-1), SIOVOCB::InvalidOrdinalIndexError);
432  TEST_THROW(oc.getObjPtr(1), SIOVOCB::InvalidOrdinalIndexError);
433  ECHO(oc.removeObj(0));
434  TEST_THROW(oc.getObjPtr(0), SIOVOCB::InvalidOrdinalIndexError);
435 }
436 
437 
439 {
441  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
443  ECHO(oc.setObj("a", 4));
444  TEST_EQUALITY_CONST(*oc.getObjPtr(0), 4);
445  TEST_THROW(oc.removeObj(-1), SIOVOCB::InvalidOrdinalIndexError);
446  TEST_THROW(oc.removeObj(1), SIOVOCB::InvalidOrdinalIndexError);
448  ECHO(oc.removeObj(0));
450  TEST_THROW(oc.removeObj(0), SIOVOCB::InvalidOrdinalIndexError);
451 }
452 
453 
455 {
457  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
459  ECHO(oc.setObj("a", 4));
461  TEST_THROW(oc.getNonconstObjPtr("does_not_exist"), SIOVOCB::InvalidKeyError);
462  ECHO(oc.removeObj("a"));
463  TEST_THROW(oc.getNonconstObjPtr("a"), SIOVOCB::InvalidKeyError);
464 }
465 
466 
468 {
470  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
472  ECHO(oc.setObj("a", 4));
473  TEST_EQUALITY_CONST(*oc.getObjPtr("a"), 4);
474  TEST_THROW(oc.getObjPtr("does_not_exist"), SIOVOCB::InvalidKeyError);
475  ECHO(oc.removeObj("a"));
476  TEST_THROW(oc.getObjPtr("a"), SIOVOCB::InvalidKeyError);
477 }
478 
479 
481 {
483  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
485  ECHO(oc.setObj("a", 4));
486  TEST_EQUALITY_CONST(*oc.getObjPtr("a"), 4);
487  TEST_THROW(oc.removeObj("does_not_exist"), SIOVOCB::InvalidKeyError);
489  ECHO(oc.removeObj("a"));
491  TEST_THROW(oc.removeObj("a"), SIOVOCB::InvalidKeyError);
492 }
493 
494 // ToDo: Test dangling object references!
495 
496 
497 } // namespace Teuchos
C++ Standard Library compatable filtered iterator.
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
String indexed ordered value-type object container class.
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
Ordinal setObj(const std::string &key, U &&obj)
Set (or reset) object by value and return its ordinal index.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
void removeObj(const Ordinal &idx)
Remove an object given its ordinal index.
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.