1 | #include "mycpp/runtime.h"
|
2 | #include "vendor/greatest.h"
|
3 |
|
4 | TEST tuple_field_masks_test() {
|
5 | if (sizeof(void *) != 8) {
|
6 | PASS();
|
7 | }
|
8 |
|
9 | auto ss = Tuple2<BigStr *, BigStr *>::obj_header();
|
10 | ASSERT_EQ_FMT(0b11, FIELD_MASK(ss), "%d");
|
11 |
|
12 | // 8 + 4 on 64 bit
|
13 | auto si = Tuple2<BigStr *, int>::obj_header();
|
14 | ASSERT_EQ_FMT(0b01, FIELD_MASK(si), "%d");
|
15 |
|
16 | // 4 + 8 on 64 bit
|
17 | auto is = Tuple2<int, BigStr *>::obj_header();
|
18 | ASSERT_EQ_FMT(0b10, FIELD_MASK(is), "%d");
|
19 |
|
20 | auto sss = Tuple3<BigStr *, BigStr *, BigStr *>::obj_header();
|
21 | ASSERT_EQ_FMT(0b111, FIELD_MASK(sss), "%d");
|
22 |
|
23 | auto iss = Tuple3<int, BigStr *, BigStr *>::obj_header();
|
24 | ASSERT_EQ_FMT(0b110, FIELD_MASK(iss), "%d");
|
25 |
|
26 | // 4 + 4 + 8 + 8, so it's 0b110 not 0b1100
|
27 | auto iiss = Tuple4<int, int, BigStr *, BigStr *>::obj_header();
|
28 | ASSERT_EQ_FMT(0b110, FIELD_MASK(iiss), "%d");
|
29 |
|
30 | PASS();
|
31 | }
|
32 |
|
33 | TEST tuple234_test() {
|
34 | Tuple2<int, int> *t2 = Alloc<Tuple2<int, int>>(5, 6);
|
35 | log("t2[0] = %d", t2->at0());
|
36 | log("t2[1] = %d", t2->at1());
|
37 |
|
38 | Tuple2<int, BigStr *> *u2 =
|
39 | Alloc<Tuple2<int, BigStr *>>(42, StrFromC("hello"));
|
40 | log("u2[0] = %d", u2->at0());
|
41 | log("u2[1] = %s", u2->at1()->data_);
|
42 |
|
43 | log("");
|
44 |
|
45 | auto t3 = Alloc<Tuple3<int, BigStr *, BigStr *>>(42, StrFromC("hello"),
|
46 | StrFromC("bye"));
|
47 | log("t3[0] = %d", t3->at0());
|
48 | log("t3[1] = %s", t3->at1()->data_);
|
49 | log("t3[2] = %s", t3->at2()->data_);
|
50 |
|
51 | log("");
|
52 |
|
53 | auto t4 = Alloc<Tuple4<int, BigStr *, BigStr *, int>>(42, StrFromC("4"),
|
54 | StrFromC("four"), -42);
|
55 |
|
56 | log("t4[0] = %d", t4->at0());
|
57 | log("t4[1] = %s", t4->at1()->data_);
|
58 | log("t4[2] = %s", t4->at2()->data_);
|
59 | log("t4[3] = %d", t4->at3());
|
60 |
|
61 | PASS();
|
62 | }
|
63 |
|
64 | TEST tuple_test() {
|
65 | gHeap.Collect();
|
66 | printf("\n");
|
67 |
|
68 | Tuple2<int, Tuple2<int, BigStr *> *> *t3 = nullptr;
|
69 | StackRoots _roots2({&t3});
|
70 |
|
71 | {
|
72 | Tuple2<int, int> *t0 = nullptr;
|
73 | Tuple2<int, BigStr *> *t1 = nullptr;
|
74 | Tuple2<int, BigStr *> *t2 = nullptr;
|
75 |
|
76 | BigStr *str0 = nullptr;
|
77 | BigStr *str1 = nullptr;
|
78 |
|
79 | StackRoots _roots({&str0, &str1, &t0, &t1, &t2});
|
80 |
|
81 | gHeap.Collect();
|
82 |
|
83 | str0 = StrFromC("foo_0");
|
84 | gHeap.Collect();
|
85 |
|
86 | str1 = StrFromC("foo_1");
|
87 |
|
88 | gHeap.Collect();
|
89 |
|
90 | t0 = Alloc<Tuple2<int, int>>(2, 3);
|
91 |
|
92 | gHeap.Collect();
|
93 |
|
94 | printf("%s\n", str0->data_);
|
95 | printf("%s\n", str1->data_);
|
96 |
|
97 | t1 = Alloc<Tuple2<int, BigStr *>>(4, str0);
|
98 | t2 = Alloc<Tuple2<int, BigStr *>>(5, str1);
|
99 |
|
100 | gHeap.Collect();
|
101 |
|
102 | printf("%s\n", str0->data_);
|
103 | printf("%s\n", str1->data_);
|
104 |
|
105 | printf("%d = %d\n", t0->at0(), t0->at1());
|
106 | printf("%d = %s\n", t1->at0(), t1->at1()->data_);
|
107 | printf("%d = %s\n", t2->at0(), t2->at1()->data_);
|
108 |
|
109 | gHeap.Collect();
|
110 |
|
111 | t3 = Alloc<Tuple2<int, Tuple2<int, BigStr *> *>>(6, t2);
|
112 |
|
113 | gHeap.Collect();
|
114 | }
|
115 |
|
116 | printf("%d = { %d = %s }\n", t3->at0(), t3->at1()->at0(),
|
117 | t3->at1()->at1()->data_);
|
118 |
|
119 | gHeap.Collect();
|
120 |
|
121 | PASS();
|
122 | }
|
123 |
|
124 | GREATEST_MAIN_DEFS();
|
125 |
|
126 | int main(int argc, char **argv) {
|
127 | gHeap.Init();
|
128 |
|
129 | GREATEST_MAIN_BEGIN();
|
130 |
|
131 | RUN_TEST(tuple_field_masks_test);
|
132 | RUN_TEST(tuple234_test);
|
133 | RUN_TEST(tuple_test);
|
134 |
|
135 | gHeap.CleanProcessExit();
|
136 |
|
137 | GREATEST_MAIN_END(); /* display results */
|
138 | return 0;
|
139 | }
|