File: Options_test.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (289 lines) | stat: -rw-r--r-- 8,545 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/CONCEPT/classTest.h>
#include <BALLTestConfig.h>

///////////////////////////

#include <BALL/DATATYPE/options.h>
#include <BALL/CONCEPT/textPersistenceManager.h>

START_TEST(Options)

using BALL::Options;
using BALL::Vector3;
using BALL::TextPersistenceManager;
using std::ofstream;
using std::ifstream;

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
	
using namespace BALL;

Options*	options;
CHECK(Options() throw())
	options = new Options;
	TEST_NOT_EQUAL(options, 0)
RESULT

CHECK(~Options() throw())
	delete options;
RESULT

CHECK(void setName(const String& name) throw())
	options = new Options;
	options->setName("ABCDEFG");
RESULT

CHECK(const String& getName() const throw())
	TEST_EQUAL(options->getName(), "ABCDEFG");
RESULT

Options*	options2;
CHECK(Options(const Options& options) throw())
	options2 = new Options(*options);
	TEST_EQUAL(options2->getName(), options->getName())
	TEST_EQUAL(options2->get("ABC"), options->get("ABC"))
RESULT

CHECK(void setBool(const String& key, const bool value) throw())
	options->setBool("BOOL", true);
	TEST_EQUAL(options->getBool("BOOL"), true)
	options->setBool("BOOL", false);
RESULT

CHECK(bool getBool(const String& key) const throw())
	TEST_EQUAL(options->getBool("BOOL"), false)
	TEST_EQUAL(options->getBool("UNDEFINED"), false)
RESULT

CHECK(void setReal(const String& key, const double value) throw())
	options->setReal("REAL", 1.23456);
RESULT

CHECK(double getReal(const String& key) const throw())
	TEST_REAL_EQUAL(1.23456, options->getReal("REAL"))
	TEST_REAL_EQUAL(options->getReal("UNDEFINED"), 0.0)
RESULT

Vector3	vector(1.0, 2.0, 3.0);
CHECK(void setVector(const String& key, const Vector3& value) throw())
	options->setVector("VECTOR", vector);
	TEST_EQUAL(vector, options->getVector("VECTOR"))
RESULT

CHECK(Vector3 getVector(const String& key) const throw())
	Vector3 v(0.0, 0.0, 0.0);
	TEST_EQUAL(options->getVector("UNDEFINED"), v)
RESULT

CHECK(void setInteger(const String& key, const long value) throw())
	options->setInteger("INT", 1234567890);
	TEST_EQUAL(1234567890, options->getInteger("INT"))
RESULT

CHECK(long getInteger(const String& key) const throw())
	TEST_EQUAL(options->getInteger("UNDEFINED"), 0)
RESULT

CHECK([EXTRA]has)
	TEST_EQUAL(options->has("BOOL"), true)
	TEST_EQUAL(options->has("UNDEFINED"), false)
RESULT

CHECK(bool isInteger(const String& key) const throw())
	TEST_EQUAL(options->isInteger("INT"), true)
	TEST_EQUAL(options->isInteger("REAL"), false)
	TEST_EQUAL(options->isInteger("BOOL"), false)
	TEST_EQUAL(options->isBool("VECTOR"), false)
	TEST_EQUAL(options->isInteger("undefined"), false)
RESULT

CHECK(bool isBool(const String& key) const throw())
	TEST_EQUAL(options->isBool("BOOL"), true)
	TEST_EQUAL(options->isBool("UNDEFINED"), false)
	TEST_EQUAL(options->isBool("INT"), false)
	TEST_EQUAL(options->isBool("REAL"), false)
	TEST_EQUAL(options->isBool("VECTOR"), false)
RESULT


CHECK(bool isReal(const String& key) const throw())
	TEST_EQUAL(options->isReal("REAL"), true)
	TEST_EQUAL(options->isReal("INT"), true)
	TEST_EQUAL(options->isReal("BOOL"), false)
	TEST_EQUAL(options->isReal("UNDEFINED"), false)
	TEST_EQUAL(options->isBool("VECTOR"), false)
RESULT


CHECK(bool isVector(const String& key) const throw())
	TEST_EQUAL(options->isVector("INT"), false)
	TEST_EQUAL(options->isVector("REAL"), false)
	TEST_EQUAL(options->isVector("BOOL"), false)
	TEST_EQUAL(options->isVector("UNDEFINED"), false)
	TEST_EQUAL(options->isVector("VECTOR"), true)
	options->set("SVECTOR", "(0.0 1.0 2.0) ");
	TEST_EQUAL(options->isVector("SVECTOR"), true)
RESULT

CHECK(bool isSet(const String& key) const throw())
	TEST_EQUAL(options->isSet("INT"), true)
	TEST_EQUAL(options->isSet("undefined"), false)
RESULT

CHECK(void set(const String& key, const String& value) throw())
	options->set("ABC", "DEF");	
	TEST_EQUAL("DEF", options->get("ABC"))
	TEST_EQUAL(options->get("UNDEFINED"), "")
RESULT

CHECK(String setDefault(const String& key, const String& value) throw())
	options->setDefault("DEF", "default");
	options->setDefault("DEF", "default2");
	TEST_EQUAL(options->get("DEF"), "default")
RESULT

CHECK(long setDefaultInteger(const String& key, const long value) throw())
	options->setDefaultInteger("DEFINT", 123456);
	options->setDefaultInteger("DEFINT", 234567);
	TEST_EQUAL(options->getInteger("DEFINT"), 123456)
RESULT

CHECK(double setDefaultReal(const String& key, const double value) throw())
	options->setDefaultReal("DEFREAL", 1.23456);
	options->setDefaultReal("DEFREAL", 2.34567);
	TEST_REAL_EQUAL(options->getReal("DEFREAL"), 1.23456)
RESULT

CHECK(bool setDefaultBool(const String& key, const bool value) throw())
	options->setDefaultBool("DEFBOOL", true);
	options->setDefaultBool("DEFBOOL", false);
	TEST_EQUAL(options->getBool("DEFBOOL"), true)
RESULT

CHECK(bool readOptionFile(const String& filename) throw())
	Options o;
	TEST_EQUAL(o.readOptionFile(BALL_TEST_DATA_PATH(OptionsFile1.txt)), true)
	TEST_EQUAL(o.getBool("BOOL"), true)
	TEST_EQUAL(o.getBool("BOOL2"), false)
	TEST_REAL_EQUAL(o.getReal("REAL"), 1.23456)
	TEST_EQUAL(o.getVector("VECTOR"), vector)
	TEST_EQUAL(o.getInteger("INT"), 1234567890)

	Options o2;
	TEST_EQUAL(o2.readOptionFile(BALL_TEST_DATA_PATH(OptionsFile2.txt)), true)
	TEST_EQUAL(o2.get("ABC"), "DEF")
	TEST_EQUAL(o2.getBool("BOOL"), false)
	TEST_EQUAL(o2.get("DEF"), "default")
	TEST_EQUAL(o2.getBool("DEFBOOL"), true)
	TEST_EQUAL(o2.getInteger("DEFINT"), 123456)
	TEST_REAL_EQUAL(o2.getReal("DEFREAL"), 1.234560)
	TEST_EQUAL(o2.getInteger("INT"), 1234567890)
	TEST_REAL_EQUAL(o2.getReal("REAL"), 1.234560)
	Vector3 v(0.0, 1.0, 2.0);
	TEST_EQUAL(o2.getVector("SVECTOR"), v)
	TEST_EQUAL(o2.getVector("VECTOR"), vector)

	//Test an options file with arbitrary deleimiters
	Options o3;
	TEST_EQUAL(o3.readOptionFile(BALL_TEST_DATA_PATH(OptionsFile4.txt)), true)
	TEST_EQUAL(o3.getBool("BOOL"), true)
	TEST_EQUAL(o3.getBool("BOOL2"), false)
	TEST_REAL_EQUAL(o3.getReal("REAL"), 1.23456)
	TEST_EQUAL(o3.getVector("VECTOR"), vector)
	TEST_EQUAL(o3.getInteger("INT"), 1234567890)
RESULT

String filename;
CHECK(bool writeOptionFile(const String& filename) const throw())
	NEW_TMP_FILE(filename)
	options->writeOptionFile(filename);
	TEST_FILE_REGEXP(filename.c_str(), BALL_TEST_DATA_PATH(OptionsFile2.txt))
RESULT


TextPersistenceManager pm;
Options o2;
CHECK(bool read(PersistenceManager& pm) throw())
	ifstream ifile(BALL_TEST_DATA_PATH(OptionsFile3.txt));
	pm.setIstream(ifile);

	bool success = o2.read(pm);
	ifile.close();
	TEST_EQUAL(success, true)
	TEST_EQUAL(o2.get("ABC"), "DEF")
	TEST_EQUAL(o2.getBool("BOOL"), false)
	TEST_EQUAL(o2.get("DEF"), "default")
	TEST_EQUAL(o2.getBool("DEFBOOL"), true)
	TEST_EQUAL(o2.getInteger("DEFINT"), 123456)
	TEST_REAL_EQUAL(o2.getReal("DEFREAL"), 1.234560)
	TEST_EQUAL(o2.getInteger("INT"), 1234567890)
	TEST_REAL_EQUAL(o2.getReal("REAL"), 1.234560)
	Vector3 v(0.0, 1.0, 2.0);
	TEST_EQUAL(o2.getVector("SVECTOR"), v)
	TEST_EQUAL(o2.getVector("VECTOR"), vector)
RESULT

String tmpname;
CHECK(void write(PersistenceManager& pm) const throw())
	NEW_TMP_FILE(tmpname)
	ofstream  ofile(tmpname.c_str(), std::ios::out);
	pm.setOstream(ofile);
	o2.write(pm);
	ofile.close();	
	TEST_FILE_REGEXP(tmpname.c_str(), BALL_TEST_DATA_PATH(OptionsFile3.txt))
RESULT

CHECK(void dump(std::ostream& s = std::cout, Size depth = 0) const throw())
	using std::ofstream;
	using std::ios;
	NEW_TMP_FILE(filename)
	std::ofstream outfile(filename.c_str(), std::ios::out);
	options->dump(outfile);
	outfile.close();
	TEST_FILE_REGEXP(filename.c_str(), BALL_TEST_DATA_PATH(Options_test.txt))
RESULT
delete options;
delete options2;


Options o;
o.setInteger("INT", 1234);
CHECK(String get(const String& key) const throw())
	TEST_EQUAL("1234", o.get("INT"))
	TEST_EQUAL("", o.get(""))
RESULT

CHECK(bool operator != (const Options& option) const throw())
	Options o2;
	TEST_EQUAL(o2 != o, true)
	o2 = o;
	TEST_EQUAL(o2 != o, false)
RESULT

CHECK(bool operator == (const Options& option) const throw())
	Options o2;
	TEST_EQUAL(o2 == o, false)
	o2 = o;
	TEST_EQUAL(o2 == o, true)
RESULT

CHECK(const Options& operator = (const Options& options) throw())
	Options o2(o);
	TEST_EQUAL(o2 == o, true)
RESULT

CHECK(void clear() throw())
	o.clear();
	Options o2;
	TEST_EQUAL(o2 == o, true)
RESULT


/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
END_TEST