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

317 lines, 197 significant
1#include <stdarg.h> // va_list, etc.
2#include <stdio.h>
3
4#include "_gen/asdl/examples/shared_variant.asdl.h"
5#include "_gen/asdl/examples/typed_arith.asdl.h"
6#include "_gen/asdl/examples/typed_demo.asdl.h" // has simple Sum, etc
7#include "mycpp/runtime.h"
8#include "prebuilt/asdl/runtime.mycpp.h"
9#include "vendor/greatest.h"
10
11using typed_arith_asdl::pipeline;
12
13using typed_arith_asdl::arith_expr; // variant type namespace
14using typed_arith_asdl::arith_expr_e; // variant tag type
15using typed_arith_asdl::arith_expr_t; // sum type
16
17using typed_arith_asdl::arith_expr__Big;
18using typed_arith_asdl::arith_expr__Const;
19using typed_arith_asdl::arith_expr__FuncCall;
20using typed_arith_asdl::arith_expr__Unary;
21using typed_arith_asdl::arith_expr__Var;
22using typed_arith_asdl::CompoundWord;
23
24using typed_demo_asdl::bool_expr__Binary;
25using typed_demo_asdl::bool_expr__LogicalBinary;
26using typed_demo_asdl::op_array;
27using typed_demo_asdl::op_id_e;
28
29using hnode_asdl::hnode__Leaf;
30using hnode_asdl::hnode_e;
31
32void PrintTag(arith_expr_t* a) {
33 switch (a->tag()) {
34 case arith_expr_e::Const:
35 log("Const");
36 break;
37 case arith_expr_e::Var:
38 log("Var");
39 break;
40 default:
41 log("OTHER");
42 }
43 log("");
44}
45
46TEST misc_test() {
47 auto c = Alloc<arith_expr::Const>(42);
48 log("sizeof *c = %d", sizeof *c); // 16 bytes
49
50 ASSERT_EQ_FMT(42, c->i, "%d");
51 log("c->tag = %d", c->tag());
52 PrintTag(c);
53
54 auto v = Alloc<arith_expr__Var>(StrFromC("foo"));
55 log("sizeof *v = %d", sizeof *v); // 24 bytes
56
57 ASSERT(str_equals(StrFromC("foo"), v->name));
58 log("v->tag = %d", v->tag());
59 PrintTag(v);
60
61 auto u = Alloc<arith_expr__Unary>(StrFromC("-"), v);
62 log("u->op = %s", u->op->data_);
63
64 auto v1 = Alloc<arith_expr__Var>(StrFromC("v1"));
65 auto v2 = Alloc<arith_expr__Var>(StrFromC("v2"));
66 auto args = NewList<arith_expr_t*>({v1, v2});
67
68 auto f = Alloc<arith_expr__FuncCall>(StrFromC("f"), args);
69 log("f->name = %s", f->name->data_);
70
71 auto p = Alloc<pipeline>(true);
72 log("p->negated = %d", p->negated);
73
74#if 0
75 if (t->tag() == hnode_e::Leaf) {
76 hnode__Leaf* t2 = static_cast<hnode__Leaf*>(t);
77 log("%s", hnode_str(t2->tag()));
78 log("%s", color_str(t2->color));
79 log("%s", t2->s->data_);
80 }
81#endif
82
83 // NOTE: This is self-initialization!!!
84 /*
85 if (t->tag == hnode_e::Leaf) {
86 hnode__Leaf* t = static_cast<hnode__Leaf*>(t);
87 log("%s", hnode_str(t->tag));
88 log("%s", color_str(t->color));
89 log("%s", t->s->data_);
90 }
91 */
92
93 PASS();
94}
95
96using shared_variant_asdl::DoubleQuoted;
97using shared_variant_asdl::word_part_e;
98using shared_variant_asdl::word_part_t;
99
100using shared_variant_asdl::tok;
101using shared_variant_asdl::tok_e;
102using shared_variant_asdl::tok_t;
103using shared_variant_asdl::Token;
104
105TEST shared_variant_test() {
106 auto* dq = Alloc<DoubleQuoted>(0, Alloc<List<BigStr*>>());
107
108 word_part_t* wp = nullptr;
109 wp = dq; // assign to base type
110
111 log("wp->tag() %d", wp->tag());
112
113 auto* token = Alloc<Token>(0, StrFromC("hi"));
114 tok_t* tok = nullptr;
115 tok = token;
116
117 log("tok->tag() for Token = %d", tok->tag());
118
119 auto* eof = tok::Eof;
120 tok = eof;
121 log("tok->tag() for Eof = %d", tok->tag());
122
123 PASS();
124}
125
126using typed_demo_asdl::bool_expr_str;
127
128TEST pretty_print_test() {
129 // typed_demo.asdl
130
131 // auto o = op_id_e::Plus;
132 // Note: this is NOT prevented at compile time, even though it's illegal.
133 // left and right are not optional.
134 // auto b = new bool_expr__LogicalBinary(o, nullptr, nullptr);
135
136 auto w1 = Alloc<typed_demo_asdl::word>(StrFromC("left"));
137 auto w2 = Alloc<typed_demo_asdl::word>(StrFromC("right"));
138 auto b = Alloc<bool_expr__Binary>(w1, w2);
139
140 hnode_t* t1 = b->PrettyTree();
141 ASSERT_EQ_FMT(hnode_e::Record, t1->tag(), "%d");
142
143 auto f = mylib::Stdout();
144 auto ast_f = Alloc<format::TextOutput>(f);
145 format::PrintTree(t1, ast_f);
146 printf("\n");
147
148 log("bool_expr_str = %s", bool_expr_str(b->tag())->data_);
149 ASSERT(str_equals0("bool_expr.Binary", bool_expr_str(b->tag())));
150
151 ASSERT(str_equals0("Binary", bool_expr_str(b->tag(), false)));
152
153 // typed_arith.asdl
154 auto c = Alloc<arith_expr__Const>(42);
155 hnode_t* t2 = c->PrettyTree();
156 ASSERT_EQ(hnode_e::Record, t2->tag());
157 format::PrintTree(t2, ast_f);
158 printf("\n");
159
160 auto big = Alloc<arith_expr__Big>(mops::BigInt(INT64_MAX));
161 hnode_t* t3 = big->PrettyTree();
162 ASSERT_EQ(hnode_e::Record, t3->tag());
163 format::PrintTree(t3, ast_f);
164 printf("\n");
165
166 PASS();
167}
168
169TEST dicts_test() {
170 auto m = typed_demo_asdl::Dicts::CreateNull();
171 log("m.ss = %p", m->ss);
172 log("m.ib = %p", m->ib);
173
174 m->ss = Alloc<Dict<BigStr*, BigStr*>>();
175 m->ib = Alloc<Dict<int, bool>>();
176
177 m->ss->set(StrFromC("foo"), StrFromC("bar"));
178
179 m->ib->set(42, true);
180 // note: Dict<int, bool>::get() doesn't compile because nullptr isn't valid
181 // to return. But Dict<int, bool>::index() does compile.
182 log("mm.ib[42] = %d", m->ib->at(42));
183
184 hnode_t* t = m->PrettyTree();
185 auto f = mylib::Stdout();
186 auto ast_f = Alloc<format::TextOutput>(f);
187 // fails with repr(void *)
188 // OK change the pretty printer!
189 format::PrintTree(t, ast_f);
190
191 PASS();
192}
193
194using typed_demo_asdl::flag_type;
195using typed_demo_asdl::flag_type__Bool;
196using typed_demo_asdl::SetToArg_;
197
198ObjHeader make_global(ObjHeader header) {
199 header.heap_tag = HeapTag::Global;
200 return header;
201}
202
203// TODO: We should always use these, rather than 'new flag_type::Bool()'
204GcGlobal<flag_type__Bool> g_ft = {make_global(flag_type__Bool::obj_header())};
205
206// Use __ style
207using typed_demo_asdl::cflow__Return;
208GcGlobal<cflow__Return> g_ret = {make_global(cflow__Return::obj_header()), {5}};
209
210int i0 = 7;
211
212// NOTE: This causes an failed assert() in the GC runtime
213#if 0
214List<int>* g_list = NewList<int>({i0, 8, 9});
215#endif
216
217// Dict<BigStr*, int> g_dict = {4, 5, 6};
218
219TEST literal_test() {
220 // Interesting, initializer list part of the constructor "runs". Otherwise
221 // this doesn't work.
222 log("g_ft.tag() = %d", g_ft.obj.tag());
223 auto ft = flag_type::Bool;
224 ASSERT_EQ(g_ft.obj.tag(), ft->tag());
225
226 log("g_ret.tag() = %d", g_ret.obj.tag());
227 log("g_ret.status = %d", g_ret.obj.status);
228 auto ret = Alloc<cflow__Return>(5);
229 ASSERT_EQ(g_ret.obj.tag(), ret->tag());
230 ASSERT_EQ(g_ret.obj.status, ret->status);
231
232#if 0
233 // Wow this works too? Is it the the constexpr interpreter, or is this code
234 // inserted before main()?
235 ASSERT_EQ(3, len(g_list));
236 ASSERT_EQ_FMT(7, g_list->at(0), "%d");
237 ASSERT_EQ_FMT(8, g_list->at(1), "%d");
238 ASSERT_EQ_FMT(9, g_list->at(2), "%d");
239#endif
240
241 PASS();
242}
243
244TEST string_defaults_test() {
245 auto st = Alloc<typed_demo_asdl::Strings>(kEmptyString, kEmptyString);
246 ASSERT_EQ(kEmptyString, st->required);
247 ASSERT_EQ(kEmptyString, st->optional);
248
249 st = typed_demo_asdl::Strings::CreateNull();
250 ASSERT_EQ(kEmptyString, st->required);
251 ASSERT_EQ(nullptr, st->optional);
252
253 st = Alloc<typed_demo_asdl::Strings>(kEmptyString, nullptr);
254 ASSERT_EQ(kEmptyString, st->required);
255 ASSERT_EQ(nullptr, st->optional);
256
257 PASS();
258}
259
260TEST list_defaults_test() {
261 auto o = op_array::CreateNull();
262 ASSERT_EQ(nullptr, o->ops);
263
264 // Empty list
265 auto o2 = op_array::CreateNull(true);
266 ASSERT_EQ(0, len(o2->ops));
267
268 PASS();
269}
270
271TEST subtype_test() {
272 // TODO: Also need to test GC header for List[int] subtypes
273
274 auto c = Alloc<CompoundWord>();
275
276 log("len = %d", len(c));
277
278 c->append(arith_expr::NoOp);
279 c->append(Alloc<arith_expr::Const>(42));
280
281 log("len = %d", len(c));
282
283#if 1
284 hnode_t* t1 = c->PrettyTree();
285 // ASSERT_EQ_FMT(hnode_e::Record, t1->tag(), "%d");
286
287 auto f = mylib::Stdout();
288 auto ast_f = Alloc<format::TextOutput>(f);
289 format::PrintTree(t1, ast_f);
290 printf("\n");
291#endif
292
293 PASS();
294}
295
296GREATEST_MAIN_DEFS();
297
298int main(int argc, char** argv) {
299 gHeap.Init();
300
301 GREATEST_MAIN_BEGIN();
302
303 RUN_TEST(misc_test);
304 RUN_TEST(shared_variant_test);
305 RUN_TEST(pretty_print_test);
306 RUN_TEST(dicts_test);
307 RUN_TEST(literal_test);
308 RUN_TEST(string_defaults_test);
309 RUN_TEST(list_defaults_test);
310 RUN_TEST(subtype_test);
311
312 gHeap.CleanProcessExit();
313
314 GREATEST_MAIN_END();
315
316 return 0;
317}