rambrain
testConfigReader.cpp
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <gtest/gtest.h>
21 #include <fstream>
22 #include <ostream>
23 #include "configreader.h"
24 
25 using namespace std;
26 using namespace rambrain;
27 
31 TEST ( configuration, Unit_DefaultValues )
32 {
34 
35  ASSERT_FALSE ( config.memoryManager.value.empty() );
36  ASSERT_GT ( config.memory.value, 0.0 );
37  ASSERT_FALSE ( config.swap.value.empty() );
38  ASSERT_FALSE ( config.swapfiles.value.empty() );
39  ASSERT_GT ( config.swapMemory.value, 0.0 );
40  ASSERT_FALSE ( config.enableDMA.value );
41  ASSERT_EQ ( swapPolicy::autoextendable, config.policy.value );
42  ASSERT_EQ ( 7u, config.configOptions.size() );
43 }
44 
48 TEST ( configReader, Unit_ParseCustomFile )
49 {
50  //Create custom config file
51  ofstream out ( "testconfig.conf" );
52  out << "[default]" << std::endl;
53  out << "memoryManager = dummyManagedMemory" << std::endl;
54  out << "memory = 5 GB" << std::endl;
55  out.close();
56 
57  // Read in the file
58  configReader reader;
59  reader.setCustomConfigPath ( "testconfig.conf" );
60 
61  ASSERT_FALSE ( reader.readSuccessfully() );
62  ASSERT_TRUE ( reader.readConfig() );
63  ASSERT_TRUE ( reader.readSuccessfully() );
64 
65  configuration config = reader.getConfig();
66 
67  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
68  ASSERT_EQ ( 5 * gig, config.memory.value );
69 }
70 
74 TEST ( configReader, Unit_IgnoreCommentLines )
75 {
76  //Create custom config file
77  ofstream out ( "testconfig.conf" );
78  out << "[default]" << std::endl;
79  out << "#memoryManager = somethingstupid" << std::endl;
80  out << "memoryManager = dummyManagedMemory#evenmorestupid" << std::endl;
81  out << "#memoryManager = somethingstupid" << std::endl;
82  out << " # memoryManager = somethingmorestupid" << std::endl;
83  out.close();
84 
85  // Read in the file
86  configReader reader;
87  reader.setCustomConfigPath ( "testconfig.conf" );
88 
89  ASSERT_TRUE ( reader.readConfig() );
90 
91  configuration config = reader.getConfig();
92 
93  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
94 }
95 
99 TEST ( configReader, Unit_IgnoreMissingVariables )
100 {
101  //Create custom config file
102  ofstream out ( "testconfig.conf" );
103  out << "[default]" << std::endl;
104  out.close();
105 
106  // Read in the file
107  configReader reader;
108  reader.setCustomConfigPath ( "testconfig.conf" );
109 
110  ASSERT_TRUE ( reader.readConfig() );
111 
112  configuration config = reader.getConfig();
113 
114  ASSERT_FALSE ( config.memoryManager.value.empty() );
115 }
116 
120 TEST ( configReader, Unit_DefaultOverwritesMissingVariables )
121 {
122  //Create custom config file
123  ofstream out ( "testconfig.conf" );
124  out << "[default]" << std::endl;
125  out << "memoryManager = cyclicManagedMemory" << std::endl;
126  out << "memory = 5GB" << std::endl;
127  out << "[rambrain-unittests]" << std::endl;
128  out << "memoryManager = dummyManagedMemory" << std::endl;
129  out << "[rambrain-tests]" << std::endl;
130  out << "memoryManager = dummyManagedMemory" << std::endl;
131  out.close();
132 
133  // Read in the file
134  configReader reader;
135  reader.setCustomConfigPath ( "testconfig.conf" );
136 
137  ASSERT_TRUE ( reader.readConfig() );
138 
139  configuration config = reader.getConfig();
140 
141  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
142  ASSERT_EQ ( 5 * gig, config.memory.value );
143 }
144 
148 TEST ( configReader, Unit_DefaultOverwritesMissingVariablesLookFurther )
149 {
150  //Create custom config file
151  ofstream out ( "testconfig.conf" );
152  out << "[rambrain-unittests]" << std::endl;
153  out << "memoryManager = dummyManagedMemory" << std::endl;
154  out << "[rambrain-tests]" << std::endl;
155  out << "memoryManager = dummyManagedMemory" << std::endl;
156  out << "[default]" << std::endl;
157  out << "memoryManager = cyclicManagedMemory" << std::endl;
158  out << "memory = 5GB" << std::endl;
159  out.close();
160 
161  // Read in the file
162  configReader reader;
163  reader.setCustomConfigPath ( "testconfig.conf" );
164 
165  ASSERT_TRUE ( reader.readConfig() );
166 
167  configuration config = reader.getConfig();
168 
169  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
170  ASSERT_EQ ( 5 * gig, config.memory.value );
171 }
172 
176 TEST ( configReader, Unit_ParseProgramName )
177 {
178  configReader reader;
179 
180  ASSERT_TRUE ( reader.getApplicationName() == "rambrain-unittests" || reader.getApplicationName() == "rambrain-tests" );
181 }
182 
186 TEST ( configReader, Unit_OverwriteDefault )
187 {
188  //Create custom config file
189  ofstream out ( "testconfig.conf" );
190  out << "[default]" << std::endl;
191  out << "memoryManager = cyclicManagedMemory" << std::endl;
192  out << "[rambrain-unittests]" << std::endl;
193  out << "memoryManager = dummyManagedMemory" << std::endl;
194  out << "[rambrain-tests]" << std::endl;
195  out << "memoryManager = dummyManagedMemory" << std::endl;
196  out.close();
197 
198  // Read in the file
199  configReader reader;
200  reader.setCustomConfigPath ( "testconfig.conf" );
201 
202  ASSERT_TRUE ( reader.readConfig() );
203 
204  configuration config = reader.getConfig();
205 
206  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
207 }
208 
212 TEST ( configReader, Unit_OverwriteDefaultInverseOrder )
213 {
214  //Create custom config file
215  ofstream out ( "testconfig.conf" );
216  out << "[rambrain-unittests]" << std::endl;
217  out << "memoryManager = dummyManagedMemory" << std::endl;
218  out << "[rambrain-tests]" << std::endl;
219  out << "memoryManager = dummyManagedMemory" << std::endl;
220  out << "[default]" << std::endl;
221  out << "memoryManager = cyclicManagedMemory" << std::endl;
222  out.close();
223 
224  // Read in the file
225  configReader reader;
226  reader.setCustomConfigPath ( "testconfig.conf" );
227 
228  ASSERT_TRUE ( reader.readConfig() );
229 
230  configuration config = reader.getConfig();
231 
232  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
233 }
234 
238 TEST ( configReader, Unit_IgnoreEmptyLines )
239 {
240  //Create custom config file
241  ofstream out ( "testconfig.conf" );
242  out << std::endl;
243  out << "[default]" << std::endl << std::endl;
244  out << "memoryManager = cyclicManagedMemory" << std::endl << std::endl << std::endl;
245  out << "[rambrain-unittests]" << std::endl;
246  out << "memoryManager = dummyManagedMemory" << std::endl << std::endl;
247  out << "[rambrain-tests]" << std::endl << std::endl << std::endl;;
248  out << "memoryManager = dummyManagedMemory" << std::endl;
249  out.close();
250 
251  // Read in the file
252  configReader reader;
253  reader.setCustomConfigPath ( "testconfig.conf" );
254 
255  ASSERT_TRUE ( reader.readConfig() );
256 
257  configuration config = reader.getConfig();
258 
259  ASSERT_EQ ( "dummyManagedMemory", config.memoryManager.value );
260 }
261 
configLine< swapPolicy > policy
Definition: configreader.h:141
configLine< bool > enableDMA
Definition: configreader.h:140
configLine< string > swapfiles
Definition: configreader.h:138
configuration & getConfig()
Simple getter.
Definition: configreader.h:192
Main struct to save configuration variables.
Definition: configreader.h:131
const global_bytesize gig
Definition: common.h:69
rambrainConfig config
You will find the object in managedMemory.cpp as we have to define it in some 'used' file in the link...
configLine< global_bytesize > memory
Definition: configreader.h:139
configLine< string > swap
Definition: configreader.h:138
bool readConfig()
Read in and parse config.
string getApplicationName() const
Extract the current binary's name out of the /proc file system.
STL namespace.
void setCustomConfigPath(const string &path)
Simple setter.
Definition: configreader.h:185
TEST(configuration, Unit_DefaultValues)
bool readSuccessfully() const
Simple getter.
Definition: configreader.h:202
Reader class to read in and properly parse config files.
Definition: configreader.h:163
configLine< string > memoryManager
Definition: configreader.h:138
configLine< global_bytesize > swapMemory
Definition: configreader.h:139
vector< configLineBase * > configOptions
Definition: configreader.h:143