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

102 lines, 69 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::bool_expr__Binary;
18using typed_demo_asdl::word;
19
20TEST 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 for (int i = 0; i < 2000; ++i) {
31 hnode_t* t1 = b->PrettyTree();
32 ASSERT_EQ(hnode_e::Record, t1->tag());
33
34 auto f = mylib::Stdout();
35 auto ast_f = Alloc<format::TextOutput>(f);
36 format::PrintTree(t1, ast_f);
37 log("");
38 }
39
40 PASS();
41}
42
43// TODO:
44// - This test is complex and not very good
45// - Maybe unify this with gen_cpp_test.cc
46
47TEST hnode_test() {
48 mylib::Writer* f = nullptr;
49 format::TextOutput* ast_f = nullptr;
50 hnode_t* h = nullptr; // base type
51 hnode__Array* array = nullptr; // base type
52 hnode__Record* rec = nullptr;
53 StackRoots _roots({&f, &ast_f, &h, &array, &rec});
54
55 f = mylib::Stdout();
56 ast_f = Alloc<format::TextOutput>(f);
57 array = hnode::Array::CreateNull(true);
58 ASSERT_EQ_FMT(4, gHeap.Collect(), "%d");
59
60 rec = hnode::Record::CreateNull(true);
61 rec->node_type = StrFromC("dummy_node");
62 ASSERT_EQ_FMT(8, gHeap.Collect(), "%d");
63
64 h = rec; // base type
65 array->children->append(h);
66
67 format::PrintTree(h, ast_f);
68 printf("\n");
69 ASSERT_EQ_FMT(9, gHeap.Collect(), "%d");
70
71 h = Alloc<hnode__Leaf>(StrFromC("zz"), color_e::TypeName);
72 array->children->append(h);
73
74 format::PrintTree(h, ast_f);
75 printf("\n");
76 ASSERT_EQ_FMT(11, gHeap.Collect(), "%d");
77
78 h = array;
79 format::PrintTree(h, ast_f);
80 printf("\n");
81 ASSERT_EQ_FMT(11, gHeap.Collect(), "%d");
82
83 PASS();
84}
85
86GREATEST_MAIN_DEFS();
87
88int main(int argc, char** argv) {
89 gHeap.Init();
90
91 GREATEST_MAIN_BEGIN();
92
93 RUN_TEST(hnode_test);
94 gHeap.Collect();
95
96 RUN_TEST(pretty_print_test);
97
98 gHeap.CleanProcessExit();
99
100 GREATEST_MAIN_END();
101 return 0;
102}