GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/FileParser/TESTS/TEST_DICO_VALUE/main.cpp Lines: 172 172 100.0 %
Date: 2024-09-10 03:06:26 Branches: 324 324 100.0 %

Line Branch Exec Source
1
2
/***************************************
3
	Auteur : Pierre Aubert
4
	Mail : pierre.aubert@lapp.in2p3.fr
5
	Licence : CeCILL-C
6
****************************************/
7
8
#include "phoenix_assert.h"
9
#include "phoenix_check.h"
10
#include "DicoValue.h"
11
12
///Save the given DicoValue in the given file
13
/**	@param dv : DicoValue to be saved
14
 * 	@param fileName : name of the file to be saved
15
 * 	@return true on success, false otherwise
16
*/
17
6
bool saveDico(const DicoValue & dv, const std::string & fileName){
18

6
	return dv.save(replaceStrInStr(replaceStrInStr(fileName, ".txt", ".dico"), CMAKE_CURRENT_TEST_DIR, CMAKE_CURRENT_BUILD_DIR));
19
}
20
21
///Check a const DicoValue
22
/**	@param dico : DicoValue
23
 * 	@return true on success, false otherwise
24
*/
25
1
bool checkConstDicoValue(const DicoValue & dico){
26
1
	bool b(true);
27
1
	b &= dico.getMap("NonExistingmap") == NULL;
28
1
	return b;
29
}
30
31
///Test the DicoValue
32
/**	@param fileName : name of the file to be used
33
 * 	@return true on success, false otherwise
34
*/
35
2
bool testDicoValue(const std::string & fileName){
36
4
	DicoValue dv;
37
2
	if(!dv.load(fileName)){
38

1
		std::cerr << "testDicoValue : cannot load file '"<<fileName<<"'" << std::endl;
39
1
		return false;
40
	}
41
1
	std::vector<DicoValue> & vecChild = dv.getVecChild();
42
43
1
	DicoValue & value = dv.getMapChild()["key"];
44
1
	bool b(true);
45
1
	b &= dv.getMap("NonExistingmap") == NULL;
46
1
	b &= checkConstDicoValue(dv);
47
1
	DicoValue dico;
48
1
	dico.setVecChild(dv.getVecChild());
49
1
	dico.setMapChild(dv.getMapChild());
50
51


1
	std::cout << "testDicoValue : value of key : '" << value.getValue() << "' with key '" << value.getKey() << "', vecChild.size = " << vecChild.size()  << std::endl;
52
1
	b &= value.getValue() == "value";
53
1
	b &= saveDico(dv, fileName);
54
1
	return b;
55
}
56
57
///Test the value of the given DicoValue
58
/**	@param dv : DicoValue to be checked
59
 * 	@param expectedValue : expected value
60
 * 	@return true on success, false otherwise
61
*/
62
3
bool testDicoValueKey(const DicoValue & dv, int expectedValue){
63
// 	std::cout << "testDicoValueKey : key '" << dv.getKey() << "' with value '" << dv.getValue() << "', hasKey = " << dv.hasKey() << "', hasMap = " << dv.hasMap()   << "', hasVec = " << dv.hasVec() << std::endl;
64
3
	return phoenix_check("testDicoValueKey", dv.getValue<int>(), expectedValue);
65
}
66
67
///Test the DicoValue
68
/**	@param fileName : name of the file to be used
69
 * 	@return true on success, false otherwise
70
*/
71
2
bool testDicoValue2(const std::string & fileName){
72
4
	DicoValue dv;
73
2
	if(!dv.load(fileName)){
74

1
		std::cerr << "testDicoValue2 : cannot load file '"<<fileName<<"'" << std::endl;
75
1
		return false;
76
	}
77

1
	bool b(testDicoValueKey(dv.getMapChild()["key1"], 1));
78

1
	b &= testDicoValueKey(dv.getMapChild()["key2"], 2);
79

1
	b &= testDicoValueKey(dv.getMapChild()["key3"], 3);
80
1
	b &= dv.isKeyExist("key3");
81
82
1
	b &= saveDico(dv, fileName);
83
1
	return b;
84
}
85
86
///Test the value of the given DicoValue
87
/**	@param dv : DicoValue to be checked
88
 * 	@param expectedValue : expected value
89
 * 	@return true on success, false otherwise
90
*/
91
4
bool testDicoValueKeyPtr(const DicoValue * dv, int expectedValue){
92
4
	if(dv == NULL){
93
1
		std::cerr << "testDicoValueKeyPtr : DicoValue for value "<<expectedValue<<" not found!!!" << std::endl;
94
1
		return false;
95
	}
96
// 	std::cout << "testDicoValueKeyPtr : key '" << dv->getKey() << "' with value '" << dv->getValue() << "'"  << std::endl;
97
3
	return dv->getValue<int>() == expectedValue;
98
}
99
100
///Test the DicoValue
101
/**	@param fileName : name of the file to be used
102
 * 	@return true on success, false otherwise
103
*/
104
2
bool testDicoValue3(const std::string & fileName){
105
4
	DicoValue dv;
106
2
	if(!dv.load(fileName)){
107

1
		std::cerr << "testDicoValue3 : cannot load file '"<<fileName<<"'" << std::endl;
108
1
		return false;
109
	}
110
1
	bool b(testDicoValueKeyPtr(dv.getMap("key1"), 1));
111
1
	b &= testDicoValueKeyPtr(dv.getMap("key2"), 2);
112
1
	b &= testDicoValueKeyPtr(dv.getMap("key3"), 3);
113
1
	b &= !testDicoValueKeyPtr(NULL, 0);
114
1
	b &= saveDico(dv, fileName);
115
1
	return b;
116
}
117
118
///Test the value of the given DicoValue
119
/**	@param dv : DicoValue to be checked
120
 * 	@param expectedValue : expected value
121
 * 	@return true on success, false otherwise
122
*/
123
8
bool testDicoVecValueKeyPtr(const DicoValue * dv, int expectedValue){
124
8
	if(dv == NULL){
125
1
		std::cerr << "testDicoVecValueKeyPtr : DicoValue for value "<<expectedValue<<" not found!!!" << std::endl;
126
1
		return false;
127
	}
128
7
	const VecDicoValue & vecVal = dv->getVecChild();
129
7
	if(vecVal.size() != 3lu){
130
1
		std::cerr << "testDicoVecValueKeyPtr : expect 3 value for first value " << expectedValue << std::endl;
131
1
		return false;
132
	}
133
6
	bool b(true);
134
24
	for(int i(0); i < 3; ++i){
135
18
		std::cout << "testDicoVecValueKeyPtr : value["<<i<<"] = " << vecVal[i].getValue<int>() << ", expect = " << (i + expectedValue) << std::endl;
136
18
		b &= vecVal[i].getValue<int>() == expectedValue + i;
137
	}
138
6
	return b;
139
}
140
141
///Test the DicoValue
142
/**	@param fileName : name of the file to be used
143
 * 	@return true on success, false otherwise
144
*/
145
2
bool testDicoVecValue(const std::string & fileName){
146
4
	DicoValue dv;
147
2
	if(!dv.load(fileName)){
148

1
		std::cerr << "testDicoVecValue : cannot load file '"<<fileName<<"'" << std::endl;
149
1
		return false;
150
	}
151
1
	bool b(testDicoVecValueKeyPtr(dv.getMap("key1"), 1));
152
1
	b &= testDicoVecValueKeyPtr(dv.getMap("key2"), 2);
153
1
	b &= testDicoVecValueKeyPtr(dv.getMap("key3"), 3);
154
1
	b &= saveDico(dv, fileName);
155
1
	return b;
156
}
157
158
///Test the DicoValue
159
/**	@param fileName : name of the file to be used
160
 * 	@return true on success, false otherwise
161
*/
162
2
bool testDicoVecString(const std::string & fileName){
163
4
	DicoValue dv;
164
2
	if(!dv.load(fileName)){
165

1
		std::cerr << "testDicoVecString : cannot load file '"<<fileName<<"'" << std::endl;
166
1
		return false;
167
	}
168
1
	bool b(testDicoVecValueKeyPtr(dv.getMap("key1"), 1));
169
1
	b &= testDicoVecValueKeyPtr(dv.getMap("key2"), 2);
170
1
	b &= testDicoVecValueKeyPtr(dv.getMap("key3"), 3);
171
172
1
	b &= !testDicoVecValueKeyPtr(NULL, 0);
173
1
	DicoValue val;
174
1
	b &= !testDicoVecValueKeyPtr(&val, 0);
175
176
1
	b &= saveDico(dv, fileName);
177
1
	return b;
178
}
179
180
///Test the value of the given DicoValue
181
/**	@param dv : DicoValue to be checked
182
 * 	@param expectedValue : expected value
183
 * 	@return true on success, false otherwise
184
*/
185
9
bool testDicoValueKey(const DicoValue * dv, int expectedValue){
186
9
	return phoenix_check("testDicoValueKey", dv->getValue<int>(), expectedValue);
187
}
188
189
///Test the value of the given DicoValue
190
/**	@param dv : DicoValue to be checked
191
 * 	@param expectedValue : expected value
192
 * 	@return true on success, false otherwise
193
*/
194
5
bool testDicoDicoValueKeyPtr(const DicoValue * dv, int expectedValue){
195
5
	if(dv == NULL){
196
1
		std::cerr << "testDicoDicoValueKeyPtr : DicoValue for value "<<expectedValue<<" not found!!!" << std::endl;
197
1
		return false;
198
	}
199
4
	const MapDicoValue & mapVal = dv->getMapChild();
200
4
	if(mapVal.size() != 3lu){
201
1
		std::cerr << "testDicoDicoValueKeyPtr : expect 3 value for first value " << expectedValue << std::endl;
202
1
		return false;
203
	}
204
3
	bool b(true);
205
3
	b &= testDicoValueKey(dv->getMap("val1"), expectedValue);
206
3
	b &= testDicoValueKey(dv->getMap("val2"), 1 + expectedValue);
207
3
	b &= testDicoValueKey(dv->getMap("val3"), 2 + expectedValue);
208
3
	phoenix_functionOk("testDicoDicoValueKeyPtr", b);
209
3
	return b;
210
}
211
212
///Test the DicoValue
213
/**	@param fileName : name of the file to be used
214
 * 	@return true on success, false otherwise
215
*/
216
2
bool testDicoDicoString(const std::string & fileName){
217
4
	DicoValue dv;
218
2
	if(!dv.load(fileName)){
219

1
		std::cerr << "testDicoDicoString : cannot load file '"<<fileName<<"'" << std::endl;
220
1
		return false;
221
	}
222
1
	bool b(testDicoDicoValueKeyPtr(dv.getMap("key1"), 1));
223
1
	b &= testDicoDicoValueKeyPtr(dv.getMap("key2"), 2);
224
1
	b &= testDicoDicoValueKeyPtr(dv.getMap("key3"), 3);
225
226
1
	b &= !testDicoDicoValueKeyPtr(NULL, 0);
227
1
	DicoValue val;
228
1
	b &= !testDicoDicoValueKeyPtr(&val, 0);
229
230
1
	b &= saveDico(dv, fileName);
231
1
	phoenix_functionOk("testDicoDicoString", b);
232
1
	return b;
233
}
234
235
///Check all the good parsing
236
/**	@param fileContent : wrong file content
237
 * 	@return true on success, false otherwise
238
*/
239
2
bool testDicoGoodParsing(const std::string & fileContent){
240
4
	std::string fileName("config.txt");
241
2
	bool b(true);
242
2
	b &= saveFileContent(fileName, fileContent);
243
2
	DicoValue dico;
244
2
	b &= dico.load(fileName);
245
2
	phoenix_functionOk("testDicoGoodParsing('"+fileContent+"')", b);
246
4
	return b;
247
}
248
249
///Check all the bad parsing
250
/**	@param fileContent : wrong file content
251
 * 	@return true on success, false otherwise
252
*/
253
5
bool testDicoBadParsing(const std::string & fileContent){
254
10
	std::string fileName("config.txt");
255
5
	bool b(true);
256
5
	b &= saveFileContent(fileName, fileContent);
257
5
	DicoValue dico;
258
5
	b &= !dico.load(fileName);
259
5
	phoenix_functionOk("testDicoBadParsing('"+fileContent+"')", b);
260
10
	return b;
261
}
262
263
///Check all the bad parsing
264
/**	@param fileContent : wrong file content
265
 * 	@return true on success, false otherwise
266
*/
267
3
bool testDicoSaveParsing(const std::string & fileContent){
268
6
	std::string fileName("config_out.txt");
269
3
	bool b(true);
270
3
	DicoValue dico;
271
3
	dico.setValue(fileContent);
272
3
	b &= dico.save(fileName);
273
3
	phoenix_functionOk("testDicoSaveParsing('"+fileContent+"')", b);
274
6
	return b;
275
}
276
277
1
int main(int argc, char** argv){
278
2
	std::string fileName0(CMAKE_CURRENT_TEST_DIR "/testInputDico.txt");
279
280

1
	phoenix_assert(testDicoValue(fileName0));
281

1
	phoenix_assert(!testDicoValue(fileName0 + "whichDoesnotExist"));
282
283
2
	std::string fileName2(CMAKE_CURRENT_TEST_DIR "/testInputDicoValue.txt");
284

1
	phoenix_assert(testDicoValue2(fileName2));
285

1
	phoenix_assert(!testDicoValue2(fileName2 + "whichDoesnotExist"));
286
287
2
	std::string fileName3(CMAKE_CURRENT_TEST_DIR "/testInputDicoValue.txt");
288

1
	phoenix_assert(testDicoValue3(fileName3));
289

1
	phoenix_assert(!testDicoValue3(fileName3 + "whichDoesnotExist"));
290
291
2
	std::string fileNameVec(CMAKE_CURRENT_TEST_DIR "/testInputDicoVecValue.txt");
292

1
	phoenix_assert(testDicoVecValue(fileNameVec));
293

1
	phoenix_assert(!testDicoVecValue(fileNameVec + "whichDoesnotExist"));
294
295
2
	std::string fileNameVecString(CMAKE_CURRENT_TEST_DIR "/testInputDicoVecString.txt");
296

1
	phoenix_assert(testDicoVecString(fileNameVecString));
297

1
	phoenix_assert(!testDicoVecString(fileNameVecString + "whichDoesnotExist"));
298
299
1
	std::string fileNameDicoString(CMAKE_CURRENT_TEST_DIR "/testInputDicoMapDico.txt");
300

1
	phoenix_assert(testDicoDicoString(fileNameDicoString));
301

1
	phoenix_assert(!testDicoDicoString(fileNameDicoString + "whichDoesnotExist"));
302
303

1
	phoenix_assert(testDicoBadParsing(""));
304

1
	phoenix_assert(testDicoGoodParsing("42"));
305

1
	phoenix_assert(testDicoGoodParsing("key other"));
306

1
	phoenix_assert(testDicoBadParsing("{  key1 {"));
307

1
	phoenix_assert(testDicoBadParsing("{  {"));
308

1
	phoenix_assert(testDicoBadParsing("{ °"));
309

1
	phoenix_assert(testDicoBadParsing("{  key1: °"));
310

1
	phoenix_assert(testDicoSaveParsing("Just some text"));
311

1
	phoenix_assert(testDicoSaveParsing("Just some text\nwith new line"));
312

1
	phoenix_assert(testDicoSaveParsing("Just some text with spécial char/"));
313
1
	return 0;
314
}
315
316