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

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