OILS / asdl / gc_test.cc View on Github | oilshell.org

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