OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // Test for correct order of exceptions in code with checks that could be moved | 7 // Test for correct order of exceptions in code with checks that could be moved |
8 // merged from successors into a dominator. | 8 // merged from successors into a dominator. |
9 | 9 |
10 get never => new DateTime.now().millisecondsSinceEpoch == 42; | 10 get never => new DateTime.now().millisecondsSinceEpoch == 42; |
11 get always => new DateTime.now().millisecondsSinceEpoch > 42; | 11 get always => new DateTime.now().millisecondsSinceEpoch > 42; |
12 | 12 |
13 // gA and gB have type [null|num], so they compile to a receiver check, and | 13 // gA and gB have type [null|num], so they compile to a receiver check, and |
14 // argument check and then the operation. | 14 // argument check and then the operation. |
15 var gA; // [null|num] | 15 var gA; // [null|num] |
16 var gB; // [null|num] | 16 var gB; // [null|num] |
17 | 17 |
18 foo1(a, b) { | 18 foo1(a, b) { |
19 // The checks on a and b are not equivalent, so can't be merged. | 19 // The checks on a and b are not equivalent, so can't be merged. |
20 if (never) { | 20 if (never) { |
21 return a ^ b; | 21 return a ^ b; |
22 } else { | 22 } else { |
23 return b ^ a; | 23 return b ^ a; |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 call1() { return foo1(gA, gB); } | 27 call1() { |
| 28 return foo1(gA, gB); |
| 29 } |
28 | 30 |
29 test1() { | 31 test1() { |
30 gA = 1; | 32 gA = 1; |
31 gB = 2; | 33 gB = 2; |
32 Expect.equals(3, call1()); | 34 Expect.equals(3, call1()); |
33 | 35 |
34 gA = null; | 36 gA = null; |
35 gB = null; | 37 gB = null; |
36 Expect.throws(call1, (e) => e is NoSuchMethodError, 'foo1($gA, $gB) NSME'); | 38 Expect.throws(call1, (e) => e is NoSuchMethodError, 'foo1($gA, $gB) NSME'); |
37 | 39 |
38 gA = 1; | 40 gA = 1; |
39 gB = null; | 41 gB = null; |
40 Expect.throws(call1, (e) => e is NoSuchMethodError, 'foo1($gA, $gB) NSME'); | 42 Expect.throws(call1, (e) => e is NoSuchMethodError, 'foo1($gA, $gB) NSME'); |
41 | 43 |
42 gA = null; | 44 gA = null; |
43 gB = 2; | 45 gB = 2; |
44 Expect.throws(call1, (e) => e is ArgumentError, 'foo1($gA, $gB) AE'); | 46 Expect.throws(call1, (e) => e is ArgumentError, 'foo1($gA, $gB) AE'); |
45 } | 47 } |
46 | 48 |
47 | |
48 foo2a(a, b) { | 49 foo2a(a, b) { |
49 // The common receiver check on [a] cannot be merged because the operation | 50 // The common receiver check on [a] cannot be merged because the operation |
50 // (selector) is different. | 51 // (selector) is different. |
51 // The common argument check on [b] cannot be merged because it must happen | 52 // The common argument check on [b] cannot be merged because it must happen |
52 // after the receiver check. | 53 // after the receiver check. |
53 if (never) { | 54 if (never) { |
54 return a ^ b; | 55 return a ^ b; |
55 } else { | 56 } else { |
56 return a & b; | 57 return a & b; |
57 } | 58 } |
58 } | 59 } |
59 | 60 |
60 foo2b(a, b) { | 61 foo2b(a, b) { |
61 // Same a foo2a except which branch dynamically taken. | 62 // Same a foo2a except which branch dynamically taken. |
62 if (always) { | 63 if (always) { |
63 return a ^ b; | 64 return a ^ b; |
64 } else { | 65 } else { |
65 return a & b; | 66 return a & b; |
66 } | 67 } |
67 } | 68 } |
68 | 69 |
69 call2a() { return foo2a(gA, gB); } | 70 call2a() { |
70 call2b() { return foo2b(gA, gB); } | 71 return foo2a(gA, gB); |
| 72 } |
| 73 |
| 74 call2b() { |
| 75 return foo2b(gA, gB); |
| 76 } |
71 | 77 |
72 checkNSME(text) { | 78 checkNSME(text) { |
73 return (e) { | 79 return (e) { |
74 Expect.isTrue(e is NoSuchMethodError, | 80 Expect.isTrue(e is NoSuchMethodError, |
75 'expecting NoSuchMethodError, got "${e.runtimeType}"'); | 81 'expecting NoSuchMethodError, got "${e.runtimeType}"'); |
76 Expect.isTrue('$e'.contains(text), '"$e".contains("$text")'); | 82 Expect.isTrue('$e'.contains(text), '"$e".contains("$text")'); |
77 return e is NoSuchMethodError; | 83 return e is NoSuchMethodError; |
78 }; | 84 }; |
79 } | 85 } |
80 | 86 |
81 test2() { | 87 test2() { |
82 gA = 1; | 88 gA = 1; |
83 gB = 2; | 89 gB = 2; |
84 Expect.equals(0, call2a()); | 90 Expect.equals(0, call2a()); |
85 Expect.equals(3, call2b()); | 91 Expect.equals(3, call2b()); |
86 | 92 |
87 gA = null; | 93 gA = null; |
88 gB = null; | 94 gB = null; |
89 Expect.throws(call2a, checkNSME(r'$and'), 'foo2($gA, $gB) NSME'); | 95 Expect.throws(call2a, checkNSME(r'$and'), 'foo2($gA, $gB) NSME'); |
90 Expect.throws(call2b, checkNSME(r'$xor'), 'foo2($gA, $gB) NSME'); | 96 Expect.throws(call2b, checkNSME(r'$xor'), 'foo2($gA, $gB) NSME'); |
91 | 97 |
92 gA = 1; | 98 gA = 1; |
93 gB = null; | 99 gB = null; |
94 Expect.throws(call2a, (e) => e is ArgumentError, 'foo2($gA, $gB) AE'); | 100 Expect.throws(call2a, (e) => e is ArgumentError, 'foo2($gA, $gB) AE'); |
95 Expect.throws(call2b, (e) => e is ArgumentError, 'foo2($gA, $gB) AE'); | 101 Expect.throws(call2b, (e) => e is ArgumentError, 'foo2($gA, $gB) AE'); |
96 | 102 |
97 gA = null; | 103 gA = null; |
98 gB = 2; | 104 gB = 2; |
99 Expect.throws(call2a, checkNSME(r'$and'), 'foo2($gA, $gB) NSME'); | 105 Expect.throws(call2a, checkNSME(r'$and'), 'foo2($gA, $gB) NSME'); |
100 Expect.throws(call2b, checkNSME(r'$xor'), 'foo2($gA, $gB) NSME'); | 106 Expect.throws(call2b, checkNSME(r'$xor'), 'foo2($gA, $gB) NSME'); |
101 } | 107 } |
102 | 108 |
103 main() { | 109 main() { |
104 test1(); | 110 test1(); |
105 test2(); | 111 test2(); |
106 } | 112 } |
OLD | NEW |