OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 // Dart test program to test operation precedence. | 4 // Dart test program to test operation precedence. |
5 | 5 |
6 library precedence_test; | 6 library precedence_test; |
| 7 |
7 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
8 | 9 |
9 main() { | 10 main() { |
10 testBang(); | 11 testBang(); |
11 testIndexWithPrefixAdd(); | 12 testIndexWithPrefixAdd(); |
12 testIndexWithPostfixAdd(); | 13 testIndexWithPostfixAdd(); |
13 testTilde(); | 14 testTilde(); |
14 testUnaryPrefixWithAdd(); | 15 testUnaryPrefixWithAdd(); |
15 testUnaryPostfixWithAdd(); | 16 testUnaryPostfixWithAdd(); |
16 testUnaryPrefixWithMultiply(); | 17 testUnaryPrefixWithMultiply(); |
17 testUnaryPostfixWithMultiply(); | 18 testUnaryPostfixWithMultiply(); |
18 } | 19 } |
19 | 20 |
20 void testBang() { | 21 void testBang() { |
21 int x = 3; | 22 int x = 3; |
22 | 23 |
23 Expect.equals(!true == false, true); | 24 Expect.equals(!true == false, true); |
24 Expect.equals(!x.isEven, true); | 25 Expect.equals(!x.isEven, true); |
25 Expect.equals(!(++x).isEven, false); | 26 Expect.equals(!(++x).isEven, false); |
26 Expect.equals(x, 4); | 27 Expect.equals(x, 4); |
27 Expect.equals(!(x++).isEven, false); | 28 Expect.equals(!(x++).isEven, false); |
28 Expect.equals(x, 5); | 29 Expect.equals(x, 5); |
29 } | 30 } |
30 | 31 |
31 void testIndexWithPrefixAdd() { | 32 void testIndexWithPrefixAdd() { |
32 var x = <int>[3]; | 33 var x = <int>[3]; |
33 | 34 |
34 Expect.equals(++x[0] + 3, 7); | 35 Expect.equals(++x[0] + 3, 7); |
35 Expect.equals(x[0], 4); | 36 Expect.equals(x[0], 4); |
36 Expect.equals(++x[0] - 3, 2); | 37 Expect.equals(++x[0] - 3, 2); |
37 Expect.equals(x[0], 5); | 38 Expect.equals(x[0], 5); |
38 Expect.equals(--x[0] + 4, 8); | 39 Expect.equals(--x[0] + 4, 8); |
39 Expect.equals(x[0], 4); | 40 Expect.equals(x[0], 4); |
40 Expect.equals(--x[0] - 4, -1); | 41 Expect.equals(--x[0] - 4, -1); |
41 Expect.equals(x[0], 3); | 42 Expect.equals(x[0], 3); |
42 | 43 |
43 Expect.equals(3 + ++x[0], 7); | 44 Expect.equals(3 + ++x[0], 7); |
44 Expect.equals(x[0], 4); | 45 Expect.equals(x[0], 4); |
45 Expect.equals(3 - ++x[0], -2); | 46 Expect.equals(3 - ++x[0], -2); |
46 Expect.equals(x[0], 5); | 47 Expect.equals(x[0], 5); |
47 Expect.equals(4 + --x[0], 8); | 48 Expect.equals(4 + --x[0], 8); |
48 Expect.equals(x[0], 4); | 49 Expect.equals(x[0], 4); |
49 Expect.equals(4 - --x[0], 1); | 50 Expect.equals(4 - --x[0], 1); |
50 Expect.equals(x[0], 3); | 51 Expect.equals(x[0], 3); |
51 } | 52 } |
52 | 53 |
53 void testIndexWithPostfixAdd() { | 54 void testIndexWithPostfixAdd() { |
54 var x = <int>[3]; | 55 var x = <int>[3]; |
55 | 56 |
56 Expect.equals(x[0]++ + 3, 6); | 57 Expect.equals(x[0]++ + 3, 6); |
57 Expect.equals(x[0], 4); | 58 Expect.equals(x[0], 4); |
58 Expect.equals(x[0]++ - 3, 1); | 59 Expect.equals(x[0]++ - 3, 1); |
59 Expect.equals(x[0], 5); | 60 Expect.equals(x[0], 5); |
60 Expect.equals(x[0]-- + 4, 9); | 61 Expect.equals(x[0]-- + 4, 9); |
61 Expect.equals(x[0], 4); | 62 Expect.equals(x[0], 4); |
62 Expect.equals(x[0]-- - 4, 0); | 63 Expect.equals(x[0]-- - 4, 0); |
63 Expect.equals(x[0], 3); | 64 Expect.equals(x[0], 3); |
64 | 65 |
65 Expect.equals(3 + x[0]++, 6); | 66 Expect.equals(3 + x[0]++, 6); |
(...skipping 10 matching lines...) Expand all Loading... |
76 int x = 3; | 77 int x = 3; |
77 | 78 |
78 Expect.equals(~x.sign, ~(x.sign)); | 79 Expect.equals(~x.sign, ~(x.sign)); |
79 Expect.equals(~x + 7, (~3) + 7); | 80 Expect.equals(~x + 7, (~3) + 7); |
80 | 81 |
81 Expect.equals(~++x + 7, (~4) + 7); | 82 Expect.equals(~++x + 7, (~4) + 7); |
82 Expect.equals(x, 4); | 83 Expect.equals(x, 4); |
83 Expect.equals(~x++ + 7, (~4) + 7); | 84 Expect.equals(~x++ + 7, (~4) + 7); |
84 Expect.equals(x, 5); | 85 Expect.equals(x, 5); |
85 | 86 |
86 Expect.equals(~--x + 7, (~4) + 7); | 87 Expect.equals(~ --x + 7, (~4) + 7); |
87 Expect.equals(x, 4); | 88 Expect.equals(x, 4); |
88 Expect.equals(~x-- + 7, (~4) + 7); | 89 Expect.equals(~x-- + 7, (~4) + 7); |
89 Expect.equals(x, 3); | 90 Expect.equals(x, 3); |
90 } | 91 } |
91 | 92 |
92 void testUnaryPrefixWithAdd() { | 93 void testUnaryPrefixWithAdd() { |
93 int x = 3; | 94 int x = 3; |
94 | 95 |
95 Expect.equals(++x + 3, 7); | 96 Expect.equals(++x + 3, 7); |
96 Expect.equals(x, 4); | 97 Expect.equals(x, 4); |
97 Expect.equals(++x - 3, 2); | 98 Expect.equals(++x - 3, 2); |
98 Expect.equals(x, 5); | 99 Expect.equals(x, 5); |
99 Expect.equals(--x + 4, 8); | 100 Expect.equals(--x + 4, 8); |
100 Expect.equals(x, 4); | 101 Expect.equals(x, 4); |
101 Expect.equals(--x - 4, -1); | 102 Expect.equals(--x - 4, -1); |
102 Expect.equals(x, 3); | 103 Expect.equals(x, 3); |
103 | 104 |
104 Expect.equals(3 + ++x, 7); | 105 Expect.equals(3 + ++x, 7); |
105 Expect.equals(x, 4); | 106 Expect.equals(x, 4); |
106 Expect.equals(3 - ++x, -2); | 107 Expect.equals(3 - ++x, -2); |
107 Expect.equals(x, 5); | 108 Expect.equals(x, 5); |
108 Expect.equals(4 + --x, 8); | 109 Expect.equals(4 + --x, 8); |
109 Expect.equals(x, 4); | 110 Expect.equals(x, 4); |
110 Expect.equals(4 - --x, 1); | 111 Expect.equals(4 - --x, 1); |
111 Expect.equals(x, 3); | 112 Expect.equals(x, 3); |
112 } | 113 } |
113 | 114 |
114 void testUnaryPostfixWithAdd() { | 115 void testUnaryPostfixWithAdd() { |
115 int x = 3; | 116 int x = 3; |
116 | 117 |
117 Expect.equals(x++ + 3, 6); | 118 Expect.equals(x++ + 3, 6); |
118 Expect.equals(x, 4); | 119 Expect.equals(x, 4); |
119 Expect.equals(x++ - 3, 1); | 120 Expect.equals(x++ - 3, 1); |
120 Expect.equals(x, 5); | 121 Expect.equals(x, 5); |
121 Expect.equals(x-- + 4, 9); | 122 Expect.equals(x-- + 4, 9); |
122 Expect.equals(x, 4); | 123 Expect.equals(x, 4); |
123 Expect.equals(x-- - 4, 0); | 124 Expect.equals(x-- - 4, 0); |
124 Expect.equals(x, 3); | 125 Expect.equals(x, 3); |
125 | 126 |
126 Expect.equals(3 + x++, 6); | 127 Expect.equals(3 + x++, 6); |
127 Expect.equals(x, 4); | 128 Expect.equals(x, 4); |
128 Expect.equals(3 - x++, -1); | 129 Expect.equals(3 - x++, -1); |
129 Expect.equals(x, 5); | 130 Expect.equals(x, 5); |
130 Expect.equals(4 + x--, 9); | 131 Expect.equals(4 + x--, 9); |
131 Expect.equals(x, 4); | 132 Expect.equals(x, 4); |
132 Expect.equals(4 - x--, 0); | 133 Expect.equals(4 - x--, 0); |
133 Expect.equals(x, 3); | 134 Expect.equals(x, 3); |
134 } | 135 } |
135 | 136 |
136 void testUnaryPrefixWithMultiply() { | 137 void testUnaryPrefixWithMultiply() { |
137 int x = 3; | 138 int x = 3; |
138 | 139 |
139 Expect.equals(++x * 3, 12); | 140 Expect.equals(++x * 3, 12); |
140 Expect.equals(x, 4); | 141 Expect.equals(x, 4); |
141 Expect.equals(++x / 5, 1.0); | 142 Expect.equals(++x / 5, 1.0); |
142 Expect.equals(x, 5); | 143 Expect.equals(x, 5); |
143 Expect.equals(--x * 3, 12); | 144 Expect.equals(--x * 3, 12); |
144 Expect.equals(x, 4); | 145 Expect.equals(x, 4); |
145 Expect.equals(--x / 4, 0.75); | 146 Expect.equals(--x / 4, 0.75); |
146 Expect.equals(x, 3); | 147 Expect.equals(x, 3); |
147 | 148 |
148 Expect.equals(3 * ++x, 12); | 149 Expect.equals(3 * ++x, 12); |
149 Expect.equals(x, 4); | 150 Expect.equals(x, 4); |
150 Expect.equals(5 / ++x, 1.0); | 151 Expect.equals(5 / ++x, 1.0); |
151 Expect.equals(x, 5); | 152 Expect.equals(x, 5); |
152 Expect.equals(3 * --x, 12); | 153 Expect.equals(3 * --x, 12); |
153 Expect.equals(x, 4); | 154 Expect.equals(x, 4); |
154 Expect.equals(6 / --x, 2.0); | 155 Expect.equals(6 / --x, 2.0); |
155 Expect.equals(x, 3); | 156 Expect.equals(x, 3); |
156 } | 157 } |
157 | 158 |
158 void testUnaryPostfixWithMultiply() { | 159 void testUnaryPostfixWithMultiply() { |
159 int x = 3; | 160 int x = 3; |
160 | 161 |
161 Expect.equals(x++ * 3, 9); | 162 Expect.equals(x++ * 3, 9); |
162 Expect.equals(x, 4); | 163 Expect.equals(x, 4); |
163 Expect.equals(x++ / 4, 1.0); | 164 Expect.equals(x++ / 4, 1.0); |
164 Expect.equals(x, 5); | 165 Expect.equals(x, 5); |
165 Expect.equals(x-- * 3, 15); | 166 Expect.equals(x-- * 3, 15); |
166 Expect.equals(x, 4); | 167 Expect.equals(x, 4); |
167 Expect.equals(x-- / 4, 1.0); | 168 Expect.equals(x-- / 4, 1.0); |
168 Expect.equals(x, 3); | 169 Expect.equals(x, 3); |
169 | 170 |
170 Expect.equals(3 * x++, 9); | 171 Expect.equals(3 * x++, 9); |
171 Expect.equals(x, 4); | 172 Expect.equals(x, 4); |
172 Expect.equals(3 / x++, 0.75); | 173 Expect.equals(3 / x++, 0.75); |
173 Expect.equals(x, 5); | 174 Expect.equals(x, 5); |
174 Expect.equals(4 * x--, 20); | 175 Expect.equals(4 * x--, 20); |
175 Expect.equals(x, 4); | 176 Expect.equals(x, 4); |
176 Expect.equals(4 / x--, 1.0); | 177 Expect.equals(4 / x--, 1.0); |
177 Expect.equals(x, 3); | 178 Expect.equals(x, 3); |
178 } | 179 } |
179 | |
OLD | NEW |