1 
2 #include "ptest.h"
3 #include "Cello.h"
4 
5 local var TestType;
6 
7 data {
8   var type;
9   int test_data;
10 } TestTypeData;
11 
TestType_New(var self,var_list vl)12 local var TestType_New(var self, var_list vl) {
13   TestTypeData* ttd = cast(self, TestType);
14   ttd->test_data = as_long(var_list_get(vl));
15   return self;
16 }
17 
TestType_Delete(var self)18 local var TestType_Delete(var self) {
19   return self;
20 }
21 
TestType_Size(void)22 local size_t TestType_Size(void) {
23   return sizeof(TestTypeData);
24 }
25 
TestType_Eq(var self,var obj)26 local var TestType_Eq(var self, var obj) {
27   TestTypeData* lhs = cast(self, TestType);
28   TestTypeData* rhs = cast(obj, TestType);
29   if (lhs->test_data == rhs->test_data) {
30     return True;
31   } else {
32     return False;
33   }
34 }
35 
36 instance(TestType, New) = { TestType_New, TestType_Delete, TestType_Size };
37 instance(TestType, Eq) = { TestType_Eq };
38 
39 class {
40   var (*return_true)(var);
41 } ReturnTrue;
42 
return_true(var self)43 local var return_true(var self) {
44   return type_class_method(self, ReturnTrue, return_true, self);
45 }
46 
IntParent_ReturnTrue(var self)47 local var IntParent_ReturnTrue(var self) {
48   return True;
49 }
50 
51 instance(IntParent, ReturnTrue) = { IntParent_ReturnTrue };
52 
53 var IntParent = type_data {
54   type_begin(IntParent),
55   type_entry(IntParent, ReturnTrue),
56   type_end(IntParent),
57 };
58 
PT_FUNC(test_type)59 PT_FUNC(test_type) {
60   PT_ASSERT(type_of($(Int, 1)) is Int);
61   PT_ASSERT(type_of($(Real, 1.0)) is Real);
62   PT_ASSERT(type_of(True) is Bool);
63   PT_ASSERT(type_of(False) is Bool);
64   PT_ASSERT(type_of(Int) is Type);
65   PT_ASSERT(type_of(Real) is Type);
66   PT_ASSERT(type_of(Type) is Type);
67 }
68 
PT_FUNC(test_cast)69 PT_FUNC(test_cast) {
70 
71   var x = $(Int, 1);
72   var y = $(Real, 2.0);
73 
74   x = cast(x, Int);
75   y = cast(y, Real);
76 
77   PT_ASSERT(x);
78   PT_ASSERT(y);
79 
80 }
81 
PT_FUNC(test_new)82 PT_FUNC(test_new) {
83 
84   var x = new(Int, $(Int, 1));
85 
86   PT_ASSERT(x);
87   PT_ASSERT(type_of(x) is Int);
88   PT_ASSERT(as_long(x) is 1);
89 
90   delete(x);
91 
92   var y = $(Real, 0.0);
93   construct(y, $(Real, 1.0));
94   PT_ASSERT(as_double(y) is 1.0);
95 
96   var z = allocate(String);
97   PT_ASSERT(z);
98   construct(z, $(String, "Hello"));
99   PT_ASSERT_STR_EQ(as_str(z), "Hello");
100   z = destruct(z);
101   deallocate(z);
102 
103 }
104 
PT_FUNC(test_assign)105 PT_FUNC(test_assign) {
106 
107   /* Integers */
108 
109   var x = new(Int, $(Int, 10));
110   var y = new(Int, $(Int, 20));
111 
112   PT_ASSERT(as_long(x) is 10);
113   PT_ASSERT(as_long(y) is 20);
114   PT_ASSERT(x isnt y);
115 
116   assign(x, y);
117 
118   PT_ASSERT(as_long(x) is 20);
119   PT_ASSERT(as_long(y) is 20);
120   PT_ASSERT(x isnt y);
121 
122   delete(x);
123   delete(y);
124 
125   /* Strings */
126 
127   var xs = new(String, $(String, "Hello"));
128   var ys = new(String, $(String, "There"));
129 
130   PT_ASSERT_STR_EQ(as_str(xs), "Hello");
131   PT_ASSERT_STR_EQ(as_str(ys), "There");
132   PT_ASSERT(as_str(xs) isnt as_str(ys));
133   PT_ASSERT(xs isnt ys);
134 
135   assign(xs, ys);
136 
137   PT_ASSERT_STR_EQ(as_str(xs), "There");
138   PT_ASSERT_STR_EQ(as_str(ys), "There");
139   PT_ASSERT(as_str(xs) isnt as_str(ys));
140   PT_ASSERT(xs isnt ys);
141 
142   delete(xs);
143   delete(ys);
144 }
145 
PT_FUNC(test_copy)146 PT_FUNC(test_copy) {
147 
148   var x = new(String, $(String, "Hello"));
149   var y = copy(x);
150 
151   PT_ASSERT_STR_EQ(as_str(x), as_str(y));
152   PT_ASSERT(as_str(x) isnt as_str(y));
153   PT_ASSERT(x isnt y);
154 
155   delete(x);
156   delete(y);
157 
158 }
159 
PT_FUNC(test_eq)160 PT_FUNC(test_eq) {
161   PT_ASSERT(  eq($(Int, 1 ), $(Int, 1 )) );
162   PT_ASSERT( neq($(Int, 2 ), $(Int, 20)) );
163   PT_ASSERT(  eq($(String, "Hello"), $(String, "Hello")) );
164   PT_ASSERT( neq($(String, "Hello"), $(String, "There")) );
165 
166   var tab1 = new(Table, String, Int);
167   var tab2 = new(Table, String, Int);
168 
169   PT_ASSERT(eq(tab1, tab2));
170   put(tab1, $(String, "apple"), $(Int, 10));
171   PT_ASSERT(neq(tab1, tab2));
172   put(tab2, $(String, "apple"), $(Int, 10));
173   PT_ASSERT(eq(tab1, tab2));
174   put(tab1, $(String, "pear"), $(Int, 20));
175   put(tab2, $(String, "pear"), $(Int, 30));
176   PT_ASSERT(neq(tab1, tab2));
177   put(tab1, $(String, "pear"), $(Int, 30));
178   PT_ASSERT(eq(tab1, tab2));
179   put(tab2, $(String, "banana"), $(Int, 10));
180   PT_ASSERT(neq(tab1, tab2));
181 
182   delete(tab1);
183   delete(tab2);
184 
185   var dict1 = new(Dictionary, 0);
186   var dict2 = new(Dictionary, 0);
187 
188   PT_ASSERT(eq(dict1, dict2));
189   put(dict1, $(String, "apple"), $(Int, 10));
190   PT_ASSERT(neq(dict1, dict2));
191   put(dict2, $(String, "apple"), $(Int, 10));
192   PT_ASSERT(eq(dict1, dict2));
193   put(dict1, $(String, "pear"), $(Int, 20));
194   put(dict2, $(String, "pear"), $(Int, 30));
195   PT_ASSERT(neq(dict1, dict2));
196   put(dict1, $(String, "pear"), $(Int, 30));
197   PT_ASSERT(eq(dict1, dict2));
198   put(dict2, $(String, "banana"), $(Int, 10));
199   PT_ASSERT(neq(dict1, dict2));
200 
201   delete(dict1);
202   delete(dict2);
203 
204   var tree1 = new(Tree, String, String);
205   var tree2 = new(Tree, String, String);
206 
207   PT_ASSERT(eq(tree1, tree2));
208   put(tree1, $(String, "name"), $(String, "Alex"));
209   PT_ASSERT(neq(tree1, tree2));
210   put(tree2, $(String, "name"), $(String, "Alex"));
211   PT_ASSERT(eq(tree1, tree2));
212   put(tree1, $(String, "age"), $(String, "28"));
213   put(tree2, $(String, "age"), $(String, "30"));
214   PT_ASSERT(neq(tree1, tree2));
215   put(tree1, $(String, "age"), $(String, "30"));
216   PT_ASSERT(eq(tree1, tree2));
217   put(tree2, $(String, "nickname"), $(String, "The Wing Man"));
218   PT_ASSERT(neq(tree1, tree2));
219 
220   delete(tree1);
221   delete(tree2);
222 
223 }
224 
PT_FUNC(test_ord)225 PT_FUNC(test_ord) {
226   PT_ASSERT(  gt($(Int, 15), $(Int, 3 )) );
227   PT_ASSERT(  lt($(Int, 70), $(Int, 81)) );
228   PT_ASSERT(  ge($(Int, 71), $(Int, 71)) );
229   PT_ASSERT(  ge($(Int, 78), $(Int, 71)) );
230   PT_ASSERT(  le($(Int, 32), $(Int, 32)) );
231   PT_ASSERT(  le($(Int, 21), $(Int, 32)) );
232 }
233 
PT_FUNC(test_hash)234 PT_FUNC(test_hash) {
235   long x = hash($(Int, 1  ));
236   long y = hash($(Int, 123));
237 
238   PT_ASSERT(x is 1);
239   PT_ASSERT(y is 123);
240 }
241 
PT_FUNC(test_collection)242 PT_FUNC(test_collection) {
243 
244   var x = new(List, $(Int, 1), $(Real, 2.0), $(String, "Hello"));
245 
246   PT_ASSERT(x);
247   PT_ASSERT(len(x) is 3);
248   PT_ASSERT(contains(x, $(Int, 1)));
249   PT_ASSERT(contains(x, $(Real, 2.0)));
250   PT_ASSERT(contains(x, $(String, "Hello")));
251 
252   discard(x, $(Real, 2.0));
253 
254   PT_ASSERT(x);
255   PT_ASSERT(len(x) is 2);
256   PT_ASSERT(contains(x, $(Int, 1)));
257   PT_ASSERT(contains(x, $(String, "Hello")));
258 
259   clear(x);
260 
261   PT_ASSERT(x);
262   PT_ASSERT(len(x) is 0);
263   PT_ASSERT(is_empty(x));
264 
265   delete(x);
266 
267   var y = new(Array, Real, $(Real, 5.2), $(Real, 7.1), $(Real, 2.2), $(Real, 1.1));
268 
269   PT_ASSERT(y);
270   PT_ASSERT(len(y) is 4);
271   PT_ASSERT(contains(y, $(Real, 5.2)));
272   PT_ASSERT(contains(y, $(Real, 7.1)));
273   PT_ASSERT(contains(y, $(Real, 2.2)));
274   PT_ASSERT(contains(y, $(Real, 1.1)));
275 
276   sort(y);
277 
278   PT_ASSERT(eq(at(y, 0), $(Real, 1.1)));
279   PT_ASSERT(eq(at(y, 1), $(Real, 2.2)));
280   PT_ASSERT(eq(at(y, 2), $(Real, 5.2)));
281   PT_ASSERT(eq(at(y, 3), $(Real, 7.1)));
282 
283   var maxval = maximum(y);
284   var minval = minimum(y);
285 
286   PT_ASSERT(eq(maxval, $(Real, 7.1)));
287   PT_ASSERT(eq(minval, $(Real, 1.1)));
288 
289   delete(y);
290 
291   var z = new(List, $(Real, 5.2), $(Real, 7.1), $(Real, 2.2), $(Real, 1.1));
292 
293   sort(z);
294 
295   PT_ASSERT(eq(at(z, 0), $(Real, 1.1)));
296   PT_ASSERT(eq(at(z, 1), $(Real, 2.2)));
297   PT_ASSERT(eq(at(z, 2), $(Real, 5.2)));
298   PT_ASSERT(eq(at(z, 3), $(Real, 7.1)));
299 
300   delete(z);
301 
302   var w = new(List, $(Int, 135), $(Int, 11), $(Int, 254), $(Int, 123213), $(Int, 22), $(Int, 1));
303 
304   sort(w);
305 
306   PT_ASSERT(eq(at(w, 0), $(Int, 1)));
307   PT_ASSERT(eq(at(w, 1), $(Int, 11)));
308   PT_ASSERT(eq(at(w, 2), $(Int, 22)));
309   PT_ASSERT(eq(at(w, 3), $(Int, 135)));
310   PT_ASSERT(eq(at(w, 4), $(Int, 254)));
311   PT_ASSERT(eq(at(w, 5), $(Int, 123213)));
312 
313   delete(w);
314 
315   var map1 = new(Map);
316   var map2 = new(Map);
317 
318   PT_ASSERT(eq(map1, map2));
319 
320   put(map1, $(String, "key"), $(String, "val"));
321   PT_ASSERT(neq(map1, map2));
322 
323   put(map2, $(String, "key"), $(String, "val"));
324   PT_ASSERT(eq(map1, map2));
325 
326   put(map1, $(String, "newkey"), $(Int, 10));
327   put(map2, $(String, "newkey"), $(String, "newval"));
328   PT_ASSERT(neq(map1, map2));
329 
330   put(map1, $(String, "newkey"), $(String, "newval"));
331   PT_ASSERT(eq(map1, map2));
332 
333   put(map2, $(String, "difkey"), $(Int, 5));
334   PT_ASSERT(neq(map1, map2));
335 
336   delete(map1);
337   delete(map2);
338 
339 }
340 
PT_FUNC(test_iter)341 PT_FUNC(test_iter) {
342 
343   var x = new(List, $(Int, 1), $(Real, 2.0), $(String, "Hello"));
344 
345   foreach(y in x) {
346     PT_ASSERT(y);
347     PT_ASSERT(type_of(y));
348   }
349 
350   delete(x);
351 }
352 
PT_FUNC(test_push)353 PT_FUNC(test_push) {
354 
355   var x = new(Array, Int);
356   var y = new(List);
357 
358   for(int i = 0; i < 1000; i++) {
359     push(x, $(Int, 1));
360     push(y, $(Int, 2));
361   }
362 
363   for(int i = 0; i < 1000; i++) {
364     pop(x);
365     pop(y);
366   }
367 
368   push(x, $(Int, 0));
369   push(x, $(Int, 5));
370   push(x, $(Int, 10));
371 
372   PT_ASSERT(eq(at(x, 0), $(Int, 0)));
373   PT_ASSERT(eq(at(x, 1), $(Int, 5)));
374   PT_ASSERT(eq(at(x, 2), $(Int, 10)));
375 
376   pop_at(x, 1);
377 
378   PT_ASSERT(eq(at(x, 0), $(Int, 0)));
379   PT_ASSERT(eq(at(x, 1), $(Int, 10)));
380 
381   delete(x);
382   delete(y);
383 }
384 
PT_FUNC(test_at)385 PT_FUNC(test_at) {
386 
387   var fst = $(Int, 1);
388   var snd = $(Real, 2.0);
389   var trd = $(String, "Hello");
390 
391   var x = new(List, fst, snd, trd);
392 
393   PT_ASSERT(at(x, 0) is fst);
394   PT_ASSERT(at(x, 1) is snd);
395   PT_ASSERT(at(x, 2) is trd);
396 
397   set(x, 1, trd);
398 
399   PT_ASSERT(at(x, 1) is trd);
400 
401   delete(x);
402 
403 }
404 
PT_FUNC(test_dict)405 PT_FUNC(test_dict) {
406 
407   var prices = new(Table, String, Int);
408   put(prices, $(String, "Apple"),  $(Int, 12));
409   put(prices, $(String, "Banana"), $(Int,  6));
410   put(prices, $(String, "Pear"),   $(Int, 55));
411 
412   var pear_price = get(prices, $(String, "Pear"));
413   var banana_price = get(prices, $(String, "Banana"));
414   var apple_price = get(prices, $(String, "Apple"));
415 
416   PT_ASSERT(as_long(pear_price) is 55);
417   PT_ASSERT(as_long(banana_price) is 6);
418   PT_ASSERT(as_long(apple_price) is 12);
419 
420   char name[5];
421 
422   for(int i = 0; i < 1000; i++) {
423     sprintf(name, "%i", i);
424     put(prices, $(String, name), $(Int, i));
425   }
426 
427   for(int i = 0; i < 1000; i++) {
428     sprintf(name, "%i", i);
429     discard(prices, $(String, name));
430   }
431 
432   delete(prices);
433 
434 }
435 
PT_FUNC(test_as_ctype)436 PT_FUNC(test_as_ctype) {
437 
438   PT_ASSERT(as_char($(Char, 'a')) is 'a');
439   PT_ASSERT(as_char($(Char, 'b')) is 'b');
440 
441   PT_ASSERT_STR_EQ(as_str($(String, "Hello")), "Hello");
442   PT_ASSERT_STR_EQ(as_str($(String, "There")), "There");
443 
444   PT_ASSERT(as_long($(Int, 5)) is 5);
445   PT_ASSERT(as_long($(Real, 5.6)) is 5);
446   PT_ASSERT(as_long($(Real, 5.5)) is 5);
447   PT_ASSERT(as_long($(Real, 5.4)) is 5);
448 
449   PT_ASSERT(as_double($(Real, 5.1)) is 5.1);
450   PT_ASSERT(as_double($(Real, 5.2)) is 5.2);
451   PT_ASSERT(as_double($(Real, 9.8)) is 9.8);
452   PT_ASSERT(as_double($(Int, 5)) is 5.0);
453   PT_ASSERT(as_double($(Int, 7)) is 7.0);
454 
455 }
456 
PT_FUNC(test_stream)457 PT_FUNC(test_stream) {
458 
459   var f = $(File, NULL);
460 
461   PT_ASSERT(f);
462 
463   stream_open(f, "test.bin", "w");
464 
465     PT_ASSERT(f);
466 
467     put(f, Int, $(Int, 1));
468     put(f, Int, $(Int, 22));
469 
470   stream_close(f);
471 
472   stream_open(f, "test.bin", "r");
473 
474     PT_ASSERT(f);
475 
476     var first = get(f, Int);
477     var second = get(f, Int);
478 
479     PT_ASSERT(as_long(first) is 1);
480     PT_ASSERT(as_long(second) is 22);
481 
482     delete(first);
483     delete(second);
484 
485   stream_close(f);
486 
487   PT_ASSERT(f);
488 }
489 
PT_FUNC(test_type_new)490 PT_FUNC(test_type_new) {
491 
492   TestType = new(Type, $(String, "TestType"), $(Int, 2),
493     (var[]){ &TestTypeNew, &TestTypeEq },
494     (const char*[]){ "New", "Eq" } );
495 
496   PT_ASSERT(TestType);
497   PT_ASSERT_STR_EQ(as_str(TestType), "TestType");
498 
499   var test_obj1 = new(TestType, $(Int, 1));
500   var test_obj2 = new(TestType, $(Int, 1));
501   var test_obj3 = new(TestType, $(Int, 4));
502 
503   PT_ASSERT(test_obj1);
504   PT_ASSERT(test_obj2);
505   PT_ASSERT(test_obj3);
506 
507   PT_ASSERT(eq(test_obj1, test_obj2));
508   PT_ASSERT(neq(test_obj1, test_obj3));
509 
510   delete(test_obj1);
511   delete(test_obj2);
512   delete(test_obj3);
513 
514   delete(TestType);
515 
516 }
517 
PT_FUNC(test_type_implements)518 PT_FUNC(test_type_implements) {
519 
520   PT_ASSERT(type_implements(Int, New));
521   PT_ASSERT(type_implements(Real, Num));
522   PT_ASSERT(type_implements(String, Eq));
523 
524   PT_ASSERT(type_class(Int, Ord));
525   PT_ASSERT(type_class(List, At));
526   PT_ASSERT(type_class(Type, AsStr));
527 
528 }
529 
PT_FUNC(test_type_parent)530 PT_FUNC(test_type_parent) {
531 
532   PT_ASSERT(not type_implements(Int, ReturnTrue));
533   PT_ASSERT(not type_implements(Real, ReturnTrue));
534   PT_ASSERT(type_implements(IntParent, ReturnTrue));
535 
536   PT_ASSERT(return_true(IntParent));
537 
538   Type_Inherit(Int, IntParent);
539 
540   PT_ASSERT(type_implements(Int, ReturnTrue));
541   PT_ASSERT(not type_implements(Real, ReturnTrue));
542   PT_ASSERT(type_implements(IntParent, ReturnTrue));
543 
544   PT_ASSERT(return_true(Int));
545 
546 }
547 
PT_FUNC(test_show)548 PT_FUNC(test_show) {
549 
550   var out = new(String, $(String, ""));
551 
552   print_to(out, 0, "This is an %s %i %i",
553     $(String, "example"), $(Int, 10), $(Int, 1));
554 
555   PT_ASSERT_STR_EQ(as_str(out), "This is an example 10 1");
556 
557   delete(out);
558 
559 }
560 
PT_FUNC(test_look)561 PT_FUNC(test_look) {
562 
563   var x = $(Int, 0);
564   var y = $(Int, 0);
565   var z = $(Int, 0);
566   var w = $(Int, 0);
567 
568   scan_from($(String, "5 10 1 0"), 0, "%i %i %i %i", x, y, z, w);
569 
570   PT_ASSERT(eq(x, $(Int, 5)));
571   PT_ASSERT(eq(y, $(Int, 10)));
572   PT_ASSERT(eq(z, $(Int, 1)));
573   PT_ASSERT(eq(w, $(Int, 0)));
574 
575 }
576 
PT_FUNC(test_module)577 PT_FUNC(test_module) {
578 
579 #ifdef _WIN32
580   with(python in module("python27.dll")) {
581 
582     const char* (*Py_GetVersion)(void) = get(python, $(String, "Py_GetVersion"));
583     const char* (*Py_GetPlatform)(void) = get(python, $(String, "Py_GetPlatform"));
584     const char* (*Py_GetCopyright)(void) = get(python, $(String, "Py_GetCopyright"));
585 
586     //println("");
587     //println("Python Version is '%s'", $(String, (char*)Py_GetVersion()));
588     //println("Python Platform is '%s'", $(String, (char*)Py_GetPlatform()));
589     //println("Python Copyright is '%s'", $(String, (char*)Py_GetCopyright()));
590 
591     PT_ASSERT(Py_GetVersion());
592     PT_ASSERT(Py_GetPlatform());
593     PT_ASSERT(Py_GetCopyright());
594 
595   }
596 #else
597   //with(math in module("libm.so")) {
598 
599     //double (*cosine)(double) = get(math, $(String, "cos"));
600 
601     //PT_ASSERT(cosine);
602 
603   //}
604 #endif
605 
606 }
607 
PT_SUITE(suite_core)608 PT_SUITE(suite_core) {
609 
610   PT_REG(test_type);
611   PT_REG(test_cast);
612   PT_REG(test_new);
613   PT_REG(test_assign);
614   PT_REG(test_copy);
615   PT_REG(test_eq);
616   PT_REG(test_ord);
617   PT_REG(test_hash);
618   PT_REG(test_collection);
619   PT_REG(test_iter);
620   PT_REG(test_push);
621   PT_REG(test_at);
622   PT_REG(test_dict);
623   PT_REG(test_as_ctype);
624   PT_REG(test_stream);
625   PT_REG(test_type_new);
626   PT_REG(test_type_implements);
627   PT_REG(test_type_parent);
628   PT_REG(test_show);
629   PT_REG(test_look);
630   PT_REG(test_module);
631 
632 }
633