OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // It is sometimes possible to compile is-checks to 'instanceof', when the class | 7 // It is sometimes possible to compile is-checks to 'instanceof', when the class |
8 // is not in an 'implements' clause or used as a mixin. | 8 // is not in an 'implements' clause or used as a mixin. |
9 | 9 |
10 // This test verifies is-checks work with simple classes that have various | 10 // This test verifies is-checks work with simple classes that have various |
11 // degrees of instantiation. | 11 // degrees of instantiation. |
12 | 12 |
13 class INSTANTIATED {} // instantiated and used in many ways | 13 class INSTANTIATED {} // instantiated and used in many ways |
14 class DEFERRED {} // instantiated after first check | 14 |
15 class UNUSED {} // used only in is-check | 15 class DEFERRED {} // instantiated after first check |
16 class REMOVED {} // allocated but optimized out of program | 16 |
17 class DEFERRED_AND_REMOVED {} // allocated after first check and removed | 17 class UNUSED {} // used only in is-check |
18 class USED_AS_TYPE_PARAMETER {} // only used as a type parameter | 18 |
19 class USED_AS_TESTED_TYPE_PARAMETER {} // only used as a type parameter | 19 class REMOVED {} // allocated but optimized out of program |
| 20 |
| 21 class DEFERRED_AND_REMOVED {} // allocated after first check and removed |
| 22 |
| 23 class USED_AS_TYPE_PARAMETER {} // only used as a type parameter |
| 24 |
| 25 class USED_AS_TESTED_TYPE_PARAMETER {} // only used as a type parameter |
20 | 26 |
21 class Check<T> { | 27 class Check<T> { |
22 bool check(x) => x is T; | 28 bool check(x) => x is T; |
23 } | 29 } |
24 | 30 |
25 class Check2<T> { | 31 class Check2<T> { |
26 bool check(x) => x is USED_AS_TYPE_PARAMETER; | 32 bool check(x) => x is USED_AS_TYPE_PARAMETER; |
27 } | 33 } |
28 | 34 |
29 void main() { | 35 void main() { |
30 var things = new List(3); | 36 var things = new List(3); |
31 things.setRange(0, 3, [new INSTANTIATED(), 1, new Object()]); | 37 things.setRange(0, 3, [new INSTANTIATED(), 1, new Object()]); |
32 | 38 |
33 var checkX = new Check<INSTANTIATED>(); | 39 var checkX = new Check<INSTANTIATED>(); |
34 var checkU1 = new Check<USED_AS_TESTED_TYPE_PARAMETER>(); | 40 var checkU1 = new Check<USED_AS_TESTED_TYPE_PARAMETER>(); |
35 var checkU2 = new Check<USED_AS_TYPE_PARAMETER>(); | 41 var checkU2 = new Check<USED_AS_TYPE_PARAMETER>(); |
36 | 42 |
37 var removed = new REMOVED(); // This is optimized out. | 43 var removed = new REMOVED(); // This is optimized out. |
38 | 44 |
39 // Tests that can be compiled to instanceof: | 45 // Tests that can be compiled to instanceof: |
40 Expect.isTrue(things[0] is INSTANTIATED); | 46 Expect.isTrue(things[0] is INSTANTIATED); |
41 Expect.isFalse(things[1] is INSTANTIATED); | 47 Expect.isFalse(things[1] is INSTANTIATED); |
42 Expect.isFalse(things[1] is REMOVED); | 48 Expect.isFalse(things[1] is REMOVED); |
43 Expect.isFalse(things[1] is DEFERRED_AND_REMOVED); | 49 Expect.isFalse(things[1] is DEFERRED_AND_REMOVED); |
44 Expect.isFalse(things[1] is DEFERRED); | 50 Expect.isFalse(things[1] is DEFERRED); |
45 // Tests that might be optimized to false since there are no allocations: | 51 // Tests that might be optimized to false since there are no allocations: |
46 Expect.isFalse(things[1] is UNUSED); | 52 Expect.isFalse(things[1] is UNUSED); |
47 Expect.isFalse(things[1] is USED_AS_TYPE_PARAMETER); | 53 Expect.isFalse(things[1] is USED_AS_TYPE_PARAMETER); |
48 | 54 |
49 Expect.isTrue(checkX.check(things[0])); | 55 Expect.isTrue(checkX.check(things[0])); |
50 Expect.isFalse(checkX.check(things[1])); | 56 Expect.isFalse(checkX.check(things[1])); |
51 Expect.isFalse(checkU1.check(things[1])); | 57 Expect.isFalse(checkU1.check(things[1])); |
52 Expect.isFalse(checkU2.check(things[1])); | 58 Expect.isFalse(checkU2.check(things[1])); |
53 | 59 |
54 var removed2 = new DEFERRED_AND_REMOVED(); // This is optimized out. | 60 var removed2 = new DEFERRED_AND_REMOVED(); // This is optimized out. |
55 | 61 |
56 // First allocation of DEFERRED is after the above tests. | 62 // First allocation of DEFERRED is after the above tests. |
57 things.setRange(0, 3, [new INSTANTIATED(), 1, new DEFERRED()]); | 63 things.setRange(0, 3, [new INSTANTIATED(), 1, new DEFERRED()]); |
58 | 64 |
59 // Tests that can be compiled to instanceof: | 65 // Tests that can be compiled to instanceof: |
60 Expect.isTrue(things[0] is INSTANTIATED); | 66 Expect.isTrue(things[0] is INSTANTIATED); |
61 Expect.isFalse(things[1] is INSTANTIATED); | 67 Expect.isFalse(things[1] is INSTANTIATED); |
62 Expect.isFalse(things[1] is REMOVED); | 68 Expect.isFalse(things[1] is REMOVED); |
63 Expect.isFalse(things[1] is DEFERRED_AND_REMOVED); | 69 Expect.isFalse(things[1] is DEFERRED_AND_REMOVED); |
64 Expect.isFalse(things[1] is DEFERRED); | 70 Expect.isFalse(things[1] is DEFERRED); |
65 Expect.isTrue(things[2] is DEFERRED); | 71 Expect.isTrue(things[2] is DEFERRED); |
66 // Tests that might be optimized to false since there are no allocations: | 72 // Tests that might be optimized to false since there are no allocations: |
67 Expect.isFalse(things[1] is UNUSED); | 73 Expect.isFalse(things[1] is UNUSED); |
68 Expect.isFalse(things[1] is USED_AS_TYPE_PARAMETER); | 74 Expect.isFalse(things[1] is USED_AS_TYPE_PARAMETER); |
69 } | 75 } |
OLD | NEW |