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::bool_expr__Binary;
|
18 | using typed_demo_asdl::word;
|
19 |
|
20 | TEST pretty_print_test() {
|
21 | auto w1 = Alloc<word>(StrFromC("left"));
|
22 | auto w2 = Alloc<word>(StrFromC("right"));
|
23 | auto b = Alloc<bool_expr__Binary>(w1, w2);
|
24 |
|
25 | #if 0
|
26 | log("sizeof b = %d", sizeof b);
|
27 | log("");
|
28 | #endif
|
29 |
|
30 | // Note: this segfaults with 1000 iterations, because it hit GC.
|
31 | // TODO: GC_ALWAYS and make it pass.
|
32 | for (int i = 0; i < 2; ++i) {
|
33 | hnode_t* t1 = b->PrettyTree();
|
34 | ASSERT_EQ(hnode_e::Record, t1->tag());
|
35 |
|
36 | auto f = mylib::Stdout();
|
37 | auto ast_f = Alloc<format::TextOutput>(f);
|
38 | format::PrintTree(t1, ast_f);
|
39 | log("");
|
40 | }
|
41 |
|
42 | PASS();
|
43 | }
|
44 |
|
45 | // TODO:
|
46 | // - This test is complex and not very good
|
47 | // - Maybe unify this with gen_cpp_test.cc
|
48 | // - Port build to Ninja
|
49 | // - Make it ASAN-clean
|
50 |
|
51 | TEST hnode_test() {
|
52 | mylib::Writer* f = nullptr;
|
53 | format::TextOutput* ast_f = nullptr;
|
54 | hnode_t* h = nullptr; // base type
|
55 | hnode__Array* array = nullptr; // base type
|
56 | hnode__Record* rec = nullptr;
|
57 | StackRoots _roots({&f, &ast_f, &h, &array, &rec});
|
58 |
|
59 | f = mylib::Stdout();
|
60 | ast_f = Alloc<format::TextOutput>(f);
|
61 | array = hnode::Array::CreateNull(true);
|
62 | ASSERT_EQ_FMT(4, gHeap.Collect(), "%d");
|
63 |
|
64 | rec = hnode::Record::CreateNull(true);
|
65 | rec->node_type = StrFromC("dummy_node");
|
66 | ASSERT_EQ_FMT(8, gHeap.Collect(), "%d");
|
67 |
|
68 | h = rec; // base type
|
69 | array->children->append(h);
|
70 |
|
71 | format::PrintTree(h, ast_f);
|
72 | printf("\n");
|
73 | ASSERT_EQ_FMT(9, gHeap.Collect(), "%d");
|
74 |
|
75 | h = Alloc<hnode__Leaf>(StrFromC("zz"), color_e::TypeName);
|
76 | array->children->append(h);
|
77 |
|
78 | format::PrintTree(h, ast_f);
|
79 | printf("\n");
|
80 | ASSERT_EQ_FMT(11, gHeap.Collect(), "%d");
|
81 |
|
82 | h = array;
|
83 | format::PrintTree(h, ast_f);
|
84 | printf("\n");
|
85 | ASSERT_EQ_FMT(11, gHeap.Collect(), "%d");
|
86 |
|
87 | PASS();
|
88 | }
|
89 |
|
90 | GREATEST_MAIN_DEFS();
|
91 |
|
92 | int main(int argc, char** argv) {
|
93 | gHeap.Init();
|
94 |
|
95 | GREATEST_MAIN_BEGIN();
|
96 |
|
97 | RUN_TEST(hnode_test);
|
98 | // TODO: fix rooting of mylib.Stdout(). Then collect after every test, so
|
99 | // that the number of live objects is accurate.
|
100 | // gHeap.Collect();
|
101 |
|
102 | RUN_TEST(pretty_print_test);
|
103 |
|
104 | gHeap.CleanProcessExit();
|
105 |
|
106 | GREATEST_MAIN_END();
|
107 | return 0;
|
108 | }
|