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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
44 
45 namespace Teuchos {
46 
47 
48 TEUCHOS_UNIT_TEST( OrdinalIndex, defaultConstruct )
49 {
51  ECHO(SIOVOCB::OrdinalIndex idx);
52  TEST_EQUALITY_CONST(idx.idx, -1); // Depends on implementation choice!
53 }
54 
55 
56 TEUCHOS_UNIT_TEST( OrdinalIndex, construct )
57 {
59  ECHO(SIOVOCB::OrdinalIndex idx(5));
60  TEST_EQUALITY_CONST(idx.idx, 5);
61 }
62 
63 
64 TEUCHOS_UNIT_TEST( KeyObjectPair, defaultConstruct )
65 {
67  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop);
68  TEST_EQUALITY_CONST(kop.first, "");
69  TEST_EQUALITY_CONST(kop.second.idx, -1);
70  TEST_EQUALITY_CONST(kop.isActive(), true);
71 }
72 
73 
74 TEUCHOS_UNIT_TEST( KeyObjectPair, construct_default_isActive )
75 {
77  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop("key_name1", 7));
78  TEST_EQUALITY_CONST(kop.first, "key_name1");
79  TEST_EQUALITY_CONST(kop.second.idx, 7);
80  TEST_EQUALITY_CONST(kop.isActive(), true);
81 }
82 
83 
84 TEUCHOS_UNIT_TEST( KeyObjectPair, makeInvalid )
85 {
87  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop =
88  SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex>::makeInvalid());
89  TEST_EQUALITY_CONST(kop.first, "");
90  TEST_EQUALITY_CONST(kop.second.idx, -1);
91  TEST_EQUALITY_CONST(kop.isActive(), false);
92 }
93 
94 
95 TEUCHOS_UNIT_TEST( KeyObjectPair, construct_set_isActive )
96 {
98  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop("key_name2", 8, false));
99  TEST_EQUALITY_CONST(kop.first, "key_name2");
100  TEST_EQUALITY_CONST(kop.second.idx, 8);
101  TEST_EQUALITY_CONST(kop.isActive(), false);
102 }
103 
104 
105 TEUCHOS_UNIT_TEST( KeyObjectPair, copyConstruct )
106 {
108  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop1("key_name", 3, false));
109  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop2(kop1));
110  TEST_EQUALITY_CONST(kop2.first, "key_name");
111  TEST_EQUALITY_CONST(kop2.second.idx, 3);
112  TEST_EQUALITY_CONST(kop2.isActive(), false);
113 }
114 
115 
116 TEUCHOS_UNIT_TEST( KeyObjectPair, assign )
117 {
119  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop1("key_name", 3, false));
120  ECHO(SIOVOCB::KeyObjectPair<SIOVOCB::OrdinalIndex> kop2);
121  TEST_EQUALITY_CONST(kop2.isActive(), true);
122  ECHO(kop2 = kop1);
123  TEST_EQUALITY_CONST(kop2.first, "key_name");
124  TEST_EQUALITY_CONST(kop2.second.idx, 3);
125  TEST_EQUALITY_CONST(kop2.isActive(), false);
126 }
127 
128 
130 {
134 }
135 
136 
138 {
139 
140  //typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB; // unused
141  //typedef SIOVOCB::Ordinal Ordinal;
143 
144  ECHO(const Ordinal my_int_1_idx1 = oc.setObj("my_int_1", 3));
145  TEST_EQUALITY_CONST(my_int_1_idx1, 0);
148  ECHO(const Ordinal my_int_1_idx2 = oc.getObjOrdinalIndex(("my_int_1")));
149  TEST_EQUALITY(my_int_1_idx2, my_int_1_idx2);
150  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr(my_int_1_idx1), 3);
151  TEST_EQUALITY_CONST(*oc.getObjPtr(my_int_1_idx1), 3);
152  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_1"), 3);
153  TEST_EQUALITY_CONST(*oc.getObjPtr("my_int_1"), 3);
154 
155  ECHO(const Ordinal my_int_2_idx1 = oc.setObj("my_int_2", 4));
156  TEST_EQUALITY_CONST(my_int_2_idx1, 1);
159  ECHO(const Ordinal my_int_2_idx2 = oc.getObjOrdinalIndex(("my_int_2")));
160  TEST_EQUALITY(my_int_2_idx2, my_int_2_idx2);
161  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr(my_int_2_idx1), 4);
162  TEST_EQUALITY_CONST(*oc.getObjPtr(my_int_2_idx1), 4);
163  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_2"), 4);
164  TEST_EQUALITY_CONST(*oc.getObjPtr("my_int_2"), 4);
165 
166 }
167 
168 
170 {
171  // Test test makes sure that objects keep the same address when adding new
172  // objects.
173  //typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB; // unused
174  //typedef SIOVOCB::Ordinal Ordinal;
176  ECHO(const Ordinal my_int_1_idx = oc.setObj("my_int_1", 3));
177  ECHO(const int &my_int_1 = *oc.getObjPtr(my_int_1_idx));
178  ECHO(oc.setObj("my_int_2", 4));
179  TEST_EQUALITY_CONST(*oc.getObjPtr(my_int_1_idx), 3);
180  TEST_EQUALITY(&my_int_1, oc.getObjPtr(my_int_1_idx).get());
181  TEST_EQUALITY_CONST(my_int_1, 3);
182 }
183 
184 
186 {
187 
189  //typedef SIOVOCB::Ordinal Ordinal;
191  ECHO(oc.setObj("my_int_1", 3));
192  TEST_EQUALITY_CONST(oc.getObjOrdinalIndex("my_int_1"), 0);
193  ECHO(oc.setObj("my_int_2", 3));
194  TEST_EQUALITY_CONST(oc.getObjOrdinalIndex("my_int_2"), 1);
195  TEST_EQUALITY_CONST(oc.getObjOrdinalIndex("does_not_exist"), SIOVOCB::getInvalidOrdinal());
196 }
197 
198 
200 {
201 
202  //typedef StringIndexedOrderedValueObjectContainerBase::Ordinal Ordinal;
204 
205  ECHO(const Ordinal my_int_1_idx1 = oc.setObj("my_int_1", 3));
206  TEST_EQUALITY_CONST(my_int_1_idx1, 0);
207  ECHO(const Ordinal my_int_1_idx2 = oc.getObjOrdinalIndex(("my_int_1")));
208  TEST_EQUALITY_CONST(my_int_1_idx2, my_int_1_idx1);
209  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_1"), 3);
210 
211  ECHO(const Ordinal my_int_1_idx3 = oc.setObj("my_int_1", 4));
212  TEST_EQUALITY_CONST(my_int_1_idx3, 0);
213  ECHO(const Ordinal my_int_1_idx4 = oc.getObjOrdinalIndex(("my_int_1")));
214  TEST_EQUALITY_CONST(my_int_1_idx3, my_int_1_idx4);
215  TEST_EQUALITY_CONST(*oc.getNonconstObjPtr("my_int_1"), 4);
216 
217 }
218 
219 
221 {
224  ECHO(oc.setObj("c", 1));
225  ECHO(oc.setObj("a", 2));
226  ECHO(oc.setObj("b", 3));
227  ECHO(Iterator itr = oc.nonconstBegin());
228  TEST_EQUALITY_CONST(itr->first, "c");
229  TEST_EQUALITY_CONST(itr->second, 1);
230  ECHO(itr->second = 5);
231  TEST_EQUALITY_CONST(itr->second, 5);
232  ECHO(++itr);
233  TEST_EQUALITY_CONST(itr->first, "a");
234  TEST_EQUALITY_CONST(itr->second, 2);
235  ECHO(itr->second = 6);
236  TEST_EQUALITY_CONST(itr->second, 6);
237  ECHO(++itr);
238  TEST_EQUALITY_CONST(itr->first, "b");
239  TEST_EQUALITY_CONST(itr->second, 3);
240  ECHO(itr->second = 7);
241  TEST_EQUALITY_CONST(itr->second, 7);
242  ECHO(++itr);
243  TEST_ITER_EQUALITY(itr, oc.nonconstEnd());
244 }
245 
246 
248 {
251  ECHO(oc.setObj("c", 1));
252  ECHO(oc.setObj("a", 2));
253  ECHO(oc.setObj("b", 3));
254  ECHO(ConstIterator itr = oc.begin());
255  TEST_EQUALITY_CONST(itr->first, "c");
256  TEST_EQUALITY_CONST(itr->second, 1);
257  ECHO(++itr);
258  TEST_EQUALITY_CONST(itr->first, "a");
259  TEST_EQUALITY_CONST(itr->second, 2);
260  ECHO(++itr);
261  TEST_EQUALITY_CONST(itr->first, "b");
262  TEST_EQUALITY_CONST(itr->second, 3);
263  ECHO(++itr);
264  TEST_ITER_EQUALITY(itr, oc.end());
265 }
266 
267 
269 {
271  typedef SIOVOCB::OrdinalIndex OI;
274  ECHO(oc.setObj("c", 1));
275  ECHO(oc.setObj("a", 2));
276  ECHO(oc.setObj("b", 3));
279  ECHO(const OI &b = *oc.getObjPtr("b"));
280  ECHO(oc.removeObj(0));
283  TEST_EQUALITY(&b, oc.getObjPtr("b").get());
284  ECHO(ConstIterator itr = oc.begin());
285  TEST_EQUALITY_CONST(itr->first, "a");
286  TEST_EQUALITY_CONST(itr->second.idx, 2);
287  TEST_EQUALITY_CONST(itr->isActive(), true);
288  ECHO(++itr);
289  TEST_EQUALITY_CONST(itr->first, "b");
290  TEST_EQUALITY_CONST(itr->second.idx, 3);
291  TEST_EQUALITY_CONST(itr->isActive(), true);
292  ECHO(++itr);
293  TEST_ITER_EQUALITY(itr, oc.end());
294 }
295 
296 
298 {
300  typedef SIOVOCB::OrdinalIndex OI;
303  ECHO(oc.setObj("c", 1));
304  ECHO(oc.setObj("a", 2));
305  ECHO(oc.setObj("b", 3));
308  ECHO(const OI &b = *oc.getObjPtr("b"));
309  ECHO(oc.removeObj("c"));
312  TEST_EQUALITY(&b, oc.getObjPtr("b").get());
313  ECHO(ConstIterator itr = oc.begin());
314  TEST_EQUALITY_CONST(itr->first, "a");
315  TEST_EQUALITY_CONST(itr->second.idx, 2);
316  TEST_EQUALITY_CONST(itr->isActive(), true);
317  ECHO(++itr);
318  TEST_EQUALITY_CONST(itr->first, "b");
319  TEST_EQUALITY_CONST(itr->second.idx, 3);
320  TEST_EQUALITY_CONST(itr->isActive(), true);
321  ECHO(++itr);
322  TEST_ITER_EQUALITY(itr, oc.end());
323 }
324 
325 
327 {
329  typedef SIOVOCB::OrdinalIndex OI;
332  ECHO(oc.setObj("c", 1));
333  ECHO(oc.setObj("a", 2));
334  ECHO(oc.setObj("b", 3));
337  ECHO(const OI &c = *oc.getObjPtr("c"));
338  ECHO(oc.removeObj(1));
339  TEST_EQUALITY(&c, oc.getObjPtr("c").get());
340  ECHO(ConstIterator itr = oc.begin());
343  TEST_EQUALITY_CONST(itr->first, "c");
344  TEST_EQUALITY_CONST(itr->second.idx, 1);
345  TEST_EQUALITY_CONST(itr->isActive(), true);
346  ECHO(++itr);
347  TEST_EQUALITY_CONST(itr->first, "b");
348  TEST_EQUALITY_CONST(itr->second.idx, 3);
349  TEST_EQUALITY_CONST(itr->isActive(), true);
350  ECHO(++itr);
351  TEST_ITER_EQUALITY(itr, oc.end());
352 }
353 
354 
356 {
358  typedef SIOVOCB::OrdinalIndex OI;
361  ECHO(oc.setObj("c", 1));
362  ECHO(oc.setObj("a", 2));
363  ECHO(oc.setObj("b", 3));
366  ECHO(const OI &c = *oc.getObjPtr("c"));
367  ECHO(oc.removeObj("a"));
368  TEST_EQUALITY(&c, oc.getObjPtr("c").get());
371  ECHO(ConstIterator itr = oc.begin());
372  TEST_EQUALITY_CONST(itr->first, "c");
373  TEST_EQUALITY_CONST(itr->second.idx, 1);
374  TEST_EQUALITY_CONST(itr->isActive(), true);
375  ECHO(++itr);
376  TEST_EQUALITY_CONST(itr->first, "b");
377  TEST_EQUALITY_CONST(itr->second.idx, 3);
378  TEST_EQUALITY_CONST(itr->isActive(), true);
379  ECHO(++itr);
380  TEST_ITER_EQUALITY(itr, oc.end());
381 }
382 
383 
385 {
387  typedef SIOVOCB::OrdinalIndex OI;
390  ECHO(oc.setObj("c", 1));
391  ECHO(oc.setObj("a", 2));
392  ECHO(oc.setObj("b", 3));
395  ECHO(const OI &a = *oc.getObjPtr("a"));
396  ECHO(oc.removeObj(2));
399  TEST_EQUALITY(&a, oc.getObjPtr("a").get());
400  ECHO(ConstIterator itr = oc.begin());
401  TEST_EQUALITY_CONST(itr->first, "c");
402  TEST_EQUALITY_CONST(itr->second.idx, 1);
403  TEST_EQUALITY_CONST(itr->isActive(), true);
404  ECHO(++itr);
405  TEST_EQUALITY_CONST(itr->first, "a");
406  TEST_EQUALITY_CONST(itr->second.idx, 2);
407  TEST_EQUALITY_CONST(itr->isActive(), true);
408  ECHO(++itr);
409  TEST_ITER_EQUALITY(itr, oc.end());
410 }
411 
412 
414 {
416  typedef SIOVOCB::OrdinalIndex OI;
419  ECHO(oc.setObj("c", 1));
420  ECHO(oc.setObj("a", 2));
421  ECHO(oc.setObj("b", 3));
424  ECHO(const OI &a = *oc.getObjPtr("a"));
425  ECHO(oc.removeObj("b"));
428  TEST_EQUALITY(&a, oc.getObjPtr("a").get());
429  ECHO(ConstIterator itr = oc.begin());
430  TEST_EQUALITY_CONST(itr->first, "c");
431  TEST_EQUALITY_CONST(itr->second.idx, 1);
432  TEST_EQUALITY_CONST(itr->isActive(), true);
433  ECHO(++itr);
434  TEST_EQUALITY_CONST(itr->first, "a");
435  TEST_EQUALITY_CONST(itr->second.idx, 2);
436  TEST_EQUALITY_CONST(itr->isActive(), true);
437  ECHO(++itr);
438  TEST_ITER_EQUALITY(itr, oc.end());
439 }
440 
441 
443 {
445  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
447  ECHO(oc.setObj("a", 4));
449  TEST_THROW(oc.getNonconstObjPtr(-1), SIOVOCB::InvalidOrdinalIndexError);
450  TEST_THROW(oc.getNonconstObjPtr(1), SIOVOCB::InvalidOrdinalIndexError);
451  ECHO(oc.removeObj(0));
452  TEST_THROW(oc.getNonconstObjPtr(0), SIOVOCB::InvalidOrdinalIndexError);
453 }
454 
455 
457 {
459  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
461  ECHO(oc.setObj("a", 4));
462  TEST_EQUALITY_CONST(*oc.getObjPtr(0), 4);
463  TEST_THROW(oc.getObjPtr(-1), SIOVOCB::InvalidOrdinalIndexError);
464  TEST_THROW(oc.getObjPtr(1), SIOVOCB::InvalidOrdinalIndexError);
465  ECHO(oc.removeObj(0));
466  TEST_THROW(oc.getObjPtr(0), SIOVOCB::InvalidOrdinalIndexError);
467 }
468 
469 
471 {
473  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
475  ECHO(oc.setObj("a", 4));
476  TEST_EQUALITY_CONST(*oc.getObjPtr(0), 4);
477  TEST_THROW(oc.removeObj(-1), SIOVOCB::InvalidOrdinalIndexError);
478  TEST_THROW(oc.removeObj(1), SIOVOCB::InvalidOrdinalIndexError);
480  ECHO(oc.removeObj(0));
482  TEST_THROW(oc.removeObj(0), SIOVOCB::InvalidOrdinalIndexError);
483 }
484 
485 
487 {
489  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
491  ECHO(oc.setObj("a", 4));
493  TEST_THROW(oc.getNonconstObjPtr("does_not_exist"), SIOVOCB::InvalidKeyError);
494  ECHO(oc.removeObj("a"));
495  TEST_THROW(oc.getNonconstObjPtr("a"), SIOVOCB::InvalidKeyError);
496 }
497 
498 
500 {
502  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
504  ECHO(oc.setObj("a", 4));
505  TEST_EQUALITY_CONST(*oc.getObjPtr("a"), 4);
506  TEST_THROW(oc.getObjPtr("does_not_exist"), SIOVOCB::InvalidKeyError);
507  ECHO(oc.removeObj("a"));
508  TEST_THROW(oc.getObjPtr("a"), SIOVOCB::InvalidKeyError);
509 }
510 
511 
513 {
515  //typedef StringIndexedOrderedValueObjectContainer<int>::Iterator Iterator; // unused
517  ECHO(oc.setObj("a", 4));
518  TEST_EQUALITY_CONST(*oc.getObjPtr("a"), 4);
519  TEST_THROW(oc.removeObj("does_not_exist"), SIOVOCB::InvalidKeyError);
521  ECHO(oc.removeObj("a"));
523  TEST_THROW(oc.removeObj("a"), SIOVOCB::InvalidKeyError);
524 }
525 
526 // ToDo: Test dangling object references!
527 
528 
529 } // 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)...
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.
Ordinal setObj(const std::string &key, const ObjType &obj)
Set (or reset) object by value and return its ordinal index.
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.