Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Unified Diff: tests/compiler/dart2js_extra/operator_test.dart

Issue 1388423004: Add more dart2js operator tests (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/compiler/dart2js_extra/operator_test.dart
diff --git a/tests/compiler/dart2js_extra/operator_test.dart b/tests/compiler/dart2js_extra/operator_test.dart
index 225976fcb55f22fdc5ed2e3ad84185cd2f8e5897..96ee9dcc030246cabb3cba5e3a4361ce9a56318e 100644
--- a/tests/compiler/dart2js_extra/operator_test.dart
+++ b/tests/compiler/dart2js_extra/operator_test.dart
@@ -3,22 +3,80 @@
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
+@NoInline() @AssumeDynamic()
+confuse(x) => x;
+
+@NoInline()
+asNum(x) {
+ var result = confuse(x);
+ if (result is num) return result;
+ throw new ArgumentError.value(x);
+}
+
+@NoInline()
+uint31(x) {
+ var result = confuse(x);
+ if (x is int) {
+ var masked = 0x7fffffff & x; // inferred uint31 type.
+ if (masked == x) return masked;
+ }
+ throw new ArgumentError('Not uint31: $x');
+}
+
+@NoInline()
+uint32(x) {
+ var result = confuse(x);
+ if (x is int) {
+ var masked = 0xffffffff & x; // inferred uint32 type.
+ if (masked == x) return masked;
+ }
+ throw new ArgumentError('Not uint32: $x');
+}
+@NoInline()
int zero() { return 0; }
+
+@NoInline()
int one() { return 1; }
+
+@NoInline()
int minus1() { return 0 - 1; }
+
+@NoInline()
+int minus2() { return 0 - 2; }
+
+@NoInline()
int two() { return 2; }
+
+@NoInline()
int three() { return 3; }
+
+@NoInline()
int five() { return 5; }
+
+@NoInline()
int minus5() { return 0 - 5; }
+
+@NoInline()
int ninetyNine() { return 99; }
+
+@NoInline()
int four99() { return 499; }
+
+@NoInline()
int four99times99() { return 499 * 99; }
+
+@NoInline()
int four99times99plus1() { return 499 * 99 + 1; }
+@NoInline()
void addTest() {
var m1 = 0 - 1;
Expect.equals(0, 0 + 0);
+ Expect.equals(0, confuse(0) + 0);
+ Expect.equals(0, asNum(0) + 0);
+ Expect.equals(0, uint31(0) + 0);
+
Expect.equals(m1, m1 + 0);
Expect.equals(0, m1 + 1);
Expect.equals(499, 400 + 99);
@@ -27,6 +85,7 @@ void addTest() {
Expect.equals(2, one() + one());
}
+@NoInline()
void subTest() {
var m1 = 0 - 1;
Expect.equals(0, 0 - 0);
@@ -38,6 +97,7 @@ void subTest() {
Expect.equals(0, one() - one());
}
+@NoInline()
void mulTest() {
var m1 = 0 - 1;
Expect.equals(0, 0 * 0);
@@ -49,23 +109,30 @@ void mulTest() {
Expect.equals(49401, four99() * 99);
}
+@NoInline()
void divTest() {
var m1 = 0.0 - 1.0;
var m2 = 0 - 2;
Expect.equals(1.0, 2 / 2);
Expect.equals(m1, m2 / 2);
+ Expect.equals(m1, minus2() / 2);
Expect.equals(0.5, 1 / 2);
Expect.equals(499.0, 49401 / 99);
Expect.equals(1.0, two() / 2);
Expect.equals(1.0, 2 / two());
Expect.equals(m1, m2 / two());
+ Expect.equals(m1, minus2() / two());
Expect.equals(m1, two() / m2);
+ Expect.equals(m1, two() / minus2());
Expect.equals(0.5, 1 / two());
Expect.equals(0.5, one() / 2);
Expect.equals(499.0, four99times99() / 99);
+
+ Expect.equals(1.5, confuse(150) / confuse(100));
}
+@NoInline()
void tdivTest() {
var m1 = 0 - 1;
var m2 = 0 - 2;
@@ -84,8 +151,62 @@ void tdivTest() {
Expect.equals(0, one() ~/ 2);
Expect.equals(499, four99times99() ~/ 99);
Expect.equals(499, four99times99plus1() ~/ 99);
+
+ Expect.equals(-33, -100 ~/ 3);
+ Expect.equals(-33, asNum(-100) ~/ 3);
+ Expect.equals(33, -100 ~/ -3);
+ Expect.equals(33, asNum(-100) ~/ -3);
+
+ // Signed int32 boundary is involved in optimizations.
+
+ Expect.equals(-0x80000000, -0x80000000 ~/ 1.0);
+ Expect.equals(-0x80000000, -0x80000000 ~/ 1.0000000000000001);
+ Expect.equals(-0x7fffffff, -0x80000000 ~/ 1.0000000000000002);
+
+ Expect.equals(-0x80000000, asNum(-0x80000000) ~/ 1.0);
+ Expect.equals(-0x80000000, asNum(-0x80000000) ~/ 1.0000000000000001);
+ Expect.equals(-0x7fffffff, asNum(-0x80000000) ~/ 1.0000000000000002);
+
+ Expect.equals(-0x80000000, asNum(0x80000000) ~/ -1.0);
+ Expect.equals(-0x80000000, asNum(0x80000000) ~/ -1.0000000000000001);
+ Expect.equals(-0x7fffffff, asNum(0x80000000) ~/ -1.0000000000000002);
+
+ Expect.equals(0x7fffffff, 0x10000000 ~/ .12500000000000002);
+ Expect.equals(0x80000000, 0x10000000 ~/ .125);
+ Expect.equals(-0x7fffffff, 0x10000000 ~/ -.12500000000000002);
+ Expect.equals(-0x80000000, 0x10000000 ~/ -.125);
+
+ Expect.equals(0x7fffffff, uint31(0x10000000) ~/ .12500000000000002);
+ Expect.equals(0x80000000, uint31(0x10000000) ~/ .125);
+ Expect.equals(-0x7fffffff, uint31(0x10000000) ~/ -.12500000000000002);
+ Expect.equals(-0x80000000, uint31(0x10000000) ~/ -.125);
+
+ // These can be compiled to `(a / 2) | 0`.
+ Expect.equals(100, uint31(200) ~/ 2);
+ Expect.equals(100, uint32(200) ~/ 2);
+
+ Expect.equals(100, asNum(200) ~/ 2);
+ Expect.equals(100, confuse(200) ~/ 2);
+ Expect.equals(-100, uint31(200) ~/ -2);
+ Expect.equals(-100, uint32(200) ~/ -2);
+ Expect.equals(-100, asNum(200) ~/ -2);
+ Expect.equals(-100, confuse(200) ~/ -2);
+
+ // These can be compiled to `((a + b) / 2) | 0`.
+ Expect.equals(100, (uint31(100) + uint31(100)) ~/ 2);
+ Expect.equals(0x7fffffff, (uint31(0x7fffffff) + uint31(0x7fffffff)) ~/ 2);
+
+ // NaN and Infinity results are errors.
+ Expect.throws(() => -1 ~/ 0);
+ Expect.throws(() => 1.5 ~/ 0);
+ Expect.throws(() => 1e200 ~/ 1e-200);
+ Expect.throws(() => -1e200 ~/ 1e-200);
+ Expect.throws(() => 1e200 ~/ -1e-200);
+ Expect.throws(() => -1e200 ~/ -1e-200);
+ Expect.throws(() => double.NAN ~/ 2);
}
+@NoInline()
void modTest() {
var m5 = 0 - 5;
var m3 = 0 - 3;
@@ -103,6 +224,36 @@ void modTest() {
Expect.equals(2, five() % m3);
}
+@NoInline()
+void remainderTest() {
+ var m5 = 0 - 5;
+ Expect.equals(2, confuse(5).remainder(3));
+ Expect.equals(0, confuse(49401).remainder(99));
+ Expect.equals(1, confuse(49402).remainder(99));
+ Expect.equals(-2, confuse(m5).remainder(3));
+ Expect.equals(2, confuse(5).remainder(-3));
+
+ Expect.equals(2, uint32(5).remainder(3));
+ Expect.equals(0, uint32(49401).remainder(99));
+ Expect.equals(1, uint32(49402).remainder(99));
+ Expect.equals(-2, (-5).remainder(uint32(3)));
+ Expect.equals(2, uint32(5).remainder(-3));
+
+ Expect.equals(2, 5.remainder(3));
+ Expect.equals(0, 49401.remainder(99));
+ Expect.equals(1, 49402.remainder(99));
+ Expect.equals(-2, (-5).remainder(3));
+ Expect.equals(2, 5.remainder(-3));
+
+ Expect.equals(2, five().remainder(3));
+ Expect.equals(2, 5.remainder(three()));
+ Expect.equals(0, four99times99().remainder(99));
+ Expect.equals(1, four99times99plus1().remainder(99));
+ Expect.equals(-2, minus5().remainder(3));
+ Expect.equals(2, five().remainder(-3));
+}
+
+@NoInline()
void shlTest() {
Expect.equals(2, 1 << 1);
Expect.equals(8, 1 << 3);
@@ -110,17 +261,38 @@ void shlTest() {
Expect.equals(10, five() << 1);
Expect.equals(24, 3 << three());
+
+ Expect.equals(10, confuse(5) << 1);
+ Expect.equals(24, 3 << confuse(3));
+
+ Expect.equals(10, uint31(5) << 1);
+ Expect.equals(24, 3 << uint31(3));
+
+ Expect.equals(10, asNum(5) << 1);
+ Expect.equals(24, 3 << asNum(3));
}
+@NoInline()
void shrTest() {
Expect.equals(1, 2 >> 1);
Expect.equals(1, 8 >> 3);
Expect.equals(3, 6 >> 1);
- var x = 0 - ninetyNine();
Expect.equals(6, ninetyNine() >> 4);
+ Expect.equals(6, confuse(99) >> 4);
+ Expect.equals(6, asNum(99) >> 4);
+ Expect.equals(6, uint31(99) >> 4);
+
+ Expect.equals(6, 99 >> 4);
+ Expect.equals(6, 99 >> confuse(4));
+ Expect.equals(6, 99 >> asNum(4));
+ Expect.equals(6, 99 >> uint31(4));
+
+ Expect.equals(0, uint31(1) >> 31);
+ Expect.equals(0, asNum(0xffffffff) >> 32);
}
+@NoInline()
void andTest() {
Expect.equals(2, 10 & 3);
Expect.equals(7, 15 & 7);
@@ -129,8 +301,12 @@ void andTest() {
Expect.equals(99, ninetyNine() & ninetyNine());
Expect.equals(34, four99() & 42);
Expect.equals(3, minus5() & 7);
+
+ Expect.equals(0, uint31(0x7ffffffe) & uint31(1));
+ Expect.equals(0, asNum(0x7ffffffe) & asNum(1));
}
+@NoInline()
void orTest() {
Expect.equals(11, 10 | 3);
Expect.equals(15, 15 | 7);
@@ -138,8 +314,13 @@ void orTest() {
Expect.equals(99, ninetyNine() | ninetyNine());
Expect.equals(507, four99() | 42);
+
+ Expect.equals(11, asNum(10) | 3);
+ Expect.equals(15, asNum(15) | 7);
+ Expect.equals(10, asNum(10) | 10);
}
+@NoInline()
void xorTest() {
Expect.equals(9, 10 ^ 3);
Expect.equals(8, 15 ^ 7);
@@ -151,21 +332,26 @@ void xorTest() {
Expect.equals(6, minus5() ^ -3);
}
+@NoInline()
void notTest() {
Expect.equals(4, ~minus5());
}
+@NoInline()
void negateTest() {
Expect.equals(minus5(), -5);
Expect.equals(-5, -five());
Expect.equals(5, -minus5());
- var x = 3;
- if (false) x = 5;
- Expect.equals(-3, -x);
- var y = -5;
- Expect.equals(8, x - y);
+
+ Expect.equals(-3, -confuse(3));
+ Expect.equals(-3, -asNum(3));
+ Expect.equals(-3, -uint31(3));
+
+ Expect.equals(3, -confuse(-3));
+ Expect.equals(3, -asNum(-3));
}
+@NoInline()
void equalsTest() {
// Equality of normal numbers is already well tested with "Expect.equals".
Expect.equals(true, true == true);
@@ -209,6 +395,7 @@ void equalsTest() {
Expect.equals(false, null == falseValue);
}
+@NoInline()
void lessTest() {
var m1 = minus1();
Expect.equals(true, 1 < 2);
@@ -230,8 +417,13 @@ void lessTest() {
Expect.equals(true, m1 < 0);
Expect.equals(false, 0 < m1);
Expect.equals(false, m1 < m1);
+
+ Expect.equals(true, minus1() < 0);
+ Expect.equals(false, 0 < minus1());
+ Expect.equals(false, minus1() < minus1());
}
+@NoInline()
void lessEqualTest() {
var m1 = minus1();
Expect.equals(true, 1 <= 2);
@@ -242,6 +434,14 @@ void lessEqualTest() {
Expect.equals(false, 1 <= 0);
Expect.equals(true, 0 <= 0);
+ Expect.equals(true, confuse(1) <= 2);
+ Expect.equals(false, confuse(2) <= 1);
+ Expect.equals(true, confuse(1) <= 1);
+
+ Expect.equals(true, confuse(0) <= 1);
+ Expect.equals(false, confuse(1) <= 0);
+ Expect.equals(true, confuse(0) <= 0);
+
Expect.equals(true, one() <= 2);
Expect.equals(false, 2 <= one());
Expect.equals(true, 1 <= one());
@@ -253,8 +453,13 @@ void lessEqualTest() {
Expect.equals(true, m1 <= 0);
Expect.equals(false, 0 <= m1);
Expect.equals(true, m1 <= m1);
+
+ Expect.equals(true, minus1() <= 0);
+ Expect.equals(false, 0 <= minus1());
+ Expect.equals(true, minus1() <= minus1());
}
+@NoInline()
void greaterTest() {
var m1 = minus1();
Expect.equals(false, 1 > 2);
@@ -276,8 +481,13 @@ void greaterTest() {
Expect.equals(false, m1 > 0);
Expect.equals(true, 0 > m1);
Expect.equals(false, m1 > m1);
+
+ Expect.equals(false, minus1() > 0);
+ Expect.equals(true, 0 > minus1());
+ Expect.equals(false, minus1() > minus1());
}
+@NoInline()
void greaterEqualTest() {
var m1 = minus1();
Expect.equals(false, 1 >= 2);
@@ -299,6 +509,10 @@ void greaterEqualTest() {
Expect.equals(false, m1 >= 0);
Expect.equals(true, 0 >= m1);
Expect.equals(true, m1 >= m1);
+
+ Expect.equals(false, minus1() >= 0);
+ Expect.equals(true, 0 >= minus1());
+ Expect.equals(true, minus1() >= minus1());
}
void main() {
@@ -308,6 +522,7 @@ void main() {
divTest();
tdivTest();
modTest();
+ remainderTest();
shlTest();
shrTest();
andTest();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698