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