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 // Dart test program for testing native byte arrays. | 5 // Dart test program for testing native byte arrays. |
6 | 6 |
7 // Library tag to be able to run in html test framework. | 7 // Library tag to be able to run in html test framework. |
8 #library("ByteArrayTest.dart"); | 8 #library("ByteArrayTest.dart"); |
9 | 9 |
10 #import('dart:scalarlist'); | 10 #import('dart:scalarlist'); |
11 | 11 |
12 void testCreateByteArray() { | 12 void testCreateByteArray() { |
13 Uint8List byteArray; | 13 { |
14 Uint8List byteArray; | |
14 | 15 |
15 byteArray = new Uint8List(0); | 16 byteArray = new Uint8List(0); |
16 Expect.equals(0, byteArray.length); | 17 Expect.isTrue(byteArray is Uint8List); |
18 Expect.isFalse(byteArray is Uint8ClampedList); | |
19 Expect.equals(0, byteArray.length); | |
17 | 20 |
18 byteArray = new Uint8List(10); | 21 byteArray = new Uint8List(10); |
19 Expect.equals(10, byteArray.length); | 22 Expect.equals(10, byteArray.length); |
20 for (int i = 0; i < 10; i++) { | 23 for (int i = 0; i < 10; i++) { |
21 Expect.equals(0, byteArray[i]); | 24 Expect.equals(0, byteArray[i]); |
25 } | |
22 } | 26 } |
27 { | |
cshapiro
2012/12/05 23:01:49
Create a new test?
srdjan
2012/12/06 19:23:14
Done.
| |
28 Uint8ClampedList clampedByteArray; | |
23 | 29 |
30 clampedByteArray = new Uint8ClampedList(0); | |
31 Expect.isTrue(clampedByteArray is Uint8ClampedList); | |
32 Expect.isFalse(clampedByteArray is Uint8List); | |
33 Expect.equals(0, clampedByteArray.length); | |
34 | |
35 clampedByteArray = new Uint8ClampedList(10); | |
36 Expect.equals(10, clampedByteArray.length); | |
37 for (int i = 0; i < 10; i++) { | |
38 Expect.equals(0, clampedByteArray[i]); | |
39 } | |
40 } | |
24 } | 41 } |
25 | 42 |
26 void testUnsignedByteArrayRange(bool check_throws) { | 43 void testUnsignedByteArrayRange(bool check_throws) { |
27 Uint8List byteArray; | 44 { |
28 byteArray = new Uint8List(10); | 45 Uint8List byteArray; |
46 byteArray = new Uint8List(10); | |
29 | 47 |
30 byteArray[1] = 255; | 48 byteArray[1] = 255; |
31 Expect.equals(255, byteArray[1]); | 49 Expect.equals(255, byteArray[1]); |
32 byteArray[1] = 0; | 50 byteArray[1] = 0; |
33 Expect.equals(0, byteArray[1]); | 51 Expect.equals(0, byteArray[1]); |
34 | 52 |
35 // These should eventually throw. | 53 // These should eventually throw. |
36 byteArray[1] = 256; | 54 byteArray[1] = 256; |
37 byteArray[1] = -1; | 55 byteArray[1] = -1; |
38 byteArray[2] = -129; | 56 byteArray[2] = -129; |
39 if (check_throws) { | 57 if (check_throws) { |
40 Expect.throws(() { | 58 Expect.throws(() { |
41 byteArray[1] = 1.2; | 59 byteArray[1] = 1.2; |
42 }); | 60 }); |
61 } | |
62 } | |
63 { | |
cshapiro
2012/12/05 23:01:49
Same here
srdjan
2012/12/06 19:23:14
Done.
| |
64 Uint8ClampedList byteArray; | |
65 byteArray = new Uint8ClampedList(10); | |
66 | |
67 byteArray[1] = 255; | |
68 Expect.equals(255, byteArray[1]); | |
69 byteArray[1] = 0; | |
70 Expect.equals(0, byteArray[1]); | |
71 | |
72 // These should eventually throw. | |
73 byteArray[1] = 256; | |
74 byteArray[2] = -129; | |
75 Expect.equals(255, byteArray[1]); | |
76 Expect.equals(0, byteArray[2]); | |
43 } | 77 } |
44 } | 78 } |
45 | 79 |
46 void testByteArrayRange(bool check_throws) { | 80 void testByteArrayRange(bool check_throws) { |
47 Int8List byteArray; | 81 Int8List byteArray; |
48 byteArray = new Int8List(10); | 82 byteArray = new Int8List(10); |
49 byteArray[1] = 0; | 83 byteArray[1] = 0; |
50 Expect.equals(0, byteArray[1]); | 84 Expect.equals(0, byteArray[1]); |
51 byteArray[2] = -128; | 85 byteArray[2] = -128; |
52 Expect.equals(-128, byteArray[2]); | 86 Expect.equals(-128, byteArray[2]); |
53 byteArray[3] = 127; | 87 byteArray[3] = 127; |
54 Expect.equals(127, byteArray[3]); | 88 Expect.equals(127, byteArray[3]); |
55 // This should eventually throw. | 89 // This should eventually throw. |
56 byteArray[0] = 128; | 90 byteArray[0] = 128; |
57 byteArray[4] = -129; | 91 byteArray[4] = -129; |
58 if (check_throws) { | 92 if (check_throws) { |
59 Expect.throws(() { | 93 Expect.throws(() { |
60 byteArray[1] = 1.2; | 94 byteArray[1] = 1.2; |
61 }); | 95 }); |
62 } | 96 } |
63 } | 97 } |
64 | 98 |
65 void testSetRange() { | |
66 Uint8List byteArray = new Uint8List(3); | |
67 | 99 |
100 void testSetRangeHelper(byteArray) { | |
68 List<int> list = [10, 11, 12]; | 101 List<int> list = [10, 11, 12]; |
69 byteArray.setRange(0, 3, list); | 102 byteArray.setRange(0, 3, list); |
70 for (int i = 0; i < 3; i++) { | 103 for (int i = 0; i < 3; i++) { |
71 Expect.equals(10 + i, byteArray[i]); | 104 Expect.equals(10 + i, byteArray[i]); |
72 } | 105 } |
73 | 106 |
74 byteArray[0] = 20; | 107 byteArray[0] = 20; |
75 byteArray[1] = 21; | 108 byteArray[1] = 21; |
76 byteArray[2] = 22; | 109 byteArray[2] = 22; |
77 list.setRange(0, 3, byteArray); | 110 list.setRange(0, 3, byteArray); |
78 for (int i = 0; i < 3; i++) { | 111 for (int i = 0; i < 3; i++) { |
79 Expect.equals(20 + i, list[i]); | 112 Expect.equals(20 + i, list[i]); |
80 } | 113 } |
81 | 114 |
82 byteArray.setRange(1, 2, const [8, 9]); | 115 byteArray.setRange(1, 2, const [8, 9]); |
83 Expect.equals(20, byteArray[0]); | 116 Expect.equals(20, byteArray[0]); |
84 Expect.equals(8, byteArray[1]); | 117 Expect.equals(8, byteArray[1]); |
85 Expect.equals(9, byteArray[2]); | 118 Expect.equals(9, byteArray[2]); |
86 } | 119 } |
87 | 120 |
88 void testIndexOutOfRange() { | 121 |
89 Uint8List byteArray = new Uint8List(3); | 122 void testSetRange() { |
123 testSetRangeHelper(new Uint8List(3)); | |
124 testSetRangeHelper(new Uint8ClampedList(3)); | |
125 } | |
126 | |
127 void testIndexOutOfRangeHelper(byteArray) { | |
90 List<int> list = const [0, 1, 2, 3]; | 128 List<int> list = const [0, 1, 2, 3]; |
91 | 129 |
92 Expect.throws(() { | 130 Expect.throws(() { |
93 byteArray.setRange(0, 4, list); | 131 byteArray.setRange(0, 4, list); |
94 }); | 132 }); |
95 | 133 |
96 Expect.throws(() { | 134 Expect.throws(() { |
97 byteArray.setRange(3, 1, list); | 135 byteArray.setRange(3, 1, list); |
98 }); | 136 }); |
99 } | 137 } |
100 | 138 |
101 void testIndexOf() { | 139 void testIndexOutOfRange() { |
102 var list = new Uint8List(10); | 140 testIndexOutOfRangeHelper(new Uint8List(3)); |
141 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); | |
142 } | |
143 | |
144 void testIndexOfHelper(list) { | |
103 for (int i = 0; i < list.length; i++) { | 145 for (int i = 0; i < list.length; i++) { |
104 list[i] = i + 10; | 146 list[i] = i + 10; |
105 } | 147 } |
106 Expect.equals(0, list.indexOf(10)); | 148 Expect.equals(0, list.indexOf(10)); |
107 Expect.equals(5, list.indexOf(15)); | 149 Expect.equals(5, list.indexOf(15)); |
108 Expect.equals(9, list.indexOf(19)); | 150 Expect.equals(9, list.indexOf(19)); |
109 Expect.equals(-1, list.indexOf(20)); | 151 Expect.equals(-1, list.indexOf(20)); |
110 | 152 |
111 list = new Float32List(10); | 153 list = new Float32List(10); |
112 for (int i = 0; i < list.length; i++) { | 154 for (int i = 0; i < list.length; i++) { |
113 list[i] = i + 10.0; | 155 list[i] = i + 10.0; |
114 } | 156 } |
115 Expect.equals(0, list.indexOf(10.0)); | 157 Expect.equals(0, list.indexOf(10.0)); |
116 Expect.equals(5, list.indexOf(15.0)); | 158 Expect.equals(5, list.indexOf(15.0)); |
117 Expect.equals(9, list.indexOf(19.0)); | 159 Expect.equals(9, list.indexOf(19.0)); |
118 Expect.equals(-1, list.indexOf(20.0)); | 160 Expect.equals(-1, list.indexOf(20.0)); |
119 } | 161 } |
120 | 162 |
121 void testSubArray() { | 163 void testIndexOf() { |
122 var list = new Uint8List(10); | 164 testIndexOfHelper(new Uint8List(10)); |
165 testIndexOfHelper(new Uint8ClampedList(10)); | |
166 } | |
167 | |
168 void testSubArrayHelper(list) { | |
123 var array = list.asByteArray(); | 169 var array = list.asByteArray(); |
124 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); | 170 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); |
125 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); | 171 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); |
126 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); | 172 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); |
127 Expect.equals(0, array.subByteArray(10).lengthInBytes()); | 173 Expect.equals(0, array.subByteArray(10).lengthInBytes()); |
128 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); | 174 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); |
129 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); | 175 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); |
130 Expect.equals(5, array.subByteArray(5, 5).lengthInBytes()); | 176 Expect.equals(5, array.subByteArray(5, 5).lengthInBytes()); |
131 Expect.equals(5, array.subByteArray(5).lengthInBytes()); | 177 Expect.equals(5, array.subByteArray(5).lengthInBytes()); |
132 Expect.equals(5, array.subByteArray(5, null).lengthInBytes()); | 178 Expect.equals(5, array.subByteArray(5, null).lengthInBytes()); |
(...skipping 19 matching lines...) Expand all Loading... | |
152 assert(checkedMode = true); | 198 assert(checkedMode = true); |
153 if (!checkedMode) { | 199 if (!checkedMode) { |
154 // In checked mode these will necessarily throw a TypeError. | 200 // In checked mode these will necessarily throw a TypeError. |
155 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); | 201 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); |
156 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); | 202 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); |
157 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); | 203 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); |
158 } | 204 } |
159 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); | 205 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); |
160 } | 206 } |
161 | 207 |
208 | |
209 void testSubArray() { | |
210 testSubArrayHelper(new Uint8List(10)); | |
211 testSubArrayHelper(new Uint8ClampedList(10)); | |
212 } | |
213 | |
162 main() { | 214 main() { |
163 for (int i = 0; i < 2000; i++) { | 215 for (int i = 0; i < 2000; i++) { |
164 testCreateByteArray(); | 216 testCreateByteArray(); |
165 testByteArrayRange(false); | 217 testByteArrayRange(false); |
166 testUnsignedByteArrayRange(false); | 218 testUnsignedByteArrayRange(false); |
167 testSetRange(); | 219 testSetRange(); |
168 testIndexOutOfRange(); | 220 testIndexOutOfRange(); |
169 testIndexOf(); | 221 testIndexOf(); |
170 testSubArray(); | 222 testSubArray(); |
171 } | 223 } |
172 testByteArrayRange(true); | 224 testByteArrayRange(true); |
173 testUnsignedByteArrayRange(true); | 225 testUnsignedByteArrayRange(true); |
174 } | 226 } |
175 | 227 |
OLD | NEW |