source: trunk/examples/objects/tree/tree.c @ 3471

Last change on this file since 3471 was 1566, checked in by dkearney, 15 years ago

updates to Rappture::Library and Rappture::Number objects to demo how the library should store objects and generate the xml for those objects upon request. opting for configure() and dump() functions for objects. the object should know how to configure it self from a string of xml, tree object or (in the future) blob of hdf5. added some rudimentary examples of using the library object.

File size: 5.3 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include "RpTree.h"
4
5int tree_0_0 ()
6{
7    const char *desc = "test creation of tree";
8    const char *testname = "tree_0_0";
9
10    const char *treeName = "mytree";
11    const char *expected = "mytree";
12    const char *received = NULL;
13
14    Rp_Tree t;
15
16    Rp_TreeCreate(treeName,&t);
17    received = Rp_TreeName(t);
18
19    if (strcmp(expected,received) != 0) {
20        printf("Error: %s\n", testname);
21        printf("\t%s\n", desc);
22        printf("\texpected \"%s\"\n",expected);
23        printf("\treceived \"%s\"\n",received);
24        return 1;
25    }
26
27    return 0;
28}
29
30int tree_1_0 ()
31{
32    const char *desc = "test creation of tree node";
33    const char *testname = "tree_1_0";
34
35    const char *treeName = "mytree";
36    const char *nodeName = "mynode";
37    const char *expected = "mynode";
38    const char *received = NULL;
39
40    Rp_Tree t;
41    Rp_TreeNode n = NULL;
42
43    Rp_TreeCreate(treeName,&t);
44    n = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), nodeName, 0);
45    received = Rp_TreeNodeLabel(n);
46
47    if (strcmp(expected,received) != 0) {
48        printf("Error: %s\n", testname);
49        printf("\t%s\n", desc);
50        printf("\texpected \"%s\"\n",expected);
51        printf("\treceived \"%s\"\n",received);
52        return 1;
53    }
54
55    return 0;
56}
57
58int tree_2_0 ()
59{
60    const char *desc = "test creation of tree node with single child";
61    const char *testname = "tree_2_0";
62
63    const char *treeName = "mytree";
64    const char *parentNodeName = "myparentnode";
65    const char *childNodeName = "mychildnode";
66    const char *expected = "mychildnode";
67    const char *received = NULL;
68
69    Rp_Tree t;
70    Rp_TreeNode parent = NULL;
71    Rp_TreeNode child = NULL;
72
73    Rp_TreeCreate(treeName,&t);
74    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
75    child = Rp_TreeCreateNode(t, parent, childNodeName, 0);
76    received = Rp_TreeNodeLabel(child);
77
78    if (strcmp(expected,received) != 0) {
79        printf("Error: %s\n", testname);
80        printf("\t%s\n", desc);
81        printf("\texpected \"%s\"\n",expected);
82        printf("\treceived \"%s\"\n",received);
83        return 1;
84    }
85
86    return 0;
87}
88
89int tree_2_1 ()
90{
91    const char *desc = "test creation of tree node with multiple children";
92    const char *testname = "tree_2_1";
93
94    const char *treeName = "mytree";
95    const char *parentNodeName = "myparentnode";
96    size_t expected = 5;
97    size_t received = 0;
98
99    Rp_Tree t;
100    Rp_TreeNode parent = NULL;
101
102    Rp_TreeCreate(treeName,&t);
103    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
104
105    Rp_TreeCreateNode(t, parent, "child1", 0);
106    Rp_TreeCreateNode(t, parent, "child2", 0);
107    Rp_TreeCreateNode(t, parent, "child3", 0);
108    Rp_TreeCreateNode(t, parent, "child4", 0);
109    Rp_TreeCreateNode(t, parent, "child5", 0);
110
111    received = Rp_TreeNodeDegree(parent);
112
113    if (expected != received) {
114        printf("Error: %s\n", testname);
115        printf("\t%s\n", desc);
116        printf("\texpected \"%zu\"\n",expected);
117        printf("\treceived \"%zu\"\n",received);
118        return 1;
119    }
120
121    return 0;
122}
123
124int tree_2_2 ()
125{
126    const char *desc = "test search for child node";
127    const char *testname = "tree_2_2";
128
129    const char *treeName = "mytree";
130    const char *parentNodeName = "myparentnode";
131    const char *childNodeName = "mychildnode";
132    const char *expected = "mychildnode";
133    const char *received = NULL;
134
135    Rp_Tree t;
136    Rp_TreeNode parent = NULL;
137    Rp_TreeNode foundChild = NULL;
138
139    Rp_TreeCreate(treeName,&t);
140    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
141
142    Rp_TreeCreateNode(t, parent, "child1", 0);
143    Rp_TreeCreateNode(t, parent, "child2", 0);
144    Rp_TreeCreateNode(t, parent, "child3", 0);
145    Rp_TreeCreateNode(t, parent, childNodeName, 0);
146    Rp_TreeCreateNode(t, parent, "child4", 0);
147    Rp_TreeCreateNode(t, parent, "child5", 0);
148
149    foundChild = Rp_TreeFindChild(parent,childNodeName);
150    received = Rp_TreeNodeLabel(foundChild);
151
152    if (strcmp(expected,received) != 0) {
153        printf("Error: %s\n", testname);
154        printf("\t%s\n", desc);
155        printf("\texpected \"%s\"\n",expected);
156        printf("\treceived \"%s\"\n",received);
157        return 1;
158    }
159
160    return 0;
161}
162
163int tree_3_0 ()
164{
165    const char *desc = "test adding node with string value";
166    const char *testname = "tree_3_0";
167
168    const char *treeName = "mytree";
169    const char *parentNodeName = "myparentnode";
170    const char *childNodeName = "mychildnode";
171    const char *childNodeValue = "quick brown fox";
172    const char *expected = "quick brown fox";
173    const char *received = NULL;
174
175    Rp_Tree t;
176    Rp_TreeNode parent = NULL;
177    Rp_TreeNode child = NULL;
178
179    Rp_TreeCreate(treeName,&t);
180    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
181
182    child = Rp_TreeCreateNode(t, parent, childNodeName, 0);
183    Rp_TreeSetValue(t,child,"value",(void *)childNodeValue);
184
185    Rp_TreeGetValue(t,child,"value",(void **)&received);
186
187    if (strcmp(expected,received) != 0) {
188        printf("Error: %s\n", testname);
189        printf("\t%s\n", desc);
190        printf("\texpected \"%s\"\n",expected);
191        printf("\treceived \"%s\"\n",received);
192        return 1;
193    }
194
195    return 0;
196}
197
198int main()
199{
200    tree_0_0();
201    tree_1_0();
202    tree_2_0();
203    tree_2_1();
204    tree_2_2();
205    tree_3_0();
206
207    // add test case where two children have the same name but different ids
208
209    return 0;
210}
Note: See TracBrowser for help on using the repository browser.