| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Dart test program for testing serialization of messages. | 5 // Dart test program for testing serialization of messages. |
| 6 // VMOptions=--enable_type_checks --enable_asserts | 6 // VMOptions=--enable_type_checks --enable_asserts |
| 7 | 7 |
| 8 library MessageTest; | 8 library MessageTest; |
| 9 | |
| 10 import 'dart:async'; | 9 import 'dart:async'; |
| 11 import 'dart:collection'; | 10 import 'dart:collection'; |
| 12 import 'dart:isolate'; | 11 import 'dart:isolate'; |
| 13 import 'package:async_helper/async_helper.dart'; | 12 import 'package:async_helper/async_helper.dart'; |
| 14 import 'package:expect/expect.dart'; | 13 import 'package:expect/expect.dart'; |
| 15 import 'dart:typed_data'; | 14 import 'dart:typed_data'; |
| 16 | 15 |
| 17 void echoMain(msg) { | 16 void echoMain(msg) { |
| 18 SendPort replyTo = msg[0]; | 17 SendPort replyTo = msg[0]; |
| 19 SendPort pong = msg[1]; | 18 SendPort pong = msg[1]; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 var gee = 123; | 55 var gee = 123; |
| 57 } | 56 } |
| 58 | 57 |
| 59 class E { | 58 class E { |
| 60 Function fun; | 59 Function fun; |
| 61 E(this.fun); | 60 E(this.fun); |
| 62 | 61 |
| 63 static fooFun() => 499; | 62 static fooFun() => 499; |
| 64 instanceFun() => 1234; | 63 instanceFun() => 1234; |
| 65 } | 64 } |
| 66 | |
| 67 barFun() => 42; | 65 barFun() => 42; |
| 68 | 66 |
| 69 class F { | 67 class F { |
| 70 final field = "field"; | 68 final field = "field"; |
| 71 const F(); | 69 const F(); |
| 72 } | 70 } |
| 73 | 71 |
| 74 class G { | 72 class G { |
| 75 final field; | 73 final field; |
| 76 const G(this.field); | 74 const G(this.field); |
| 77 } | 75 } |
| 78 | 76 |
| 79 class Value { | 77 class Value { |
| 80 final val; | 78 final val; |
| 81 Value(this.val); | 79 Value(this.val); |
| 82 | 80 |
| 83 operator ==(other) { | 81 operator==(other) { |
| 84 if (other is! Value) return false; | 82 if (other is! Value) return false; |
| 85 return other.val == val; | 83 return other.val == val; |
| 86 } | 84 } |
| 87 | 85 |
| 88 get hashCode => val; | 86 get hashCode => val; |
| 89 } | 87 } |
| 90 | 88 |
| 91 void runTests(SendPort ping, Queue checks) { | 89 void runTests(SendPort ping, Queue checks) { |
| 92 ping.send("abc"); | 90 ping.send("abc"); |
| 93 checks.add((x) => Expect.equals("abc", x)); | 91 checks.add((x) => Expect.equals("abc", x)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 108 fixed[0] = 0; | 106 fixed[0] = 0; |
| 109 fixed[1] = 1; | 107 fixed[1] = 1; |
| 110 ping.send(fixed); | 108 ping.send(fixed); |
| 111 checks.add((x) { | 109 checks.add((x) { |
| 112 Expect.isTrue(x is List); | 110 Expect.isTrue(x is List); |
| 113 Expect.listEquals([0, 1], x); | 111 Expect.listEquals([0, 1], x); |
| 114 // List must be mutable. | 112 // List must be mutable. |
| 115 x[0] = 3; | 113 x[0] = 3; |
| 116 Expect.equals(3, x[0]); | 114 Expect.equals(3, x[0]); |
| 117 // List must be fixed length. | 115 // List must be fixed length. |
| 118 Expect.throws(() { | 116 Expect.throws(() { x.add(5); }); |
| 119 x.add(5); | |
| 120 }); | |
| 121 }); | 117 }); |
| 122 | 118 |
| 123 List cyclic = []; | 119 List cyclic = []; |
| 124 cyclic.add(cyclic); | 120 cyclic.add(cyclic); |
| 125 ping.send(cyclic); | 121 ping.send(cyclic); |
| 126 checks.add((x) { | 122 checks.add((x) { |
| 127 Expect.isTrue(x is List); | 123 Expect.isTrue(x is List); |
| 128 Expect.equals(1, x.length); | 124 Expect.equals(1, x.length); |
| 129 Expect.identical(x, x[0]); | 125 Expect.identical(x, x[0]); |
| 130 // List must be mutable. | 126 // List must be mutable. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 ping.send(byteBuffer); // //# byteBuffer: ok | 175 ping.send(byteBuffer); // //# byteBuffer: ok |
| 180 checks.add( // //# byteBuffer: ok | 176 checks.add( // //# byteBuffer: ok |
| 181 (x) { | 177 (x) { |
| 182 Expect.isTrue(x is ByteBuffer); | 178 Expect.isTrue(x is ByteBuffer); |
| 183 Uint16List uint16View = new Uint16List.view(x); | 179 Uint16List uint16View = new Uint16List.view(x); |
| 184 Expect.equals(2, uint16View.length); | 180 Expect.equals(2, uint16View.length); |
| 185 Expect.equals(0, uint16View[0]); | 181 Expect.equals(0, uint16View[0]); |
| 186 Expect.equals(1, uint16View[1]); | 182 Expect.equals(1, uint16View[1]); |
| 187 } | 183 } |
| 188 ) // //# byteBuffer: ok | 184 ) // //# byteBuffer: ok |
| 189 ; | 185 ; |
| 190 | 186 |
| 191 Int32x4List list32x4 = new Int32x4List(2); | 187 Int32x4List list32x4 = new Int32x4List(2); |
| 192 list32x4[0] = new Int32x4(1, 2, 3, 4); | 188 list32x4[0] = new Int32x4(1, 2, 3, 4); |
| 193 list32x4[1] = new Int32x4(5, 6, 7, 8); | 189 list32x4[1] = new Int32x4(5, 6, 7, 8); |
| 194 ping.send(list32x4); // //# int32x4: ok | 190 ping.send(list32x4); // //# int32x4: ok |
| 195 checks.add( // //# int32x4: ok | 191 checks.add( // //# int32x4: ok |
| 196 (x) { | 192 (x) { |
| 197 Expect.isTrue(x is Int32x4List); | 193 Expect.isTrue(x is Int32x4List); |
| 198 Expect.equals(2, x.length); | 194 Expect.equals(2, x.length); |
| 199 Int32x4 entry1 = x[0]; | 195 Int32x4 entry1 = x[0]; |
| 200 Int32x4 entry2 = x[1]; | 196 Int32x4 entry2 = x[1]; |
| 201 Expect.equals(1, entry1.x); | 197 Expect.equals(1, entry1.x); |
| 202 Expect.equals(2, entry1.y); | 198 Expect.equals(2, entry1.y); |
| 203 Expect.equals(3, entry1.z); | 199 Expect.equals(3, entry1.z); |
| 204 Expect.equals(4, entry1.w); | 200 Expect.equals(4, entry1.w); |
| 205 Expect.equals(5, entry2.x); | 201 Expect.equals(5, entry2.x); |
| 206 Expect.equals(6, entry2.y); | 202 Expect.equals(6, entry2.y); |
| 207 Expect.equals(7, entry2.z); | 203 Expect.equals(7, entry2.z); |
| 208 Expect.equals(8, entry2.w); | 204 Expect.equals(8, entry2.w); |
| 209 } | 205 } |
| 210 ) // //# int32x4: ok | 206 ) // //# int32x4: ok |
| 211 ; | 207 ; |
| 212 | 208 |
| 213 ping.send({"foo": 499, "bar": 32}); | 209 ping.send({"foo": 499, "bar": 32}); |
| 214 checks.add((x) { | 210 checks.add((x) { |
| 215 Expect.isTrue(x is LinkedHashMap); | 211 Expect.isTrue(x is LinkedHashMap); |
| 216 Expect.listEquals(["foo", "bar"], x.keys.toList()); | 212 Expect.listEquals(["foo", "bar"], x.keys.toList()); |
| 217 Expect.listEquals([499, 32], x.values.toList()); | 213 Expect.listEquals([499, 32], x.values.toList()); |
| 218 Expect.equals(499, x["foo"]); | 214 Expect.equals(499, x["foo"]); |
| 219 Expect.equals(32, x["bar"]); | 215 Expect.equals(32, x["bar"]); |
| 220 // Must be mutable. | 216 // Must be mutable. |
| 221 x["foo"] = 22; | 217 x["foo"] = 22; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 Expect.identical(constF, x); // //# constInstance: ok | 380 Expect.identical(constF, x); // //# constInstance: ok |
| 385 }); | 381 }); |
| 386 | 382 |
| 387 G g1 = new G(nonConstF); | 383 G g1 = new G(nonConstF); |
| 388 G g2 = new G(constF); | 384 G g2 = new G(constF); |
| 389 G g3 = const G(constF); | 385 G g3 = const G(constF); |
| 390 ping.send(g1); | 386 ping.send(g1); |
| 391 ping.send(g2); | 387 ping.send(g2); |
| 392 ping.send(g3); | 388 ping.send(g3); |
| 393 | 389 |
| 394 checks.add((x) { | 390 checks.add((x) { // g1. |
| 395 // g1. | |
| 396 Expect.isTrue(x is G); | 391 Expect.isTrue(x is G); |
| 397 Expect.isFalse(identical(g1, x)); | 392 Expect.isFalse(identical(g1, x)); |
| 398 F f = x.field; | 393 F f = x.field; |
| 399 Expect.equals("field", f.field); | 394 Expect.equals("field", f.field); |
| 400 Expect.isFalse(identical(nonConstF, f)); | 395 Expect.isFalse(identical(nonConstF, f)); |
| 401 }); | 396 }); |
| 402 checks.add((x) { | 397 checks.add((x) { // g2. |
| 403 // g2. | |
| 404 Expect.isTrue(x is G); | 398 Expect.isTrue(x is G); |
| 405 Expect.isFalse(identical(g1, x)); | 399 Expect.isFalse(identical(g1, x)); |
| 406 F f = x.field; | 400 F f = x.field; |
| 407 Expect.equals("field", f.field); | 401 Expect.equals("field", f.field); |
| 408 Expect.identical(constF, f); // //# constInstance: continued | 402 Expect.identical(constF, f); // //# constInstance: continued |
| 409 }); | 403 }); |
| 410 checks.add((x) { | 404 checks.add((x) { // g3. |
| 411 // g3. | |
| 412 Expect.isTrue(x is G); | 405 Expect.isTrue(x is G); |
| 413 Expect.identical(g3, x); // //# constInstance: continued | 406 Expect.identical(g3, x); // //# constInstance: continued |
| 414 F f = x.field; | 407 F f = x.field; |
| 415 Expect.equals("field", f.field); | 408 Expect.equals("field", f.field); |
| 416 Expect.identical(constF, f); // //# constInstance: continued | 409 Expect.identical(constF, f); // //# constInstance: continued |
| 417 }); | 410 }); |
| 418 | 411 |
| 419 // Make sure objects in a map are serialized and deserialized in the correct | 412 // Make sure objects in a map are serialized and deserialized in the correct |
| 420 // order. | 413 // order. |
| 421 Map m = new Map(); | 414 Map m = new Map(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 450 Function check = checks.removeFirst(); | 443 Function check = checks.removeFirst(); |
| 451 check(msg); | 444 check(msg); |
| 452 if (checks.isEmpty) { | 445 if (checks.isEmpty) { |
| 453 completer.complete(); | 446 completer.complete(); |
| 454 testPort.close(); | 447 testPort.close(); |
| 455 } | 448 } |
| 456 }); | 449 }); |
| 457 | 450 |
| 458 ReceivePort initialReplyPort = new ReceivePort(); | 451 ReceivePort initialReplyPort = new ReceivePort(); |
| 459 Isolate | 452 Isolate |
| 460 .spawn(echoMain, [initialReplyPort.sendPort, testPort.sendPort]) | 453 .spawn(echoMain, [initialReplyPort.sendPort, testPort.sendPort]) |
| 461 .then((_) => initialReplyPort.first) | 454 .then((_) => initialReplyPort.first) |
| 462 .then((SendPort ping) { | 455 .then((SendPort ping) { |
| 463 runTests(ping, checks); | 456 runTests(ping, checks); |
| 464 Expect.isTrue(checks.length > 0); | 457 Expect.isTrue(checks.length > 0); |
| 465 completer.future.then((_) => ping.send("halt")).then((_) => asyncEnd()); | 458 completer.future |
| 466 }); | 459 .then((_) => ping.send("halt")) |
| 460 .then((_) => asyncEnd()); |
| 461 }); |
| 467 } | 462 } |
| OLD | NEW |