OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #define TESTING | |
6 | |
7 #include <stdint.h> | |
8 #include <sys/time.h> | |
9 | |
10 #include <cstdio> | |
11 | |
12 #include "src/shared/assert.h" // NOLINT(build/include) | |
13 #include "conformance_test_shared.h" // NOLINT(build/include) | |
14 #include "cc/conformance_service.h" // NOLINT(build/include) | |
15 | |
16 static void BuildPerson(PersonBuilder person, int n) { | |
17 person.setAge(n * 20); | |
18 if (n > 1) { | |
19 List<PersonBuilder> children = person.initChildren(2); | |
20 BuildPerson(children[0], n - 1); | |
21 BuildPerson(children[1], n - 1); | |
22 } | |
23 } | |
24 | |
25 static int Depth(Node node) { | |
26 if (node.isNum()) return 1; | |
27 int left = Depth(node.getCons().getFst()); | |
28 int right = Depth(node.getCons().getSnd()); | |
29 return 1 + ((left > right) ? left : right); | |
30 } | |
31 | |
32 static void* FooCallbackData = reinterpret_cast<void*>(101); | |
33 static void FooCallback(void* data) { | |
34 EXPECT_EQ(FooCallbackData, data); | |
35 } | |
36 | |
37 static void* BarCallbackData = reinterpret_cast<void*>(102); | |
38 static void BarCallback(int i, void* data) { | |
39 EXPECT_EQ(BarCallbackData, data); | |
40 EXPECT_EQ(24, i); | |
41 } | |
42 | |
43 static void PingCallback(int result, void* data) { | |
44 EXPECT_EQ(42, result); | |
45 } | |
46 | |
47 static void CreateAgeStatsCallback(AgeStats stats, void* data) { | |
48 EXPECT_EQ(42, stats.getAverageAge()); | |
49 EXPECT_EQ(42, stats.getSum()); | |
50 stats.Delete(); | |
51 } | |
52 | |
53 static void CreatePersonCallback(Person generated, void* data) { | |
54 EXPECT_EQ(42, generated.getAge()); | |
55 char* name = generated.getName(); | |
56 int name_length = strlen(name); | |
57 EXPECT_EQ(6, name_length); | |
58 EXPECT(strcmp(name, "person") == 0); | |
59 free(name); | |
60 List<uint16_t> name_data = generated.getNameData(); | |
61 EXPECT_EQ(6, name_data.length()); | |
62 EXPECT_EQ('p', name_data[0]); | |
63 EXPECT_EQ('n', name_data[5]); | |
64 | |
65 List<Person> children = generated.getChildren(); | |
66 EXPECT_EQ(10, children.length()); | |
67 for (int i = 0; i < children.length(); i++) { | |
68 EXPECT_EQ(12 + i * 2, children[i].getAge()); | |
69 } | |
70 generated.Delete(); | |
71 } | |
72 | |
73 static void CreateNodeCallback(Node node, void* data) { | |
74 EXPECT_EQ(24680, node.ComputeUsed()); | |
75 EXPECT_EQ(10, Depth(node)); | |
76 node.Delete(); | |
77 } | |
78 | |
79 static void GetAgeCallback(int age, void* data) { | |
80 EXPECT_EQ(140, age); | |
81 } | |
82 | |
83 static void CountCallback(int count, void* data) { | |
84 EXPECT_EQ(127, count); | |
85 } | |
86 | |
87 static void GetAgeStatsCallback(AgeStats stats, void* data) { | |
88 EXPECT_EQ(39, stats.getAverageAge()); | |
89 EXPECT_EQ(4940, stats.getSum()); | |
90 stats.Delete(); | |
91 } | |
92 | |
93 static void FlipTableCallback(TableFlip flip_result, void* data) { | |
94 const char* expected_flip = "(╯°□°)╯︵ ┻━┻"; | |
95 char* result = flip_result.getFlip(); | |
96 EXPECT(strcmp(result, expected_flip) == 0); | |
97 free(result); | |
98 flip_result.Delete(); | |
99 } | |
100 | |
101 static void InternalFieldsCallback(InternalFields internalFields, void* data) { | |
102 const int32_t expected_offset = 1337; | |
103 const char* expected_segment = "h4x0r"; | |
104 int32_t offset = internalFields.getOffset(); | |
105 char* segment = internalFields.getSegment(); | |
106 EXPECT(offset == expected_offset); | |
107 EXPECT(strcmp(segment, expected_segment) == 0); | |
108 free(segment); | |
109 internalFields.Delete(); | |
110 } | |
111 | |
112 static void RunPersonTests() { | |
113 { | |
114 MessageBuilder builder(512); | |
115 PersonBuilder person = builder.initRoot<PersonBuilder>(); | |
116 BuildPerson(person, 7); | |
117 EXPECT_EQ(3136, builder.ComputeUsed()); | |
118 int age = ConformanceService::getAge(person); | |
119 EXPECT_EQ(140, age); | |
120 } | |
121 | |
122 { | |
123 MessageBuilder builder(512); | |
124 PersonBuilder person = builder.initRoot<PersonBuilder>(); | |
125 BuildPerson(person, 7); | |
126 EXPECT_EQ(3136, builder.ComputeUsed()); | |
127 ConformanceService::getAgeAsync(person, GetAgeCallback, NULL); | |
128 } | |
129 | |
130 { | |
131 MessageBuilder builder(512); | |
132 PersonBuilder person = builder.initRoot<PersonBuilder>(); | |
133 BuildPerson(person, 7); | |
134 EXPECT_EQ(3136, builder.ComputeUsed()); | |
135 int count = ConformanceService::count(person); | |
136 EXPECT_EQ(127, count); | |
137 } | |
138 | |
139 { | |
140 MessageBuilder builder(512); | |
141 PersonBuilder person = builder.initRoot<PersonBuilder>(); | |
142 BuildPerson(person, 7); | |
143 EXPECT_EQ(3136, builder.ComputeUsed()); | |
144 ConformanceService::countAsync(person, CountCallback, NULL); | |
145 } | |
146 | |
147 { | |
148 MessageBuilder builder(512); | |
149 PersonBuilder person = builder.initRoot<PersonBuilder>(); | |
150 BuildPerson(person, 7); | |
151 EXPECT_EQ(3136, builder.ComputeUsed()); | |
152 AgeStats stats = ConformanceService::getAgeStats(person); | |
153 EXPECT_EQ(39, stats.getAverageAge()); | |
154 EXPECT_EQ(4940, stats.getSum()); | |
155 stats.Delete(); | |
156 } | |
157 | |
158 { | |
159 MessageBuilder builder(512); | |
160 PersonBuilder person = builder.initRoot<PersonBuilder>(); | |
161 BuildPerson(person, 7); | |
162 EXPECT_EQ(3136, builder.ComputeUsed()); | |
163 ConformanceService::getAgeStatsAsync(person, GetAgeStatsCallback, NULL); | |
164 } | |
165 | |
166 { | |
167 AgeStats stats = ConformanceService::createAgeStats(42, 42); | |
168 EXPECT_EQ(42, stats.getAverageAge()); | |
169 EXPECT_EQ(42, stats.getSum()); | |
170 stats.Delete(); | |
171 } | |
172 | |
173 ConformanceService::createAgeStatsAsync(42, 42, CreateAgeStatsCallback, NULL); | |
174 | |
175 { | |
176 Person generated = ConformanceService::createPerson(10); | |
177 char* name = generated.getName(); | |
178 int name_length = strlen(name); | |
179 EXPECT_EQ(42, generated.getAge()); | |
180 EXPECT_EQ(6, name_length); | |
181 EXPECT(strcmp(name, "person") == 0); | |
182 free(name); | |
183 List<uint16_t> name_data = generated.getNameData(); | |
184 EXPECT_EQ(6, name_data.length()); | |
185 EXPECT_EQ('p', name_data[0]); | |
186 EXPECT_EQ('n', name_data[5]); | |
187 | |
188 List<Person> children = generated.getChildren(); | |
189 EXPECT_EQ(10, children.length()); | |
190 for (int i = 0; i < children.length(); i++) { | |
191 EXPECT_EQ(12 + i * 2, children[i].getAge()); | |
192 } | |
193 generated.Delete(); | |
194 } | |
195 | |
196 ConformanceService::createPersonAsync(10, CreatePersonCallback, NULL); | |
197 | |
198 ConformanceService::foo(); | |
199 ConformanceService::fooAsync(FooCallback, FooCallbackData); | |
200 | |
201 { | |
202 MessageBuilder builder(512); | |
203 EmptyBuilder empty = builder.initRoot<EmptyBuilder>(); | |
204 int i = ConformanceService::bar(empty); | |
205 EXPECT_EQ(24, i); | |
206 } | |
207 | |
208 { | |
209 MessageBuilder builder(512); | |
210 EmptyBuilder empty = builder.initRoot<EmptyBuilder>(); | |
211 ConformanceService::barAsync(empty, BarCallback, BarCallbackData); | |
212 } | |
213 | |
214 EXPECT_EQ(42, ConformanceService::ping()); | |
215 ConformanceService::pingAsync(PingCallback, NULL); | |
216 | |
217 { | |
218 MessageBuilder builder(512); | |
219 TableFlipBuilder flip = builder.initRoot<TableFlipBuilder>(); | |
220 const char* expected_flip = "(╯°□°)╯︵ ┻━┻"; | |
221 flip.setFlip(expected_flip); | |
222 TableFlip flip_result = ConformanceService::flipTable(flip); | |
223 char* result = flip_result.getFlip(); | |
224 EXPECT(strcmp(result, expected_flip) == 0); | |
225 free(result); | |
226 flip_result.Delete(); | |
227 } | |
228 | |
229 { | |
230 MessageBuilder builder(512); | |
231 TableFlipBuilder flip = builder.initRoot<TableFlipBuilder>(); | |
232 flip.setFlip("(╯°□°)╯︵ ┻━┻"); | |
233 ConformanceService::flipTableAsync(flip, FlipTableCallback, NULL); | |
234 } | |
235 | |
236 { | |
237 MessageBuilder builder(512); | |
238 InternalFieldsBuilder internalFields = | |
239 builder.initRoot<InternalFieldsBuilder>(); | |
240 internalFields.setOffset(1337); | |
241 internalFields.setSegment("h4x0r"); | |
242 ConformanceService::internalizeAsync(internalFields, | |
243 InternalFieldsCallback, | |
244 NULL); | |
245 } | |
246 } | |
247 | |
248 static void RunPersonBoxTests() { | |
249 MessageBuilder builder(512); | |
250 | |
251 PersonBoxBuilder box = builder.initRoot<PersonBoxBuilder>(); | |
252 PersonBuilder person = box.initPerson(); | |
253 person.setAge(87); | |
254 person.setName("fisk"); | |
255 | |
256 int age = ConformanceService::getBoxedAge(box); | |
257 EXPECT_EQ(87, age); | |
258 } | |
259 | |
260 static void BuildNode(NodeBuilder node, int n) { | |
261 if (n > 1) { | |
262 ConsBuilder cons = node.initCons(); | |
263 BuildNode(cons.initFst(), n - 1); | |
264 BuildNode(cons.initSnd(), n - 1); | |
265 } else { | |
266 node.setCond(true); | |
267 node.setNum(42); | |
268 } | |
269 } | |
270 | |
271 static void RunNodeTests() { | |
272 MessageBuilder builder(512); | |
273 | |
274 NodeBuilder root = builder.initRoot<NodeBuilder>(); | |
275 BuildNode(root, 10); | |
276 int depth = ConformanceService::depth(root); | |
277 EXPECT_EQ(10, depth); | |
278 | |
279 Node node = ConformanceService::createNode(10); | |
280 EXPECT_EQ(24680, node.ComputeUsed()); | |
281 EXPECT_EQ(10, Depth(node)); | |
282 node.Delete(); | |
283 | |
284 ConformanceService::createNodeAsync(10, CreateNodeCallback, NULL); | |
285 } | |
286 | |
287 static void InteractWithService() { | |
288 ConformanceService::setup(); | |
289 RunPersonTests(); | |
290 RunPersonBoxTests(); | |
291 RunNodeTests(); | |
292 ConformanceService::tearDown(); | |
293 } | |
294 | |
295 int main(int argc, char** argv) { | |
296 if (argc < 2) { | |
297 printf("Usage: %s <snapshot>\n", argv[0]); | |
298 return 1; | |
299 } | |
300 SetupConformanceTest(argc, argv); | |
301 InteractWithService(); | |
302 TearDownConformanceTest(); | |
303 return 0; | |
304 } | |
OLD | NEW |