2015-09-16 20:28:49 +00:00
|
|
|
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
2017-04-24 12:53:46 +00:00
|
|
|
// Tests that lhs of a compound assignment is executed only once.
|
2017-10-13 22:46:23 +00:00
|
|
|
// VMOptions=--optimization-counter-threshold=10 --no-background-compilation
|
2015-09-16 20:28:49 +00:00
|
|
|
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
|
|
|
|
class Indexed {
|
2017-04-17 21:52:57 +00:00
|
|
|
Indexed()
|
|
|
|
: _f = new List(10),
|
|
|
|
count = 0 {
|
2015-09-16 20:28:49 +00:00
|
|
|
_f[0] = 100;
|
|
|
|
_f[1] = 200;
|
|
|
|
}
|
|
|
|
operator [](i) {
|
|
|
|
count++;
|
|
|
|
return _f;
|
|
|
|
}
|
2017-04-17 21:52:57 +00:00
|
|
|
|
2015-09-16 20:28:49 +00:00
|
|
|
var count;
|
|
|
|
var _f;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result;
|
|
|
|
|
|
|
|
class A {
|
2017-04-17 21:52:57 +00:00
|
|
|
get field {
|
|
|
|
result.add(1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:28:49 +00:00
|
|
|
set field(value) {}
|
|
|
|
|
2017-04-17 21:52:57 +00:00
|
|
|
static get static_field {
|
|
|
|
result.add(0);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-16 20:28:49 +00:00
|
|
|
|
2017-04-17 21:52:57 +00:00
|
|
|
static set static_field(value) {
|
|
|
|
result.add(1);
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 20:28:49 +00:00
|
|
|
|
|
|
|
class CompoundAssignmentOperatorTest {
|
|
|
|
static void testIndexed() {
|
|
|
|
Indexed indexed = new Indexed();
|
|
|
|
Expect.equals(0, indexed.count);
|
|
|
|
var tmp = indexed[0];
|
|
|
|
Expect.equals(1, indexed.count);
|
|
|
|
Expect.equals(100, indexed[4][0]);
|
|
|
|
Expect.equals(2, indexed.count);
|
|
|
|
Expect.equals(100, indexed[4][0]++);
|
|
|
|
Expect.equals(3, indexed.count);
|
|
|
|
Expect.equals(101, indexed[4][0]);
|
|
|
|
Expect.equals(4, indexed.count);
|
|
|
|
indexed[4][0] += 10;
|
|
|
|
Expect.equals(5, indexed.count);
|
|
|
|
Expect.equals(111, indexed[4][0]);
|
|
|
|
var i = 0;
|
|
|
|
indexed[3][i++] += 1;
|
|
|
|
Expect.equals(1, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static testIndexedMore() {
|
2017-04-17 21:52:57 +00:00
|
|
|
result = [];
|
|
|
|
array() {
|
|
|
|
result.add(0);
|
|
|
|
return [0];
|
|
|
|
}
|
|
|
|
|
|
|
|
index() {
|
|
|
|
result.add(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
middle() {
|
|
|
|
result.add(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence(a, b, c) {
|
|
|
|
result.add(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence(array()[index()] += 1, middle(), array()[index()] += 1);
|
|
|
|
Expect.listEquals([0, 1, 2, 0, 1, 3], result);
|
2015-09-16 20:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static testIndexedMoreMore() {
|
|
|
|
result = [];
|
2017-04-17 21:52:57 +00:00
|
|
|
middle() {
|
|
|
|
result.add(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj() {
|
|
|
|
result.add(0);
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence(a, b, c) {
|
|
|
|
result.add(3);
|
|
|
|
}
|
2015-09-16 20:28:49 +00:00
|
|
|
|
|
|
|
sequence(obj().field += 1, middle(), obj().field += 1);
|
|
|
|
Expect.listEquals([0, 1, 2, 0, 1, 3], result);
|
|
|
|
|
|
|
|
result = [];
|
|
|
|
sequence(A.static_field++, middle(), A.static_field++);
|
|
|
|
Expect.listEquals([0, 1, 2, 0, 1, 3], result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testMain() {
|
|
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
testIndexed();
|
|
|
|
testIndexedMore();
|
|
|
|
testIndexedMoreMore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
CompoundAssignmentOperatorTest.testMain();
|
|
|
|
}
|