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