zen.test.c (2868B)
1 #include <stdio.h> 2 #include <assert.h> 3 4 #include "zen.h" 5 #include "symbol.h" 6 #include "schema.h" 7 8 #define ts(g,c,e) do {instruct(g, intern(c)); assert(get_status(g) == e);} while(0); 9 10 void 11 test_start() 12 { 13 schema *s = new_schema(intern("a")); 14 garden *g = new_garden(s); 15 assert(get_status(g) == WAITING_FOR_OP); 16 ts(g, "start", WAITING_FOR_A); 17 } 18 19 void 20 test_invalid_constructor_start_fail() 21 { 22 schema *s = new_schema(intern("a")); 23 garden *g = new_garden(s); 24 ts(g, "start", WAITING_FOR_A ); 25 ts(g, "anything", ERROR ); 26 } 27 28 void 29 test_invalid_key_fail() 30 { 31 schema *s = new_schema(intern("a")); 32 add_constructor(s, intern("c")); 33 garden *g = new_garden(s); 34 35 ts(g, "start", WAITING_FOR_A); 36 ts(g, "c", WAITING_FOR_OP); 37 ts(g, "fill", WAITING_FOR_B); 38 ts(g, "anything", ERROR); 39 } 40 41 void 42 test_valid_key_success() 43 { 44 schema *s = new_schema(intern("a")); 45 add_constructor(s, intern("c")); 46 mark_as_leaf(s, intern("c"), intern("k")); 47 48 garden *g = new_garden(s); 49 ts(g, "start", WAITING_FOR_A); 50 ts(g, "c", WAITING_FOR_OP); 51 ts(g, "fill", WAITING_FOR_B); 52 ts(g, "k", WAITING_FOR_A); 53 ts(g, "anything", WAITING_FOR_OP); 54 ts(g, "done", WAITING_FOR_OP); 55 } 56 57 void 58 test_sub_success() 59 { 60 schema *s = new_schema(intern("a")); 61 add_constructor(s, intern("c")); 62 schema *t = new_schema(intern("b")); 63 add_constructor(t, intern("d")); 64 assign_subschema(s, intern("c"), intern("k"), t); 65 66 garden *g = new_garden(s); 67 68 ts(g, "start", WAITING_FOR_A ); 69 ts(g, "c", WAITING_FOR_OP ); 70 ts(g, "sub", WAITING_FOR_B ); 71 ts(g, "k", WAITING_FOR_A ); 72 ts(g, "d", WAITING_FOR_OP ); 73 ts(g, "done", WAITING_FOR_OP ); 74 ts(g, "done", WAITING_FOR_OP ); 75 } 76 77 void 78 test_sub_invalid_constructor_fail() 79 { 80 schema *s = new_schema(intern("a")); 81 add_constructor(s, intern("c")); 82 garden *g = new_garden(s); 83 84 ts(g, "start", WAITING_FOR_A ); 85 ts(g, "c", WAITING_FOR_OP ); 86 ts(g, "sub", WAITING_FOR_B ); 87 ts(g, "k", ERROR ); 88 } 89 90 void 91 test_sup_invalid_constructor_fail() 92 { 93 schema *s = new_schema(intern("a")); 94 add_constructor(s, intern("c")); 95 garden *g = new_garden(s); 96 97 ts(g, "start", WAITING_FOR_A); 98 ts(g, "c", WAITING_FOR_OP); 99 ts(g, "sup", WAITING_FOR_B); 100 ts(g, "d", ERROR); 101 } 102 103 void 104 test_sup_success() 105 { 106 schema *s = new_schema(intern("a")); 107 add_constructor(s, intern("c")); 108 add_constructor(s, intern("nil")); 109 assign_subschema(s, intern("c"), intern("k"), s); 110 garden *g = new_garden(s); 111 112 ts(g, "start", WAITING_FOR_A); 113 ts(g, "nil", WAITING_FOR_OP); 114 ts(g, "sup", WAITING_FOR_B); 115 ts(g, "c", WAITING_FOR_A); 116 ts(g, "k", WAITING_FOR_OP); 117 } 118 119 int 120 main() 121 { 122 test_start(); 123 test_invalid_constructor_start_fail(); 124 test_invalid_key_fail(); 125 test_valid_key_success(); 126 test_sub_invalid_constructor_fail(); 127 test_sub_success(); 128 test_sup_invalid_constructor_fail(); 129 test_sup_success(); 130 return 0; 131 }