source: trunk/test/src/RpLibrary_test.cc @ 1300

Last change on this file since 1300 was 1022, checked in by gah, 16 years ago

added missing Makefiles

File size: 17.4 KB
Line 
1/**
2 *
3 * RpParser_test.c
4 *
5 * test file for the RpParser.so library based off of the scew
6 * libaray, a simple wrapper around the expat parser
7 *
8 * Copyright (c) 2004-2005  Purdue Research Foundation
9 *
10 * See the file "license.terms" for information on usage and
11 * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 */
13
14#include "RpLibrary.h"
15#include <list>
16#include <string>
17#include <iostream>
18
19using namespace std;
20
21int test_element (RpLibrary* lib, string path);
22int test_parent (RpLibrary* lib, string path);
23int test_get (RpLibrary* lib, string path);
24int test_getString (RpLibrary* lib, string path);
25int test_getDouble (RpLibrary* lib, string path);
26int test_getInt (RpLibrary* lib, string path);
27int test_getBool (RpLibrary* lib, string path);
28int test_putObj (RpLibrary* fromLib, string fromPath,
29                 RpLibrary* toLib, string toPath);
30int test_copy (RpLibrary* fromLib, string fromPath,
31               RpLibrary* toLib, string toPath);
32int test_remove (RpLibrary* lib, string path);
33
34int test_element (RpLibrary* lib, string path)
35{
36    int retVal = 1;
37    RpLibrary* searchEle = lib->element(path);
38
39    cout << "TESTING ELEMENT: path = " << path << endl;
40
41    if (!searchEle) {
42        cout << "searchEle is NULL" << endl;
43        retVal = 1;
44    }
45    else {
46        cout << "searchEle path = :" << searchEle->nodePath() << ":" << endl;
47        cout << "searchEle comp = :" << searchEle->nodeComp() << ":" << endl;
48        cout << "searchEle   id = :" << searchEle->nodeId()   << ":" << endl;
49        cout << "searchEle type = :" << searchEle->nodeType() << ":" << endl;
50        retVal = 0;
51    }
52
53    return retVal;
54}
55
56int test_parent (RpLibrary* lib, string path)
57{
58    int retVal = 1;
59    RpLibrary* searchEle = lib->parent(path);
60
61    cout << "TESTING PARENT: path = " << path << endl;
62
63    if (!searchEle) {
64        cout << "searchEle is NULL" << endl;
65        retVal = 1;
66    } else {
67        cout << "searchParent path = :" << searchEle->nodePath() << ":" << endl;
68        cout << "searchParent comp = :" << searchEle->nodeComp() << ":" << endl;
69        cout << "searchParent   id = :" << searchEle->nodeId()   << ":" << endl;
70        cout << "searchParent type = :" << searchEle->nodeType() << ":" << endl;
71        retVal = 0;
72    }
73
74    return retVal;
75}
76
77int test_get (RpLibrary* lib, string path)
78{
79    int retVal = 1;
80    string searchVal = lib->get(path);
81
82    cout << "TESTING GET       : path = " << path << endl;
83
84    if (searchVal.empty()) {
85        cout << "searchVal is EMPTY STRING" << endl;
86        retVal = 1;
87    }
88    else {
89        cout << "searchVal = :" << searchVal << ":" << endl;
90        retVal = 0;
91    }
92
93    return retVal;
94}
95
96int test_getString (RpLibrary* lib, string path)
97{
98    int retVal = 1;
99    string searchVal = lib->getString(path);
100
101    cout << "TESTING GET String: path = " << path << endl;
102
103    if (searchVal.empty()) {
104        cout << "searchVal is EMPTY STRING" << endl;
105        retVal = 1;
106    }
107    else {
108        cout << "searchVal = :" << searchVal << ":" << endl;
109        retVal = 0;
110    }
111
112    return retVal;
113}
114
115int test_getDouble (RpLibrary* lib, string path)
116{
117    int retVal = 1;
118    double searchVal = lib->getDouble(path);
119
120    cout << "TESTING GET Double: path = " << path << endl;
121
122    cout << "searchVal = :" << searchVal << ":" << endl;
123    retVal = 0;
124
125    return retVal;
126}
127
128int test_getInt (RpLibrary* lib, string path)
129{
130    int retVal = 1;
131    int searchVal = lib->getInt(path);
132
133    cout << "TESTING GET INT: path = " << path << endl;
134
135    cout << "searchVal = :" << searchVal << ":" << endl;
136    retVal = 0;
137
138    return retVal;
139}
140
141int
142test_getBool (RpLibrary* lib, string path)
143{
144    int retVal = 1;
145    bool searchVal = lib->getBool(path);
146
147    cout << "TESTING GET Bool: path = " << path << endl;
148
149    cout << "searchVal = :" << searchVal << ":" << endl;
150    retVal = 0;
151
152    return retVal;
153}
154
155int
156test_putObj (   RpLibrary* fromLib, string fromPath,
157                    RpLibrary* toLib, string toPath)
158{
159    int retVal = 1;
160    RpLibrary* fromEle = fromLib->element(fromPath);
161    RpLibrary* foundEle = NULL;
162    string newNodeName = "";
163
164    cout << "TESTING PUT Object:" << endl;
165    cout << "fromPath = " << fromPath << endl;
166    cout << "toPath = " << toPath << endl;
167
168    // put the element into the new library/xmltree
169    toLib->put(toPath,fromEle);
170
171    // check the result
172    // create the name of where the element should be
173    newNodeName = toPath + "." + fromEle->nodeComp();
174    // grab the element
175    if ((foundEle = toLib->element(newNodeName))){
176        cout << "SUCCESS: foundEle was found" << endl;
177        retVal = 0;
178    }
179
180    return retVal;
181}
182
183int test_copy (   RpLibrary* fromLib, string fromPath,
184                    RpLibrary* toLib, string toPath)
185{
186    int retVal = 1;
187    RpLibrary* fromEle = fromLib->element(fromPath);
188    RpLibrary* foundEle = NULL;
189    string newNodeName = "";
190
191    cout << "TESTING COPY:" << endl;
192    cout << "fromPath = " << fromPath << endl;
193    cout << "toPath = " << toPath << endl;
194
195    // put the element into the new library/xmltree
196    toLib->copy(toPath,fromPath);
197
198    // check the result
199    // create the name of where the element should be
200    newNodeName = toPath + "." + fromEle->nodeComp();
201    // grab the element
202    if ((foundEle = toLib->element(newNodeName))){
203        cout << "SUCCESS: foundEle was found" << endl;
204        retVal = 0;
205    }
206    else {
207        cout << "FAILURE: foundEle WAS NOT found" << endl;
208    }
209
210    return retVal;
211}
212
213int test_remove (RpLibrary* lib, string path)
214{
215    RpLibrary* getRslt = NULL;
216    int retVal = 1;
217    cout << "TESTING REMOVE:" << endl;
218    // cout << "lib's xml:" << endl << lib->xml() << endl;
219    if (lib) {
220        lib->remove(path);
221        if (lib) {
222            getRslt = lib->element(path);
223            if (getRslt) {
224                cout << "FAILURE: " << path << " not removed" << endl;
225                cout << "lib's xml:" << endl << lib->xml() << endl;
226            }
227            else {
228                cout << "SUCCESS: " << path << " removed" << endl;
229                retVal = 0;
230            }
231        }
232    }
233
234    return retVal;
235}
236
237int test_entities (RpLibrary* lib)
238{
239    list<string>::iterator iter;
240    list<string> elist = lib->entities();
241
242    iter = elist.begin();
243
244    cout << "TESTING ENTITIES BEGIN" << endl;
245    cout << "path = "<< lib->nodePath() << endl;
246
247    while (iter != elist.end() ) {
248        cout << *iter << endl;
249        iter++;
250    }
251
252    cout << "TESTING ENTITIES END" << endl;
253
254    return 0;
255}
256
257int test_diff (RpLibrary* lib1, RpLibrary* lib2)
258{
259    list<string>::iterator iter;
260    list<string> elist;
261
262    elist = lib1->diff(lib2,"input");
263
264    iter = elist.begin();
265
266    cout << "TESTING DIFF BEGIN" << endl;
267
268    int count = 0;
269    while (iter != elist.end() ) {
270        cout << *iter << " ";
271        iter++;
272        count++;
273        if (count == 4) {
274            cout << endl;
275            count = 0;
276        }
277
278    }
279
280    cout << "TESTING DIFF END" << endl;
281
282    return 0;
283}
284
285/*
286int test_children (RpLibrary* lib, string path, string type )
287{
288    int retVal = 1;
289    int childNum = -1;
290
291    cout << "TESTING CHILDREN: path = " << path << endl;
292    RpLibrary** searchEle = lib->children(path,"",type);
293
294    if (!searchEle || !*searchEle) {
295        cout << "searchEle is NULL -> NO CHILDREN" << endl;
296        retVal = 1;
297    }
298    else {
299
300        while (searchEle[++childNum]) {
301            cout << "searchEle comp = :" << searchEle[childNum]->nodeComp()
302                << ":" << endl;
303            cout << "searchEle   id = :" << searchEle[childNum]->nodeId()   
304                << ":" << endl;
305            cout << "searchEle type = :" << searchEle[childNum]->nodeType()
306                << ":" << endl;
307            delete (searchEle[childNum]);
308            searchEle[childNum] = NULL;
309        }
310
311        retVal = 0;
312
313    }
314
315    delete[] searchEle;
316
317    return retVal;
318}
319*/
320
321int test_children (RpLibrary* lib, string path, string type )
322{
323    int retVal = 1;
324    RpLibrary* childEle = NULL;
325
326    cout << "TESTING CHILDREN: path = " << path << endl;
327
328    while ( (childEle = lib->children(path,childEle,type)) ) {
329
330        cout << "childEle path = :" << childEle->nodePath()
331            << ":" << endl;
332        cout << "childEle comp = :" << childEle->nodeComp()
333            << ":" << endl;
334        cout << "childEle   id = :" << childEle->nodeId()   
335            << ":" << endl;
336        cout << "childEle type = :" << childEle->nodeType()
337            << ":" << endl;
338
339        retVal = 0;
340
341    }
342
343    if (!childEle) {
344        cout << "No more children" << endl;
345    }
346
347    return retVal;
348}
349
350int
351main(int argc, char** argv)
352{
353    RpLibrary* lib = NULL;
354    RpLibrary lib2;
355
356    if (argc < 2)
357    {
358        printf("usage: RpLibrary_test infile.xml\n");
359        return EXIT_FAILURE;
360    }
361
362    lib = new RpLibrary(string(argv[1]));
363
364    test_element(lib,"");
365    test_element(lib,"input.number(min)");
366    test_element(lib,"input.number(max)");
367    test_element(lib,"output.curve(result)");
368
369    test_parent(lib,"input.number(min).current");
370    test_parent(lib,"input.number(max)");
371    test_parent(lib,"output");
372
373    test_getString(lib, "input.number(min).default");
374    test_getString(lib, "input.(min).current");
375    test_getString(lib, "input.number(max).current");
376    test_getString(lib, "output.curve.about.label");
377
378    lib->put("input.number(test_one).default", "3000");
379    test_get(lib, "input.number(test_one).default");
380    lib->put("input.number(test).default", "1000");
381    test_getString(lib, "input.number(test).default");
382    lib->put("input.number(test).current", 2000);
383    test_getDouble(lib, "input.number(test).current");
384
385    lib->put("input.number(test).current", 5);
386    test_getInt(lib, "input.number(test).current");
387    lib->put("input.number(test).current", 50);
388    test_getInt(lib, "input.number(test).current");
389    lib->put("input.number(test).current", 53.6);
390    test_getInt(lib, "input.number(test).current");
391    lib->put("input.number(test).current", 52.94);
392    test_getInt(lib, "input.number(test).current");
393
394    lib->put("input.boolean(boolval).current", "yes");
395    test_getBool(lib, "input.boolean(boolval).current");
396    lib->put("input.boolean(boolval).current", "no");
397    test_getBool(lib, "input.boolean(boolval).current");
398    lib->put("input.boolean(boolval).current", "true");
399    test_getBool(lib, "input.boolean(boolval).current");
400    lib->put("input.boolean(boolval).current", "false");
401    test_getBool(lib, "input.boolean(boolval).current");
402    lib->put("input.boolean(boolval).current", "on");
403    test_getBool(lib, "input.boolean(boolval).current");
404    lib->put("input.boolean(boolval).current", "off");
405    test_getBool(lib, "input.boolean(boolval).current");
406    lib->put("input.boolean(boolval).current", "1");
407    test_getBool(lib, "input.boolean(boolval).current");
408    lib->put("input.boolean(boolval).current", "0");
409    test_getBool(lib, "input.boolean(boolval).current");
410    lib->put("input.boolean(boolval).current", "ye");
411    test_getBool(lib, "input.boolean(boolval).current");
412    lib->put("input.boolean(boolval).current", "n");
413    test_getBool(lib, "input.boolean(boolval).current");
414    lib->put("input.boolean(boolval).current", "tr");
415    test_getBool(lib, "input.boolean(boolval).current");
416    lib->put("input.boolean(boolval).current", "fa");
417    test_getBool(lib, "input.boolean(boolval).current");
418    lib->put("input.boolean(boolval).current", "on");
419    test_getBool(lib, "input.boolean(boolval).current");
420    lib->put("input.boolean(boolval).current", "of");
421    test_getBool(lib, "input.boolean(boolval).current");
422
423    test_putObj(lib, "input.number(max)", lib, "input.test");
424    test_copy(lib, "input.number(min)", lib, "input.test.number(min)");
425
426    test_children(lib,"","");
427    test_children(lib,"input.number(test)","");
428    test_children(lib,"input","");
429    test_children(lib,"input","number");
430
431    cout << lib->xml() << endl;
432
433    RpLibrary* remEle = lib->element("input.test.(min)");
434    test_remove(remEle,"");
435    remEle->remove();
436    test_remove(lib, "input.test.(min)");
437    test_remove(lib, "input.test.(max)");
438    cout << lib->xml() << endl;
439
440    RpLibrary* libInput = lib->element("input");
441    test_entities(lib);
442    test_entities(libInput);
443
444    /*
445    list<string> l1 = lib->value("input.number(min)");
446    list<string> l2 = lib->value("input.number(max)");
447    cout << "l1 = " << l1.front() << " " << l1.back() << endl;
448    cout << "l2 = " << l2.front() << " " << l2.back() << endl;
449    */
450
451    RpLibrary* dlib1 = new RpLibrary(string(argv[1]));
452    RpLibrary* dlib2 = new RpLibrary(string(argv[1]));
453    test_diff(dlib1,dlib2);
454    cout << "dlib1-------------------------------------" << endl;
455    cout << dlib1->xml() << endl;
456    cout << "dlib2-------------------------------------" << endl;
457    cout << dlib1->xml() << endl;
458    dlib1 = lib;
459    test_diff(dlib1,dlib2);
460    cout << "dlib1-------------------------------------" << endl;
461    cout << dlib1->xml() << endl;
462    cout << "dlib2-------------------------------------" << endl;
463    cout << dlib2->xml() << endl;
464
465
466
467    // test copy assignment operator
468    lib2 = *lib;
469
470    delete lib;
471
472    lib2.put("input.output.curve(curve1).xy.default", "1e-4 0.0\n");
473    lib2.put("input.output.curve(curve1).xy.default", "1e-2 1.0\n","",1);
474    lib2.put("input.output.curve(curve1).xy.default", "1e-30 2.0\n","",1);
475    lib2.put("input.output.curve(curve1).xy.default", "1e+4 3.0\n","",1);
476    lib2.put("input.output.curve(curve1).xy.default", "1.7e-4 4.0\n","",1);
477    lib2.put("input.output.curve(curve1).xy.default", "15e-4 5.0\n","",1);
478
479
480    // test copy constructor
481    RpLibrary lib3 = lib2;
482
483    lib2.put("input.output.curve(curve2).xy.current", "1e-4 0.0\n");
484    lib2.put("input.output.curve(curve2).xy.current", "1e-2 1.0\n","",1);
485    lib2.put("input.output.curve(curve2).xy.current", "1e-30 2.0\n","",1);
486    lib2.put("input.output.curve(curve2).xy.current", "1e+4 3.0\n","",1);
487    lib2.put("input.output.curve(curve2).xy.current", "1.7e-4 4.0\n","",1);
488    lib2.put("input.output.curve(curve2).xy.current", "15e-4 5.0\n","",1);
489
490
491    cout << "//////////////////// LIB 2 ////////////////////" << endl;
492    cout << lib2.xml() << endl;
493    cout << "//////////////////// LIB 3 ////////////////////" << endl;
494    cout << lib3.xml() << endl;
495
496    lib2.result();
497
498
499    cout << "testing with &lt;number&gt;" << endl;
500    lib2.put("input.dsk.test", "slkdjfs slkdfj lks &lt;number&gt; sdlkfj sdlkjf","",1);
501    cout << lib2.xml() << endl;
502    cout << "testing get &lt;number&gt;" << endl;
503    cout << lib2.get("input.dsk.test") << endl;
504    cout << "testing with <number>" << endl;
505    lib2.put("input.dsk.test2", "slkdjfs slkdfj lks <number> sdlkfj sdlkjf","",1);
506    cout << lib2.xml() << endl;
507
508    /*
509    RpLibrary* childEle  = NULL;
510    RpLibrary* childEle2 = NULL;
511    while ( (childEle = lib2.children("input",childEle)) ) {
512        cout << "childEle path = :" << childEle->nodePath() << ":" << endl;
513        while ( (childEle2 = childEle->children("",childEle2)) ) {
514            cout << "childEle2 path = :" << childEle2->nodePath() << ":" << endl;
515        }
516        childEle2 = NULL;
517        if (!childEle) {
518            cout << "childEle Not null" << endl;
519        }
520    }
521    */
522
523    return 0;
524}
525
526    /*
527    put(lib, "element3.sub_element(value3)", "put_element3", NULL, 1);
528   
529    search_element = _find(lib->element,"element3.sub_element(value3)",0);
530    printf("search_element name = %s\n", scew_element_name(search_element));
531
532    // search_element2 = element(root->element, "element3.sub_element(value3)", "object");
533
534    search_element2 = element(lib, "element3.sub_element(value3)", OBJECT);
535    printf("search_element2 name = %s\n", scew_element_name(search_element2->element));
536    freeRpLibrary(&search_element2);
537
538    search_element2 = element(lib, "element3.sub_element(value3)", COMPONENT);
539    printf("search_element2 comp = %s\n", search_element2->stringVal);
540    freeRpLibrary(&search_element2);
541   
542    search_element2 = element(lib, "element3.sub_element(value3)", ID);
543    printf("search_element2 id = %s\n", search_element2->stringVal);
544    freeRpLibrary(&search_element2);
545   
546    search_element2 = element(lib, "element3.sub_element(value3)", TYPE);
547    printf("search_element2 type = %s\n", search_element2->xml_char);
548    freeRpLibrary(&search_element2);
549   
550   
551    get_element = get(lib, "element");
552    printf("get element's contents = %s\n", get_element->xml_char);
553    freeRpLibrary(&get_element);
554
555
556       
557    put(lib, "element4.sub_element(value)", "put1_element", NULL, 0);
558    put(lib, "element4.sub_element(value2)", "put2_element", NULL, 0);
559    put(lib, "element4.sub_element(value3)", "put3_element", NULL, 0);
560    put(lib, "element4.sub_element(value4)", "put4_element", NULL, 0);
561    get_element = get(lib, "element4.sub_element(value)");
562    printf("put1 element's contents = %s\n", get_element->xml_char);
563    get_element = get(lib, "element4.sub_element(value2)");
564    printf("put2 element's contents = %s\n", get_element->xml_char);
565    get_element = get(lib, "element4.sub_element(value3)");
566    printf("put3 element's contents = %s\n", get_element->xml_char);
567    get_element = get(lib, "element4.sub_element(value4)");
568    printf("put4 element's contents = %s\n", get_element->xml_char);
569
570    */
571    /**
572     * Save an XML tree to a file.
573     */
574    /*
575    if (!scew_writer_tree_file(lib->tree, argv[2]))
576    {
577        printf("Unable to create %s\n", argv[2]);
578        return EXIT_FAILURE;
579    }
580    */
581
582
583    /*
584   
585    freeRpLibrary(&search_element2);
586    freeRpLibrary(&get_element);
587    freeRpLibrary(&lib);
588
589    if (tagName) { free (tagName); }
590    if (id) { free(id); }
591    */
Note: See TracBrowser for help on using the repository browser.