OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 // Dart test program for testing if statement. |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 // For logical-or conditions dart2js sometimes inlined expressions, leading to |
| 9 // completely broken programs. |
| 10 |
| 11 int globalCounter = 0; |
| 12 |
| 13 falseWithSideEffect() { |
| 14 bool confuse() => new DateTime.now().millisecondsSinceEpoch == 42; |
| 15 |
| 16 var result = confuse(); |
| 17 |
| 18 // Make it harder to inline. |
| 19 if (result) { |
| 20 try { |
| 21 try { |
| 22 if (confuse()) falseWithSideEffect(); |
| 23 if (confuse()) return 499; |
| 24 } catch (e) { |
| 25 rethrow; |
| 26 } |
| 27 } catch (e) { |
| 28 rethrow; |
| 29 } |
| 30 } |
| 31 globalCounter++; |
| 32 return result; |
| 33 } |
| 34 |
| 35 falseWithoutSideEffect() { |
| 36 bool confuse() => new DateTime.now().millisecondsSinceEpoch == 42; |
| 37 |
| 38 var result = confuse(); |
| 39 |
| 40 // Make it harder to inline. |
| 41 if (result) { |
| 42 try { |
| 43 try { |
| 44 if (confuse()) falseWithSideEffect(); |
| 45 if (confuse()) return 499; |
| 46 } catch (e) { |
| 47 rethrow; |
| 48 } |
| 49 } catch (e) { |
| 50 rethrow; |
| 51 } |
| 52 } |
| 53 return result; |
| 54 } |
| 55 |
| 56 testLogicalOr() { |
| 57 globalCounter = 0; |
| 58 bool cond1 = falseWithSideEffect(); |
| 59 if (cond1 || falseWithoutSideEffect()) Expect.fail("must be false"); |
| 60 if (cond1 || falseWithoutSideEffect()) Expect.fail("must be false"); |
| 61 if (cond1 || falseWithoutSideEffect()) Expect.fail("must be false"); |
| 62 Expect.equals(1, globalCounter); |
| 63 |
| 64 cond1 = (falseWithSideEffect() == 499); |
| 65 if (cond1 || falseWithoutSideEffect()) Expect.fail("must be false"); |
| 66 if (cond1 || falseWithoutSideEffect()) Expect.fail("must be false"); |
| 67 if (cond1 || falseWithoutSideEffect()) Expect.fail("must be false"); |
| 68 Expect.equals(2, globalCounter); |
| 69 } |
| 70 |
| 71 List globalList = []; |
| 72 void testLogicalOr2() { |
| 73 globalList.clear(); |
| 74 testValueOr([]); |
| 75 testValueOr(null); |
| 76 Expect.listEquals([1, 2, 3], globalList); |
| 77 } |
| 78 |
| 79 void testValueOr(List list) { |
| 80 if (list == null) globalList.add(1); |
| 81 if (list == null || list.contains("2")) globalList.add(2); |
| 82 if (list == null || list.contains("3")) globalList.add(3); |
| 83 } |
| 84 |
| 85 testLogicalAnd() { |
| 86 globalCounter = 0; |
| 87 bool cond1 = falseWithSideEffect(); |
| 88 if (cond1 && falseWithoutSideEffect()) Expect.fail("must be false"); |
| 89 if (cond1 && falseWithoutSideEffect()) Expect.fail("must be false"); |
| 90 if (cond1 && falseWithoutSideEffect()) Expect.fail("must be false"); |
| 91 Expect.equals(1, globalCounter); |
| 92 |
| 93 cond1 = (falseWithSideEffect() == 499); |
| 94 if (cond1 && falseWithoutSideEffect()) Expect.fail("must be false"); |
| 95 if (cond1 && falseWithoutSideEffect()) Expect.fail("must be false"); |
| 96 if (cond1 && falseWithoutSideEffect()) Expect.fail("must be false"); |
| 97 Expect.equals(2, globalCounter); |
| 98 } |
| 99 |
| 100 void testLogicalAnd2() { |
| 101 globalList.clear(); |
| 102 testValueAnd([]); |
| 103 testValueAnd(null); |
| 104 Expect.listEquals([1, 2, 3], globalList); |
| 105 } |
| 106 |
| 107 void testValueAnd(List list) { |
| 108 if (list == null) globalList.add(1); |
| 109 if (list == null && globalList.contains(1)) globalList.add(2); |
| 110 if (list == null && globalList.contains(1)) globalList.add(3); |
| 111 } |
| 112 |
| 113 main() { |
| 114 testLogicalOr(); |
| 115 testLogicalOr2(); |
| 116 |
| 117 testLogicalAnd(); |
| 118 testLogicalAnd2(); |
| 119 } |
OLD | NEW |