| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // Dart test program for testing native byte arrays. | |
| 6 | |
| 7 // Library tag to be able to run in html test framework. | |
| 8 library ByteArrayTest; | |
| 9 | |
| 10 import 'dart:scalarlist'; | |
| 11 | |
| 12 void testCreateUint8ByteArray() { | |
| 13 Uint8List byteArray; | |
| 14 | |
| 15 byteArray = new Uint8List(0); | |
| 16 Expect.isTrue(byteArray is Uint8List); | |
| 17 Expect.isFalse(byteArray is Uint8ClampedList); | |
| 18 Expect.equals(0, byteArray.length); | |
| 19 | |
| 20 byteArray = new Uint8List(10); | |
| 21 Expect.equals(10, byteArray.length); | |
| 22 for (int i = 0; i < 10; i++) { | |
| 23 Expect.equals(0, byteArray[i]); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 void testCreateClampedUint8ByteArray() { | |
| 28 Uint8ClampedList clampedByteArray; | |
| 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 Expect.equals(0, clampedByteArray.lengthInBytes()); | |
| 35 | |
| 36 clampedByteArray = new Uint8ClampedList(10); | |
| 37 Expect.equals(10, clampedByteArray.length); | |
| 38 for (int i = 0; i < 10; i++) { | |
| 39 Expect.equals(0, clampedByteArray[i]); | |
| 40 } | |
| 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 externalClampedByteArray[0] = -1; | |
| 59 Expect.equals(0, externalClampedByteArray[0]); | |
| 60 | |
| 61 for (int i = 0; i < 10; i++) { | |
| 62 externalClampedByteArray[i] = i + 250; | |
| 63 } | |
| 64 for (int i = 0; i < 10; i++) { | |
| 65 Expect.equals(i + 250 > 255 ? 255 : i + 250, externalClampedByteArray[i]); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void testUnsignedByteArrayRange(bool check_throws) { | |
| 70 Uint8List byteArray; | |
| 71 byteArray = new Uint8List(10); | |
| 72 | |
| 73 byteArray[1] = 255; | |
| 74 Expect.equals(255, byteArray[1]); | |
| 75 byteArray[1] = 0; | |
| 76 Expect.equals(0, byteArray[1]); | |
| 77 | |
| 78 for (int i = 0; i < byteArray.length; i++) { | |
| 79 byteArray[i] = i; | |
| 80 } | |
| 81 for (int i = 0; i < byteArray.length; i++) { | |
| 82 Expect.equals(i, byteArray[i]); | |
| 83 } | |
| 84 | |
| 85 // These should eventually throw. | |
| 86 byteArray[1] = 256; | |
| 87 byteArray[1] = -1; | |
| 88 byteArray[2] = -129; | |
| 89 if (check_throws) { | |
| 90 Expect.throws(() { | |
| 91 byteArray[1] = 1.2; | |
| 92 }); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void testClampedUnsignedByteArrayRangeHelper(Uint8ClampedList byteArray, | |
| 97 bool check_throws) { | |
| 98 Uint8ClampedList byteArray; | |
| 99 byteArray = new Uint8ClampedList(10); | |
| 100 | |
| 101 byteArray[1] = 255; | |
| 102 Expect.equals(255, byteArray[1]); | |
| 103 byteArray[1] = 0; | |
| 104 Expect.equals(0, byteArray[1]); | |
| 105 for (int i = 0; i < byteArray.length; i++) { | |
| 106 byteArray[i] = i; | |
| 107 } | |
| 108 for (int i = 0; i < byteArray.length; i++) { | |
| 109 Expect.equals(i, byteArray[i]); | |
| 110 } | |
| 111 | |
| 112 // These should eventually throw. | |
| 113 byteArray[1] = 256; | |
| 114 byteArray[2] = -129; | |
| 115 Expect.equals(255, byteArray[1]); | |
| 116 Expect.equals(0, byteArray[2]); | |
| 117 } | |
| 118 | |
| 119 void testClampedUnsignedByteArrayRange(bool check_throws) { | |
| 120 testClampedUnsignedByteArrayRangeHelper(new Uint8ClampedList(10), | |
| 121 check_throws); | |
| 122 } | |
| 123 | |
| 124 | |
| 125 void testExternalClampedUnsignedByteArrayRange(bool check_throws) { | |
| 126 testClampedUnsignedByteArrayRangeHelper(new Uint8ClampedList.transferable(10), | |
| 127 check_throws); | |
| 128 } | |
| 129 | |
| 130 | |
| 131 void testByteArrayRange(bool check_throws) { | |
| 132 Int8List byteArray; | |
| 133 byteArray = new Int8List(10); | |
| 134 byteArray[1] = 0; | |
| 135 Expect.equals(0, byteArray[1]); | |
| 136 byteArray[2] = -128; | |
| 137 Expect.equals(-128, byteArray[2]); | |
| 138 byteArray[3] = 127; | |
| 139 Expect.equals(127, byteArray[3]); | |
| 140 // This should eventually throw. | |
| 141 byteArray[0] = 128; | |
| 142 byteArray[4] = -129; | |
| 143 if (check_throws) { | |
| 144 Expect.throws(() { | |
| 145 byteArray[1] = 1.2; | |
| 146 }); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void testSetRangeHelper(byteArray) { | |
| 151 List<int> list = [10, 11, 12]; | |
| 152 byteArray.setRange(0, 3, list); | |
| 153 for (int i = 0; i < 3; i++) { | |
| 154 Expect.equals(10 + i, byteArray[i]); | |
| 155 } | |
| 156 | |
| 157 byteArray[0] = 20; | |
| 158 byteArray[1] = 21; | |
| 159 byteArray[2] = 22; | |
| 160 list.setRange(0, 3, byteArray); | |
| 161 for (int i = 0; i < 3; i++) { | |
| 162 Expect.equals(20 + i, list[i]); | |
| 163 } | |
| 164 | |
| 165 byteArray.setRange(1, 2, const [8, 9]); | |
| 166 Expect.equals(20, byteArray[0]); | |
| 167 Expect.equals(8, byteArray[1]); | |
| 168 Expect.equals(9, byteArray[2]); | |
| 169 } | |
| 170 | |
| 171 void testSetRange() { | |
| 172 testSetRangeHelper(new Uint8List(3)); | |
| 173 testSetRangeHelper(new Uint8List.transferable(3)); | |
| 174 testSetRangeHelper(new Uint8ClampedList(3)); | |
| 175 testSetRangeHelper(new Uint8ClampedList.transferable(3)); | |
| 176 } | |
| 177 | |
| 178 void testIndexOutOfRangeHelper(byteArray) { | |
| 179 List<int> list = const [0, 1, 2, 3]; | |
| 180 | |
| 181 Expect.throws(() { | |
| 182 byteArray.setRange(0, 4, list); | |
| 183 }); | |
| 184 | |
| 185 Expect.throws(() { | |
| 186 byteArray.setRange(3, 1, list); | |
| 187 }); | |
| 188 } | |
| 189 | |
| 190 void testIndexOutOfRange() { | |
| 191 testIndexOutOfRangeHelper(new Uint8List(3)); | |
| 192 testIndexOutOfRangeHelper(new Uint8List.transferable(3)); | |
| 193 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); | |
| 194 testIndexOutOfRangeHelper(new Uint8ClampedList.transferable(3)); | |
| 195 } | |
| 196 | |
| 197 void testIndexOfHelper(list) { | |
| 198 for (int i = 0; i < list.length; i++) { | |
| 199 list[i] = i + 10; | |
| 200 } | |
| 201 Expect.equals(0, list.indexOf(10)); | |
| 202 Expect.equals(5, list.indexOf(15)); | |
| 203 Expect.equals(9, list.indexOf(19)); | |
| 204 Expect.equals(-1, list.indexOf(20)); | |
| 205 | |
| 206 list = new Float32List(10); | |
| 207 for (int i = 0; i < list.length; i++) { | |
| 208 list[i] = i + 10.0; | |
| 209 } | |
| 210 Expect.equals(0, list.indexOf(10.0)); | |
| 211 Expect.equals(5, list.indexOf(15.0)); | |
| 212 Expect.equals(9, list.indexOf(19.0)); | |
| 213 Expect.equals(-1, list.indexOf(20.0)); | |
| 214 } | |
| 215 | |
| 216 void testIndexOf() { | |
| 217 testIndexOfHelper(new Uint8List(10)); | |
| 218 testIndexOfHelper(new Uint8List.transferable(10)); | |
| 219 testIndexOfHelper(new Uint8ClampedList(10)); | |
| 220 testIndexOfHelper(new Uint8ClampedList.transferable(10)); | |
| 221 } | |
| 222 | |
| 223 void testSubArrayHelper(list) { | |
| 224 var array = list.asByteArray(); | |
| 225 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); | |
| 226 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); | |
| 227 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); | |
| 228 Expect.equals(0, array.subByteArray(10).lengthInBytes()); | |
| 229 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); | |
| 230 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); | |
| 231 Expect.equals(5, array.subByteArray(5, 5).lengthInBytes()); | |
| 232 Expect.equals(5, array.subByteArray(5).lengthInBytes()); | |
| 233 Expect.equals(5, array.subByteArray(5, null).lengthInBytes()); | |
| 234 Expect.equals(10, array.subByteArray(0, 10).lengthInBytes()); | |
| 235 Expect.equals(10, array.subByteArray(0).lengthInBytes()); | |
| 236 Expect.equals(10, array.subByteArray(0, null).lengthInBytes()); | |
| 237 Expect.equals(10, array.subByteArray().lengthInBytes()); | |
| 238 testThrowsIndex(function) { | |
| 239 Expect.throws(function, (e) => e is RangeError); | |
| 240 } | |
| 241 testThrowsIndex(() => array.subByteArray(0, -1)); | |
| 242 testThrowsIndex(() => array.subByteArray(1, -1)); | |
| 243 testThrowsIndex(() => array.subByteArray(10, -1)); | |
| 244 testThrowsIndex(() => array.subByteArray(-1, 0)); | |
| 245 testThrowsIndex(() => array.subByteArray(-1)); | |
| 246 testThrowsIndex(() => array.subByteArray(-1, null)); | |
| 247 testThrowsIndex(() => array.subByteArray(11, 0)); | |
| 248 testThrowsIndex(() => array.subByteArray(11)); | |
| 249 testThrowsIndex(() => array.subByteArray(11, null)); | |
| 250 testThrowsIndex(() => array.subByteArray(6, 5)); | |
| 251 | |
| 252 bool checkedMode = false; | |
| 253 assert(checkedMode = true); | |
| 254 if (!checkedMode) { | |
| 255 // In checked mode these will necessarily throw a TypeError. | |
| 256 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); | |
| 257 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); | |
| 258 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); | |
| 259 } | |
| 260 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); | |
| 261 } | |
| 262 | |
| 263 | |
| 264 void testSubArray() { | |
| 265 testSubArrayHelper(new Uint8List(10)); | |
| 266 testSubArrayHelper(new Uint8List.transferable(10)); | |
| 267 testSubArrayHelper(new Uint8ClampedList(10)); | |
| 268 testSubArrayHelper(new Uint8ClampedList.transferable(10)); | |
| 269 } | |
| 270 | |
| 271 main() { | |
| 272 for (int i = 0; i < 2000; i++) { | |
| 273 testCreateUint8ByteArray(); | |
| 274 testCreateClampedUint8ByteArray(); | |
| 275 testCreateExternalClampedUint8ByteArray(); | |
| 276 testByteArrayRange(false); | |
| 277 testUnsignedByteArrayRange(false); | |
| 278 testClampedUnsignedByteArrayRange(false); | |
| 279 testExternalClampedUnsignedByteArrayRange(false); | |
| 280 testSetRange(); | |
| 281 testIndexOutOfRange(); | |
| 282 testIndexOf(); | |
| 283 testSubArray(); | |
| 284 } | |
| 285 testByteArrayRange(true); | |
| 286 testUnsignedByteArrayRange(true); | |
| 287 testExternalClampedUnsignedByteArrayRange(true); | |
| 288 } | |
| 289 | |
| OLD | NEW |