OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // VMOptions=--error_on_bad_type --error_on_bad_override | 4 // VMOptions=--error_on_bad_type --error_on_bad_override |
5 | 5 |
6 import 'package:observatory/object_graph.dart'; | 6 import 'package:observatory/object_graph.dart'; |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
9 | 9 |
10 testRoundTrip(final int n) { | 10 testRoundTrip(final int n) { |
11 var bytes = []; | 11 var bytes = []; |
12 var remaining = n; | 12 var remaining = n; |
13 while (remaining > 127) { | 13 while (remaining > 127) { |
14 bytes.add(remaining & 127); | 14 bytes.add(remaining & 127); |
15 remaining = remaining >> 7; | 15 remaining = remaining >> 7; |
16 } | 16 } |
17 bytes.add(remaining + 128); | 17 bytes.add(remaining + 128); |
18 | 18 |
19 print("Encoded $n as $bytes"); | 19 print("Encoded $n as $bytes"); |
20 | 20 |
21 var typedBytes = new ByteData.view(new Uint8List.fromList(bytes).buffer); | 21 var typedBytes = new ByteData.view(new Uint8List.fromList(bytes).buffer); |
22 var stream = new ReadStream([typedBytes]); | 22 var stream = new ReadStream([typedBytes]); |
23 stream.readUnsigned(); | 23 stream.readUnsigned(); |
24 | 24 |
25 expect(stream.isZero, equals(n == 0)); | 25 expect(stream.isZero, equals(n == 0)); |
26 | 26 |
27 expect(stream.low, equals((n >> 0) & 0xFFFFFFF)); | 27 expect(stream.low, equals((n >> 0) & 0xFFFFFFF)); |
28 expect(stream.mid, equals((n >> 28) & 0xFFFFFFF)); | 28 expect(stream.mid, equals((n >> 28) & 0xFFFFFFF)); |
29 expect(stream.high, equals((n >> 56) & 0xFFFFFFF)); | 29 expect(stream.high, equals((n >> 56) & 0xFFFFFFF)); |
30 | 30 |
31 const kMaxUint32 = (1 << 32) - 1; | 31 const kMaxUint32 = (1 << 32) - 1; |
32 if (n > kMaxUint32) { | 32 if (n > kMaxUint32) { |
33 expect(stream.clampedUint32, equals(kMaxUint32)); | 33 expect(stream.clampedUint32, equals(kMaxUint32)); |
34 } else { | 34 } else { |
35 expect(stream.clampedUint32, equals(n)); | 35 expect(stream.clampedUint32, equals(n)); |
36 } | 36 } |
37 | 37 |
38 expect(stream.position, equals(bytes.length)); | 38 expect(stream.position, equals(bytes.length)); |
39 } | 39 } |
40 | 40 |
41 main() { | 41 main() { |
42 const kMaxUint64 = (1 << 64) - 1; | 42 const kMaxUint64 = (1 << 64) - 1; |
43 | 43 |
44 var n = 3; | 44 var n = 3; |
45 while (n < kMaxUint64) { | 45 while (n < kMaxUint64) { |
46 testRoundTrip(n); | 46 testRoundTrip(n); |
47 n <<= 1; | 47 n <<= 1; |
48 } | 48 } |
49 | 49 |
50 n = 5; | 50 n = 5; |
51 while (n < kMaxUint64) { | 51 while (n < kMaxUint64) { |
52 testRoundTrip(n); | 52 testRoundTrip(n); |
53 n <<= 1; | 53 n <<= 1; |
54 } | 54 } |
55 } | 55 } |
OLD | NEW |