OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 |
| 7 // Tests basic expressions. Does not attempt to validate the details of arithmet
ic, coercion, and |
| 8 // so forth. |
| 9 class ExpressionTest { |
| 10 |
| 11 ExpressionTest() {} |
| 12 |
| 13 int foo; |
| 14 |
| 15 static testMain() { |
| 16 var test = new ExpressionTest(); |
| 17 test.testBinary(); |
| 18 test.testUnary(); |
| 19 test.testShifts(); |
| 20 test.testBitwise(); |
| 21 test.testIncrement(); |
| 22 test.testMangling(); |
| 23 } |
| 24 |
| 25 testBinary() { |
| 26 int x = 4, y = 2; |
| 27 Expect.equals(6, x + y); |
| 28 Expect.equals(2, x - y); |
| 29 Expect.equals(8, x * y); |
| 30 Expect.equals(2, x / y); |
| 31 Expect.equals(0, x % y); |
| 32 } |
| 33 |
| 34 testUnary() { |
| 35 int x = 4, y = 2, z = -5; |
| 36 bool t = true, f = false; |
| 37 Expect.equals(-4, -x); |
| 38 Expect.equals(4, ~z); |
| 39 Expect.equals(f, !t); |
| 40 } |
| 41 |
| 42 testShifts() { |
| 43 int x = 4, y = 2; |
| 44 Expect.equals(y, x >> 1); |
| 45 Expect.equals(x, y << 1); |
| 46 } |
| 47 |
| 48 testBitwise() { |
| 49 int x = 4, y = 2; |
| 50 Expect.equals(6, (x | y)); |
| 51 Expect.equals(0, (x & y)); |
| 52 Expect.equals(6, (x ^ y)); |
| 53 } |
| 54 |
| 55 operator [](int index) { |
| 56 return foo; |
| 57 } |
| 58 |
| 59 operator []=(int index, int value) { |
| 60 foo = value; |
| 61 } |
| 62 |
| 63 testIncrement() { |
| 64 int x = 4, a = x++; |
| 65 Expect.equals(4, a); |
| 66 Expect.equals(5, x); |
| 67 Expect.equals(6, ++x); |
| 68 Expect.equals(6, x++); |
| 69 Expect.equals(7, x); |
| 70 Expect.equals(6, --x); |
| 71 Expect.equals(6, x--); |
| 72 Expect.equals(5, x); |
| 73 |
| 74 this.foo = 0; |
| 75 Expect.equals(0, this.foo++); |
| 76 Expect.equals(1, this.foo); |
| 77 Expect.equals(2, ++this.foo); |
| 78 Expect.equals(2, this.foo); |
| 79 Expect.equals(2, this.foo--); |
| 80 Expect.equals(1, this.foo); |
| 81 Expect.equals(0, --this.foo); |
| 82 Expect.equals(0, this.foo); |
| 83 |
| 84 Expect.equals(0, this[0]++); |
| 85 Expect.equals(1, this[0]); |
| 86 Expect.equals(2, ++this[0]); |
| 87 Expect.equals(2, this[0]); |
| 88 Expect.equals(2, this[0]--); |
| 89 Expect.equals(1, this[0]); |
| 90 Expect.equals(0, --this[0]); |
| 91 Expect.equals(0, this[0]); |
| 92 |
| 93 int $0 = 42, $1 = 87, $2 = 117; |
| 94 Expect.equals(42, $0++); |
| 95 Expect.equals(43, $0); |
| 96 Expect.equals(44, ++$0); |
| 97 Expect.equals(88, $0 += $0); |
| 98 Expect.equals(87, $1++); |
| 99 Expect.equals(88, $1); |
| 100 Expect.equals(89, ++$1); |
| 101 Expect.equals(178, ($1 += $1)); |
| 102 Expect.equals(117, $2++); |
| 103 Expect.equals(118, $2); |
| 104 Expect.equals(119, ++$2); |
| 105 } |
| 106 |
| 107 void testMangling() { |
| 108 int $0 = 42, $1 = 87, $2 = 117; |
| 109 this[0] = 0; |
| 110 Expect.equals(42, (this[0] += $0)); |
| 111 Expect.equals(129, (this[0] += $1)); |
| 112 Expect.equals(246, (this[0] += $2)); |
| 113 } |
| 114 } |
| 115 |
| 116 main() { |
| 117 ExpressionTest.testMain(); |
| 118 } |
OLD | NEW |