OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Dart test program for testing native byte arrays. | 5 // Dart test program for testing native byte arrays. |
6 | 6 |
7 void testCreateByteArray() { | 7 void testCreateByteArray() { |
8 ByteArray byteArray; | 8 Uint8List byteArray; |
9 | 9 |
10 byteArray = new ByteArray(0); | 10 byteArray = new Uint8List(0); |
11 Expect.equals(0, byteArray.length); | 11 Expect.equals(0, byteArray.length); |
12 | 12 |
13 byteArray = new ByteArray(10); | 13 byteArray = new Uint8List(10); |
14 Expect.equals(10, byteArray.length); | 14 Expect.equals(10, byteArray.length); |
15 for (int i = 0; i < 10; i++) { | 15 for (int i = 0; i < 10; i++) { |
16 Expect.equals(0, byteArray[i]); | 16 Expect.equals(0, byteArray[i]); |
17 } | 17 } |
18 } | 18 } |
19 | 19 |
20 void testSetRange() { | 20 void testSetRange() { |
21 ByteArray byteArray = new ByteArray(3); | 21 Uint8List byteArray = new Uint8List(3); |
22 | 22 |
23 List<int> list = [10, 11, 12]; | 23 List<int> list = [10, 11, 12]; |
24 byteArray.setRange(0, 3, list); | 24 byteArray.setRange(0, 3, list); |
25 for (int i = 0; i < 3; i++) { | 25 for (int i = 0; i < 3; i++) { |
26 Expect.equals(10 + i, byteArray[i]); | 26 Expect.equals(10 + i, byteArray[i]); |
27 } | 27 } |
28 | 28 |
29 byteArray[0] = 20; | 29 byteArray[0] = 20; |
30 byteArray[1] = 21; | 30 byteArray[1] = 21; |
31 byteArray[2] = 22; | 31 byteArray[2] = 22; |
32 list.setRange(0, 3, byteArray); | 32 list.setRange(0, 3, byteArray); |
33 for (int i = 0; i < 3; i++) { | 33 for (int i = 0; i < 3; i++) { |
34 Expect.equals(20 + i, list[i]); | 34 Expect.equals(20 + i, list[i]); |
35 } | 35 } |
36 | 36 |
37 byteArray.setRange(1, 2, const [8, 9]); | 37 byteArray.setRange(1, 2, const [8, 9]); |
38 Expect.equals(20, byteArray[0]); | 38 Expect.equals(20, byteArray[0]); |
39 Expect.equals(8, byteArray[1]); | 39 Expect.equals(8, byteArray[1]); |
40 Expect.equals(9, byteArray[2]); | 40 Expect.equals(9, byteArray[2]); |
41 } | 41 } |
42 | 42 |
43 void testIndexOutOfRange() { | 43 void testIndexOutOfRange() { |
44 ByteArray byteArray = new ByteArray(3); | 44 Uint8List byteArray = new Uint8List(3); |
45 List<int> list = const [0, 1, 2, 3]; | 45 List<int> list = const [0, 1, 2, 3]; |
46 | 46 |
47 Expect.throws(() { | 47 Expect.throws(() { |
48 byteArray.setRange(0, 4, list); | 48 byteArray.setRange(0, 4, list); |
49 }); | 49 }); |
50 | 50 |
51 Expect.throws(() { | 51 Expect.throws(() { |
52 byteArray.setRange(3, 1, list); | 52 byteArray.setRange(3, 1, list); |
53 }); | 53 }); |
54 } | 54 } |
55 | 55 |
56 main() { | 56 main() { |
57 for (int i = 0; i < 2000; i++) { | 57 for (int i = 0; i < 2000; i++) { |
58 testCreateByteArray(); | 58 testCreateByteArray(); |
59 testSetRange(); | 59 testSetRange(); |
60 testIndexOutOfRange(); | 60 testIndexOutOfRange(); |
61 } | 61 } |
62 } | 62 } |
OLD | NEW |