libMesh
vectormap_test.C
Go to the documentation of this file.
1 #include "libmesh/vectormap.h"
2 
3 // Ignore unused parameter warnings coming from cppunit headers
4 #include <libmesh/ignore_warnings.h>
5 #include <cppunit/extensions/HelperMacros.h>
6 #include <cppunit/TestCase.h>
7 #include <libmesh/restore_warnings.h>
8 
9 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
10 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
11 // warnings. These can be ignored in GCC as long as we wrap the
12 // offending code in appropriate pragmas. We can't get away with a
13 // single ignore_warnings.h inclusion at the beginning of this file,
14 // since the libmesh headers pull in a restore_warnings.h at some
15 // point. We also don't bother restoring warnings at the end of this
16 // file since it's not a header.
17 #include <libmesh/ignore_warnings.h>
18 
19 #define VECTORMAPOBJECTTEST \
20  CPPUNIT_TEST( testCreate ); \
21 
22 using namespace libMesh;
23 
24 class VectormapTest : public CppUnit::TestCase
25 {
26 public:
27  CPPUNIT_TEST_SUITE ( VectormapTest );
28 
29  CPPUNIT_TEST( testCreate );
30  CPPUNIT_TEST( testInsert );
31  CPPUNIT_TEST( testIterate );
32  CPPUNIT_TEST( testFind );
33 
34  CPPUNIT_TEST_SUITE_END();
35 
36 private:
37 
38  template <typename Key, typename Val>
39  void create()
40  {
42  }
43 
44  template <typename Key, typename Val>
45  void insert()
46  {
48 
49  Val val(0); // requires default constructor for val type.
50 
51  for (Key key=1; key<32; key*=2)
52  vm.insert (std::make_pair(key,val));
53 
54  vm.sort();
55  }
56 
57  template <typename Key, typename Val>
58  void iterate(const Val &default_value=0)
59  {
61 
62  Val val(default_value); // requires default constructor for val type.
63 
64  for (Key key=1; key<32; key*=2)
65  vm.insert (std::make_pair(key,val));
66 
67  vm.sort();
68 
69  for (typename vectormap<Key,Val>::const_iterator it=vm.begin();
70  it != vm.end(); ++it)
71  {
72  const Key &ikey = it->first;
73  const Val &ival = it->second;
74 
75  CPPUNIT_ASSERT ( vm.count(ikey) == 1 );
76  CPPUNIT_ASSERT_EQUAL (vm[ikey], ival);
77  CPPUNIT_ASSERT_EQUAL (ival, val);
78  }
79  }
80 
81 public:
82 
83  // virtual void setUp()
84  // {}
85 
86  // virtual void tearDown()
87  // {}
88 
89 
90  void testCreate()
91  {
92  create<int, int> ();
93  create<int*,int> ();
94  create<int*,int*>();
95  create<int, std::vector<int>>();
96  }
97 
98  void testInsert()
99  {
100  insert<int, int> ();
101  insert<char,int> ();
102  insert<long,int*>();
103  insert<int, std::vector<int>>();
104  }
105 
106  void testIterate()
107  {
108  iterate<int, int> ();
109  iterate<char,int> ();
110  iterate<long,int*>();
111  iterate<int, std::string>("test_string");
112  }
113 
114  void testFind()
115  {
117  for (int i=16; i<32; ++i)
118  vm.insert(std::make_pair(i,i));
119 
121  it1 = vm.find(24),
122  it2 = vm.find(4);
123 
124  CPPUNIT_ASSERT(it1 != vm.end());
125  CPPUNIT_ASSERT(it2 == vm.end());
126  CPPUNIT_ASSERT(vm.count(24) == 1);
127  CPPUNIT_ASSERT(vm.count(4) == 0);
128  }
129 };
130 
iterator find(const key_type &key)
Definition: vectormap.h:160
difference_type count(const key_type &key) const
Definition: vectormap.h:210
vector_type::const_iterator const_iterator
Definition: vectormap.h:72
The libMesh namespace provides an interface to certain functionality in the library.
void sort()
Sort & unique the vectormap, preparing for use.
Definition: vectormap.h:125
void insert(const value_type &x)
Inserts x into the vectormap.
Definition: vectormap.h:116
CPPUNIT_TEST_SUITE_REGISTRATION(VectormapTest)
vector_type::iterator iterator
Definition: vectormap.h:71
This vectormap templated class is intended to provide the performance characteristics of a sorted std...
Definition: vectormap.h:61
void iterate(const Val &default_value=0)