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