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 import dartino.*; | |
6 | |
7 import java.io.FileInputStream; | |
8 import java.io.FileNotFoundException; | |
9 import java.io.IOException; | |
10 | |
11 import java.util.List; | |
12 | |
13 class ConformanceTest { | |
14 public static void main(String args[]) { | |
15 // Expecting a snapshot of the dart service code on the command line. | |
16 if (args.length != 1) { | |
17 System.out.println("Usage: java ConformanceTest <snapshot>"); | |
18 System.out.println(" or: java ConformanceTest <port>"); | |
19 System.exit(1); | |
20 } | |
21 | |
22 // Load libdartino.so. | |
23 System.loadLibrary("dartino"); | |
24 | |
25 // Setup Dartino. | |
26 DartinoApi.Setup(); | |
27 DartinoServiceApi.Setup(); | |
28 DartinoApi.AddDefaultSharedLibrary("libdartino.so"); | |
29 | |
30 int port = 0; | |
31 try { | |
32 port = Integer.parseInt(args[0]); | |
33 } catch (NumberFormatException e) { | |
34 // Since the argument is not a number we expect it to be the snapshot. | |
35 } | |
36 | |
37 if (port > 0) { | |
38 System.out.println( | |
39 "Waiting for debugger connection on localhost:" + port); | |
40 Thread dartThread = new Thread(new DebugRunner(port)); | |
41 dartThread.start(); | |
42 } else { | |
43 try { | |
44 // Load snapshot and start Dart code on a separate thread. | |
45 FileInputStream snapshotStream = new FileInputStream(args[0]); | |
46 int available = snapshotStream.available(); | |
47 byte[] snapshot = new byte[available]; | |
48 snapshotStream.read(snapshot); | |
49 Thread dartThread = new Thread(new SnapshotRunner(snapshot)); | |
50 dartThread.start(); | |
51 } catch (FileNotFoundException e) { | |
52 System.err.println("Failed loading snapshot"); | |
53 System.exit(1); | |
54 } catch (IOException e) { | |
55 System.err.println("Failed loading snapshot"); | |
56 System.exit(1); | |
57 } | |
58 } | |
59 | |
60 // Run conformance tests. | |
61 ConformanceService.Setup(); | |
62 try { | |
63 runPersonTests(); | |
64 runPersonBoxTests(); | |
65 runNodeTests(); | |
66 } finally { | |
67 ConformanceService.TearDown(); | |
68 } | |
69 } | |
70 | |
71 private static void buildPerson(PersonBuilder person, int n) { | |
72 person.setAge(n * 20); | |
73 if (n > 1) { | |
74 PersonListBuilder children = person.initChildren(2); | |
75 buildPerson(children.get(0), n - 1); | |
76 buildPerson(children.get(1), n - 1); | |
77 } | |
78 } | |
79 | |
80 private static void runPersonTests() { | |
81 { | |
82 MessageBuilder builder = new MessageBuilder(512); | |
83 PersonBuilder person = new PersonBuilder(); | |
84 builder.initRoot(person, PersonBuilder.kSize); | |
85 buildPerson(person, 7); | |
86 assert 3144 == builder.computeUsed(); | |
87 int age = ConformanceService.getAge(person); | |
88 assert 140 == age; | |
89 } | |
90 | |
91 { | |
92 MessageBuilder builder = new MessageBuilder(512); | |
93 PersonBuilder person = new PersonBuilder(); | |
94 builder.initRoot(person, PersonBuilder.kSize); | |
95 buildPerson(person, 7); | |
96 assert 3144 == builder.computeUsed(); | |
97 ConformanceService.getAgeAsync( | |
98 person, | |
99 new ConformanceService.GetAgeCallback() { | |
100 public void handle(int age) { | |
101 assert 140 == age; | |
102 } | |
103 }); | |
104 } | |
105 | |
106 { | |
107 MessageBuilder builder = new MessageBuilder(512); | |
108 PersonBuilder person = new PersonBuilder(); | |
109 builder.initRoot(person, PersonBuilder.kSize); | |
110 buildPerson(person, 7); | |
111 assert 3144 == builder.computeUsed(); | |
112 int count = ConformanceService.count(person); | |
113 assert 127 == count; | |
114 } | |
115 | |
116 { | |
117 MessageBuilder builder = new MessageBuilder(512); | |
118 PersonBuilder person = new PersonBuilder(); | |
119 builder.initRoot(person, PersonBuilder.kSize); | |
120 buildPerson(person, 7); | |
121 assert 3144 == builder.computeUsed(); | |
122 ConformanceService.countAsync( | |
123 person, | |
124 new ConformanceService.CountCallback() { | |
125 public void handle(int count) { | |
126 assert 127 == count; | |
127 } | |
128 }); | |
129 } | |
130 | |
131 { | |
132 MessageBuilder builder = new MessageBuilder(512); | |
133 PersonBuilder person = new PersonBuilder(); | |
134 builder.initRoot(person, PersonBuilder.kSize); | |
135 buildPerson(person, 7); | |
136 assert 3144 == builder.computeUsed(); | |
137 AgeStats stats = ConformanceService.getAgeStats(person); | |
138 assert 39 == stats.getAverageAge(); | |
139 assert 4940 == stats.getSum(); | |
140 } | |
141 | |
142 { | |
143 MessageBuilder builder = new MessageBuilder(512); | |
144 PersonBuilder person = new PersonBuilder(); | |
145 builder.initRoot(person, PersonBuilder.kSize); | |
146 buildPerson(person, 7); | |
147 assert 3144 == builder.computeUsed(); | |
148 ConformanceService.getAgeStatsAsync( | |
149 person, | |
150 new ConformanceService.GetAgeStatsCallback() { | |
151 public void handle(AgeStats stats) { | |
152 assert 39 == stats.getAverageAge(); | |
153 assert 4940 == stats.getSum(); | |
154 } | |
155 }); | |
156 } | |
157 | |
158 { | |
159 AgeStats stats = ConformanceService.createAgeStats(42, 42); | |
160 assert 42 == stats.getAverageAge(); | |
161 assert 42 == stats.getSum(); | |
162 } | |
163 | |
164 { | |
165 ConformanceService.createAgeStatsAsync( | |
166 42, 42, | |
167 new ConformanceService.CreateAgeStatsCallback() { | |
168 public void handle(AgeStats stats) { | |
169 assert 42 == stats.getAverageAge(); | |
170 assert 42 == stats.getSum(); | |
171 } | |
172 }); | |
173 } | |
174 | |
175 { | |
176 Person generated = ConformanceService.createPerson(10); | |
177 assert 42 == generated.getAge(); | |
178 String name = generated.getName(); | |
179 assert 6 == name.length(); | |
180 assert name.equals("person"); | |
181 Uint16List nameData = generated.getNameData(); | |
182 assert 6 == nameData.size(); | |
183 assert "p".charAt(0) == nameData.get(0); | |
184 assert "n".charAt(0) == nameData.get(5); | |
185 PersonList children = generated.getChildren(); | |
186 assert 10 == children.size(); | |
187 for (int i = 0; i < children.size(); i++) { | |
188 assert (12 + i * 2) == children.get(i).getAge(); | |
189 } | |
190 } | |
191 | |
192 { | |
193 ConformanceService.createPersonAsync( | |
194 10, | |
195 new ConformanceService.CreatePersonCallback() { | |
196 public void handle(Person generated) { | |
197 assert 42 == generated.getAge(); | |
198 String name = generated.getName(); | |
199 assert 6 == name.length(); | |
200 assert name.equals("person"); | |
201 Uint16List nameData = generated.getNameData(); | |
202 assert 6 == nameData.size(); | |
203 assert "p".charAt(0) == nameData.get(0); | |
204 assert "n".charAt(0) == nameData.get(5); | |
205 PersonList children = generated.getChildren(); | |
206 assert 10 == children.size(); | |
207 for (int i = 0; i < children.size(); i++) { | |
208 assert (12 + i * 2) == children.get(i).getAge(); | |
209 } | |
210 } | |
211 }); | |
212 } | |
213 | |
214 ConformanceService.foo(); | |
215 ConformanceService.fooAsync(new ConformanceService.FooCallback() { | |
216 final int data = 101; | |
217 public void handle() { | |
218 assert 101 == data; | |
219 } | |
220 }); | |
221 | |
222 { | |
223 MessageBuilder builder = new MessageBuilder(512); | |
224 EmptyBuilder empty = new EmptyBuilder(); | |
225 builder.initRoot(empty, EmptyBuilder.kSize); | |
226 int i = ConformanceService.bar(empty); | |
227 assert 24 == i; | |
228 } | |
229 | |
230 { | |
231 MessageBuilder builder = new MessageBuilder(512); | |
232 EmptyBuilder empty = new EmptyBuilder(); | |
233 builder.initRoot(empty, EmptyBuilder.kSize); | |
234 ConformanceService.barAsync(empty, new ConformanceService.BarCallback() { | |
235 final int data = 102; | |
236 public void handle(int i) { | |
237 assert 102 == data; | |
238 assert 24 == i; | |
239 } | |
240 }); | |
241 } | |
242 | |
243 assert 42 == ConformanceService.ping(); | |
244 ConformanceService.pingAsync(new ConformanceService.PingCallback() { | |
245 public void handle(int result) { assert 42 == result; } | |
246 }); | |
247 | |
248 { | |
249 MessageBuilder builder = new MessageBuilder(512); | |
250 TableFlipBuilder flip = new TableFlipBuilder(); | |
251 builder.initRoot(flip, TableFlipBuilder.kSize); | |
252 String expectedFlip = "(╯°□°)╯︵ ┻━┻"; | |
253 flip.setFlip(expectedFlip); | |
254 TableFlip flipResult = ConformanceService.flipTable(flip); | |
255 assert flipResult.getFlip().equals(expectedFlip); | |
256 } | |
257 | |
258 { | |
259 MessageBuilder builder = new MessageBuilder(512); | |
260 TableFlipBuilder flip = new TableFlipBuilder(); | |
261 builder.initRoot(flip, TableFlipBuilder.kSize); | |
262 final String expectedFlip = "(╯°□°)╯︵ ┻━┻"; | |
263 flip.setFlip(expectedFlip); | |
264 ConformanceService.flipTableAsync( | |
265 flip, | |
266 new ConformanceService.FlipTableCallback() { | |
267 public void handle(TableFlip flipResult) { | |
268 assert flipResult.getFlip().equals(expectedFlip); | |
269 }; | |
270 }); | |
271 } | |
272 } | |
273 | |
274 private static void runPersonBoxTests() { | |
275 MessageBuilder builder = new MessageBuilder(512); | |
276 | |
277 PersonBoxBuilder box = new PersonBoxBuilder(); | |
278 builder.initRoot(box, PersonBoxBuilder.kSize); | |
279 PersonBuilder person = box.initPerson(); | |
280 person.setAge(87); | |
281 person.setName("fisk"); | |
282 int age = ConformanceService.getBoxedAge(box); | |
283 assert 87 == age; | |
284 } | |
285 | |
286 | |
287 private static int depth(Node node) { | |
288 if (node.isNum()) return 1; | |
289 int left = depth(node.getCons().getFst()); | |
290 int right = depth(node.getCons().getSnd()); | |
291 return 1 + ((left > right) ? left : right); | |
292 } | |
293 | |
294 private static void buildNode(NodeBuilder node, int n) { | |
295 if (n > 1) { | |
296 ConsBuilder cons = node.initCons(); | |
297 buildNode(cons.initFst(), n - 1); | |
298 buildNode(cons.initSnd(), n - 1); | |
299 } else { | |
300 node.setCond(true); | |
301 node.setNum(42); | |
302 } | |
303 } | |
304 | |
305 private static void runNodeTests() { | |
306 MessageBuilder builder = new MessageBuilder(512); | |
307 | |
308 NodeBuilder root = new NodeBuilder(); | |
309 builder.initRoot(root, NodeBuilder.kSize); | |
310 buildNode(root, 10); | |
311 int depth = ConformanceService.depth(root); | |
312 assert 10 == depth; | |
313 | |
314 Node node = ConformanceService.createNode(10); | |
315 assert 24680 == node.computeUsed(); | |
316 assert 10 == depth(node); | |
317 } | |
318 } | |
OLD | NEW |