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 // Tests that lhs of a compound assignement is executed only once. | 4 // Tests that lhs of a compound assignement is executed only once. |
5 // VMOptions=--optimization-counter-threshold=10 --no-background-compilation | 5 // VMOptions=--optimization-counter-threshold=10 --no-background-compilation |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 class Indexed { | 9 class Indexed { |
10 Indexed() : _f = new List(10), count = 0 { | 10 Indexed() |
| 11 : _f = new List(10), |
| 12 count = 0 { |
11 _f[0] = 100; | 13 _f[0] = 100; |
12 _f[1] = 200; | 14 _f[1] = 200; |
13 } | 15 } |
14 operator [](i) { | 16 operator [](i) { |
15 count++; | 17 count++; |
16 return _f; | 18 return _f; |
17 } | 19 } |
| 20 |
18 var count; | 21 var count; |
19 var _f; | 22 var _f; |
20 } | 23 } |
21 | 24 |
22 var result; | 25 var result; |
23 | 26 |
24 class A { | 27 class A { |
25 get field { result.add(1); return 1; } | 28 get field { |
| 29 result.add(1); |
| 30 return 1; |
| 31 } |
| 32 |
26 set field(value) {} | 33 set field(value) {} |
27 | 34 |
28 static get static_field { result.add(0); return 1; } | 35 static get static_field { |
29 static set static_field(value) { result.add(1); } | 36 result.add(0); |
| 37 return 1; |
| 38 } |
| 39 |
| 40 static set static_field(value) { |
| 41 result.add(1); |
| 42 } |
30 } | 43 } |
31 | 44 |
32 | |
33 class CompoundAssignmentOperatorTest { | 45 class CompoundAssignmentOperatorTest { |
34 | |
35 static void testIndexed() { | 46 static void testIndexed() { |
36 Indexed indexed = new Indexed(); | 47 Indexed indexed = new Indexed(); |
37 Expect.equals(0, indexed.count); | 48 Expect.equals(0, indexed.count); |
38 var tmp = indexed[0]; | 49 var tmp = indexed[0]; |
39 Expect.equals(1, indexed.count); | 50 Expect.equals(1, indexed.count); |
40 Expect.equals(100, indexed[4][0]); | 51 Expect.equals(100, indexed[4][0]); |
41 Expect.equals(2, indexed.count); | 52 Expect.equals(2, indexed.count); |
42 Expect.equals(100, indexed[4][0]++); | 53 Expect.equals(100, indexed[4][0]++); |
43 Expect.equals(3, indexed.count); | 54 Expect.equals(3, indexed.count); |
44 Expect.equals(101, indexed[4][0]); | 55 Expect.equals(101, indexed[4][0]); |
45 Expect.equals(4, indexed.count); | 56 Expect.equals(4, indexed.count); |
46 indexed[4][0] += 10; | 57 indexed[4][0] += 10; |
47 Expect.equals(5, indexed.count); | 58 Expect.equals(5, indexed.count); |
48 Expect.equals(111, indexed[4][0]); | 59 Expect.equals(111, indexed[4][0]); |
49 var i = 0; | 60 var i = 0; |
50 indexed[3][i++] += 1; | 61 indexed[3][i++] += 1; |
51 Expect.equals(1, i); | 62 Expect.equals(1, i); |
52 } | 63 } |
53 | 64 |
54 static testIndexedMore() { | 65 static testIndexedMore() { |
55 result = []; | 66 result = []; |
56 array() { result.add(0); return [0]; } | 67 array() { |
57 index() { result.add(1); return 0; } | 68 result.add(0); |
58 middle() { result.add(2); } | 69 return [0]; |
59 sequence(a, b, c) { result.add(3); } | 70 } |
60 | 71 |
61 sequence(array()[index()] += 1, middle(), array()[index()] += 1); | 72 index() { |
62 Expect.listEquals([0, 1, 2, 0, 1, 3], result); | 73 result.add(1); |
| 74 return 0; |
| 75 } |
| 76 |
| 77 middle() { |
| 78 result.add(2); |
| 79 } |
| 80 |
| 81 sequence(a, b, c) { |
| 82 result.add(3); |
| 83 } |
| 84 |
| 85 sequence(array()[index()] += 1, middle(), array()[index()] += 1); |
| 86 Expect.listEquals([0, 1, 2, 0, 1, 3], result); |
63 } | 87 } |
64 | 88 |
65 static testIndexedMoreMore() { | 89 static testIndexedMoreMore() { |
66 result = []; | 90 result = []; |
67 middle() { result.add(2); } | 91 middle() { |
68 obj() { result.add(0); return new A(); } | 92 result.add(2); |
69 sequence(a, b, c) { result.add(3); } | 93 } |
| 94 |
| 95 obj() { |
| 96 result.add(0); |
| 97 return new A(); |
| 98 } |
| 99 |
| 100 sequence(a, b, c) { |
| 101 result.add(3); |
| 102 } |
70 | 103 |
71 sequence(obj().field += 1, middle(), obj().field += 1); | 104 sequence(obj().field += 1, middle(), obj().field += 1); |
72 Expect.listEquals([0, 1, 2, 0, 1, 3], result); | 105 Expect.listEquals([0, 1, 2, 0, 1, 3], result); |
73 | 106 |
74 result = []; | 107 result = []; |
75 sequence(A.static_field++, middle(), A.static_field++); | 108 sequence(A.static_field++, middle(), A.static_field++); |
76 Expect.listEquals([0, 1, 2, 0, 1, 3], result); | 109 Expect.listEquals([0, 1, 2, 0, 1, 3], result); |
77 } | 110 } |
78 | 111 |
79 static void testMain() { | 112 static void testMain() { |
80 for (int i = 0; i < 20; i++) { | 113 for (int i = 0; i < 20; i++) { |
81 testIndexed(); | 114 testIndexed(); |
82 testIndexedMore(); | 115 testIndexedMore(); |
83 testIndexedMoreMore(); | 116 testIndexedMoreMore(); |
84 } | 117 } |
85 } | 118 } |
86 } | 119 } |
87 | 120 |
88 main() { | 121 main() { |
89 CompoundAssignmentOperatorTest.testMain(); | 122 CompoundAssignmentOperatorTest.testMain(); |
90 } | 123 } |
OLD | NEW |