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

Side by Side Diff: tests/corelib_strong/bit_twiddling_test.dart

Issue 2977403002: Migrate test block 3 to Dart 2.0. (Closed)
Patch Set: Addressed Bob's nits Created 3 years, 5 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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 // Testing Bigints.
5
6 library bit_twiddling_test;
7
8 import "package:expect/expect.dart";
9
10 bool haveBigints() {
11 return 100000000000000000000 + 1 != 100000000000000000000;
12 }
13
14 testBitLength() {
15 check(int i, width) {
16 Expect.equals(width, i.bitLength, '$i.bitLength == $width');
17 // (~i) written as (-i-1) to avoid issues with limited range of dart2js ops.
18 Expect.equals(width, (-i - 1).bitLength, '(~$i).bitLength == $width');
19 }
20
21 check(0, 0);
22 check(1, 1);
23 check(2, 2);
24 check(3, 2);
25 check(4, 3);
26 check(5, 3);
27 check(6, 3);
28 check(7, 3);
29 check(8, 4);
30 check(127, 7);
31 check(128, 8);
32 check(129, 8);
33 check(2147483646, 31);
34 check(2147483647, 31);
35 check(2147483648, 32);
36 check(2147483649, 32);
37 check(4294967295, 32);
38 check(4294967296, 33);
39 check(0xffffffffff, 40);
40 check(0xfffffffffff, 44);
41 check(0xffffffffffff, 48);
42 check(0x1000000000000, 49);
43 check(0x1000000000001, 49);
44 check(0x1ffffffffffff, 49);
45 check(0x2000000000000, 50);
46 check(0x2000000000001, 50);
47
48 if (haveBigints()) {
49 check(0xffffffffffffff, 56);
50 check(0xffffffffffffffff, 64);
51 check(0xffffffffffffffffff, 72);
52 check(0x1000000000000000000, 73);
53 check(0x1000000000000000001, 73);
54
55 check(0xfffffffffffffffffffffffffffffffffffffe, 152);
56 check(0xffffffffffffffffffffffffffffffffffffff, 152);
57 check(0x100000000000000000000000000000000000000, 153);
58 check(0x100000000000000000000000000000000000001, 153);
59 }
60 }
61
62 testToUnsigned() {
63 checkU(src, width, expected) {
64 Expect.equals(expected, src.toUnsigned(width));
65 }
66
67 checkU(1, 8, 1);
68 checkU(0xff, 8, 0xff);
69 checkU(0xffff, 8, 0xff);
70 checkU(-1, 8, 0xff);
71 checkU(0xffffffff, 32, 0xffffffff);
72
73 checkU(0x7fffffff, 30, 0x3fffffff);
74 checkU(0x7fffffff, 31, 0x7fffffff);
75 checkU(0x7fffffff, 32, 0x7fffffff);
76 checkU(0x80000000, 30, 0);
77 checkU(0x80000000, 31, 0);
78 checkU(0x80000000, 32, 0x80000000);
79 checkU(0xffffffff, 30, 0x3fffffff);
80 checkU(0xffffffff, 31, 0x7fffffff);
81 checkU(0xffffffff, 32, 0xffffffff);
82 checkU(0x100000000, 30, 0);
83 checkU(0x100000000, 31, 0);
84 checkU(0x100000000, 32, 0);
85 checkU(0x1ffffffff, 30, 0x3fffffff);
86 checkU(0x1ffffffff, 31, 0x7fffffff);
87 checkU(0x1ffffffff, 32, 0xffffffff);
88
89 checkU(-1, 0, 0);
90 checkU(0, 0, 0);
91 checkU(1, 0, 0);
92 checkU(2, 0, 0);
93 checkU(3, 0, 0);
94
95 checkU(-1, 1, 1);
96 checkU(0, 1, 0);
97 checkU(1, 1, 1);
98 checkU(2, 1, 0);
99 checkU(3, 1, 1);
100 checkU(4, 1, 0);
101
102 checkU(-1, 2, 3);
103 checkU(0, 2, 0);
104 checkU(1, 2, 1);
105 checkU(2, 2, 2);
106 checkU(3, 2, 3);
107 checkU(4, 2, 0);
108
109 checkU(-1, 3, 7);
110 checkU(0, 3, 0);
111 checkU(1, 3, 1);
112 checkU(2, 3, 2);
113 checkU(3, 3, 3);
114 checkU(4, 3, 4);
115 }
116
117 testToSigned() {
118 checkS(src, width, expected) {
119 Expect.equals(
120 expected, src.toSigned(width), '$src.toSigned($width) == $expected');
121 }
122
123 checkS(1, 8, 1);
124 checkS(0xff, 8, -1);
125 checkS(0xffff, 8, -1);
126 checkS(-1, 8, -1);
127 checkS(128, 8, -128);
128 checkS(0xffffffff, 32, -1);
129
130 checkS(0x7fffffff, 30, -1);
131 checkS(0x7fffffff, 31, -1);
132 checkS(0x7fffffff, 32, 0x7fffffff);
133 checkS(0x80000000, 30, 0);
134 checkS(0x80000000, 31, 0);
135 checkS(0x80000000, 32, -2147483648);
136 checkS(0xffffffff, 30, -1);
137 checkS(0xffffffff, 31, -1);
138 checkS(0xffffffff, 32, -1);
139
140 checkS(0x100000000, 30, 0);
141 checkS(0x100000000, 31, 0);
142 checkS(0x100000000, 32, 0);
143 checkS(0x1ffffffff, 30, -1);
144 checkS(0x1ffffffff, 31, -1);
145 checkS(0x1ffffffff, 32, -1);
146
147 checkS(-1, 1, -1);
148 checkS(0, 1, 0);
149 checkS(1, 1, -1); // The only bit is the sign bit.
150 checkS(2, 1, 0);
151 checkS(3, 1, -1);
152 checkS(4, 1, 0);
153
154 checkS(-1, 2, -1);
155 checkS(0, 2, 0);
156 checkS(1, 2, 1);
157 checkS(2, 2, -2);
158 checkS(3, 2, -1);
159 checkS(4, 2, 0);
160
161 checkS(-1, 3, -1);
162 checkS(0, 3, 0);
163 checkS(1, 3, 1);
164 checkS(2, 3, 2);
165 checkS(3, 3, 3);
166 checkS(4, 3, -4);
167 }
168
169 main() {
170 testBitLength();
171 testToUnsigned();
172 testToSigned();
173 }
OLDNEW
« no previous file with comments | « tests/corelib_strong/bit_twiddling_bigint_test.dart ('k') | tests/corelib_strong/bool_from_environment2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698