| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 typed data. | 5 // Dart test program for testing typed data. |
| 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 TypedDataTest; | 8 library TypedDataTest; |
| 9 | 9 |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 Expect.equals(0, typed_data.length); | 34 Expect.equals(0, typed_data.length); |
| 35 Expect.equals(0, typed_data.lengthInBytes); | 35 Expect.equals(0, typed_data.lengthInBytes); |
| 36 | 36 |
| 37 typed_data = new Uint8ClampedList(10); | 37 typed_data = new Uint8ClampedList(10); |
| 38 Expect.equals(10, typed_data.length); | 38 Expect.equals(10, typed_data.length); |
| 39 for (int i = 0; i < 10; i++) { | 39 for (int i = 0; i < 10; i++) { |
| 40 Expect.equals(0, typed_data[i]); | 40 Expect.equals(0, typed_data[i]); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 void testCreateExternalClampedUint8TypedData() { |
| 45 List typed_data; |
| 46 |
| 47 typed_data = new Uint8ClampedList.transferable(0); |
| 48 Expect.isTrue(typed_data is Uint8ClampedList); |
| 49 Expect.isFalse(typed_data is Uint8List); |
| 50 Expect.equals(0, typed_data.length); |
| 51 Expect.equals(0, typed_data.lengthInBytes); |
| 52 |
| 53 typed_data = new Uint8ClampedList.transferable(10); |
| 54 Expect.equals(10, typed_data.length); |
| 55 for (int i = 0; i < 10; i++) { |
| 56 Expect.equals(0, typed_data[i]); |
| 57 } |
| 58 |
| 59 typed_data[0] = -1; |
| 60 Expect.equals(0, typed_data[0]); |
| 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 |
| 44 void testTypedDataRange(bool check_throws) { | 70 void testTypedDataRange(bool check_throws) { |
| 45 Int8List typed_data; | 71 Int8List typed_data; |
| 46 typed_data = new Int8List(10); | 72 typed_data = new Int8List(10); |
| 47 typed_data[1] = 0; | 73 typed_data[1] = 0; |
| 48 Expect.equals(0, typed_data[1]); | 74 Expect.equals(0, typed_data[1]); |
| 49 typed_data[2] = -128; | 75 typed_data[2] = -128; |
| 50 Expect.equals(-128, typed_data[2]); | 76 Expect.equals(-128, typed_data[2]); |
| 51 typed_data[3] = 127; | 77 typed_data[3] = 127; |
| 52 Expect.equals(127, typed_data[3]); | 78 Expect.equals(127, typed_data[3]); |
| 53 // This should eventually throw. | 79 // This should eventually throw. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 typed_data[2] = -129; | 134 typed_data[2] = -129; |
| 109 Expect.equals(255, typed_data[1]); | 135 Expect.equals(255, typed_data[1]); |
| 110 Expect.equals(0, typed_data[2]); | 136 Expect.equals(0, typed_data[2]); |
| 111 } | 137 } |
| 112 | 138 |
| 113 void testClampedUnsignedTypedDataRange(bool check_throws) { | 139 void testClampedUnsignedTypedDataRange(bool check_throws) { |
| 114 testClampedUnsignedTypedDataRangeHelper(new Uint8ClampedList(10), | 140 testClampedUnsignedTypedDataRangeHelper(new Uint8ClampedList(10), |
| 115 check_throws); | 141 check_throws); |
| 116 } | 142 } |
| 117 | 143 |
| 144 void testExternalClampedUnsignedTypedDataRange(bool check_throws) { |
| 145 testClampedUnsignedTypedDataRangeHelper(new Uint8ClampedList.transferable(10), |
| 146 check_throws); |
| 147 } |
| 148 |
| 118 void testSetRangeHelper(typed_data) { | 149 void testSetRangeHelper(typed_data) { |
| 119 List<int> list = [10, 11, 12]; | 150 List<int> list = [10, 11, 12]; |
| 120 typed_data.setRange(0, 3, list); | 151 typed_data.setRange(0, 3, list); |
| 121 for (int i = 0; i < 3; i++) { | 152 for (int i = 0; i < 3; i++) { |
| 122 Expect.equals(10 + i, typed_data[i]); | 153 Expect.equals(10 + i, typed_data[i]); |
| 123 } | 154 } |
| 124 | 155 |
| 125 typed_data[0] = 20; | 156 typed_data[0] = 20; |
| 126 typed_data[1] = 21; | 157 typed_data[1] = 21; |
| 127 typed_data[2] = 22; | 158 typed_data[2] = 22; |
| 128 list.setRange(0, 3, typed_data); | 159 list.setRange(0, 3, typed_data); |
| 129 for (int i = 0; i < 3; i++) { | 160 for (int i = 0; i < 3; i++) { |
| 130 Expect.equals(20 + i, list[i]); | 161 Expect.equals(20 + i, list[i]); |
| 131 } | 162 } |
| 132 | 163 |
| 133 typed_data.setRange(1, 3, const [8, 9]); | 164 typed_data.setRange(1, 3, const [8, 9]); |
| 134 Expect.equals(20, typed_data[0]); | 165 Expect.equals(20, typed_data[0]); |
| 135 Expect.equals(8, typed_data[1]); | 166 Expect.equals(8, typed_data[1]); |
| 136 Expect.equals(9, typed_data[2]); | 167 Expect.equals(9, typed_data[2]); |
| 137 } | 168 } |
| 138 | 169 |
| 139 void testSetRange() { | 170 void testSetRange() { |
| 140 testSetRangeHelper(new Uint8List(3)); | 171 testSetRangeHelper(new Uint8List(3)); |
| 172 testSetRangeHelper(new Uint8List.transferable(3)); |
| 141 testSetRangeHelper(new Uint8ClampedList(3)); | 173 testSetRangeHelper(new Uint8ClampedList(3)); |
| 174 testSetRangeHelper(new Uint8ClampedList.transferable(3)); |
| 142 } | 175 } |
| 143 | 176 |
| 144 void testIndexOutOfRangeHelper(typed_data) { | 177 void testIndexOutOfRangeHelper(typed_data) { |
| 145 List<int> list = const [0, 1, 2, 3]; | 178 List<int> list = const [0, 1, 2, 3]; |
| 146 | 179 |
| 147 Expect.throws(() { | 180 Expect.throws(() { |
| 148 typed_data.setRange(0, 4, list); | 181 typed_data.setRange(0, 4, list); |
| 149 }); | 182 }); |
| 150 | 183 |
| 151 Expect.throws(() { | 184 Expect.throws(() { |
| 152 typed_data.setRange(3, 4, list); | 185 typed_data.setRange(3, 4, list); |
| 153 }); | 186 }); |
| 154 } | 187 } |
| 155 | 188 |
| 156 void testIndexOutOfRange() { | 189 void testIndexOutOfRange() { |
| 157 testIndexOutOfRangeHelper(new Uint8List(3)); | 190 testIndexOutOfRangeHelper(new Uint8List(3)); |
| 191 testIndexOutOfRangeHelper(new Uint8List.transferable(3)); |
| 158 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); | 192 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); |
| 193 testIndexOutOfRangeHelper(new Uint8ClampedList.transferable(3)); |
| 159 } | 194 } |
| 160 | 195 |
| 161 void testIndexOfHelper(list) { | 196 void testIndexOfHelper(list) { |
| 162 for (int i = 0; i < list.length; i++) { | 197 for (int i = 0; i < list.length; i++) { |
| 163 list[i] = i + 10; | 198 list[i] = i + 10; |
| 164 } | 199 } |
| 165 Expect.equals(0, list.indexOf(10)); | 200 Expect.equals(0, list.indexOf(10)); |
| 166 Expect.equals(5, list.indexOf(15)); | 201 Expect.equals(5, list.indexOf(15)); |
| 167 Expect.equals(9, list.indexOf(19)); | 202 Expect.equals(9, list.indexOf(19)); |
| 168 Expect.equals(-1, list.indexOf(20)); | 203 Expect.equals(-1, list.indexOf(20)); |
| 169 | 204 |
| 170 list = new Float32List(10); | 205 list = new Float32List(10); |
| 171 for (int i = 0; i < list.length; i++) { | 206 for (int i = 0; i < list.length; i++) { |
| 172 list[i] = i + 10.0; | 207 list[i] = i + 10.0; |
| 173 } | 208 } |
| 174 Expect.equals(0, list.indexOf(10.0)); | 209 Expect.equals(0, list.indexOf(10.0)); |
| 175 Expect.equals(5, list.indexOf(15.0)); | 210 Expect.equals(5, list.indexOf(15.0)); |
| 176 Expect.equals(9, list.indexOf(19.0)); | 211 Expect.equals(9, list.indexOf(19.0)); |
| 177 Expect.equals(-1, list.indexOf(20.0)); | 212 Expect.equals(-1, list.indexOf(20.0)); |
| 178 } | 213 } |
| 179 | 214 |
| 180 void testIndexOf() { | 215 void testIndexOf() { |
| 181 testIndexOfHelper(new Uint8List(10)); | 216 testIndexOfHelper(new Uint8List.transferable(10)); |
| 182 testIndexOfHelper(new Uint8ClampedList(10)); | 217 testIndexOfHelper(new Uint8ClampedList(10)); |
| 218 testIndexOfHelper(new Uint8ClampedList.transferable(10)); |
| 183 } | 219 } |
| 184 | 220 |
| 185 void testGetAtIndex(TypedData list, num initial_value) { | 221 void testGetAtIndex(TypedData list, num initial_value) { |
| 186 var bdata = new ByteData.view(list.buffer); | 222 var bdata = new ByteData.view(list.buffer); |
| 187 for (int i = 0; i < bdata.lengthInBytes; i++) { | 223 for (int i = 0; i < bdata.lengthInBytes; i++) { |
| 188 Expect.equals(42, bdata.getUint8(i)); | 224 Expect.equals(42, bdata.getUint8(i)); |
| 189 Expect.equals(42, bdata.getInt8(i)); | 225 Expect.equals(42, bdata.getInt8(i)); |
| 190 } | 226 } |
| 191 for (int i = 0; i < bdata.lengthInBytes-1; i+=2) { | 227 for (int i = 0; i < bdata.lengthInBytes-1; i+=2) { |
| 192 Expect.equals(10794, bdata.getUint16(i)); | 228 Expect.equals(10794, bdata.getUint16(i)); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 Expect.throws(() => ints[0] = doubles[0]); | 336 Expect.throws(() => ints[0] = doubles[0]); |
| 301 Expect.throws(() => doubles[0] = ints[0]); | 337 Expect.throws(() => doubles[0] = ints[0]); |
| 302 } | 338 } |
| 303 } | 339 } |
| 304 } | 340 } |
| 305 | 341 |
| 306 main() { | 342 main() { |
| 307 for (int i = 0; i < 2000; i++) { | 343 for (int i = 0; i < 2000; i++) { |
| 308 testCreateUint8TypedData(); | 344 testCreateUint8TypedData(); |
| 309 testCreateClampedUint8TypedData(); | 345 testCreateClampedUint8TypedData(); |
| 346 testCreateExternalClampedUint8TypedData(); |
| 310 testTypedDataRange(false); | 347 testTypedDataRange(false); |
| 311 testUnsignedTypedDataRange(false); | 348 testUnsignedTypedDataRange(false); |
| 312 testClampedUnsignedTypedDataRange(false); | 349 testClampedUnsignedTypedDataRange(false); |
| 350 testExternalClampedUnsignedTypedDataRange(false); |
| 313 testSetRange(); | 351 testSetRange(); |
| 314 testIndexOutOfRange(); | 352 testIndexOutOfRange(); |
| 315 testIndexOf(); | 353 testIndexOf(); |
| 316 | 354 |
| 317 var int8list = new Int8List(128); | 355 var int8list = new Int8List(128); |
| 318 testSetAtIndex(int8list, 42); | 356 testSetAtIndex(int8list, 42); |
| 319 testGetAtIndex(int8list, 42); | 357 testGetAtIndex(int8list, 42); |
| 320 | 358 |
| 321 var uint8list = new Uint8List(128); | 359 var uint8list = new Uint8List(128); |
| 322 testSetAtIndex(uint8list, 42); | 360 testSetAtIndex(uint8list, 42); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 349 var float32list = new Float32List(32); | 387 var float32list = new Float32List(32); |
| 350 testSetAtIndex(float32list, 1.511366173271439e-13, true); | 388 testSetAtIndex(float32list, 1.511366173271439e-13, true); |
| 351 testGetAtIndex(float32list, 1.511366173271439e-13); | 389 testGetAtIndex(float32list, 1.511366173271439e-13); |
| 352 | 390 |
| 353 var float64list = new Float64List(16); | 391 var float64list = new Float64List(16); |
| 354 testSetAtIndex(float64list, 1.4260258159703532e-105, true); | 392 testSetAtIndex(float64list, 1.4260258159703532e-105, true); |
| 355 testGetAtIndex(float64list, 1.4260258159703532e-105); | 393 testGetAtIndex(float64list, 1.4260258159703532e-105); |
| 356 } | 394 } |
| 357 testTypedDataRange(true); | 395 testTypedDataRange(true); |
| 358 testUnsignedTypedDataRange(true); | 396 testUnsignedTypedDataRange(true); |
| 397 testExternalClampedUnsignedTypedDataRange(true); |
| 359 testViewCreation(); | 398 testViewCreation(); |
| 360 testWhere(); | 399 testWhere(); |
| 361 testCreationFromList(); | 400 testCreationFromList(); |
| 362 } | 401 } |
| 363 | 402 |
| OLD | NEW |