OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart 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 file. | |
4 | |
5 // Dart test program for testing serialization of messages. | |
6 // VMOptions=--enable_type_checks --enable_asserts | |
7 | |
8 #library('MessageTest'); | |
9 #import("dart:isolate"); | |
10 #import('../../../lib/unittest/unittest.dart'); | |
11 | |
12 // --------------------------------------------------------------------------- | |
13 // Message passing test. | |
14 // --------------------------------------------------------------------------- | |
15 | |
16 class MessageTest { | |
17 static final List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; | |
18 static final List list2 = const [null, list1, list1, list1, list1]; | |
19 static final List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; | |
20 static final Map map1 = const { | |
21 "a=1" : 1, "b=2" : 2, "c=3" : 3, | |
22 }; | |
23 static final Map map2 = const { | |
24 "list1" : list1, "list2" : list2, "list3" : list3, | |
25 }; | |
26 static final List list4 = const [map1, map2]; | |
27 static final List elms = const [ | |
28 list1, list2, list3, list4, | |
29 ]; | |
30 | |
31 static void VerifyMap(Map expected, Map actual) { | |
32 Expect.equals(true, expected is Map); | |
33 Expect.equals(true, actual is Map); | |
34 Expect.equals(expected.length, actual.length); | |
35 testForEachMap(key, value) { | |
36 if (value is List) { | |
37 VerifyList(value, actual[key]); | |
38 } else { | |
39 Expect.equals(value, actual[key]); | |
40 } | |
41 } | |
42 expected.forEach(testForEachMap); | |
43 } | |
44 | |
45 static void VerifyList(List expected, List actual) { | |
46 for (int i = 0; i < expected.length; i++) { | |
47 if (expected[i] is List) { | |
48 VerifyList(expected[i], actual[i]); | |
49 } else if (expected[i] is Map) { | |
50 VerifyMap(expected[i], actual[i]); | |
51 } else { | |
52 Expect.equals(expected[i], actual[i]); | |
53 } | |
54 } | |
55 } | |
56 | |
57 static void VerifyObject(int index, var actual) { | |
58 var expected = elms[index]; | |
59 Expect.equals(true, expected is List); | |
60 Expect.equals(true, actual is List); | |
61 Expect.equals(expected.length, actual.length); | |
62 VerifyList(expected, actual); | |
63 } | |
64 } | |
65 | |
66 class PingPongServer extends Isolate { | |
67 PingPongServer() : super() {} | |
68 | |
69 void main() { | |
70 int count = 0; | |
71 this.port.receive( | |
72 (var message, SendPort replyTo) { | |
73 if (message == -1) { | |
74 this.port.close(); | |
75 replyTo.send(count, null); | |
76 } else { | |
77 // Check if the received object is correct. | |
78 if (count < MessageTest.elms.length) { | |
79 MessageTest.VerifyObject(count, message); | |
80 } | |
81 // Bounce the received object back so that the sender | |
82 // can make sure that the object matches. | |
83 replyTo.send(message, null); | |
84 count++; | |
85 } | |
86 }); | |
87 } | |
88 } | |
89 | |
90 main() { | |
91 test("send objects and receive them back", () { | |
92 new PingPongServer().spawn().then(expectAsync1((SendPort remote) { | |
93 | |
94 // Send objects and receive them back. | |
95 for (int i = 0; i < MessageTest.elms.length; i++) { | |
96 var sentObject = MessageTest.elms[i]; | |
97 // TODO(asiva): remove this local var idx once thew new for-loop | |
98 // semantics for closures is implemented. | |
99 var idx = i; | |
100 remote.call(sentObject).then(expectAsync1((var receivedObject) { | |
101 MessageTest.VerifyObject(idx, receivedObject); | |
102 })); | |
103 } | |
104 | |
105 // Send recursive objects and receive them back. | |
106 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; | |
107 List local_list2 = [null, local_list1, local_list1 ]; | |
108 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; | |
109 List sendObject = new List(5); | |
110 sendObject[0] = local_list1; | |
111 sendObject[1] = sendObject; | |
112 sendObject[2] = local_list2; | |
113 sendObject[3] = sendObject; | |
114 sendObject[4] = local_list3; | |
115 remote.call(sendObject).then((var replyObject) { | |
116 Expect.equals(true, sendObject is List); | |
117 Expect.equals(true, replyObject is List); | |
118 Expect.equals(sendObject.length, replyObject.length); | |
119 Expect.equals(true, replyObject[1] === replyObject); | |
120 Expect.equals(true, replyObject[3] === replyObject); | |
121 Expect.equals(true, replyObject[0] === replyObject[2][1]); | |
122 Expect.equals(true, replyObject[0] === replyObject[2][2]); | |
123 Expect.equals(true, replyObject[2] === replyObject[4][0]); | |
124 Expect.equals(true, replyObject[0][0] === replyObject[0][2]); | |
125 // Bigint literals are not canonicalized so do a == check. | |
126 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); | |
127 }); | |
128 | |
129 // Shutdown the MessageServer. | |
130 remote.call(-1).then(expectAsync1((int message) { | |
131 Expect.equals(MessageTest.elms.length + 1, message); | |
132 })); | |
133 })); | |
134 }); | |
135 } | |
OLD | NEW |