torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfigMapUnitTest.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Unit test for the ConfigMap class.
18 
19 #include <boost/test/unit_test.hpp>
22 
23 namespace torc {
24 namespace physical {
25 
26 BOOST_AUTO_TEST_SUITE(physical)
27 
28 /// \brief Unit test for the ConfigMap class.
29 BOOST_AUTO_TEST_CASE(ConfigMapUnitTest) {
30  // create some configuration entries
31  Config config1("name1", "value1");
32  Config config2("name2", "value2");
33  Config config3("name3", "value3");
34  Config config4("name4", "value4");
35 
36  // functions tested:
37  // ConfigMap(void);
38  // void setConfig(const string& inSetting, const Config& inConfig);
39  // static bool allowConfigDuplicates(const string& inSetting);
40  // create a configuration map and add the configuration entries to it
41  ConfigMap configMap1;
42  configMap1.setConfig("config", config1);
43  configMap1.setConfig("config", config2);
44  configMap1.setConfig("_config", config3);
45  configMap1.setConfig("_config", config4);
46  ConfigMap configMap2;
47 
48  // functions tested:
49  // bool getConfig(const string& inSetting, string& outName, string& outValue);
50  // void setConfig(const string& inSetting, const string& inName, const string& inValue);
51  std::string name;
52  std::string value;
53  configMap1.getConfig("config", name, value);
54  configMap2.setConfig("ending", name, value);
55 
56  // functions tested:
57  // bool hasConfig(const string& inSetting) const;
58  // size_type getMultiConfigCount(const string& inSetting);
59  // size_t getConfigCount(void) const;
60  // bool configIsEmpty(void) const;
61  // verify that the configuration entries exist in the proper quantities
62  // (settings prefixed with an underscore can exist multiple times, while other settings cannot)
63  BOOST_CHECK(configMap1.hasConfig("config"));
64  BOOST_CHECK(configMap1.hasConfig("_config"));
65  BOOST_CHECK_EQUAL((size_t) 1, configMap1.getMultiConfigCount("config"));
66  BOOST_CHECK_EQUAL((size_t) 2, configMap1.getMultiConfigCount("_config"));
67  BOOST_CHECK(configMap1.getConfigCount() == 3);
68  BOOST_CHECK(configMap1.configIsEmpty() == false);
69 
70  // functions tested:
71  // bool getConfig(const string& inSetting, Config& outConfig);
72  // const_iterator configBegin(void) const;
73  // const_iterator configEnd(void) const;
74  // read out and check expected settings
75  Config config5;
76  configMap1.getConfig("config", config5);
77  BOOST_CHECK_EQUAL(config2, config5);
78  // verify the expected iterator behavior
79  ConfigMap::const_iterator p = configMap1.configBegin();
80  ConfigMap::const_iterator e = configMap1.configEnd();
81  BOOST_CHECK_EQUAL(config3, p->second); p++;
82  BOOST_CHECK_EQUAL(config4, p->second); p++;
83  BOOST_CHECK_EQUAL(config2, p->second); p++;
84 
85  // functions tested:
86  // std::pair<iterator, iterator> getMultiConfigValues(const string& inSetting);
87  std::pair<ConfigMap::const_iterator, ConfigMap::const_iterator> range
88  = configMap1.getMultiConfigValues("_config");
89  p = range.first;
90  e = range.second;
91  BOOST_CHECK_EQUAL(config3, p->second); p++;
92  BOOST_CHECK_EQUAL(config4, p->second); p++;
93  BOOST_CHECK(p == e);
94  // p = configMap1.configBegin();
95  // e = configMap1.configEnd();
96  // while(p != e) {
97  // std::cout << p->first << ":" << p->second.getName() << ":" << p->second.getValue() << std::endl;
98  // p++;
99  // }
100 
101  // functions tested:
102  // void clearConfig(void);
103  // void addConfigs(const ConfigMap& inConfigMap);
104  // size_t getConfigCount(void) const;
105  // bool configIsEmpty(void) const;
106  configMap2.addConfigs(configMap1);
107  configMap1.clearConfig();
108  BOOST_CHECK(configMap1.getConfigCount() == 0);
109  BOOST_CHECK(configMap1.configIsEmpty() == true);
110  BOOST_CHECK(configMap2.getConfigCount() == 4);
111  BOOST_CHECK(configMap2.configIsEmpty() == false);
112 }
113 
114 BOOST_AUTO_TEST_SUITE_END()
115 
116 } // namespace physical
117 } // namespace torc
void clearConfig(void)
Clears the configuration map.
Definition: ConfigMap.hpp:67
void setConfig(const string &inSetting, const Config &inConfig)
Sets the configuration for the given setting.
Definition: ConfigMap.hpp:119
Header for the ConfigMap class.
bool getConfig(const string &inSetting, Config &outConfig)
Looks up the specified setting in the map.
Definition: ConfigMap.hpp:82
size_t getConfigCount(void) const
Returns the number of configurations in the map.
Definition: ConfigMap.hpp:63
void addConfigs(const ConfigMap &inConfigMap)
Merges the configurations from the given ConfigMap into this one.
Definition: ConfigMap.hpp:159
std::string string
BOOST_AUTO_TEST_CASE(XdlUnpackUnitTest)
Unit test for the XdlUnpack class.
const_iterator const_iterator
Constant iterator to {setting,Config} pairs.
Definition: ConfigMap.hpp:52
const_iterator configBegin(void) const
Returns the begin constant iterator for configurations.
Definition: ConfigMap.hpp:58
size_type getMultiConfigCount(const string &inSetting)
Returns the number of configurations for the given setting.
Definition: ConfigMap.hpp:186
Configuration. A {name:value} pair.
Definition: Config.hpp:39
bool hasConfig(const string &inSetting) const
Returns true if the specified setting exists in the map.
Definition: ConfigMap.hpp:69
Header for torc::physical output stream helpers.
Configuration setting map.
Definition: ConfigMap.hpp:39
bool configIsEmpty(void) const
Returns true if the configuration map is empty.
Definition: ConfigMap.hpp:65
std::pair< iterator, iterator > getMultiConfigValues(const string &inSetting)
Returns a range that encompasses all of the configurations for the given setting. ...
Definition: ConfigMap.hpp:183
const_iterator configEnd(void) const
Returns the end constant iterator for configurations.
Definition: ConfigMap.hpp:60