PhoenixLecture  2.0.0
Set of tools to make lectures
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 "parser_toml.h"
10 
12 
16 bool checkKeyMapValue(const DicoValue * mapKey, const PString & expectedValue){
17  if(mapKey == NULL){
18  std::cout << "checkKeyMapValue : map NULL for expectedValue = '"<<expectedValue<<"'" << std::endl;
19  return expectedValue == "";
20  }
21 // std::cout << "checkKeyMapValue : map '"<<mapKey->getKey()<<"' => '"<<mapKey->getValue()<<"', expectedValue = '"<<expectedValue<<"'" << std::endl;
22  bool b(mapKey->getValue() == expectedValue);
23 // std::cout << "checkKeyMapValue : b = " << b << std::endl;
24  return b;
25 }
26 
28 
32 bool checkKeyMapVecValue(const DicoValue * mapKey, const PVecString & vecExpectedValue){
33  if(mapKey == NULL){
34  std::cout << "checkKeyMapVecValue : map NULL for vecExpectedValue = " << std::endl;
35 // phoenix_print(vecExpectedValue);
36  return vecExpectedValue.size() == 0lu;
37  }
38 // std::cout << "checkKeyMapVecValue : map '"<<mapKey->getKey()<<"' => '"<<mapKey->getValue()<<"', vecExpectedValue = " << std::endl;
39 // phoenix_print(vecExpectedValue);
40  bool b(true);
41  const VecDicoValue & vecValue = mapKey->getVecChild();
42  b &= vecValue.size() == vecExpectedValue.size();
43 // std::cout << "checkKeyMapVecValue : vecValue.size() = " << vecValue.size() << ", vecExpectedValue.size() = "<< vecExpectedValue.size() << ", isOk = " << b << std::endl;
44  VecDicoValue::const_iterator it(vecValue.begin());
45  PVecString::const_iterator itRef(vecExpectedValue.begin());
46  while(b && it != vecValue.end() && itRef != vecExpectedValue.end()){
47  b &= it->getValue() == *itRef;
48  std::cout << "\tvalue = '" << it->getValue() << "', reference = '"<< *itRef << "', isOk = " << b << std::endl;
49  ++it;
50  ++itRef;
51  }
52 // std::cout << "checkKeyMapVecValue : b = " << b << std::endl;
53  return b;
54 }
55 
58  checkKeyMapValue(NULL, "");
59  PVecString vecNoValue;
60  checkKeyMapVecValue(NULL, vecNoValue);
61 }
62 
65  PString fileContent("[package]\nname = \"hello_hdf5\"\ndescription = \"some string in double quotes\"\nenable_option = true\ndisable_option = false\n\n");
66  PPath tomlFile("test.toml");
67  phoenix_assert(tomlFile.saveFileContent(fileContent));
68 
69  DicoValue dico;
70  phoenix_assert(parser_toml(dico, tomlFile));
71 // std::cout << "checkParserToml : output DicoValue :" << std::endl;
72 // dico.print();
73 
74  DicoValue * mapPackage = dico.getMap("package");
75 // bool isKeyExist = mapPackage != NULL;
76 // std::cout << "checkParserToml : isKeyExist = " << isKeyExist << std::endl;
77  DicoValue * mapPackageName = mapPackage->getMap("name");
78  phoenix_assert(checkKeyMapValue(mapPackageName, "\"hello_hdf5\""));
79 
80  phoenix_assert(phoenix_get_string(*mapPackage, "name", "SomeVar", "") == "hello_hdf5");
81  phoenix_assert(phoenix_get_string(*mapPackage, "undefined_var", "SomeVar", "") == "SomeVar");
82  phoenix_assert(phoenix_get_string(*mapPackage, "undefined_var", "", "SomeVar") == "SomeVar");
83  phoenix_assert(phoenix_get_string(*mapPackage, "undefined_var", "SomeVar") == "SomeVar");
84 
85  DicoValue * mapPackageEnableOption = mapPackage->getMap("enable_option");
86  phoenix_assert(checkKeyMapValue(mapPackageEnableOption, "true"));
87 
88  DicoValue * mapPackageDisableOption = mapPackage->getMap("disable_option");
89  phoenix_assert(checkKeyMapValue(mapPackageDisableOption, "false"));
90 }
91 
94  PString fileContent("[package]\nname = \"hello_hdf5\"\ndescription = 'some string in simple quotes'\nsome_list = [\"one\", \"two\", \"three\"]\nlist_value = [1, 2, 3, 4]\n\n[dependencies]\nhdf5 = \"0.8.1\"\nndarray = '0.15.6'\n\n");
95 
96  PPath tomlFile("test_toml_list.toml");
97  phoenix_assert(tomlFile.saveFileContent(fileContent));
98 
99  DicoValue dico;
100  phoenix_assert(parser_toml(dico, tomlFile));
101 // std::cout << "checkTomlList : output DicoValue :" << std::endl;
102 // dico.print();
103 
104  DicoValue * mapPackage = dico.getMap("package");
105 // bool isKeyExist = mapPackage != NULL;
106 // std::cout << "checkTomlList : isKeyExist = " << isKeyExist << std::endl;
107  DicoValue * mapPackageName = mapPackage->getMap("name");
108  phoenix_assert(checkKeyMapValue(mapPackageName, "\"hello_hdf5\""));
109  DicoValue * mapPackageDescription = mapPackage->getMap("description");
110  phoenix_assert(checkKeyMapValue(mapPackageDescription, "'some string in simple quotes'"));
111 
112  DicoValue * mapSomeListKey = mapPackage->getMap("some_list");
113 // bool isSomeListExist = mapSomeListKey != NULL;
114 // std::cout << "checkTomlList : isSomeListExist = " << isSomeListExist << std::endl;
115 
116  PVecString vecExpectedValue;
117  vecExpectedValue.push_back("\"one\"");
118  vecExpectedValue.push_back("\"two\"");
119  vecExpectedValue.push_back("\"three\"");
120  phoenix_assert(checkKeyMapVecValue(mapSomeListKey, vecExpectedValue));
121 
122  DicoValue * mapOtherListKey = mapPackage->getMap("list_value");
123 // bool isOtherListExist = mapOtherListKey != NULL;
124 // std::cout << "checkTomlList : isOtherListExist = " << isOtherListExist << std::endl;
125 
126  PVecString vecOtherExpectedValue;
127  vecOtherExpectedValue.push_back("1");
128  vecOtherExpectedValue.push_back("2");
129  vecOtherExpectedValue.push_back("3");
130  vecOtherExpectedValue.push_back("4");
131  phoenix_assert(checkKeyMapVecValue(mapOtherListKey, vecOtherExpectedValue));
132 
133  DicoValue * mapDependencies = dico.getMap("dependencies");
134  DicoValue * mapDependenciesHdf5 = mapDependencies->getMap("hdf5");
135  phoenix_assert(checkKeyMapValue(mapDependenciesHdf5, "\"0.8.1\""));
136  DicoValue * mapDependenciesNdarray = mapDependencies->getMap("ndarray");
137  phoenix_assert(checkKeyMapValue(mapDependenciesNdarray, "'0.15.6'"));
138 }
139 
142  PString fileContent("[package]\nsome_list = []\n\n");
143 
144  PPath tomlFile("test_toml_empty_list.toml");
145  phoenix_assert(tomlFile.saveFileContent(fileContent));
146 
147  DicoValue dico;
148  phoenix_assert(parser_toml(dico, tomlFile));
149 // std::cout << "checkTomlEmptyList : output DicoValue :" << std::endl;
150 // dico.print();
151 
152  DicoValue * mapPackage = dico.getMap("package");
153 // bool isKeyExist = mapPackage != NULL;
154 // std::cout << "checkTomlEmptyList : isKeyExist = " << isKeyExist << std::endl;
155 
156  DicoValue * mapSomeListKey = mapPackage->getMap("some_list");
157 // bool isSomeListExist = mapSomeListKey != NULL;
158 // std::cout << "checkTomlEmptyList : isSomeListExist = " << isSomeListExist << std::endl;
159  phoenix_assert(mapSomeListKey->getVecChild().size() == 0lu);
160 }
161 
162 
165  PString fileContent("[package]\nsome_nested_list = [1, 2, [3, 4]]\n\n");
166 
167  PPath tomlFile("test_toml_nested_list.toml");
168  phoenix_assert(tomlFile.saveFileContent(fileContent));
169 
170  DicoValue dico;
171  phoenix_assert(parser_toml(dico, tomlFile));
172 // std::cout << "checkTomlNestedList : output DicoValue :" << std::endl;
173 // dico.print();
174 
175  DicoValue * mapPackage = dico.getMap("package");
176 // bool isKeyExist = mapPackage != NULL;
177 // std::cout << "checkTomlNestedList : isKeyExist = " << isKeyExist << std::endl;
178  DicoValue * mapOtherListKey = mapPackage->getMap("some_nested_list");
179 // bool isOtherListExist = mapOtherListKey != NULL;
180 // std::cout << "checkTomlNestedList : isOtherListExist = " << isOtherListExist << std::endl;
181 
182  const VecDicoValue & vecValue = mapOtherListKey->getVecChild();
183  phoenix_assert(vecValue.size() == 3lu);
184  phoenix_assert(vecValue[0].getValue() == "1");
185  phoenix_assert(vecValue[1].getValue() == "2");
186 
187  const DicoValue & subList = vecValue[2];
188  const VecDicoValue & vecSubValue = subList.getVecChild();
189  phoenix_assert(vecSubValue.size() == 2lu);
190  phoenix_assert(vecSubValue[0].getValue() == "3");
191  phoenix_assert(vecSubValue[1].getValue() == "4");
192 }
193 
194 
197  PString fileContent("[first.second]\nsome_value = 42\n\n");
198 
199  PPath tomlFile("test_toml_nested_map.toml");
200  phoenix_assert(tomlFile.saveFileContent(fileContent));
201 
202  DicoValue dico;
203  phoenix_assert(parser_toml(dico, tomlFile));
204 // std::cout << "checkTomNestedlMap : output DicoValue :" << std::endl;
205 // dico.print();
206 
207  DicoValue * mapFirst = dico.getMap("first");
208 // bool isKeyExist = mapFirst != NULL;
209 // std::cout << "checkTomNestedlMap : isKeyExist = " << isKeyExist << std::endl;
210  DicoValue * mapSecond = mapFirst->getMap("second");
211  DicoValue * mapOtherListKey = mapSecond->getMap("some_value");
212  phoenix_assert(checkKeyMapValue(mapOtherListKey, "42"));
213 }
214 
217  PString fileContent("[[program]]\nname = \"shadok\"\nage = 42\n\n[[program]]\nname = \"gibi\"\nage = 23\n\n");
218 
219  PPath tomlFile("test_toml_table.toml");
220  phoenix_assert(tomlFile.saveFileContent(fileContent));
221 
222  DicoValue dico;
223  phoenix_assert(parser_toml(dico, tomlFile));
224 // std::cout << "checkTomTable : output DicoValue :" << std::endl;
225 // dico.print();
226  DicoValue * mapProgram = dico.getMap("program");
227 // bool isKeyExist = mapProgram != NULL;
228 // std::cout << "checkTomTable : isKeyExist = " << isKeyExist << std::endl;
229  const VecDicoValue & vecProgramValue = mapProgram->getVecChild();
230  phoenix_assert(vecProgramValue.size() == 2lu);
231  const DicoValue * mapProgram0Name = vecProgramValue[0].getMap("name");
232  phoenix_assert(checkKeyMapValue(mapProgram0Name, "\"shadok\""));
233  const DicoValue * mapProgram0Age = vecProgramValue[0].getMap("age");
234  phoenix_assert(checkKeyMapValue(mapProgram0Age, "42"));
235 
236  const DicoValue * mapProgram1Name = vecProgramValue[1].getMap("name");
237  phoenix_assert(checkKeyMapValue(mapProgram1Name, "\"gibi\""));
238  const DicoValue * mapProgram1Age = vecProgramValue[1].getMap("age");
239  phoenix_assert(checkKeyMapValue(mapProgram1Age, "23"));
240 }
241 
244  PString fileContent("[program]\nname = {url = \"someUrl\"}\nage = 42\n\n");
245 
246  PPath tomlFile("test_toml_compact_dico.toml");
247  phoenix_assert(tomlFile.saveFileContent(fileContent));
248 
249  DicoValue dico;
250  phoenix_assert(parser_toml(dico, tomlFile));
251 // std::cout << "checkTomlCompactDico : output DicoValue :" << std::endl;
252 // dico.print();
253  DicoValue * mapProgram = dico.getMap("program");
254 // bool isKeyExist = mapProgram != NULL;
255 // std::cout << "checkTomlCompactDico : isKeyExist = " << isKeyExist << std::endl;
256  const DicoValue * mapName = mapProgram->getMap("name");
257  const DicoValue * mapNameUrl = mapName->getMap("url");
258  phoenix_assert(checkKeyMapValue(mapNameUrl, "\"someUrl\""));
259  const DicoValue * mapAge = mapProgram->getMap("age");
260  phoenix_assert(checkKeyMapValue(mapAge, "42"));
261 }
262 
265  PString fileContent("[program]\nname = {url = \"someUrl\",\n#Some comment\n version = \"1.0.0\"}\n# Some Comment\nage = 42\nemptyDico = {}\n\n");
266  PPath tomlFile("test_toml_compact_dico.toml");
267  phoenix_assert(tomlFile.saveFileContent(fileContent));
268  DicoValue dico;
269  phoenix_assert(parser_toml(dico, tomlFile));
270 // std::cout << "checkTomlCompactDico : output DicoValue :" << std::endl;
271 // dico.print();
272  DicoValue * mapProgram = dico.getMap("program");
273 // bool isKeyExist = mapProgram != NULL;
274 // std::cout << "checkTomlCompactDico : isKeyExist = " << isKeyExist << std::endl;
275  const DicoValue * mapName = mapProgram->getMap("name");
276  const DicoValue * mapNameUrl = mapName->getMap("url");
277  phoenix_assert(checkKeyMapValue(mapNameUrl, "\"someUrl\""));
278  const DicoValue * mapNameVersion = mapName->getMap("version");
279  phoenix_assert(checkKeyMapValue(mapNameVersion, "\"1.0.0\""));
280  const DicoValue * mapAge = mapProgram->getMap("age");
281  phoenix_assert(checkKeyMapValue(mapAge, "42"));
282 }
283 
285 
289 bool checkIsParserOk(const PPath & fileName, const PString & fileContent){
290  bool b(true);
291  b &= fileName.saveFileContent(fileContent);
292  DicoValue dico;
293  b &= parser_toml(dico, fileName);
294  return b;
295 }
296 
299  DicoValue dico;
300  phoenix_assert(!parser_toml(dico, PString("UnexsitingFileName.toml")));
301  phoenix_assert(!checkIsParserOk(PString("unextected_token.toml"), "=\n"));
302  phoenix_assert(!checkIsParserOk(PString("missing_equal_of_variable.toml"), "[package]\nvariable value\n"));
303  phoenix_assert(!checkIsParserOk(PString("missing_value_of_variable.toml"), "[package]\nvariable =\n"));
304  phoenix_assert(!checkIsParserOk(PString("unclosed_list.toml"), "[package]\nlist = [42\n"));
305  phoenix_assert(!checkIsParserOk(PString("unclosed_empty_list.toml"), "[package]\nlist = [\n"));
306  phoenix_assert(!checkIsParserOk(PString("unclosed_dico.toml"), "[package]\ndico = {name\n"));
307  phoenix_assert(!checkIsParserOk(PString("unclosed_empty_dico.toml"), "[package]\ndico = {\n"));
308  phoenix_assert(checkIsParserOk(PString("some_comment.toml"), "# Some value\n[package]\nvalue = 42\n"));
309  phoenix_assert(checkIsParserOk(PString("some_triple_double_quote_string.toml"), "# Some value\n[package]\nstrvalue = \"\"\"\nsome string\n\"\"\"\n"));
310 }
311 
312 int main(int argc, char** argv){
313  testCheckValue();
315  checkParserToml();
316  checkTomlList();
320  checkTomTable();
323  return 0;
324 }
325 
326 
std::vector< DicoValue > VecDicoValue
Vector of DicoValue.
Definition: DicoValue.h:77
std::vector< PString > PVecString
Definition: PString.h:96
int main(int argc, char **argv)
Definition: main.cpp:228
void testCheckValue()
Test if data size is Ok.
Definition: main.cpp:11
void checkTomlList()
Check the embeded dico.
Definition: main.cpp:93
bool checkKeyMapVecValue(const DicoValue *mapKey, const PVecString &vecExpectedValue)
Check the value of a DicoValue.
Definition: main.cpp:32
void checkTomlCompactDico2()
Check the compact dico.
Definition: main.cpp:264
void checkTomTable()
Check the embeded dico.
Definition: main.cpp:216
void checkTomlCompactDico()
Check the compact dico.
Definition: main.cpp:243
void checkTomNestedlMap()
Check the embeded dico.
Definition: main.cpp:196
void checkTomNestedlList()
Check the embeded dico.
Definition: main.cpp:164
void checkParserToml()
Check the YML parser.
Definition: main.cpp:64
bool checkKeyMapValue(const DicoValue *mapKey, const PString &expectedValue)
Check the value of a DicoValue.
Definition: main.cpp:16
void checkParserTomlFail()
Check the YML parser.
Definition: main.cpp:298
void checkTomlEmptyList()
Check the embeded dico.
Definition: main.cpp:141
bool checkIsParserOk(const PPath &fileName, const PString &fileContent)
Check if the parsing of a file is OK.
Definition: main.cpp:289
Dictionnary of values.
Definition: DicoValue.h:17
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
T getValue() const
Convert the value of the current DicoValue into a type.
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
Extends the std::string.
Definition: PString.h:16
bool parser_toml(DicoValue &dico, const PPath &fileName)
#define phoenix_assert(isOk)
PString phoenix_get_string(const DicoValue &dico, const PString &varName, const PString &defaultValue)
Get the string from a dictionnary.