1 | // asdl/gc_test.cc
|
2 |
|
3 | #include "_gen/asdl/examples/typed_demo.asdl.h"
|
4 | #include "_gen/asdl/hnode.asdl.h"
|
5 | #include "mycpp/runtime.h"
|
6 | #include "prebuilt/asdl/runtime.mycpp.h"
|
7 | #include "vendor/greatest.h"
|
8 |
|
9 | using hnode_asdl::color_e;
|
10 | using hnode_asdl::hnode;
|
11 | using hnode_asdl::hnode__Array;
|
12 | using hnode_asdl::hnode__Leaf;
|
13 | using hnode_asdl::hnode__Record;
|
14 | using hnode_asdl::hnode_e;
|
15 | using hnode_asdl::hnode_t;
|
16 |
|
17 | using typed_demo_asdl::a_word;
|
18 | using typed_demo_asdl::a_word_e;
|
19 | using typed_demo_asdl::a_word_t;
|
20 | using typed_demo_asdl::arith_expr;
|
21 | using typed_demo_asdl::arith_expr_e;
|
22 | using typed_demo_asdl::bool_expr__Binary;
|
23 | using typed_demo_asdl::bool_expr_t;
|
24 | using typed_demo_asdl::CompoundWord;
|
25 | using typed_demo_asdl::word;
|
26 |
|
27 | TEST pretty_print_test() {
|
28 | bool_expr_t* b = nullptr;
|
29 | StackRoot _r1(&b);
|
30 |
|
31 | auto w1 = Alloc<word>(StrFromC("left"));
|
32 | auto w2 = Alloc<word>(StrFromC("right"));
|
33 | b = Alloc<bool_expr__Binary>(w1, w2);
|
34 |
|
35 | #if 0
|
36 | log("sizeof b = %d", sizeof b);
|
37 | log("");
|
38 | #endif
|
39 |
|
40 | for (int i = 0; i < 2; ++i) {
|
41 | hnode_t* t1 = b->PrettyTree(false);
|
42 | ASSERT_EQ(hnode_e::Record, t1->tag());
|
43 |
|
44 | auto f = mylib::Stdout();
|
45 | format::HNodePrettyPrint(t1, f);
|
46 | log("");
|
47 | }
|
48 |
|
49 | PASS();
|
50 | }
|
51 |
|
52 | // TODO:
|
53 | // - This test is complex and not very good
|
54 | // - Maybe unify this with gen_cpp_test.cc
|
55 |
|
56 | TEST hnode_test() {
|
57 | mylib::Writer* f = nullptr;
|
58 | hnode_t* h = nullptr; // base type
|
59 | hnode__Array* array = nullptr; // base type
|
60 | hnode__Record* rec = nullptr;
|
61 | StackRoots _roots({&f, &h, &array, &rec});
|
62 |
|
63 | f = mylib::Stdout();
|
64 | array = hnode::Array::CreateNull(true);
|
65 | ASSERT_EQ_FMT(3, gHeap.Collect(), "%d");
|
66 |
|
67 | rec = hnode::Record::CreateNull(true);
|
68 | rec->node_type = StrFromC("dummy_node");
|
69 | ASSERT_EQ_FMT(6, gHeap.Collect(), "%d");
|
70 |
|
71 | h = rec; // base type
|
72 | array->children->append(h);
|
73 |
|
74 | format::HNodePrettyPrint(h, f);
|
75 | printf("\n");
|
76 | ASSERT_EQ_FMT(7, gHeap.Collect(), "%d");
|
77 |
|
78 | h = Alloc<hnode__Leaf>(StrFromC("zz"), color_e::TypeName);
|
79 | array->children->append(h);
|
80 |
|
81 | format::HNodePrettyPrint(h, f);
|
82 | printf("\n");
|
83 | ASSERT_EQ_FMT(9, gHeap.Collect(), "%d");
|
84 |
|
85 | h = array;
|
86 | format::HNodePrettyPrint(h, f);
|
87 | printf("\n");
|
88 | ASSERT_EQ_FMT(9, gHeap.Collect(), "%d");
|
89 |
|
90 | PASS();
|
91 | }
|
92 |
|
93 | TEST subtype_test() {
|
94 | List<CompoundWord*>* li = nullptr;
|
95 | StackRoot _r(&li);
|
96 |
|
97 | // Test the GC header
|
98 |
|
99 | li = NewList<CompoundWord*>();
|
100 |
|
101 | int n = 1000;
|
102 | for (int i = 0; i < n; ++i) {
|
103 | auto* c = Alloc<CompoundWord>();
|
104 |
|
105 | c->append(arith_expr::NoOp);
|
106 | c->append(Alloc<arith_expr::Const>(42));
|
107 |
|
108 | // log("len(c) %d", len(c));
|
109 | // log("i = %d", i);
|
110 |
|
111 | ASSERT_EQ_FMT(i, len(li), "%d");
|
112 | li->append(c);
|
113 | mylib::MaybeCollect();
|
114 | }
|
115 |
|
116 | log("len(li) = %d", len(li));
|
117 | ASSERT_EQ(n, len(li));
|
118 |
|
119 | // Now test the type tag
|
120 |
|
121 | List<a_word_t*>* words = nullptr;
|
122 | StackRoot _r2(&words);
|
123 |
|
124 | words = NewList<a_word_t*>();
|
125 |
|
126 | #if 1
|
127 | n = 100;
|
128 | for (int i = 0; i < n; ++i) {
|
129 | words->append(Alloc<CompoundWord>());
|
130 | words->append(Alloc<a_word::String>(kEmptyString));
|
131 |
|
132 | // mylib::MaybeCollect();
|
133 | }
|
134 | #endif
|
135 |
|
136 | log("len(words) = %d", len(words));
|
137 | ASSERT_EQ(n * 2, len(words));
|
138 |
|
139 | int num_c = 0;
|
140 | int num_s = 0;
|
141 | for (int i = 0; i < len(words); ++i) {
|
142 | auto* w = words->at(i);
|
143 | switch (w->tag()) {
|
144 | case a_word_e::CompoundWord: {
|
145 | // printf("CompoundWord\n");
|
146 | num_c++;
|
147 | break;
|
148 | }
|
149 | case a_word_e::String: {
|
150 | // printf("String\n");
|
151 | num_s++;
|
152 | break;
|
153 | }
|
154 | default: {
|
155 | FAIL();
|
156 | }
|
157 | }
|
158 | }
|
159 | log("CompoundWord %d, String %d", num_c, num_s);
|
160 |
|
161 | PASS();
|
162 | }
|
163 |
|
164 | TEST print_subtype_test() {
|
165 | // TODO: Also need to test GC header for List[int] subtypes
|
166 |
|
167 | auto c = Alloc<CompoundWord>();
|
168 |
|
169 | log("len = %d", len(c));
|
170 |
|
171 | c->append(arith_expr::NoOp);
|
172 | c->append(Alloc<arith_expr::Const>(42));
|
173 |
|
174 | log("len = %d", len(c));
|
175 |
|
176 | #if 1
|
177 | hnode_t* t1 = c->PrettyTree(false);
|
178 | // ASSERT_EQ_FMT(hnode_e::Record, t1->tag(), "%d");
|
179 |
|
180 | auto f = mylib::Stdout();
|
181 | format::HNodePrettyPrint(t1, f);
|
182 | printf("\n");
|
183 | #endif
|
184 |
|
185 | PASS();
|
186 | }
|
187 |
|
188 | GREATEST_MAIN_DEFS();
|
189 |
|
190 | int main(int argc, char** argv) {
|
191 | gHeap.Init();
|
192 |
|
193 | GREATEST_MAIN_BEGIN();
|
194 |
|
195 | RUN_TEST(hnode_test);
|
196 | gHeap.Collect();
|
197 |
|
198 | RUN_TEST(pretty_print_test);
|
199 |
|
200 | RUN_TEST(subtype_test);
|
201 | RUN_TEST(print_subtype_test);
|
202 |
|
203 | gHeap.CleanProcessExit();
|
204 |
|
205 | GREATEST_MAIN_END();
|
206 | return 0;
|
207 | }
|