1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <unittest/unittest.h>
6 
7 #include <banjo/flat_ast.h>
8 #include <banjo/lexer.h>
9 #include <banjo/parser.h>
10 #include <banjo/source_file.h>
11 
12 #include "test_library.h"
13 
14 namespace {
15 
16 class MaxBytesLibrary : public TestLibrary {
17 public:
MaxBytesLibrary()18     MaxBytesLibrary()
19         : TestLibrary("max_bytes.banjo", R"BANJO(
20 library banjo.test.maxbytes;
21 
22 struct OneBool {
23   bool b;
24 };
25 
26 struct OptionalOneBool {
27   OneBool? s;
28 };
29 
30 struct TwoBools {
31   bool a;
32   bool b;
33 };
34 
35 struct OptionalTwoBools {
36   TwoBools? s;
37 };
38 
39 struct BoolAndU32 {
40   bool b;
41   uint32 u;
42 };
43 
44 struct OptionalBoolAndU32 {
45   BoolAndU32? s;
46 };
47 
48 struct BoolAndU64 {
49   bool b;
50   uint64 u;
51 };
52 
53 struct OptionalBoolAndU64 {
54   BoolAndU64? s;
55 };
56 
57 union UnionOfThings {
58   OneBool ob;
59   BoolAndU64 bu;
60 };
61 
62 struct OptionalUnion {
63   UnionOfThings? u;
64 };
65 
66 struct PaddedVector {
67   vector<int32>:3 pv;
68 };
69 
70 struct UnboundedVector {
71   vector<int32> uv;
72 };
73 
74 struct UnboundedVectors {
75   vector<int32> uv1;
76   vector<int32> uv2;
77 };
78 
79 struct ShortString {
80   string:5 s;
81 };
82 
83 struct UnboundedString {
84   string s;
85 };
86 
87 struct AnArray {
88   array<int64>:5 a;
89 };
90 
91 )BANJO") {}
92 };
93 
simple_structs(void)94 static bool simple_structs(void) {
95     BEGIN_TEST;
96 
97     MaxBytesLibrary test_library;
98     EXPECT_TRUE(test_library.Compile());
99 
100     auto one_bool = test_library.LookupStruct("OneBool");
101     EXPECT_NONNULL(one_bool);
102     EXPECT_EQ(one_bool->typeshape.Size(), 1);
103     EXPECT_EQ(one_bool->typeshape.MaxOutOfLine(), 0);
104 
105     auto two_bools = test_library.LookupStruct("TwoBools");
106     EXPECT_NONNULL(two_bools);
107     EXPECT_EQ(two_bools->typeshape.Size(), 2);
108     EXPECT_EQ(two_bools->typeshape.MaxOutOfLine(), 0);
109 
110     auto bool_and_u32 = test_library.LookupStruct("BoolAndU32");
111     EXPECT_NONNULL(bool_and_u32);
112     EXPECT_EQ(bool_and_u32->typeshape.Size(), 8);
113     EXPECT_EQ(bool_and_u32->typeshape.MaxOutOfLine(), 0);
114 
115     auto bool_and_u64 = test_library.LookupStruct("BoolAndU64");
116     EXPECT_NONNULL(bool_and_u64);
117     EXPECT_EQ(bool_and_u64->typeshape.Size(), 16);
118     EXPECT_EQ(bool_and_u64->typeshape.MaxOutOfLine(), 0);
119 
120     END_TEST;
121 }
122 
optional_structs(void)123 static bool optional_structs(void) {
124     BEGIN_TEST;
125 
126     MaxBytesLibrary test_library;
127     EXPECT_TRUE(test_library.Compile());
128 
129     auto one_bool = test_library.LookupStruct("OptionalOneBool");
130     EXPECT_NONNULL(one_bool);
131     EXPECT_EQ(one_bool->typeshape.Size(), 8);
132     EXPECT_EQ(one_bool->typeshape.MaxOutOfLine(), 8);
133 
134     auto two_bools = test_library.LookupStruct("OptionalTwoBools");
135     EXPECT_NONNULL(two_bools);
136     EXPECT_EQ(two_bools->typeshape.Size(), 8);
137     EXPECT_EQ(two_bools->typeshape.MaxOutOfLine(), 8);
138 
139     auto bool_and_u32 = test_library.LookupStruct("OptionalBoolAndU32");
140     EXPECT_NONNULL(bool_and_u32);
141     EXPECT_EQ(bool_and_u32->typeshape.Size(), 8);
142     EXPECT_EQ(bool_and_u32->typeshape.MaxOutOfLine(), 8);
143 
144     auto bool_and_u64 = test_library.LookupStruct("OptionalBoolAndU64");
145     EXPECT_NONNULL(bool_and_u64);
146     EXPECT_EQ(bool_and_u64->typeshape.Size(), 8);
147     EXPECT_EQ(bool_and_u64->typeshape.MaxOutOfLine(), 16);
148 
149     END_TEST;
150 }
unions(void)151 static bool unions(void) {
152     BEGIN_TEST;
153 
154     MaxBytesLibrary test_library;
155     EXPECT_TRUE(test_library.Compile());
156 
157     auto a_union = test_library.LookupUnion("UnionOfThings");
158     EXPECT_NONNULL(a_union);
159     EXPECT_EQ(a_union->typeshape.Size(), 24);
160     EXPECT_EQ(a_union->typeshape.MaxOutOfLine(), 0);
161 
162     auto optional_union = test_library.LookupStruct("OptionalUnion");
163     EXPECT_NONNULL(optional_union);
164     EXPECT_EQ(optional_union->typeshape.Size(), 8);
165     EXPECT_EQ(optional_union->typeshape.MaxOutOfLine(), 24);
166 
167     END_TEST;
168 }
169 
vectors(void)170 static bool vectors(void) {
171     BEGIN_TEST;
172 
173     MaxBytesLibrary test_library;
174     EXPECT_TRUE(test_library.Compile());
175 
176     auto padded_vector = test_library.LookupStruct("PaddedVector");
177     EXPECT_NONNULL(padded_vector);
178     EXPECT_EQ(padded_vector->typeshape.Size(), 16);
179     EXPECT_EQ(padded_vector->typeshape.MaxOutOfLine(), 16);
180 
181     auto unbounded_vector = test_library.LookupStruct("UnboundedVector");
182     EXPECT_NONNULL(unbounded_vector);
183     EXPECT_EQ(unbounded_vector->typeshape.Size(), 16);
184     EXPECT_EQ(unbounded_vector->typeshape.MaxOutOfLine(), std::numeric_limits<uint32_t>::max());
185 
186     auto unbounded_vectors = test_library.LookupStruct("UnboundedVectors");
187     EXPECT_NONNULL(unbounded_vectors);
188     EXPECT_EQ(unbounded_vectors->typeshape.Size(), 32);
189     EXPECT_EQ(unbounded_vectors->typeshape.MaxOutOfLine(), std::numeric_limits<uint32_t>::max());
190 
191     END_TEST;
192 }
193 
strings(void)194 static bool strings(void) {
195     BEGIN_TEST;
196 
197     MaxBytesLibrary test_library;
198     EXPECT_TRUE(test_library.Compile());
199 
200     auto short_string = test_library.LookupStruct("ShortString");
201     EXPECT_NONNULL(short_string);
202     EXPECT_EQ(short_string->typeshape.Size(), 16);
203     EXPECT_EQ(short_string->typeshape.MaxOutOfLine(), 8);
204 
205     auto unbounded_string = test_library.LookupStruct("UnboundedString");
206     EXPECT_NONNULL(unbounded_string);
207     EXPECT_EQ(unbounded_string->typeshape.Size(), 16);
208     EXPECT_EQ(unbounded_string->typeshape.MaxOutOfLine(), std::numeric_limits<uint32_t>::max());
209 
210     END_TEST;
211 }
212 
arrays(void)213 static bool arrays(void) {
214     BEGIN_TEST;
215 
216     MaxBytesLibrary test_library;
217     EXPECT_TRUE(test_library.Compile());
218 
219     auto an_array = test_library.LookupStruct("AnArray");
220     EXPECT_NONNULL(an_array);
221     EXPECT_EQ(an_array->typeshape.Size(), 40);
222     EXPECT_EQ(an_array->typeshape.MaxOutOfLine(), 0);
223 
224     END_TEST;
225 }
226 
227 } // namespace
228 
229 BEGIN_TEST_CASE(max_bytes_tests);
230 RUN_TEST(simple_structs);
231 RUN_TEST(optional_structs);
232 RUN_TEST(unions);
233 RUN_TEST(vectors);
234 RUN_TEST(strings);
235 RUN_TEST(arrays);
236 END_TEST_CASE(max_bytes_tests);
237