ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
mtree_spec.h
Go to the documentation of this file.
1 
7 #include "../src/ceptr.h"
8 #include "../src/receptor.h"
9 #include "http_example.h"
10 
11 /*
12 #define TT(t) ((t) && (*((int *)t) == matrixImpl))
13 #define TE(t,te,me) ((TT(t)) ? (me):(te))
14 #define TNEW(impl,parent,symbol,surface,size) ((impl == matrixImpl) ? (GT)_m_new(parent,symbol,surface,size) : (GT)_t_new(parent,symbol,surface,size))
15 #define TFREE(t) TE(t,_t_free((T*)t),_m_free((M*)t))
16 #define TSIZE(t) TE(t,_t_size((T *)t),_m_size((M *)t))
17 #define TKIDS(t) TE(t,_t_children((T *)t),_m_children((M *)t))
18 #define TSURFACE(t) TE(t,_t_surface((T *)t),_m_surface((M *)t))
19 
20 void testCreateTreeNodesT() {
21  uint32_t impl;
22  for(impl=FIRST_TREE_IMPL_TYPE;impl != 0;impl++) {
23  GT t = TNEW(impl,0,TEST_STR_SYMBOL,"hello",6);
24  spec_is_long_equal(TSIZE(t),(size_t)6);
25  spec_is_equal(TKIDS(t),0);
26  spec_is_str_equal((char *)TSURFACE(t),"hello");
27 
28  TFREE(t);
29  }
30 }
31 */
32 
33 void mtd(H h) {
34  H hh;
35  int i;
36  printf("\n");
37  for(i=0;i<h.m->levels;i++) {
38  hh.m = h.m;
39  hh.a.l = i;
40  L *l = _GET_LEVEL(h,i);
41  int j;
42  printf("%d(%d):",i,l->nodes);
43  for(j=0;j<l->nodes;j++) {
44  hh.a.i = j;
45  N *n = _GET_NODE(h,l,j);
46  if (!(n->flags & TFLAG_DELETED))
47  printf("%s:%d, ",(char *)_m_surface(hh),n->parenti);
48  else
49  printf("X, ");
50  }
51  printf("\n");
52  }
53 }
54 
55 
56 void _walkfn(H h,N *n,void *data,MwalkState *state,Maddr ap) {
57  char *s = (char *)data;
58  s += strlen(s);
59  // sprintf(s,(char *)"%d.%d:%s, ",ac.l,ac.i,(char *) (on->flags &TFLAG_ALLOCATED?on->surface:&on->surface));
60  sprintf(s,(char *)"%d.%d, ",h.a.l,h.a.i);
61 }
62 void testCreateTreeNodesM(){
63 
64  H h,h1,h11,h2,h21,h22,h3;
65 
66  // test adding a new root
67  h = _m_new(null_H,TEST_STR_SYMBOL,"hello",6);
68  spec_is_equal(h.a.l,0);
69  spec_is_equal(h.a.i,0);
70  spec_is_equal(h.m->levels,1);
71  spec_is_long_equal(_m_size(h),(size_t)6);
72  spec_is_equal(_m_children(h),0);
73  spec_is_str_equal((char *)_m_surface(h),"hello");
74  spec_is_maddr_equal(_m_parent(h),null_H.a);
75  spec_is_maddr_equal(_m_child(h,1),null_H.a);
76  spec_is_maddr_equal(_m_child(h,NULL_ADDR),null_H.a);
77  spec_is_true(semeq(_m_symbol(h),TEST_STR_SYMBOL));
78 
79  // test adding first child
80  h1 = _m_new(h,TEST_STR_SYMBOL,"t1",3);
81  spec_is_equal(h.m->levels,2);
82  spec_is_str_equal((char *)_m_surface(h1),"t1");
83  spec_is_maddr_equal(_m_parent(h1),h.a);
84  spec_is_equal(_m_children(h),1);
85  spec_is_maddr_equal(_m_child(h,1),h1.a);
86  spec_is_maddr_equal(_m_child(h,NULL_ADDR),h1.a);
87  spec_is_maddr_equal(_m_child(h1,NULL_ADDR),null_H.a);
88 
89  // test adding second child
90  h2 = _m_new(h,TEST_STR_SYMBOL,"t2",3);
91  spec_is_equal(h.m->levels,2);
92  spec_is_str_equal((char *)_m_surface(h2),"t2");
93  spec_is_maddr_equal(_m_parent(h2),h.a);
94  spec_is_equal(_m_children(h),2);
95  spec_is_maddr_equal(_m_child(h,2),h2.a);
96  spec_is_maddr_equal(_m_child(h,NULL_ADDR),h2.a);
97  spec_is_equal(_m_children(h2),0);
98 
99  // test adding child to second child
100  h21 = _m_new(h2,TEST_STR_SYMBOL,"t21",4);
101  spec_is_equal(h21.a.l,2);
102  spec_is_equal(h21.a.i,0);
103  spec_is_equal(h.m->levels,3);
104  spec_is_str_equal((char *)_m_surface(h21),"t21");
105  spec_is_maddr_equal(_m_parent(h21),h2.a);
106  spec_is_equal(_m_children(h2),1);
107  spec_is_maddr_equal(_m_child(h2,1),h21.a);
108  spec_is_maddr_equal(_m_child(h2,NULL_ADDR),h21.a);
109 
110  // now adding in a child to the first child (i.e. out of order)
111  spec_is_maddr_equal(_m_child(h1,NULL_ADDR),null_H.a);
112  h11 = _m_new(h1,TEST_STR_SYMBOL,"t11",4);
113  spec_is_equal(h11.a.l,2);
114  spec_is_equal(h11.a.i,1); //should be inserted after the t21 node
115  spec_is_equal(h.m->levels,3);
116  spec_is_str_equal((char *)_m_surface(h11),"t11");
117  spec_is_maddr_equal(_m_parent(h11),h1.a);
118  spec_is_equal(_m_children(h1),1);
119  spec_is_maddr_equal(_m_child(h1,1),h11.a);
120  spec_is_maddr_equal(_m_child(h1,NULL_ADDR),h11.a);
121 
122  // this test is here because it makes sure that things work even
123  // if nodes are inserted out of order
124  h22 = _m_new(h2,TEST_STR_SYMBOL,"t22",4);
125  spec_is_equal(h22.a.l,2);
126  spec_is_equal(h22.a.i,2); //should be inserted after the t11 node
127  spec_is_equal(h.m->levels,3);
128  spec_is_str_equal((char *)_m_surface(h22),"t22");
129  spec_is_maddr_equal(_m_parent(h22),h2.a);
130  spec_is_equal(_m_children(h2),2);
131  spec_is_maddr_equal(_m_child(h2,2),h22.a);
132  spec_is_maddr_equal(_m_child(h2,NULL_ADDR),h22.a);
133 
134  Symbol s99 = {0,0,99};
135 
136  // test adding in non allocated integer surface
137  h3 = _m_newi(h,s99,101);
138  spec_is_maddr_equal(_m_parent(h3),h.a);
139  spec_is_equal(_m_children(h),3);
140  spec_is_equal(*(int *)_m_surface(h3),101);
141 
142  // test next sibling
143  spec_is_maddr_equal(_m_next_sibling(h1),h2.a);
144  spec_is_maddr_equal(_m_next_sibling(h2),h3.a);
145  spec_is_maddr_equal(_m_next_sibling(h3),null_H.a);
146  spec_is_maddr_equal(_m_next_sibling(h),null_H.a);
147 
148  h21.a = _m_child(h2,1); // we have to do this because the h21 handle is stale
149  spec_is_maddr_equal(_m_next_sibling(h21),h22.a);
150  spec_is_maddr_equal(_m_next_sibling(h11),null_H.a);
151 
152  // test add
153  H h12 = _m_new(null_H,TEST_TREE_SYMBOL,"t12",4);
154  spec_is_str_equal((char *)_m_surface(h12),"t12");
155  _m_new(h12,TEST_STR_SYMBOL,"t121",5);
156  H h221 = _m_new(h22,TEST_STR_SYMBOL,"t221",5);
157  spec_is_equal(h221.a.l,3);
158  spec_is_equal(h221.a.i,0); //should be first and only node on third level
159  spec_is_equal(GET_LEVEL(h221)->nodes,1);
160 
161  //mtd(h);
162  char buf[2000] ={0};
163  Maddr ac = {0,0};
164 
165  _m_walk(h,_walkfn,buf);
166  spec_is_str_equal(buf,"0.0, 1.0, 2.1, 1.1, 2.0, 2.2, 3.0, 1.2, ");
167 
168  h12 = _m_add(h1,h12);
169  spec_is_equal(h12.a.l,2);
170  spec_is_equal(h12.a.i,3); //should have been appended
171  // mtd(h);
172  spec_is_equal(_m_children(h1),2);
173  spec_is_maddr_equal(_m_child(h1,2),h12.a);
174  h22.a = _m_child(h2,2);
175  spec_is_equal(h22.a.l,2);
176  spec_is_equal(h22.a.i,2); //should remain the same
177 
178  spec_is_equal(GET_LEVEL(h221)->nodes,2);
179  spec_is_str_equal((char *)_m_surface(h221),"t221");
180 
181  h221.a = _m_child(h22,1);
182  spec_is_str_equal((char *)_m_surface(h221),"t221");
183 
184  H h121;
185  h121.m = h.m;
186  h121.a = _m_child(h12,1);
187  char *xx = (char *)_m_surface(h121);
188  spec_is_str_equal(xx,"t121");
189  spec_is_equal(h121.a.l,3);
190  spec_is_equal(h121.a.i,1); //should be last because it was appended by add
191 
192 
193  //S *s = _m_serialize(h.m);
194  // writeFile("web/test1.cmt",s,size);
195  //free(s);
196 
197  buf[0] = 0;
198  _m_walk(h2,_walkfn,buf);
199  spec_is_str_equal(buf,"1.1, 2.0, 2.2, 3.0, ");
200 
201  spec_is_equal(_m_children(h),3);
202  h2 = _m_detatch(h2);
203  spec_is_maddr_equal(_m_parent(h2),null_H.a);
204  // mtd(h2);
205  // mtd(h);
206 
207  spec_is_true(h2.m != h.m);
208  spec_is_equal(_m_children(h2),2);
209  spec_is_equal(_m_children(h),2);
210  _m_free(h2);
211 
212  _m_free(h);
213 }
214 
215 void testMTreeOrthogonal() {
216  H h = _m_new(null_H,TEST_STR_SYMBOL,"hello",6);
217  H h1 = _m_newi(null_H,TEST_INT_SYMBOL,314);
218  _m_newt(h,TEST_TREE_SYMBOL,h1);
219 
220  T *t = _t_new_from_m(h);
221  spec_is_str_equal(t2s(t),"(TEST_STR_SYMBOL:hello (TEST_TREE_SYMBOL:{(TEST_INT_SYMBOL:314)}))");
222  _m_free(h);
223 
224  // now make sure converting works for orthogonal trees in the other direction too.
225 
226  h = _m_new_from_t(t);
227  _t_free(t);
228 
229  t = _t_new_from_m(h);
230  spec_is_str_equal(t2s(t),"(TEST_STR_SYMBOL:hello (TEST_TREE_SYMBOL:{(TEST_INT_SYMBOL:314)}))");
231 
232  _t_free(t);
233  _m_free(h);
234 
235 }
236 
237 void testMTreeReceptor() {
238  Receptor *r = _r_new(G_sem,TEST_RECEPTOR);
239  T *t = _t_newi(0,TEST_INT_SYMBOL,0);
240  T *tr = _t_new_receptor(t,TEST_RECEPTOR,r);
241 
242  H hr = _m_new_from_t(tr);
243 
244  T *t2 = _t_new_from_m(hr);
245  spec_is_ptr_equal(*(Receptor **)_m_surface(hr),r);
246  spec_is_ptr_equal(_t_surface(t2),r);
247 
248  spec_is_str_equal(t2s(t2),"(TEST_RECEPTOR:{(RECEPTOR_INSTANCE (INSTANCE_OF:TEST_RECEPTOR) (CONTEXT_NUM:3) (PARENT_CONTEXT_NUM:0) (RECEPTOR_STATE (FLUX (DEFAULT_ASPECT (EXPECTATIONS) (SIGNALS))) (PENDING_SIGNALS) (PENDING_RESPONSES) (CONVERSATIONS) (RECEPTOR_ELAPSED_TIME:0)))})");
249 
250  _t_free(t);
251  _t_free(t2);
252  _m_free(hr);
253 }
254 
257  T *t = _makeTestHTTPRequestTree(); // GET /groups/5/users.json?sort_by=last_name?page=2 HTTP/1.0
258  // puts(__t2s(G_sem,t,INDENT));
259  H h = _m_new_from_t(t);
260  char buf[2000] ={0};
261  Maddr ac = {0,0};
262  _m_walk(h,_walkfn,buf);
263  spec_is_str_equal(buf,"0.0, 1.0, 2.0, 2.1, 1.1, 1.2, 2.2, 3.0, 3.1, 2.3, 3.2, 3.3, 2.4, 3.4, 4.0, 5.0, 5.1, 4.1, 5.2, 5.3, ");
264 
265  //start walk from a node
266  h.a.l = 1;
267  h.a.i = 2;
268  buf[0] = 0;
269  _m_walk(h,_walkfn,buf);
270  spec_is_str_equal(buf,"1.2, 2.2, 3.0, 3.1, 2.3, 3.2, 3.3, 2.4, 3.4, 4.0, 5.0, 5.1, 4.1, 5.2, 5.3, ");
271 
272  _m_free(h);
273  _t_free(t);
275 }
276 
279  T *t = _makeTestHTTPRequestTree(); // GET /groups/5/users.json?sort_by=last_name?page=2 HTTP/1.0
280  H h = _m_new_from_t(t);
281 
282  spec_is_symbol_equal(0,_m_symbol(h),HTTP_REQUEST_LINE);
283  H hh = h;
284  hh.a.l = 1;
285  spec_is_symbol_equal(0,_m_symbol(hh),HTTP_REQUEST_VERSION);
286  hh.a.l = 5;
287  hh.a.i = 2;
288  spec_is_str_equal((char *)_m_surface(hh),"page");
289  // mtd(h);
290  // puts(__t2s(G_sem,t,INDENT));
291  T *t1 = _t_new_from_m(h);
292 
293  char buf[2000] = {0};
294  char buf1[2000] = {0};
295 
296  __t_dump(G_sem,t,0,buf);
297  __t_dump(G_sem,t1,0,buf1);
298 
299  spec_is_str_equal(buf1,buf);
300 
301  _m_free(h);
302  _t_free(t);
303  _t_free(t1);
304 
305  // test that converting run-trees works too
306 
307  T *n = _t_newr(0,ADD_INT);
308  _t_newi(n,TEST_INT_SYMBOL,99);
309  _t_newi(n,TEST_INT_SYMBOL,100);
310 
311  T *c = _t_rclone(n);
312  t = _t_new_root(RUN_TREE);
313  _t_add(t,c);
314 
315  h = _m_new_from_t(t);
316  _t_free(t);
317  t = _t_new_from_m(h);
318 
319  spec_is_str_equal(t2s(t),"(RUN_TREE (process:ADD_INT (TEST_INT_SYMBOL:99) (TEST_INT_SYMBOL:100)))");
320  //debug_enable(D_REDUCE+D_REDUCEV);
321  _p_reduce(G_sem,t);
322  debug_disable(D_REDUCE+D_REDUCEV);
323  spec_is_str_equal(t2s(t),"(RUN_TREE (TEST_INT_SYMBOL:199))");
324 
325  _m_free(h);
326  _t_free(n);
327  _t_free(t);
329 }
330 
331 void testMTreeSerialize() {
332  T *t = _makeTestHTTPRequestTree(); // GET /groups/5/users.json?sort_by=last_name?page=2 HTTP/1.0
333  H h = _m_new_from_t(t);
334 
335  S *s = _m_serialize(h.m);
336 
337  spec_is_equal(s->magic,h.m->magic);
338  spec_is_equal(s->total_size,929);
339  spec_is_equal(s->levels,h.m->levels);
340  spec_is_equal(s->level_offsets[0],0);
341  L *l = GET_LEVEL(h);
342  spec_is_equal(s->level_offsets[1], (int)SERIALIZED_LEVEL_SIZE(l));
343 
344  H h1 = _m_unserialize(s);
345  T *t1 = _t_new_from_m(h1);
346 
347  char buf[2000] = {0};
348  char buf1[2000] = {0};
349 
350  __t_dump(G_sem,t,0,buf);
351  __t_dump(G_sem,t1,0,buf1);
352 
353  spec_is_str_equal(buf1,buf);
354 
355  writeFile("web/test.cmt",s,s->total_size);
356  _m_free(h);free(s);
357 
358  h = _m_new_from_t(__sem_get_defs(G_sem,SEM_TYPE_STRUCTURE,INTERNET_CONTEXT));
359  s = _m_serialize(h.m);
360  writeFile("web/httpstructures.cmt",s,s->total_size);
361  _m_free(h);free(s);
362 
363  h = _m_new_from_t(__sem_get_defs(G_sem,SEM_TYPE_SYMBOL,INTERNET_CONTEXT));
364  s = _m_serialize(h.m);
365  writeFile("web/httpsymbols.cmt",s,s->total_size);
366  _m_free(h);free(s);
367 
368  h = _m_new_from_t(__sem_get_defs(G_sem,SEM_TYPE_STRUCTURE,SYS_CONTEXT));
369  s = _m_serialize(h.m);
370  writeFile("web/sysstructures.cmt",s,s->total_size);
371  _m_free(h);free(s);
372 
373  h = _m_new_from_t(__sem_get_defs(G_sem,SEM_TYPE_SYMBOL,SYS_CONTEXT));
374  s = _m_serialize(h.m);
375  writeFile("web/syssymbols.cmt",s,s->total_size);
376  _m_free(h);free(s);
377 
378  _m_free(h1);
379  _t_free(t1);
380  _t_free(t);
381 
382  // test serialization of orthogonal trees
383  h1 = _m_newr(null_H,ADD_INT);
384  _m_newi(h1,TEST_INT_SYMBOL,314);
385  _m_newi(h1,TEST_INT_SYMBOL,1000);
386  H h2 = _m_newt(null_H,TEST_TREE_SYMBOL,h1);
387  h = _m_newt(null_H,TEST_TREE_SYMBOL,h2);
388 
389  s = _m_serialize(h.m);
390  h1 = _m_unserialize(s);
391  t = _t_new_from_m(h1);
392 
393  spec_is_str_equal(t2s(t),"(TEST_TREE_SYMBOL:{(TEST_TREE_SYMBOL:{(process:ADD_INT (TEST_INT_SYMBOL:314) (TEST_INT_SYMBOL:1000))})})");
394 
395  free(s);
396  _t_free(t);
397  _m_free(h);
398  _m_free(h1);
399 }
400 
401 
402 void testMTree() {
403  testCreateTreeNodesM();
404  testMTreeOrthogonal();
405  testMTreeReceptor();
406  testMTreeWalk();
407  testTreeConvert();
408  testMTreeSerialize();
409 }
T * _t_new_root(Symbol symbol)
Definition: tree.c:160
void _m_walk(H h, void(*walkfn)(H, N *, void *, MwalkState *, Maddr), void *user_data)
Definition: mtree.c:524
Definition: ceptr_types.h:114
Definition: ceptr_types.h:42
H _m_unserialize(S *s)
Definition: mtree.c:743
Symbol _m_symbol(H h)
Definition: mtree.c:448
void * _m_surface(H h)
Definition: mtree.c:352
H _m_detatch(H oh)
Definition: mtree.c:631
S * _m_serialize(M *m)
Definition: mtree.c:645
T * _t_new_receptor(T *parent, Symbol symbol, Receptor *r)
Definition: tree.c:204
void testMTreeWalk()
Definition: mtree_spec.h:255
Definition: ceptr_types.h:68
void * _t_surface(T *t)
Definition: tree.c:1215
Maddr _m_child(H h, Mindex c)
Definition: mtree.c:383
void testTreeConvert()
Definition: mtree_spec.h:277
Receptor * _r_new(SemTable *sem, SemanticID r)
Creates a new receptor.
Definition: receptor.c:88
SState * state(StateType type, int *statesP, int level)
Definition: semtrex.c:103
size_t _m_size(H h)
Definition: mtree.c:276
T * _t_new_from_m(H h)
Definition: mtree.c:250
Definition: ceptr_types.h:51
H _m_new_from_t(T *t)
Definition: mtree.c:206
H _m_add(H parent, H h)
Definition: mtree.c:488
H _m_newi(H parent, Symbol symbol, int surface)
Definition: mtree.c:157
Error _p_reduce(SemTable *sem, T *rt)
Definition: process.c:1506
T * _makeTestHTTPRequestTree()
[makeTestHTTPRequestTree]
Definition: http_example.h:26
Definition: ceptr_types.h:83
Maddr _m_parent(H h)
Definition: mtree.c:366
int _m_children(H h)
Definition: mtree.c:315
void _t_add(T *t, T *c)
Definition: tree.c:261
void _t_free(T *t)
Definition: tree.c:526
Maddr _m_next_sibling(H h)
Definition: mtree.c:459
H _m_newr(H parent, Symbol s)
Definition: mtree.c:145