[1560] | 1 | #include <iostream> |
---|
| 2 | #include <errno.h> |
---|
| 3 | #include <fstream> |
---|
| 4 | #include <sys/stat.h> |
---|
| 5 | #include "RpParserXML.h" |
---|
[1528] | 6 | |
---|
[1560] | 7 | size_t |
---|
| 8 | readFile ( |
---|
| 9 | const char *filePath, |
---|
| 10 | const char **buf) |
---|
| 11 | { |
---|
| 12 | if (buf == NULL) { |
---|
| 13 | fprintf(stderr,"buf is NULL while opening file \"%s\"", filePath); |
---|
| 14 | return 0; |
---|
| 15 | } |
---|
[1528] | 16 | |
---|
[1560] | 17 | FILE *f; |
---|
| 18 | f = fopen(filePath, "rb"); |
---|
| 19 | if (f == NULL) { |
---|
| 20 | fprintf(stderr,"can't open \"%s\": %s", filePath, strerror(errno)); |
---|
| 21 | return 0; |
---|
| 22 | } |
---|
| 23 | struct stat stat; |
---|
| 24 | if (fstat(fileno(f), &stat) < 0) { |
---|
| 25 | fprintf(stderr,"can't stat \"%s\": %s", filePath, strerror(errno)); |
---|
| 26 | return 0; |
---|
| 27 | } |
---|
| 28 | off_t size; |
---|
| 29 | size = stat.st_size; |
---|
| 30 | char* memblock; |
---|
| 31 | memblock = new char [size+1]; |
---|
| 32 | if (memblock == NULL) { |
---|
| 33 | fprintf(stderr,"can't allocate %zu bytes for file \"%s\": %s", |
---|
[1581] | 34 | (size_t) size, filePath, strerror(errno)); |
---|
[1560] | 35 | fclose(f); |
---|
| 36 | return 0; |
---|
| 37 | } |
---|
[1528] | 38 | |
---|
[1560] | 39 | size_t nRead; |
---|
| 40 | nRead = fread(memblock, sizeof(char), size, f); |
---|
| 41 | fclose(f); |
---|
[1528] | 42 | |
---|
[1560] | 43 | if (nRead != (size_t)size) { |
---|
[1581] | 44 | fprintf(stderr,"can't read %zu bytes from \"%s\": %s", (size_t) size, filePath, |
---|
[1560] | 45 | strerror(errno)); |
---|
| 46 | return 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | memblock[size] = '\0'; |
---|
| 50 | *buf = memblock; |
---|
| 51 | return nRead; |
---|
[1528] | 52 | } |
---|
| 53 | |
---|
[1560] | 54 | int xmlparser_0_0 () |
---|
| 55 | { |
---|
| 56 | const char *desc = "test creating xml parser"; |
---|
| 57 | const char *testname = "xmlparser_0_0"; |
---|
| 58 | int retVal = 0; |
---|
[1528] | 59 | |
---|
[1560] | 60 | const char *expected = "initialized ParserXml Object"; |
---|
| 61 | const char *received = NULL; |
---|
| 62 | |
---|
| 63 | Rp_ParserXml *p = NULL; |
---|
| 64 | |
---|
| 65 | p = Rp_ParserXmlCreate(); |
---|
| 66 | |
---|
| 67 | if (p == NULL) { |
---|
| 68 | printf("Error: %s\n", testname); |
---|
| 69 | printf("\t%s\n", desc); |
---|
| 70 | printf("\texpected \"%s\"\n",expected); |
---|
| 71 | printf("\treceived \"%s\"\n",received); |
---|
| 72 | retVal = 1; |
---|
[1528] | 73 | } |
---|
| 74 | |
---|
[1560] | 75 | if (p) { |
---|
| 76 | Rp_ParserXmlDestroy(&p); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | return retVal; |
---|
[1528] | 80 | } |
---|
| 81 | |
---|
[1560] | 82 | int xmlparser_1_0 () |
---|
[1528] | 83 | { |
---|
[1560] | 84 | const char *desc = "test sending xml text to xml parser"; |
---|
| 85 | const char *testname = "xmlparser_1_0"; |
---|
| 86 | const char *infile = "xmlparser_1_0_in.xml"; |
---|
| 87 | const char *outfile = "xmlparser_1_0_out.xml"; |
---|
| 88 | int retVal = 0; |
---|
[1528] | 89 | |
---|
[1560] | 90 | const char *xmltext = NULL; |
---|
| 91 | const char *expected = NULL; |
---|
[1528] | 92 | |
---|
[1560] | 93 | readFile(infile,&xmltext); |
---|
| 94 | readFile(outfile,&expected); |
---|
| 95 | |
---|
| 96 | const char *received = NULL; |
---|
| 97 | |
---|
| 98 | Rp_ParserXml *p = NULL; |
---|
| 99 | |
---|
| 100 | p = Rp_ParserXmlCreate(); |
---|
| 101 | |
---|
| 102 | Rp_ParserXmlParse(p, xmltext); |
---|
| 103 | |
---|
| 104 | received = Rp_ParserXmlXml(p); |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | if (strcmp(expected,received) != 0) { |
---|
| 108 | printf("Error: %s\n", testname); |
---|
| 109 | printf("\t%s\n", desc); |
---|
| 110 | printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected); |
---|
| 111 | printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received); |
---|
| 112 | retVal = 1; |
---|
[1528] | 113 | } |
---|
| 114 | |
---|
[1560] | 115 | if (p) { |
---|
| 116 | Rp_ParserXmlDestroy(&p); |
---|
| 117 | } |
---|
| 118 | |
---|
[2289] | 119 | if (xmltext) { |
---|
| 120 | delete[] xmltext; |
---|
| 121 | xmltext = NULL; |
---|
| 122 | } |
---|
| 123 | if (expected) { |
---|
| 124 | delete[] expected; |
---|
| 125 | expected = NULL; |
---|
| 126 | } |
---|
| 127 | |
---|
[1560] | 128 | return retVal; |
---|
[1528] | 129 | } |
---|
| 130 | |
---|
[1560] | 131 | |
---|
[1581] | 132 | int xmlparser_1_1 () |
---|
| 133 | { |
---|
| 134 | const char *desc = "test sending xml text to xml parser"; |
---|
| 135 | const char *testname = "xmlparser_1_1"; |
---|
| 136 | const char *infile = "xmlparser_1_1_in.xml"; |
---|
| 137 | const char *outfile = "xmlparser_1_1_out.xml"; |
---|
| 138 | int retVal = 0; |
---|
| 139 | |
---|
| 140 | const char *xmltext = NULL; |
---|
| 141 | const char *expected = NULL; |
---|
| 142 | |
---|
| 143 | readFile(infile,&xmltext); |
---|
| 144 | readFile(outfile,&expected); |
---|
| 145 | |
---|
| 146 | const char *received = NULL; |
---|
| 147 | |
---|
| 148 | Rp_ParserXml *p = NULL; |
---|
| 149 | |
---|
| 150 | p = Rp_ParserXmlCreate(); |
---|
| 151 | |
---|
| 152 | Rp_ParserXmlParse(p, xmltext); |
---|
| 153 | |
---|
| 154 | received = Rp_ParserXmlXml(p); |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | if (strcmp(expected,received) != 0) { |
---|
| 158 | printf("Error: %s\n", testname); |
---|
| 159 | printf("\t%s\n", desc); |
---|
| 160 | printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected); |
---|
| 161 | printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received); |
---|
| 162 | retVal = 1; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if (p) { |
---|
| 166 | Rp_ParserXmlDestroy(&p); |
---|
| 167 | } |
---|
| 168 | |
---|
[2289] | 169 | if (xmltext) { |
---|
| 170 | delete[] xmltext; |
---|
| 171 | xmltext = NULL; |
---|
| 172 | } |
---|
| 173 | if (expected) { |
---|
| 174 | delete[] expected; |
---|
| 175 | expected = NULL; |
---|
| 176 | } |
---|
| 177 | |
---|
[1581] | 178 | return retVal; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
[1560] | 182 | int xmlparser_2_0 () |
---|
[1528] | 183 | { |
---|
[1560] | 184 | const char *desc = "test printing xml as path/val combos"; |
---|
| 185 | const char *testname = "xmlparser_2_0"; |
---|
| 186 | const char *infile = "xmlparser_2_0_in.xml"; |
---|
| 187 | int retVal = 0; |
---|
[1528] | 188 | |
---|
[1560] | 189 | const char *xmltext = NULL; |
---|
[1528] | 190 | |
---|
[1560] | 191 | readFile(infile,&xmltext); |
---|
[1528] | 192 | |
---|
[1560] | 193 | const char *expected = "\ |
---|
| 194 | run.tool.about Press Simulate to view results.\n\ |
---|
| 195 | run.tool.command tclsh @tool/fermi.tcl @driver\n\ |
---|
| 196 | run.input.number(Ef).about.label Fermi Level\n\ |
---|
| 197 | run.input.number(Ef).about.description Energy at center of distribution.\n\ |
---|
| 198 | run.input.number(Ef).units eV\n\ |
---|
| 199 | run.input.number(Ef).min -10eV\n\ |
---|
| 200 | run.input.number(Ef).max 10eV\n\ |
---|
| 201 | run.input.number(Ef).default 0eV\n\ |
---|
| 202 | run.input.number(Ef).current 0eV\n"; |
---|
[1528] | 203 | |
---|
[1560] | 204 | const char *received = NULL; |
---|
[1528] | 205 | |
---|
[1560] | 206 | Rp_ParserXml *p = NULL; |
---|
[1528] | 207 | |
---|
[1560] | 208 | p = Rp_ParserXmlCreate(); |
---|
| 209 | |
---|
| 210 | Rp_ParserXmlParse(p, xmltext); |
---|
| 211 | |
---|
| 212 | received = Rp_ParserXmlPathVal(p); |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | if (strcmp(expected,received) != 0) { |
---|
| 216 | printf("Error: %s\n", testname); |
---|
| 217 | printf("\t%s\n", desc); |
---|
| 218 | printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected); |
---|
| 219 | printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received); |
---|
| 220 | retVal = 1; |
---|
[1528] | 221 | } |
---|
| 222 | |
---|
[1560] | 223 | if (p) { |
---|
| 224 | Rp_ParserXmlDestroy(&p); |
---|
[1528] | 225 | } |
---|
| 226 | |
---|
[2289] | 227 | if (xmltext) { |
---|
| 228 | delete[] xmltext; |
---|
| 229 | xmltext = NULL; |
---|
| 230 | } |
---|
| 231 | |
---|
[1560] | 232 | return retVal; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | int xmlparser_3_0 () |
---|
[1528] | 236 | { |
---|
[1560] | 237 | const char *desc = "test getting xml value"; |
---|
| 238 | const char *testname = "xmlparser_3_0"; |
---|
| 239 | const char *infile = "xmlparser_2_0_in.xml"; |
---|
| 240 | int retVal = 0; |
---|
[1528] | 241 | |
---|
[1560] | 242 | const char *xmltext = NULL; |
---|
| 243 | |
---|
| 244 | readFile(infile,&xmltext); |
---|
| 245 | |
---|
| 246 | const char *expected = "0eV"; |
---|
| 247 | |
---|
| 248 | const char *received = NULL; |
---|
| 249 | |
---|
| 250 | Rp_ParserXml *p = NULL; |
---|
| 251 | |
---|
| 252 | p = Rp_ParserXmlCreate(); |
---|
| 253 | |
---|
| 254 | Rp_ParserXmlParse(p, xmltext); |
---|
| 255 | |
---|
| 256 | received = Rp_ParserXmlGet(p,"input.number(Ef).current"); |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | if (strcmp(expected,received) != 0) { |
---|
| 260 | printf("Error: %s\n", testname); |
---|
| 261 | printf("\t%s\n", desc); |
---|
| 262 | printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected); |
---|
| 263 | printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received); |
---|
| 264 | retVal = 1; |
---|
[1528] | 265 | } |
---|
| 266 | |
---|
[1560] | 267 | if (p) { |
---|
| 268 | Rp_ParserXmlDestroy(&p); |
---|
[1528] | 269 | } |
---|
| 270 | |
---|
[2289] | 271 | if (xmltext) { |
---|
| 272 | delete[] xmltext; |
---|
| 273 | xmltext = NULL; |
---|
| 274 | } |
---|
| 275 | |
---|
[1560] | 276 | return retVal; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | int xmlparser_4_0 () |
---|
| 280 | { |
---|
| 281 | const char *desc = "test putting xml value"; |
---|
| 282 | const char *testname = "xmlparser_4_0"; |
---|
| 283 | const char *infile = "xmlparser_2_0_in.xml"; |
---|
| 284 | int retVal = 0; |
---|
| 285 | |
---|
| 286 | const char *xmltext = NULL; |
---|
| 287 | |
---|
| 288 | readFile(infile,&xmltext); |
---|
| 289 | |
---|
| 290 | const char *expected = "hi mom"; |
---|
| 291 | |
---|
| 292 | const char *received = NULL; |
---|
| 293 | |
---|
| 294 | Rp_ParserXml *p = NULL; |
---|
| 295 | |
---|
| 296 | p = Rp_ParserXmlCreate(); |
---|
| 297 | |
---|
| 298 | Rp_ParserXmlParse(p, xmltext); |
---|
| 299 | |
---|
| 300 | Rp_ParserXmlPut(p,"input.number(gg).current","hi mom",0); |
---|
| 301 | received = Rp_ParserXmlGet(p,"input.number(gg).current"); |
---|
| 302 | |
---|
| 303 | if (strcmp(expected,received) != 0) { |
---|
| 304 | printf("Error: %s\n", testname); |
---|
| 305 | printf("\t%s\n", desc); |
---|
| 306 | printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected); |
---|
| 307 | printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received); |
---|
| 308 | retVal = 1; |
---|
[1528] | 309 | } |
---|
| 310 | |
---|
[1560] | 311 | if (p) { |
---|
| 312 | Rp_ParserXmlDestroy(&p); |
---|
[1528] | 313 | } |
---|
| 314 | |
---|
[2289] | 315 | if (xmltext) { |
---|
| 316 | delete[] xmltext; |
---|
| 317 | xmltext = NULL; |
---|
| 318 | } |
---|
| 319 | |
---|
[1560] | 320 | return retVal; |
---|
[1528] | 321 | } |
---|
| 322 | |
---|
[1560] | 323 | int xmlparser_5_0 () |
---|
[1528] | 324 | { |
---|
[1560] | 325 | const char *desc = "test printing xml with degrees of presets"; |
---|
| 326 | const char *testname = "xmlparser_5_0"; |
---|
| 327 | const char *infile = "xmlparser_5_0_in.xml"; |
---|
| 328 | int retVal = 0; |
---|
[1528] | 329 | |
---|
[1560] | 330 | const char *xmltext = NULL; |
---|
[1528] | 331 | |
---|
[1560] | 332 | readFile(infile,&xmltext); |
---|
[1528] | 333 | |
---|
[1560] | 334 | const char *expected = "\ |
---|
| 335 | run.tool.about Press Simulate to view results.\n\ |
---|
| 336 | run.tool.command tclsh @tool/fermi.tcl @driver\n\ |
---|
| 337 | run.input.number(Ef).about.label Fermi Level\n\ |
---|
| 338 | run.input.number(Ef).about.description Energy at center of distribution.\n\ |
---|
| 339 | run.input.number(Ef).units eV\n\ |
---|
| 340 | run.input.number(Ef).min -10eV\n\ |
---|
| 341 | run.input.number(Ef).max 10eV\n\ |
---|
| 342 | run.input.number(Ef).default 0eV\n\ |
---|
[1566] | 343 | run.input.number(Ef).current 0eV\n\ |
---|
| 344 | run.input.number(Ef).preset.value 300K\n\ |
---|
| 345 | run.input.number(Ef).preset.label 300K (room temperature)\n\ |
---|
| 346 | run.input.number(Ef).preset.value 77K\n\ |
---|
| 347 | run.input.number(Ef).preset.label 77K (liquid nitrogen)\n\ |
---|
| 348 | run.input.number(Ef).preset.value 4.2K\n\ |
---|
| 349 | run.input.number(Ef).preset.label 4.2K (liquid helium)\n\ |
---|
| 350 | "; |
---|
[1528] | 351 | |
---|
[1560] | 352 | const char *received = NULL; |
---|
[1528] | 353 | |
---|
[1560] | 354 | Rp_ParserXml *p = NULL; |
---|
[1528] | 355 | |
---|
[1560] | 356 | p = Rp_ParserXmlCreate(); |
---|
| 357 | |
---|
| 358 | Rp_ParserXmlParse(p, xmltext); |
---|
| 359 | |
---|
| 360 | received = Rp_ParserXmlPathVal(p); |
---|
| 361 | |
---|
| 362 | |
---|
| 363 | if (strcmp(expected,received) != 0) { |
---|
| 364 | printf("Error: %s\n", testname); |
---|
| 365 | printf("\t%s\n", desc); |
---|
| 366 | printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected); |
---|
| 367 | printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received); |
---|
| 368 | retVal = 1; |
---|
[1528] | 369 | } |
---|
| 370 | |
---|
[1560] | 371 | if (p) { |
---|
| 372 | Rp_ParserXmlDestroy(&p); |
---|
[1528] | 373 | } |
---|
| 374 | |
---|
[2289] | 375 | if (xmltext) { |
---|
| 376 | delete[] xmltext; |
---|
| 377 | xmltext = NULL; |
---|
| 378 | } |
---|
| 379 | |
---|
[1560] | 380 | return retVal; |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | // FIXME: test what happens when parser sees self closing tag <tag/> |
---|
| 384 | // FIXME: test get function |
---|
| 385 | // FIXME: test put function |
---|
[1566] | 386 | // FIXME: test putf function |
---|
| 387 | // FIXME: test appendf function |
---|
[1560] | 388 | // FIXME: look into why Rp_ParserXmlPathVal hits some nodes twice in gdb |
---|
| 389 | |
---|
| 390 | int main() |
---|
| 391 | { |
---|
| 392 | xmlparser_0_0(); |
---|
| 393 | xmlparser_1_0(); |
---|
[1581] | 394 | xmlparser_1_1(); |
---|
[1560] | 395 | xmlparser_2_0(); |
---|
| 396 | xmlparser_3_0(); |
---|
| 397 | xmlparser_4_0(); |
---|
| 398 | xmlparser_5_0(); |
---|
| 399 | |
---|
[1528] | 400 | return 0; |
---|
| 401 | } |
---|