OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 4 |
5 import 'dart:html'; | 5 import 'dart:html'; |
6 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
7 | 7 |
8 import 'package:expect/minitest.dart'; | 8 import 'package:expect/minitest.dart'; |
9 | 9 |
10 main() { | 10 main() { |
11 // Only perform tests if ArrayBuffer is supported. | 11 // Only perform tests if ArrayBuffer is supported. |
12 if (!Platform.supportsTypedData) { | 12 if (!Platform.supportsTypedData) { |
13 return; | 13 return; |
14 } | 14 } |
15 | 15 |
16 test('viewTest_dynamic', () { | 16 test('viewTest_dynamic', () { |
17 var a1 = new Uint8List(1024); | 17 var a1 = new Uint8List(1024); |
18 for (int i = 0; i < a1.length; i++) { | 18 for (int i = 0; i < a1.length; i++) { |
19 a1[i] = i; // 0,1,2,...,254,255,0,1,2,... | 19 a1[i] = i; // 0,1,2,...,254,255,0,1,2,... |
20 } | 20 } |
21 | 21 |
22 var a2 = new Uint32List.view(a1.buffer); | 22 var a2 = new Uint32List.view(a1.buffer); |
23 expect(1024 ~/ 4, a2.length); | 23 expect(1024 ~/ 4, a2.length); |
24 expect(a2[0], 0x03020100); | 24 expect(a2[0], 0x03020100); |
25 expect(a2[1], 0x07060504); | 25 expect(a2[1], 0x07060504); |
26 expect(a2[2], 0x0B0A0908); | 26 expect(a2[2], 0x0B0A0908); |
27 expect(a2[50], 0xCBCAC9C8); | 27 expect(a2[50], 0xCBCAC9C8); |
28 expect(a2[51], 0xCFCECDCC); | 28 expect(a2[51], 0xCFCECDCC); |
29 expect(a2[64], 0x03020100); | 29 expect(a2[64], 0x03020100); |
30 | 30 |
31 a2 = new Uint32List.view(a1.buffer, 200); | 31 a2 = new Uint32List.view(a1.buffer, 200); |
32 expect(a2.length, (1024 - 200) ~/ 4); | 32 expect(a2.length, (1024 - 200) ~/ 4); |
33 expect(a2[0], 0xCBCAC9C8); | 33 expect(a2[0], 0xCBCAC9C8); |
34 expect(a2[1], 0xCFCECDCC); | 34 expect(a2[1], 0xCFCECDCC); |
35 expect(a2[14], 0x03020100); | 35 expect(a2[14], 0x03020100); |
36 | 36 |
37 a2 = new Uint32List.view(a1.buffer, 456, 20); | 37 a2 = new Uint32List.view(a1.buffer, 456, 20); |
38 expect(a2.length, 20); | 38 expect(a2.length, 20); |
39 expect(a2[0], 0xCBCAC9C8); | 39 expect(a2[0], 0xCBCAC9C8); |
40 expect(a2[1], 0xCFCECDCC); | 40 expect(a2[1], 0xCFCECDCC); |
41 expect(a2[14], 0x03020100); | 41 expect(a2[14], 0x03020100); |
42 | 42 |
43 // OPTIONALS a2 = new Uint32List.view(a1.buffer, length: 30, byteOffset: 4
56); | 43 // OPTIONALS a2 = new Uint32List.view(a1.buffer, length: 30, byteOffset: 456
); |
44 a2 = new Uint32List.view(a1.buffer, 456, 30); | 44 a2 = new Uint32List.view(a1.buffer, 456, 30); |
45 expect(a2.length, 30); | 45 expect(a2.length, 30); |
46 expect(a2[0], 0xCBCAC9C8); | 46 expect(a2[0], 0xCBCAC9C8); |
47 expect(a2[1], 0xCFCECDCC); | 47 expect(a2[1], 0xCFCECDCC); |
48 expect(a2[14], 0x03020100); | 48 expect(a2[14], 0x03020100); |
49 }); | 49 }); |
50 | 50 |
51 test('viewTest_typed', () { | 51 test('viewTest_typed', () { |
52 Uint8List a1 = new Uint8List(1024); | 52 Uint8List a1 = new Uint8List(1024); |
53 for (int i = 0; i < a1.length; i++) { | 53 for (int i = 0; i < a1.length; i++) { |
54 a1[i] = i; | 54 a1[i] = i; |
55 } | 55 } |
56 | 56 |
57 Uint32List a2 = new Uint32List.view(a1.buffer); | 57 Uint32List a2 = new Uint32List.view(a1.buffer); |
58 expect(a2.length, 1024 ~/ 4); | 58 expect(a2.length, 1024 ~/ 4); |
59 expect(a2[0], 0x03020100); | 59 expect(a2[0], 0x03020100); |
60 expect(a2[50], 0xCBCAC9C8); | 60 expect(a2[50], 0xCBCAC9C8); |
61 expect(a2[51], 0xCFCECDCC); | 61 expect(a2[51], 0xCFCECDCC); |
62 expect(a2[64], 0x03020100); | 62 expect(a2[64], 0x03020100); |
63 | 63 |
64 a2 = new Uint32List.view(a1.buffer, 200); | 64 a2 = new Uint32List.view(a1.buffer, 200); |
65 expect(a2.length, (1024 - 200) ~/ 4); | 65 expect(a2.length, (1024 - 200) ~/ 4); |
66 expect(a2[0], 0xCBCAC9C8); | 66 expect(a2[0], 0xCBCAC9C8); |
67 expect(a2[1], 0xCFCECDCC); | 67 expect(a2[1], 0xCFCECDCC); |
68 expect(a2[14], 0x03020100); | 68 expect(a2[14], 0x03020100); |
69 | 69 |
70 a2 = new Uint32List.view(a1.buffer, 456, 20); | 70 a2 = new Uint32List.view(a1.buffer, 456, 20); |
71 expect(20, a2.length); | 71 expect(20, a2.length); |
72 expect(a2[0], 0xCBCAC9C8); | 72 expect(a2[0], 0xCBCAC9C8); |
73 expect(a2[1], 0xCFCECDCC); | 73 expect(a2[1], 0xCFCECDCC); |
74 expect(a2[14], 0x03020100); | 74 expect(a2[14], 0x03020100); |
75 | 75 |
76 // OPTIONALS a2 = new Uint32List.view(a1.buffer, length: 30, byteOffset: 4
56); | 76 // OPTIONALS a2 = new Uint32List.view(a1.buffer, length: 30, byteOffset: 456
); |
77 a2 = new Uint32List.view(a1.buffer, 456, 30); | 77 a2 = new Uint32List.view(a1.buffer, 456, 30); |
78 expect(a2.length, 30); | 78 expect(a2.length, 30); |
79 expect(a2[0], 0xCBCAC9C8); | 79 expect(a2[0], 0xCBCAC9C8); |
80 expect(a2[1], 0xCFCECDCC); | 80 expect(a2[1], 0xCFCECDCC); |
81 expect(a2[14], 0x03020100); | 81 expect(a2[14], 0x03020100); |
82 }); | 82 }); |
83 } | 83 } |
OLD | NEW |