PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
main.cpp
Go to the documentation of this file.
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 
13 
17 bool saveDico(const DicoValue & dv, const PPath & fileName){
18  return dv.save(fileName.replace(".txt", ".dico").replace(CMAKE_CURRENT_TEST_DIR, CMAKE_CURRENT_BUILD_DIR));
19 }
20 
22 
25 bool checkConstDicoValue(const DicoValue & dico){
26  bool b(true);
27  b &= dico.getMap("NonExistingmap") == NULL;
28  return b;
29 }
30 
32 
35 bool testDicoValue(const PPath & fileName){
36  DicoValue dv;
37  if(!dv.load(fileName)){
38  std::cerr << "testDicoValue : cannot load file '"<<fileName<<"'" << std::endl;
39  return false;
40  }
41  std::vector<DicoValue> & vecChild = dv.getVecChild();
42 
43  DicoValue & value = dv.getMapChild()["key"];
44  bool b(true);
45  b &= dv.getMap("NonExistingmap") == NULL;
46  b &= checkConstDicoValue(dv);
47  DicoValue dico;
48  dico.setVecChild(dv.getVecChild());
49  dico.setMapChild(dv.getMapChild());
50 
51  std::cout << "testDicoValue : value of key : '" << value.getValue() << "' with key '" << value.getKey() << "', vecChild.size = " << vecChild.size() << std::endl;
52  b &= value.getValue() == "value";
53  b &= saveDico(dv, fileName);
54  return b;
55 }
56 
58 
62 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  return phoenix_check("testDicoValueKey", dv.getValue<int>(), expectedValue);
65 }
66 
68 
71 bool testDicoValue2(const PPath & fileName){
72  DicoValue dv;
73  if(!dv.load(fileName)){
74  std::cerr << "testDicoValue2 : cannot load file '"<<fileName<<"'" << std::endl;
75  return false;
76  }
77  bool b(testDicoValueKey(dv.getMapChild()["key1"], 1));
78  b &= testDicoValueKey(dv.getMapChild()["key2"], 2);
79  b &= testDicoValueKey(dv.getMapChild()["key3"], 3);
80  b &= dv.isKeyExist("key3");
81 
82  b &= saveDico(dv, fileName);
83  return b;
84 }
85 
87 
91 bool testDicoValueKeyPtr(const DicoValue * dv, int expectedValue){
92  if(dv == NULL){
93  std::cerr << "testDicoValueKeyPtr : DicoValue for value "<<expectedValue<<" not found!!!" << std::endl;
94  return false;
95  }
96 // std::cout << "testDicoValueKeyPtr : key '" << dv->getKey() << "' with value '" << dv->getValue() << "'" << std::endl;
97  return dv->getValue<int>() == expectedValue;
98 }
99 
101 
104 bool testDicoValue3(const PPath & fileName){
105  DicoValue dv;
106  if(!dv.load(fileName)){
107  std::cerr << "testDicoValue3 : cannot load file '"<<fileName<<"'" << std::endl;
108  return false;
109  }
110  bool b(testDicoValueKeyPtr(dv.getMap("key1"), 1));
111  b &= testDicoValueKeyPtr(dv.getMap("key2"), 2);
112  b &= testDicoValueKeyPtr(dv.getMap("key3"), 3);
113  b &= !testDicoValueKeyPtr(NULL, 0);
114  b &= saveDico(dv, fileName);
115  return b;
116 }
117 
119 
123 bool testDicoVecValueKeyPtr(const DicoValue * dv, int expectedValue){
124  if(dv == NULL){
125  std::cerr << "testDicoVecValueKeyPtr : DicoValue for value "<<expectedValue<<" not found!!!" << std::endl;
126  return false;
127  }
128  const VecDicoValue & vecVal = dv->getVecChild();
129  if(vecVal.size() != 3lu){
130  std::cerr << "testDicoVecValueKeyPtr : expect 3 value for first value " << expectedValue << std::endl;
131  return false;
132  }
133  bool b(true);
134  for(int i(0); i < 3; ++i){
135  std::cout << "testDicoVecValueKeyPtr : value["<<i<<"] = " << vecVal[i].getValue<int>() << ", expect = " << (i + expectedValue) << std::endl;
136  b &= vecVal[i].getValue<int>() == expectedValue + i;
137  }
138  return b;
139 }
140 
142 
145 bool testDicoVecValue(const PPath & fileName){
146  DicoValue dv;
147  if(!dv.load(fileName)){
148  std::cerr << "testDicoVecValue : cannot load file '"<<fileName<<"'" << std::endl;
149  return false;
150  }
151  bool b(testDicoVecValueKeyPtr(dv.getMap("key1"), 1));
152  b &= testDicoVecValueKeyPtr(dv.getMap("key2"), 2);
153  b &= testDicoVecValueKeyPtr(dv.getMap("key3"), 3);
154  b &= saveDico(dv, fileName);
155  return b;
156 }
157 
159 
162 bool testDicoVecString(const PPath & fileName){
163  DicoValue dv;
164  if(!dv.load(fileName)){
165  std::cerr << "testDicoVecString : cannot load file '"<<fileName<<"'" << std::endl;
166  return false;
167  }
168  bool b(testDicoVecValueKeyPtr(dv.getMap("key1"), 1));
169  b &= testDicoVecValueKeyPtr(dv.getMap("key2"), 2);
170  b &= testDicoVecValueKeyPtr(dv.getMap("key3"), 3);
171 
172  b &= !testDicoVecValueKeyPtr(NULL, 0);
173  DicoValue val;
174  b &= !testDicoVecValueKeyPtr(&val, 0);
175 
176  b &= saveDico(dv, fileName);
177  return b;
178 }
179 
181 
185 bool testDicoValueKey(const DicoValue * dv, int expectedValue){
186  return phoenix_check("testDicoValueKey", dv->getValue<int>(), expectedValue);
187 }
188 
190 
194 bool testDicoDicoValueKeyPtr(const DicoValue * dv, int expectedValue){
195  if(dv == NULL){
196  std::cerr << "testDicoDicoValueKeyPtr : DicoValue for value "<<expectedValue<<" not found!!!" << std::endl;
197  return false;
198  }
199  const MapDicoValue & mapVal = dv->getMapChild();
200  if(mapVal.size() != 3lu){
201  std::cerr << "testDicoDicoValueKeyPtr : expect 3 value for first value " << expectedValue << std::endl;
202  return false;
203  }
204  bool b(true);
205  b &= testDicoValueKey(dv->getMap("val1"), expectedValue);
206  b &= testDicoValueKey(dv->getMap("val2"), 1 + expectedValue);
207  b &= testDicoValueKey(dv->getMap("val3"), 2 + expectedValue);
208  return b;
209 }
210 
212 
215 bool testDicoDicoString(const PPath & fileName){
216  DicoValue dv;
217  if(!dv.load(fileName)){
218  std::cerr << "testDicoDicoString : cannot load file '"<<fileName<<"'" << std::endl;
219  return false;
220  }
221  bool b(testDicoDicoValueKeyPtr(dv.getMap("key1"), 1));
222  b &= testDicoDicoValueKeyPtr(dv.getMap("key2"), 2);
223  b &= testDicoDicoValueKeyPtr(dv.getMap("key3"), 3);
224 
225  b &= !testDicoDicoValueKeyPtr(NULL, 0);
226  DicoValue val;
227  b &= !testDicoDicoValueKeyPtr(&val, 0);
228 
229  b &= saveDico(dv, fileName);
230  return b;
231 }
232 
234 
237 bool testDicoGoodParsing(const PString & fileContent){
238  PPath fileName("config.txt");
239  bool b(true);
240  b &= fileName.saveFileContent(fileContent);
241  DicoValue dico;
242  b &= dico.load(fileName);
243  return b;
244 }
245 
247 
250 bool testDicoBadParsing(const PString & fileContent){
251  PPath fileName("config.txt");
252  bool b(true);
253  b &= fileName.saveFileContent(fileContent);
254  DicoValue dico;
255  b &= !dico.load(fileName);
256  return b;
257 }
258 
260 
263 bool testDicoSaveParsing(const PString & fileContent){
264  PPath fileName("config_out.txt");
265  bool b(true);
266  DicoValue dico;
267  dico.setValue(fileContent);
268  b &= dico.save(fileName);
269  return b;
270 }
271 
274  PPath fileName(CMAKE_CURRENT_TEST_DIR "/testInputDicoMapDico.json");
275 
276  DicoValue dv;
277  phoenix_assert(dv.load(fileName));
278  phoenix_assert(dv.save(fileName.getFileName(), "\""));
279 
280  DicoValue compact;
281  phoenix_assert(compact.fromString("{\"key1\": \"1\", \"key2\": \"2\", \"key3\": \"3\" }"));
282  phoenix_assert(phoenix_check("Test compact JSON", compact.toString("\"", "", ""), "{\"key1\": \"1\",\"key2\": \"2\",\"key3\": \"3\"}"));
283 }
284 
285 int main(int argc, char** argv){
286  PPath fileName0(CMAKE_CURRENT_TEST_DIR "/testInputDico.txt");
287 
288  phoenix_assert(testDicoValue(fileName0));
289  phoenix_assert(!testDicoValue(PString(fileName0 + "whichDoesnotExist")));
290 
291  PPath fileName2(CMAKE_CURRENT_TEST_DIR "/testInputDicoValue.txt");
292  phoenix_assert(testDicoValue2(fileName2));
293  phoenix_assert(!testDicoValue2(PString(fileName2 + "whichDoesnotExist")));
294 
295  PPath fileName3(CMAKE_CURRENT_TEST_DIR "/testInputDicoValue.txt");
296  phoenix_assert(testDicoValue3(fileName3));
297  phoenix_assert(!testDicoValue3(PString(fileName3 + "whichDoesnotExist")));
298 
299  PPath fileNameVec(CMAKE_CURRENT_TEST_DIR "/testInputDicoVecValue.txt");
300  phoenix_assert(testDicoVecValue(fileNameVec));
301  phoenix_assert(!testDicoVecValue(PString(fileNameVec + "whichDoesnotExist")));
302 
303  PPath fileNameVecString(CMAKE_CURRENT_TEST_DIR "/testInputDicoVecString.txt");
304  phoenix_assert(testDicoVecString(fileNameVecString));
305  phoenix_assert(!testDicoVecString(PString(fileNameVecString + "whichDoesnotExist")));
306 
307  PPath fileNameDicoString(CMAKE_CURRENT_TEST_DIR "/testInputDicoMapDico.txt");
308  phoenix_assert(testDicoDicoString(fileNameDicoString));
309  phoenix_assert(!testDicoDicoString(PString(fileNameDicoString + "whichDoesnotExist")));
310 
311  //Test with a JSON
312  phoenix_assert(testDicoDicoString(PPath(CMAKE_CURRENT_TEST_DIR "/testInputDicoMapDico.json")));
313 
316  phoenix_assert(testDicoGoodParsing("key other"));
317  phoenix_assert(testDicoBadParsing("{ key1 {"));
320  phoenix_assert(testDicoBadParsing("{ key1: °"));
321  phoenix_assert(testDicoSaveParsing("Just some text"));
322  phoenix_assert(testDicoSaveParsing("Just some text\nwith new line"));
323  phoenix_assert(testDicoSaveParsing("Just some text with spécial char/"));
325  return 0;
326 }
327 
328 
std::map< PString, DicoValue > MapDicoValue
Vector of DicoValue.
Definition: DicoValue.h:79
std::vector< DicoValue > VecDicoValue
Vector of DicoValue.
Definition: DicoValue.h:77
int main(int argc, char **argv)
Definition: main.cpp:228
bool checkConstDicoValue(const DicoValue &dico)
Check a const DicoValue.
Definition: main.cpp:25
bool testDicoVecString(const PPath &fileName)
Test the DicoValue.
Definition: main.cpp:162
void testFromJSonToJSon()
Test JSON to JSON convertion.
Definition: main.cpp:273
bool testDicoVecValueKeyPtr(const DicoValue *dv, int expectedValue)
Test the value of the given DicoValue.
Definition: main.cpp:123
bool testDicoValue2(const PPath &fileName)
Test the DicoValue.
Definition: main.cpp:71
bool testDicoVecValue(const PPath &fileName)
Test the DicoValue.
Definition: main.cpp:145
bool testDicoDicoValueKeyPtr(const DicoValue *dv, int expectedValue)
Test the value of the given DicoValue.
Definition: main.cpp:194
bool testDicoValue3(const PPath &fileName)
Test the DicoValue.
Definition: main.cpp:104
bool saveDico(const DicoValue &dv, const PPath &fileName)
Save the given DicoValue in the given file.
Definition: main.cpp:17
bool testDicoValueKey(const DicoValue &dv, int expectedValue)
Test the value of the given DicoValue.
Definition: main.cpp:62
bool testDicoDicoString(const PPath &fileName)
Test the DicoValue.
Definition: main.cpp:215
bool testDicoSaveParsing(const PString &fileContent)
Check all the bad parsing.
Definition: main.cpp:263
bool testDicoBadParsing(const PString &fileContent)
Check all the bad parsing.
Definition: main.cpp:250
bool testDicoValueKeyPtr(const DicoValue *dv, int expectedValue)
Test the value of the given DicoValue.
Definition: main.cpp:91
bool testDicoValue(const PPath &fileName)
Test the DicoValue.
Definition: main.cpp:35
bool testDicoGoodParsing(const PString &fileContent)
Check all the good parsing.
Definition: main.cpp:237
Dictionnary of values.
Definition: DicoValue.h:17
void setVecChild(const std::vector< DicoValue > &vecChild)
Sets the vecChild of the DicoValue.
Definition: DicoValue.cpp:155
PString toString(const PString &valueDecorator="", PString baseIndentation="\t", PString baseNewLine="\n") const
Convert the DicoValue into a string.
Definition: DicoValue.cpp:74
const PString & getKey() const
Gets the key of the DicoValue.
Definition: DicoValue.cpp:190
bool isKeyExist(const PString &key) const
Say if the given key exists in the map of children.
Definition: DicoValue.cpp:107
bool fromString(const PString &content)
Create a DicoValue from a PString.
Definition: DicoValue.cpp:62
const std::vector< DicoValue > & getVecChild() const
Gets the vecChild of the DicoValue.
Definition: DicoValue.cpp:204
const DicoValue * getMap(const PString &key) const
Get a DicoValue in the map of the current one.
Definition: DicoValue.cpp:116
bool save(const PPath &fileName, const PString &valueDecorator="", PString baseIndentation="\t", PString baseNewLine="\n") const
Save the DicoValue with a text file.
Definition: DicoValue.cpp:53
void setValue(const PString &value)
Sets the value of the DicoValue.
Definition: DicoValue.cpp:141
const std::map< PString, DicoValue > & getMapChild() const
Gets the mapChild of the DicoValue.
Definition: DicoValue.cpp:218
T getValue() const
Convert the value of the current DicoValue into a type.
bool load(const PPath &fileName)
Load the DicoValue with a text file.
Definition: DicoValue.cpp:40
void setMapChild(const std::map< PString, DicoValue > &mapChild)
Sets the mapChild of the DicoValue.
Definition: DicoValue.cpp:162
Path of a directory or a file.
Definition: PPath.h:17
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
Extends the std::string.
Definition: PString.h:16
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:204
#define phoenix_assert(isOk)
bool phoenix_check(const std::string &testName, const std::string &val, const std::string &reference)
Check two string.