| 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 'dart:typeddata'; | 10 import 'dart:typeddata'; |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 Expect.equals(9, list.indexOf(19.0)); | 210 Expect.equals(9, list.indexOf(19.0)); |
| 211 Expect.equals(-1, list.indexOf(20.0)); | 211 Expect.equals(-1, list.indexOf(20.0)); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void testIndexOf() { | 214 void testIndexOf() { |
| 215 testIndexOfHelper(new Uint8List.transferable(10)); | 215 testIndexOfHelper(new Uint8List.transferable(10)); |
| 216 testIndexOfHelper(new Uint8ClampedList(10)); | 216 testIndexOfHelper(new Uint8ClampedList(10)); |
| 217 testIndexOfHelper(new Uint8ClampedList.transferable(10)); | 217 testIndexOfHelper(new Uint8ClampedList.transferable(10)); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void testGetAtIndex() { | 220 void testGetAtIndex(TypedData list, num initial_value) { |
| 221 var list = new Uint8List(8); | |
| 222 for (int i = 0; i < list.length; i++) { | |
| 223 list[i] = 42; | |
| 224 } | |
| 225 var bdata = new ByteData.view(list); | 221 var bdata = new ByteData.view(list); |
| 226 for (int i = 0; i < list.length; i++) { | 222 for (int i = 0; i < bdata.lengthInBytes; i++) { |
| 227 Expect.equals(42, bdata.getUint8(i)); | 223 Expect.equals(42, bdata.getUint8(i)); |
| 228 Expect.equals(42, bdata.getInt8(i)); | 224 Expect.equals(42, bdata.getInt8(i)); |
| 229 } | 225 } |
| 230 for (int i = 0; i < list.length ~/ 2; i++) { | 226 for (int i = 0; i < bdata.lengthInBytes-1; i+=2) { |
| 231 Expect.equals(10794, bdata.getUint16(i)); | 227 Expect.equals(10794, bdata.getUint16(i)); |
| 232 Expect.equals(10794, bdata.getInt16(i)); | 228 Expect.equals(10794, bdata.getInt16(i)); |
| 233 } | 229 } |
| 234 for (int i = 0; i < list.length ~/ 4; i++) { | 230 for (int i = 0; i < bdata.lengthInBytes-3; i+=4) { |
| 235 Expect.equals(707406378, bdata.getUint32(i)); | 231 Expect.equals(707406378, bdata.getUint32(i)); |
| 236 Expect.equals(707406378, bdata.getInt32(i)); | 232 Expect.equals(707406378, bdata.getInt32(i)); |
| 237 Expect.equals(1.511366173271439e-13, bdata.getFloat32(i)); | 233 Expect.equals(1.511366173271439e-13, bdata.getFloat32(i)); |
| 238 } | 234 } |
| 239 for (int i = 0; i < list.length ~/ 8; i++) { | 235 for (int i = 0; i < bdata.lengthInBytes-7; i+=8) { |
| 240 Expect.equals(3038287259199220266, bdata.getUint64(i)); | 236 Expect.equals(3038287259199220266, bdata.getUint64(i)); |
| 241 Expect.equals(3038287259199220266, bdata.getInt64(i)); | 237 Expect.equals(3038287259199220266, bdata.getInt64(i)); |
| 242 Expect.equals(1.4260258159703532e-105, bdata.getFloat64(i)); | 238 Expect.equals(1.4260258159703532e-105, bdata.getFloat64(i)); |
| 243 } | 239 } |
| 244 } | 240 } |
| 245 | 241 |
| 246 void testSetAtIndex() { | 242 void testSetAtIndex(TypedData list, |
| 247 var list = new Uint8List(8); | 243 num initial_value, [bool use_double = false]) { |
| 248 void validate() { | 244 void validate([reinit = true]) { |
| 249 for (int i = 0; i < list.length; i++) { | 245 for (int i = 0; i < list.length; i++) { |
| 250 Expect.equals(42, list[i]); | 246 Expect.equals(initial_value, list[i]); |
| 247 if (reinit) list[i] = use_double? 0.0 : 0; |
| 251 } | 248 } |
| 252 } | 249 } |
| 253 var bdata = new ByteData.view(list); | 250 var bdata = new ByteData.view(list); |
| 254 for (int i = 0; i < list.length; i++) bdata.setUint8(i, 42); | 251 for (int i = 0; i < bdata.lengthInBytes; i++) bdata.setUint8(i, 42); |
| 255 validate(); | 252 validate(); |
| 256 for (int i = 0; i < list.length; i++) bdata.setInt8(i, 42); | 253 for (int i = 0; i < bdata.lengthInBytes; i++) bdata.setInt8(i, 42); |
| 257 validate(); | 254 validate(); |
| 258 for (int i = 0; i < list.length ~/ 2; i++) bdata.setUint16(i, 10794); | 255 for (int i = 0; i < bdata.lengthInBytes-1; i+=2) bdata.setUint16(i, 10794); |
| 259 validate(); | 256 validate(); |
| 260 for (int i = 0; i < list.length ~/ 2; i++) bdata.setInt16(i, 10794); | 257 for (int i = 0; i < bdata.lengthInBytes-1; i+=2) bdata.setInt16(i, 10794); |
| 261 validate(); | 258 validate(); |
| 262 for (int i = 0; i < list.length ~/ 4; i++) bdata.setUint32(i, 707406378); | 259 for (int i = 0; i < bdata.lengthInBytes-3; i+=4) |
| 260 bdata.setUint32(i, 707406378); |
| 263 validate(); | 261 validate(); |
| 264 for (int i = 0; i < list.length ~/ 4; i++) bdata.setInt32(i, 707406378); | 262 for (int i = 0; i < bdata.lengthInBytes-3; i+=4) bdata.setInt32(i, 707406378); |
| 265 validate(); | 263 validate(); |
| 266 for (int i = 0; i < list.length ~/ 4; i++) { | 264 for (int i = 0; i < bdata.lengthInBytes-3; i+=4) { |
| 267 bdata.setFloat32(i, 1.511366173271439e-13); | 265 bdata.setFloat32(i, 1.511366173271439e-13); |
| 268 } | 266 } |
| 269 validate(); | 267 validate(); |
| 270 for (int i = 0; i < list.length ~/ 8; i++) { | 268 for (int i = 0; i < bdata.lengthInBytes-7; i+=8) { |
| 271 bdata.setUint64(i, 3038287259199220266); | 269 bdata.setUint64(i, 3038287259199220266); |
| 272 } | 270 } |
| 273 validate(); | 271 validate(); |
| 274 for (int i = 0; i < list.length ~/ 8; i++) { | 272 for (int i = 0; i < bdata.lengthInBytes-7; i+=8) { |
| 275 bdata.setInt64(i, 3038287259199220266); | 273 bdata.setInt64(i, 3038287259199220266); |
| 276 } | 274 } |
| 277 validate(); | 275 validate(); |
| 278 for (int i = 0; i < list.length ~/ 8; i++) { | 276 for (int i = 0; i < bdata.lengthInBytes-7; i+=8) { |
| 279 bdata.setFloat64(i, 1.4260258159703532e-105); | 277 bdata.setFloat64(i, 1.4260258159703532e-105); |
| 280 } | 278 } |
| 281 validate(); | 279 validate(false); |
| 282 } | 280 } |
| 283 | 281 |
| 284 main() { | 282 main() { |
| 285 for (int i = 0; i < 2000; i++) { | 283 for (int i = 0; i < 2000; i++) { |
| 286 testCreateUint8TypedData(); | 284 testCreateUint8TypedData(); |
| 287 testCreateClampedUint8TypedData(); | 285 testCreateClampedUint8TypedData(); |
| 288 testCreateExternalClampedUint8TypedData(); | 286 testCreateExternalClampedUint8TypedData(); |
| 289 testTypedDataRange(false); | 287 testTypedDataRange(false); |
| 290 testUnsignedTypedDataRange(false); | 288 testUnsignedTypedDataRange(false); |
| 291 testClampedUnsignedTypedDataRange(false); | 289 testClampedUnsignedTypedDataRange(false); |
| 292 testExternalClampedUnsignedTypedDataRange(false); | 290 testExternalClampedUnsignedTypedDataRange(false); |
| 293 testSetRange(); | 291 testSetRange(); |
| 294 testIndexOutOfRange(); | 292 testIndexOutOfRange(); |
| 295 testIndexOf(); | 293 testIndexOf(); |
| 296 testGetAtIndex(); | 294 |
| 297 testSetAtIndex(); | 295 var int8list = new Int8List(128); |
| 296 testSetAtIndex(int8list, 42); |
| 297 testGetAtIndex(int8list, 42); |
| 298 |
| 299 var uint8list = new Uint8List(128); |
| 300 testSetAtIndex(uint8list, 42); |
| 301 testGetAtIndex(uint8list, 42); |
| 302 |
| 303 var int16list = new Int16List(64); |
| 304 testSetAtIndex(int16list, 10794); |
| 305 testGetAtIndex(int16list, 10794); |
| 306 |
| 307 var uint16list = new Uint16List(64); |
| 308 testSetAtIndex(uint16list, 10794); |
| 309 testGetAtIndex(uint16list, 10794); |
| 310 |
| 311 var int32list = new Int32List(32); |
| 312 testSetAtIndex(int32list, 707406378); |
| 313 testGetAtIndex(int32list, 707406378); |
| 314 |
| 315 var uint32list = new Uint32List(32); |
| 316 testSetAtIndex(uint32list, 707406378); |
| 317 testGetAtIndex(uint32list, 707406378); |
| 318 |
| 319 var int64list = new Int64List(16); |
| 320 testSetAtIndex(int64list, 3038287259199220266); |
| 321 testGetAtIndex(int64list, 3038287259199220266); |
| 322 |
| 323 var uint64list = new Uint64List(16); |
| 324 testSetAtIndex(uint64list, 3038287259199220266); |
| 325 testGetAtIndex(uint64list, 3038287259199220266); |
| 326 |
| 327 var float32list = new Float32List(32); |
| 328 testSetAtIndex(float32list, 1.511366173271439e-13, true); |
| 329 testGetAtIndex(float32list, 1.511366173271439e-13); |
| 330 |
| 331 var float64list = new Float64List(16); |
| 332 testSetAtIndex(float64list, 1.4260258159703532e-105, true); |
| 333 testGetAtIndex(float64list, 1.4260258159703532e-105); |
| 298 } | 334 } |
| 299 testTypedDataRange(true); | 335 testTypedDataRange(true); |
| 300 testUnsignedTypedDataRange(true); | 336 testUnsignedTypedDataRange(true); |
| 301 testExternalClampedUnsignedTypedDataRange(true); | 337 testExternalClampedUnsignedTypedDataRange(true); |
| 302 } | 338 } |
| 303 | 339 |
| OLD | NEW |