COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-cmap.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for Map component"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include "common/CF.hpp"
13 #include "common/Map.hpp"
14 #include "common/Log.hpp"
15 #include "common/Foreach.hpp"
16 
18 
19 using namespace cf3;
20 using namespace cf3::common;
21 
22 struct MapFixture
23 {
26  {
27  }
28 
31  {
32  }
33 
34 private:
35 
36 };
37 
39 
40 BOOST_FIXTURE_TEST_SUITE( MapTests, MapFixture )
41 
42 
45 {
46 
47  boost::shared_ptr< Map<std::string,Uint> > map_ptr ( allocate_component< Map<std::string,Uint> > ("map"));
48  Map<std::string,Uint>& map = *map_ptr;
49 
50 
51  BOOST_CHECK_EQUAL(map.type_name() , "Map<string,unsigned>");
52 
53  BOOST_CHECK(map.find("first") == map.end());
54 
55  map.push_back(std::string("first"), (Uint) 1);
56  BOOST_CHECK_EQUAL(map.size() , 1u);
57  BOOST_CHECK_EQUAL(map["first"] , 1u);
58  map.push_back(std::string("second"), (Uint) 2);
59  BOOST_CHECK_EQUAL(map.size() , 2u);
60  BOOST_CHECK_EQUAL(map["second"] , 2u);
61  map["third"] = 3;
62  BOOST_CHECK_EQUAL(map.size() , 3u);
63  BOOST_CHECK_EQUAL(map["third"] , 3u);
64 
65  BOOST_CHECK(map.find("fourth") == map.end());
66  map["fourth"] = 4;
67  BOOST_CHECK_EQUAL(map.find("fourth")->second,4u);
68  BOOST_CHECK_EQUAL(map.size() , 4u);
69 
70 
71 }
72 
73 BOOST_AUTO_TEST_CASE ( test_Map_looping )
74 {
75 
76  boost::shared_ptr< Map<std::string,Uint> > map_ptr ( allocate_component< Map<std::string,Uint> > ("map"));
77  Map<std::string,Uint>& map = *map_ptr;
78 
79 
80  map.push_back(std::string("first"), (Uint) 1);
81  map.push_back(std::string("second"), (Uint) 2);
82  map.push_back(std::string("third"), (Uint) 3);
83  map.push_back(std::string("fourth"), (Uint) 4);
84 
85  map.sort_keys();
86 
87  foreach_container((const std::string& key)(Uint data),map)
88  BOOST_CHECK_EQUAL( map[key] , data );
89 
90  std::pair<Map<std::string,Uint>::iterator,bool> ret;
91  ret = map.insert(std::make_pair("fifth",5u));
92  BOOST_CHECK_EQUAL(ret.second, true); // --> new element
93  BOOST_CHECK_EQUAL(ret.first->first, "fifth");
94  BOOST_CHECK_EQUAL(ret.first->second, 5u);
95 
96  // try to insert one that is already there
97  ret = map.insert(std::make_pair("fifth",1000));
98  BOOST_CHECK_EQUAL(ret.second, false); // --> already exists
99  BOOST_CHECK_EQUAL(ret.first->first, "fifth");
100  BOOST_CHECK_EQUAL(ret.first->second, 5u); // --> value did not change
101 
102  BOOST_CHECK(map.erase("first"));
103  BOOST_CHECK(map.find("first") == map.end());
104 
105  map.erase( map.find("fourth") );
106  BOOST_CHECK(map.find("fourth") == map.end());
107 
108  const Map<std::string,Uint>& const_map = *map_ptr;
109  BOOST_CHECK(const_map.find("second") != const_map.end());
110 
111 }
112 
113 BOOST_AUTO_TEST_CASE ( test_Map_exceptions )
114 {
115 
116  boost::shared_ptr< Map<int, int> > map_ptr ( allocate_component< Map<int,int> > ("map"));
117  Map<int,int>& map = *map_ptr;
118 
119  BOOST_CHECK_EQUAL(map.type_name() , "Map<integer,integer>");
120 
121  map.push_back(1,1);
122  map.push_back(2,2);
123  map.push_back(3,3);
124  map.push_back(4,4);
125 
126  // const Map<int,int>& const_map = *map_ptr;
127  // BOOST_CHECK_THROW(const_map.find(2),IllegalCall);
128  // BOOST_CHECK_THROW(const_map[2],IllegalCall);
129 
130  map.sort_keys();
131  // BOOST_CHECK_THROW(const_map[6],ValueNotFound);
132 
133  map.push_back(2,3); // adding another entry with key 2 should not be allowed
134  // BOOST_CHECK_THROW(map.sort_keys(),ValueExists);
135 
136 }
137 
138 BOOST_AUTO_TEST_CASE ( test_Map_copy_std_map )
139 {
140 
141  boost::shared_ptr< Map<std::string,int> > map_ptr ( allocate_component< Map<std::string,int> > ("map"));
142  Map<std::string,int>& map = *map_ptr;
143 
144  BOOST_CHECK_EQUAL(map.type_name() , "Map<string,integer>");
145 
146 
147  std::map<std::string,int> stl_map;
148  stl_map["first"] = 1;
149  stl_map["second"] = 2;
150  stl_map["third"] = 3;
151  stl_map["fourth"] = 4;
152 
153  map.copy_std_map(stl_map);
154  BOOST_CHECK_EQUAL(map.size(), 4u);
155  BOOST_CHECK_EQUAL(map["third"], 3);
156 }
157 
159 
160 
161 BOOST_AUTO_TEST_SUITE_END()
162 
163 
static std::string type_name()
Get the class name.
Definition: Map.hpp:72
iterator end()
Definition: Map.hpp:469
void erase(iterator itr)
Erase the given iterator from the map.
Definition: Map.hpp:130
BOOST_AUTO_TEST_CASE(test_Map)
Definition: utest-cmap.cpp:44
#define foreach_container(VARS, COL)
boost_foreach version that allows to use std::map's, boost::tuple's, std::pair's internal variables ...
Definition: Foreach.hpp:36
MapFixture()
common setup for each test case
Definition: utest-cmap.cpp:25
void copy_std_map(std::map< key_type, data_type > &map)
Copy a std::map into the Map.
Definition: Map.hpp:293
~MapFixture()
common tear-down for each test case
Definition: utest-cmap.cpp:30
Top-level namespace for coolfluid.
Definition: Action.cpp:18
iterator find(const key_type &key)
Find the iterator matching with the given KEY.
Definition: Map.hpp:335
void sort_keys()
Sort all the pairs in the map by key.
Definition: Map.hpp:438
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
std::pair< iterator, bool > insert(const value_type &v)
Insert pair the same way a std::map would.
Definition: Map.hpp:317
coolfluid3 header, included almost everywhere
void map(Field &field, Eigen::Map< Derived > &v, const Uint row_idx, const Uint var_idx)
Uint push_back(const key_type &key, const data_type &data)
Insert pair without any checks if it is already present.
Definition: Map.hpp:307
Most basic kernel library.
Definition: Action.cpp:19
boost::shared_ptr< T > allocate_component(const std::string &name)
Stand-alone function to allocate components of a given type.
Definition: Component.hpp:55
size_t size() const
Get the number of pairs already inserted.
Definition: Map.hpp:406
Send comments to:
COOLFluiD Web Admin