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; | 8 library ByteArrayTest; |
9 | 9 |
10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
(...skipping 13 matching lines...) Expand all Loading... |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 void testCreateClampedUint8ByteArray() { | 27 void testCreateClampedUint8ByteArray() { |
28 Uint8ClampedList clampedByteArray; | 28 Uint8ClampedList clampedByteArray; |
29 | 29 |
30 clampedByteArray = new Uint8ClampedList(0); | 30 clampedByteArray = new Uint8ClampedList(0); |
31 Expect.isTrue(clampedByteArray is Uint8ClampedList); | 31 Expect.isTrue(clampedByteArray is Uint8ClampedList); |
32 Expect.isFalse(clampedByteArray is Uint8List); | 32 Expect.isFalse(clampedByteArray is Uint8List); |
33 Expect.equals(0, clampedByteArray.length); | 33 Expect.equals(0, clampedByteArray.length); |
| 34 Expect.equals(0, clampedByteArray.lengthInBytes()); |
34 | 35 |
35 clampedByteArray = new Uint8ClampedList(10); | 36 clampedByteArray = new Uint8ClampedList(10); |
36 Expect.equals(10, clampedByteArray.length); | 37 Expect.equals(10, clampedByteArray.length); |
37 for (int i = 0; i < 10; i++) { | 38 for (int i = 0; i < 10; i++) { |
38 Expect.equals(0, clampedByteArray[i]); | 39 Expect.equals(0, clampedByteArray[i]); |
39 } | 40 } |
40 } | 41 } |
41 | 42 |
| 43 void testCreateExternalClampedUint8ByteArray() { |
| 44 List externalClampedByteArray; |
| 45 |
| 46 externalClampedByteArray = new Uint8ClampedList.transferable(0); |
| 47 Expect.isTrue(externalClampedByteArray is Uint8ClampedList); |
| 48 Expect.isFalse(externalClampedByteArray is Uint8List); |
| 49 Expect.equals(0, externalClampedByteArray.length); |
| 50 Expect.equals(0, externalClampedByteArray.lengthInBytes()); |
| 51 |
| 52 externalClampedByteArray = new Uint8ClampedList.transferable(10); |
| 53 Expect.equals(10, externalClampedByteArray.length); |
| 54 for (int i = 0; i < 10; i++) { |
| 55 Expect.equals(0, externalClampedByteArray[i]); |
| 56 } |
| 57 |
| 58 } |
| 59 |
42 void testUnsignedByteArrayRange(bool check_throws) { | 60 void testUnsignedByteArrayRange(bool check_throws) { |
43 Uint8List byteArray; | 61 Uint8List byteArray; |
44 byteArray = new Uint8List(10); | 62 byteArray = new Uint8List(10); |
45 | 63 |
46 byteArray[1] = 255; | 64 byteArray[1] = 255; |
47 Expect.equals(255, byteArray[1]); | 65 Expect.equals(255, byteArray[1]); |
48 byteArray[1] = 0; | 66 byteArray[1] = 0; |
49 Expect.equals(0, byteArray[1]); | 67 Expect.equals(0, byteArray[1]); |
50 | 68 |
51 for (int i = 0; i < byteArray.length; i++) { | 69 for (int i = 0; i < byteArray.length; i++) { |
52 byteArray[i] = i; | 70 byteArray[i] = i; |
53 } | 71 } |
54 for (int i = 0; i < byteArray.length; i++) { | 72 for (int i = 0; i < byteArray.length; i++) { |
55 Expect.equals(i, byteArray[i]); | 73 Expect.equals(i, byteArray[i]); |
56 } | 74 } |
57 | 75 |
58 // These should eventually throw. | 76 // These should eventually throw. |
59 byteArray[1] = 256; | 77 byteArray[1] = 256; |
60 byteArray[1] = -1; | 78 byteArray[1] = -1; |
61 byteArray[2] = -129; | 79 byteArray[2] = -129; |
62 if (check_throws) { | 80 if (check_throws) { |
63 Expect.throws(() { | 81 Expect.throws(() { |
64 byteArray[1] = 1.2; | 82 byteArray[1] = 1.2; |
65 }); | 83 }); |
66 } | 84 } |
67 } | 85 } |
68 | 86 |
69 void testClampedUnsignedByteArrayRange(bool check_throws) { | 87 void testClampedUnsignedByteArrayRangeHelper(Uint8ClampedList byteArray, |
| 88 bool check_throws) { |
70 Uint8ClampedList byteArray; | 89 Uint8ClampedList byteArray; |
71 byteArray = new Uint8ClampedList(10); | 90 byteArray = new Uint8ClampedList(10); |
72 | 91 |
73 byteArray[1] = 255; | 92 byteArray[1] = 255; |
74 Expect.equals(255, byteArray[1]); | 93 Expect.equals(255, byteArray[1]); |
75 byteArray[1] = 0; | 94 byteArray[1] = 0; |
76 Expect.equals(0, byteArray[1]); | 95 Expect.equals(0, byteArray[1]); |
77 for (int i = 0; i < byteArray.length; i++) { | 96 for (int i = 0; i < byteArray.length; i++) { |
78 byteArray[i] = i; | 97 byteArray[i] = i; |
79 } | 98 } |
80 for (int i = 0; i < byteArray.length; i++) { | 99 for (int i = 0; i < byteArray.length; i++) { |
81 Expect.equals(i, byteArray[i]); | 100 Expect.equals(i, byteArray[i]); |
82 } | 101 } |
83 | 102 |
84 // These should eventually throw. | 103 // These should eventually throw. |
85 byteArray[1] = 256; | 104 byteArray[1] = 256; |
86 byteArray[2] = -129; | 105 byteArray[2] = -129; |
87 Expect.equals(255, byteArray[1]); | 106 Expect.equals(255, byteArray[1]); |
88 Expect.equals(0, byteArray[2]); | 107 Expect.equals(0, byteArray[2]); |
89 } | 108 } |
90 | 109 |
| 110 void testClampedUnsignedByteArrayRange(bool check_throws) { |
| 111 testClampedUnsignedByteArrayRangeHelper(new Uint8ClampedList(10), |
| 112 check_throws); |
| 113 } |
| 114 |
| 115 |
| 116 void testExternalClampedUnsignedByteArrayRange(bool check_throws) { |
| 117 testClampedUnsignedByteArrayRangeHelper(new Uint8ClampedList.transferable(10), |
| 118 check_throws); |
| 119 } |
| 120 |
| 121 |
91 void testByteArrayRange(bool check_throws) { | 122 void testByteArrayRange(bool check_throws) { |
92 Int8List byteArray; | 123 Int8List byteArray; |
93 byteArray = new Int8List(10); | 124 byteArray = new Int8List(10); |
94 byteArray[1] = 0; | 125 byteArray[1] = 0; |
95 Expect.equals(0, byteArray[1]); | 126 Expect.equals(0, byteArray[1]); |
96 byteArray[2] = -128; | 127 byteArray[2] = -128; |
97 Expect.equals(-128, byteArray[2]); | 128 Expect.equals(-128, byteArray[2]); |
98 byteArray[3] = 127; | 129 byteArray[3] = 127; |
99 Expect.equals(127, byteArray[3]); | 130 Expect.equals(127, byteArray[3]); |
100 // This should eventually throw. | 131 // This should eventually throw. |
101 byteArray[0] = 128; | 132 byteArray[0] = 128; |
102 byteArray[4] = -129; | 133 byteArray[4] = -129; |
103 if (check_throws) { | 134 if (check_throws) { |
104 Expect.throws(() { | 135 Expect.throws(() { |
105 byteArray[1] = 1.2; | 136 byteArray[1] = 1.2; |
106 }); | 137 }); |
107 } | 138 } |
108 } | 139 } |
109 | 140 |
110 | |
111 void testSetRangeHelper(byteArray) { | 141 void testSetRangeHelper(byteArray) { |
112 List<int> list = [10, 11, 12]; | 142 List<int> list = [10, 11, 12]; |
113 byteArray.setRange(0, 3, list); | 143 byteArray.setRange(0, 3, list); |
114 for (int i = 0; i < 3; i++) { | 144 for (int i = 0; i < 3; i++) { |
115 Expect.equals(10 + i, byteArray[i]); | 145 Expect.equals(10 + i, byteArray[i]); |
116 } | 146 } |
117 | 147 |
118 byteArray[0] = 20; | 148 byteArray[0] = 20; |
119 byteArray[1] = 21; | 149 byteArray[1] = 21; |
120 byteArray[2] = 22; | 150 byteArray[2] = 22; |
121 list.setRange(0, 3, byteArray); | 151 list.setRange(0, 3, byteArray); |
122 for (int i = 0; i < 3; i++) { | 152 for (int i = 0; i < 3; i++) { |
123 Expect.equals(20 + i, list[i]); | 153 Expect.equals(20 + i, list[i]); |
124 } | 154 } |
125 | 155 |
126 byteArray.setRange(1, 2, const [8, 9]); | 156 byteArray.setRange(1, 2, const [8, 9]); |
127 Expect.equals(20, byteArray[0]); | 157 Expect.equals(20, byteArray[0]); |
128 Expect.equals(8, byteArray[1]); | 158 Expect.equals(8, byteArray[1]); |
129 Expect.equals(9, byteArray[2]); | 159 Expect.equals(9, byteArray[2]); |
130 } | 160 } |
131 | 161 |
132 | |
133 void testSetRange() { | 162 void testSetRange() { |
134 testSetRangeHelper(new Uint8List(3)); | 163 testSetRangeHelper(new Uint8List(3)); |
| 164 testSetRangeHelper(new Uint8List.transferable(3)); |
135 testSetRangeHelper(new Uint8ClampedList(3)); | 165 testSetRangeHelper(new Uint8ClampedList(3)); |
| 166 testSetRangeHelper(new Uint8ClampedList.transferable(3)); |
136 } | 167 } |
137 | 168 |
138 void testIndexOutOfRangeHelper(byteArray) { | 169 void testIndexOutOfRangeHelper(byteArray) { |
139 List<int> list = const [0, 1, 2, 3]; | 170 List<int> list = const [0, 1, 2, 3]; |
140 | 171 |
141 Expect.throws(() { | 172 Expect.throws(() { |
142 byteArray.setRange(0, 4, list); | 173 byteArray.setRange(0, 4, list); |
143 }); | 174 }); |
144 | 175 |
145 Expect.throws(() { | 176 Expect.throws(() { |
146 byteArray.setRange(3, 1, list); | 177 byteArray.setRange(3, 1, list); |
147 }); | 178 }); |
148 } | 179 } |
149 | 180 |
150 void testIndexOutOfRange() { | 181 void testIndexOutOfRange() { |
151 testIndexOutOfRangeHelper(new Uint8List(3)); | 182 testIndexOutOfRangeHelper(new Uint8List(3)); |
| 183 testIndexOutOfRangeHelper(new Uint8List.transferable(3)); |
152 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); | 184 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); |
| 185 testIndexOutOfRangeHelper(new Uint8ClampedList.transferable(3)); |
153 } | 186 } |
154 | 187 |
155 void testIndexOfHelper(list) { | 188 void testIndexOfHelper(list) { |
156 for (int i = 0; i < list.length; i++) { | 189 for (int i = 0; i < list.length; i++) { |
157 list[i] = i + 10; | 190 list[i] = i + 10; |
158 } | 191 } |
159 Expect.equals(0, list.indexOf(10)); | 192 Expect.equals(0, list.indexOf(10)); |
160 Expect.equals(5, list.indexOf(15)); | 193 Expect.equals(5, list.indexOf(15)); |
161 Expect.equals(9, list.indexOf(19)); | 194 Expect.equals(9, list.indexOf(19)); |
162 Expect.equals(-1, list.indexOf(20)); | 195 Expect.equals(-1, list.indexOf(20)); |
163 | 196 |
164 list = new Float32List(10); | 197 list = new Float32List(10); |
165 for (int i = 0; i < list.length; i++) { | 198 for (int i = 0; i < list.length; i++) { |
166 list[i] = i + 10.0; | 199 list[i] = i + 10.0; |
167 } | 200 } |
168 Expect.equals(0, list.indexOf(10.0)); | 201 Expect.equals(0, list.indexOf(10.0)); |
169 Expect.equals(5, list.indexOf(15.0)); | 202 Expect.equals(5, list.indexOf(15.0)); |
170 Expect.equals(9, list.indexOf(19.0)); | 203 Expect.equals(9, list.indexOf(19.0)); |
171 Expect.equals(-1, list.indexOf(20.0)); | 204 Expect.equals(-1, list.indexOf(20.0)); |
172 } | 205 } |
173 | 206 |
174 void testIndexOf() { | 207 void testIndexOf() { |
175 testIndexOfHelper(new Uint8List(10)); | 208 testIndexOfHelper(new Uint8List(10)); |
| 209 testIndexOfHelper(new Uint8List.transferable(10)); |
176 testIndexOfHelper(new Uint8ClampedList(10)); | 210 testIndexOfHelper(new Uint8ClampedList(10)); |
| 211 testIndexOfHelper(new Uint8ClampedList.transferable(10)); |
177 } | 212 } |
178 | 213 |
179 void testSubArrayHelper(list) { | 214 void testSubArrayHelper(list) { |
180 var array = list.asByteArray(); | 215 var array = list.asByteArray(); |
181 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); | 216 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); |
182 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); | 217 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); |
183 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); | 218 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); |
184 Expect.equals(0, array.subByteArray(10).lengthInBytes()); | 219 Expect.equals(0, array.subByteArray(10).lengthInBytes()); |
185 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); | 220 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); |
186 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); | 221 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); |
(...skipping 25 matching lines...) Expand all Loading... |
212 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); | 247 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); |
213 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); | 248 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); |
214 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); | 249 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); |
215 } | 250 } |
216 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); | 251 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); |
217 } | 252 } |
218 | 253 |
219 | 254 |
220 void testSubArray() { | 255 void testSubArray() { |
221 testSubArrayHelper(new Uint8List(10)); | 256 testSubArrayHelper(new Uint8List(10)); |
| 257 testSubArrayHelper(new Uint8List.transferable(10)); |
222 testSubArrayHelper(new Uint8ClampedList(10)); | 258 testSubArrayHelper(new Uint8ClampedList(10)); |
| 259 testSubArrayHelper(new Uint8ClampedList.transferable(10)); |
223 } | 260 } |
224 | 261 |
225 main() { | 262 main() { |
226 for (int i = 0; i < 2000; i++) { | 263 for (int i = 0; i < 2000; i++) { |
227 testCreateUint8ByteArray(); | 264 testCreateUint8ByteArray(); |
228 testCreateClampedUint8ByteArray(); | 265 testCreateClampedUint8ByteArray(); |
| 266 testCreateExternalClampedUint8ByteArray(); |
229 testByteArrayRange(false); | 267 testByteArrayRange(false); |
230 testUnsignedByteArrayRange(false); | 268 testUnsignedByteArrayRange(false); |
231 testClampedUnsignedByteArrayRange(false); | 269 testClampedUnsignedByteArrayRange(false); |
| 270 testExternalClampedUnsignedByteArrayRange(false); |
232 testSetRange(); | 271 testSetRange(); |
233 testIndexOutOfRange(); | 272 testIndexOutOfRange(); |
234 testIndexOf(); | 273 testIndexOf(); |
235 testSubArray(); | 274 testSubArray(); |
236 } | 275 } |
237 testByteArrayRange(true); | 276 testByteArrayRange(true); |
238 testUnsignedByteArrayRange(true); | 277 testUnsignedByteArrayRange(true); |
| 278 testExternalClampedUnsignedByteArrayRange(true); |
239 } | 279 } |
240 | 280 |
OLD | NEW |