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 // Tests typed-data buffer classes. | 5 // Tests typed-data buffer classes. |
6 | 6 |
7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
8 | 8 |
9 import "package:test/test.dart"; | 9 import "package:test/test.dart"; |
10 import "package:typed_data/typed_buffers.dart"; | 10 import "package:typed_data/typed_buffers.dart"; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 145 } |
146 }); | 146 }); |
147 } | 147 } |
148 | 148 |
149 double roundToFloat(double value) { | 149 double roundToFloat(double value) { |
150 return (new Float32List(1)..[0] = value)[0]; | 150 return (new Float32List(1)..[0] = value)[0]; |
151 } | 151 } |
152 | 152 |
153 typedef int Rounder(int value); | 153 typedef int Rounder(int value); |
154 | 154 |
155 Rounder roundUint(bits) { | 155 Rounder uintRounder(bits) { |
156 int halfbits = (1 << (bits ~/ 2)) - 1; | 156 int halfbits = (1 << (bits ~/ 2)) - 1; |
157 int mask = halfbits | (halfbits << (bits ~/ 2)); | 157 int mask = halfbits | (halfbits << (bits ~/ 2)); |
158 return (int x) => x & mask; | 158 return (int x) => x & mask; |
159 } | 159 } |
160 | 160 |
161 Rounder roundInt(bits) { | 161 Rounder intRounder(bits) { |
162 int highBit = 1 << (bits - 1); | 162 int highBit = 1 << (bits - 1); |
163 int mask = highBit - 1; | 163 int mask = highBit - 1; |
164 return (int x) => (x & mask) - (x & highBit); | 164 return (int x) => (x & mask) - (x & highBit); |
165 } | 165 } |
166 | 166 |
167 int clampUint8(x) => x < 0 ? 0 : x > 255 ? 255 : x; | 167 int clampUint8(x) => x < 0 ? 0 : x > 255 ? 255 : x; |
168 | 168 |
169 void testUint(int bits, var buffer, {String testOn}) { | 169 void testUint(int bits, buffer(int length), {String testOn}) { |
170 int min = 0; | 170 int min = 0; |
171 Function round = roundUint(bits); | 171 var rounder = uintRounder(bits); |
172 int max = round(-1); | 172 int max = rounder(-1); |
173 test("Uint${bits}Buffer", () { | 173 test("Uint${bits}Buffer", () { |
174 testIntBuffer(bits, min, max, buffer, round); | 174 testIntBuffer(bits, min, max, buffer, rounder); |
175 }, testOn: testOn); | 175 }, testOn: testOn); |
176 } | 176 } |
177 | 177 |
178 void testInt(int bits, var buffer, {String testOn}) { | 178 void testInt(int bits, buffer(int length), {String testOn}) { |
179 int min = -(1 << (bits - 1)); | 179 int min = -(1 << (bits - 1)); |
180 int max = -(min + 1); | 180 int max = -(min + 1); |
181 test("Int${bits}Buffer", () { | 181 test("Int${bits}Buffer", () { |
182 testIntBuffer(bits, min, max, buffer, roundInt(bits)); | 182 testIntBuffer(bits, min, max, buffer, intRounder(bits)); |
183 }, testOn: testOn); | 183 }, testOn: testOn); |
184 } | 184 } |
185 | 185 |
186 const List<int> intSamples = const [ | 186 const List<int> intSamples = const [ |
187 0x10000000000000001, | 187 0x10000000000000001, |
188 0x10000000000000000, // 2^64 | 188 0x10000000000000000, // 2^64 |
189 0x0ffffffffffffffff, | 189 0x0ffffffffffffffff, |
190 0xaaaaaaaaaaaaaaaa, | 190 0xaaaaaaaaaaaaaaaa, |
191 0x8000000000000001, | 191 0x8000000000000001, |
192 0x8000000000000000, // 2^63 | 192 0x8000000000000000, // 2^63 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 // Takes bit-size, min value, max value, function to create a buffer, and | 224 // Takes bit-size, min value, max value, function to create a buffer, and |
225 // the rounding that is applied when storing values outside the valid range | 225 // the rounding that is applied when storing values outside the valid range |
226 // into the buffer. | 226 // into the buffer. |
227 void testIntBuffer(int bits, int min, int max, | 227 void testIntBuffer(int bits, int min, int max, |
228 create(int length), | 228 create(int length), |
229 int round(int)) { | 229 int round(int)) { |
230 assert(round(min) == min); | 230 assert(round(min) == min); |
231 assert(round(max) == max); | 231 assert(round(max) == max); |
232 // All int buffers default to the value 0. | 232 // All int buffers default to the value 0. |
233 var buffer = create(0); | 233 var buffer = create(0); |
234 List<int> list = buffer; // Check the type. | 234 expect(buffer, new isInstanceOf<List<int>>()); |
235 expect(buffer.length, equals(0)); | 235 expect(buffer.length, equals(0)); |
236 var bytes = bits ~/ 8; | 236 var bytes = bits ~/ 8; |
237 | 237 |
238 expect(buffer.elementSizeInBytes, equals(bytes)); | 238 expect(buffer.elementSizeInBytes, equals(bytes)); |
239 expect(buffer.lengthInBytes, equals(0)); | 239 expect(buffer.lengthInBytes, equals(0)); |
240 expect(buffer.offsetInBytes, equals(0)); | 240 expect(buffer.offsetInBytes, equals(0)); |
241 | 241 |
242 buffer.add(min); | 242 buffer.add(min); |
243 expect(buffer.length, equals(1)); | 243 expect(buffer.length, equals(1)); |
244 expect(buffer[0], equals(min)); | 244 expect(buffer[0], equals(min)); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 } else { | 331 } else { |
332 if (x != y) { | 332 if (x != y) { |
333 } | 333 } |
334 expect(x, equals(y)); | 334 expect(x, equals(y)); |
335 } | 335 } |
336 } | 336 } |
337 | 337 |
338 testFloatBuffer(int bitSize, List samples, create(), double round(double v)) { | 338 testFloatBuffer(int bitSize, List samples, create(), double round(double v)) { |
339 test("Float${bitSize}Buffer", () { | 339 test("Float${bitSize}Buffer", () { |
340 var buffer = create(); | 340 var buffer = create(); |
341 List<double> list = buffer; // Test type. | 341 expect(buffer, new isInstanceOf<List<double>>()); |
342 int byteSize = bitSize ~/ 8; | 342 int byteSize = bitSize ~/ 8; |
343 | 343 |
344 expect(buffer.length, equals(0)); | 344 expect(buffer.length, equals(0)); |
345 buffer.add(0.0); | 345 buffer.add(0.0); |
346 expect(buffer.length, equals(1)); | 346 expect(buffer.length, equals(1)); |
347 expect(buffer.removeLast(), equals(0.0)); | 347 expect(buffer.removeLast(), equals(0.0)); |
348 expect(buffer.length, equals(0)); | 348 expect(buffer.length, equals(0)); |
349 | 349 |
350 for (double value in samples) { | 350 for (double value in samples) { |
351 buffer.add(value); | 351 buffer.add(value); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 int tmp = bytes[i]; | 386 int tmp = bytes[i]; |
387 bytes[i] = bytes[byteSize + i]; | 387 bytes[i] = bytes[byteSize + i]; |
388 bytes[byteSize + i] = tmp; | 388 bytes[byteSize + i] = tmp; |
389 } | 389 } |
390 doubleEqual(buffer[0], round(samples[1])); | 390 doubleEqual(buffer[0], round(samples[1])); |
391 doubleEqual(buffer[1], round(samples[0])); | 391 doubleEqual(buffer[1], round(samples[0])); |
392 }); | 392 }); |
393 } | 393 } |
394 | 394 |
395 testFloat32x4Buffer(List floatSamples) { | 395 testFloat32x4Buffer(List floatSamples) { |
396 List float4Samples = []; | 396 var float4Samples = <Float32x4>[]; |
397 for (int i = 0; i < floatSamples.length - 3; i++) { | 397 for (int i = 0; i < floatSamples.length - 3; i++) { |
398 float4Samples.add(new Float32x4(floatSamples[i], | 398 float4Samples.add(new Float32x4(floatSamples[i], |
399 floatSamples[i + 1], | 399 floatSamples[i + 1], |
400 floatSamples[i + 2], | 400 floatSamples[i + 2], |
401 floatSamples[i + 3])); | 401 floatSamples[i + 3])); |
402 } | 402 } |
403 | 403 |
404 void floatEquals(x, y) { | 404 void floatEquals(x, y) { |
405 if (y.isNaN) { | 405 if (y.isNaN) { |
406 expect(x.isNaN, isTrue); | 406 expect(x.isNaN, isTrue); |
407 } else { | 407 } else { |
408 expect(x, equals(y)); | 408 expect(x, equals(y)); |
409 } | 409 } |
410 } | 410 } |
411 | 411 |
412 void x4Equals(Float32x4 x, Float32x4 y) { | 412 void x4Equals(Float32x4 x, Float32x4 y) { |
413 floatEquals(x.x, y.x); | 413 floatEquals(x.x, y.x); |
414 floatEquals(x.y, y.y); | 414 floatEquals(x.y, y.y); |
415 floatEquals(x.z, y.z); | 415 floatEquals(x.z, y.z); |
416 floatEquals(x.w, y.w); | 416 floatEquals(x.w, y.w); |
417 } | 417 } |
418 | 418 |
419 test("Float32x4Buffer", () { | 419 test("Float32x4Buffer", () { |
420 var buffer = new Float32x4Buffer(5); | 420 var buffer = new Float32x4Buffer(5); |
421 List<Float32x4> list = buffer; | 421 expect(buffer, new isInstanceOf<List<Float32x4>>()); |
422 | 422 |
423 expect(buffer.length, equals(5)); | 423 expect(buffer.length, equals(5)); |
424 expect(buffer.elementSizeInBytes, equals(128 ~/ 8)); | 424 expect(buffer.elementSizeInBytes, equals(128 ~/ 8)); |
425 expect(buffer.lengthInBytes, equals(5 * 128 ~/ 8)); | 425 expect(buffer.lengthInBytes, equals(5 * 128 ~/ 8)); |
426 expect(buffer.offsetInBytes, equals(0)); | 426 expect(buffer.offsetInBytes, equals(0)); |
427 | 427 |
428 x4Equals(buffer[0], new Float32x4.zero()); | 428 x4Equals(buffer[0], new Float32x4.zero()); |
429 buffer.length = 0; | 429 buffer.length = 0; |
430 expect(buffer.length, equals(0)); | 430 expect(buffer.length, equals(0)); |
431 | 431 |
(...skipping 19 matching lines...) Expand all Loading... |
451 buffer[0] = float4Samples[0]; // Does not contain NaN. | 451 buffer[0] = float4Samples[0]; // Does not contain NaN. |
452 | 452 |
453 Float32List floats = new Float32List.view(buffer.buffer); | 453 Float32List floats = new Float32List.view(buffer.buffer); |
454 expect(floats[0], equals(buffer[0].x)); | 454 expect(floats[0], equals(buffer[0].x)); |
455 expect(floats[1], equals(buffer[0].y)); | 455 expect(floats[1], equals(buffer[0].y)); |
456 expect(floats[2], equals(buffer[0].z)); | 456 expect(floats[2], equals(buffer[0].z)); |
457 expect(floats[3], equals(buffer[0].w)); | 457 expect(floats[3], equals(buffer[0].w)); |
458 }); | 458 }); |
459 } | 459 } |
460 | 460 |
461 void testInt32x4Buffer(intSamples) { | 461 void testInt32x4Buffer(List<int> intSamples) { |
462 test("Int32x4Buffer", () { | 462 test("Int32x4Buffer", () { |
463 Function round = roundInt(32); | 463 Function rounder = intRounder(32); |
464 int bits = 128; | |
465 int bytes = 128 ~/ 8; | 464 int bytes = 128 ~/ 8; |
466 Matcher equals32x4(Int32x4 expected) => new MatchesInt32x4(expected); | 465 Matcher equals32x4(Int32x4 expected) => new MatchesInt32x4(expected); |
467 | 466 |
468 var buffer = new Int32x4Buffer(0); | 467 var buffer = new Int32x4Buffer(0); |
469 List<Int32x4> list = buffer; // It's a List. | 468 expect(buffer, new isInstanceOf<List<Int32x4>>()); |
470 expect(buffer.length, equals(0)); | 469 expect(buffer.length, equals(0)); |
471 | 470 |
472 expect(buffer.elementSizeInBytes, equals(bytes)); | 471 expect(buffer.elementSizeInBytes, equals(bytes)); |
473 expect(buffer.lengthInBytes, equals(0)); | 472 expect(buffer.lengthInBytes, equals(0)); |
474 expect(buffer.offsetInBytes, equals(0)); | 473 expect(buffer.offsetInBytes, equals(0)); |
475 | 474 |
476 Int32x4 sample = new Int32x4(-0x80000000, -1, 0, 0x7fffffff); | 475 Int32x4 sample = new Int32x4(-0x80000000, -1, 0, 0x7fffffff); |
477 buffer.add(sample); | 476 buffer.add(sample); |
478 expect(buffer.length, equals(1)); | 477 expect(buffer.length, equals(1)); |
479 expect(buffer[0], equals32x4(sample)); | 478 expect(buffer[0], equals32x4(sample)); |
480 | 479 |
481 expect(buffer.elementSizeInBytes, equals(bytes)); | 480 expect(buffer.elementSizeInBytes, equals(bytes)); |
482 expect(buffer.lengthInBytes, equals(bytes)); | 481 expect(buffer.lengthInBytes, equals(bytes)); |
483 expect(buffer.offsetInBytes, equals(0)); | 482 expect(buffer.offsetInBytes, equals(0)); |
484 | 483 |
485 buffer.length = 0; | 484 buffer.length = 0; |
486 expect(buffer.length, equals(0)); | 485 expect(buffer.length, equals(0)); |
487 | 486 |
488 var samples = intSamples | 487 var samples = intSamples |
489 .where((value) => value == round(value)) // Issue 15130 | 488 .where((value) => value == rounder(value)) // Issue 15130 |
490 .map((value) => new Int32x4(value, -value, ~value, ~-value)) | 489 .map((value) => new Int32x4(value, -value, ~value, ~-value)) |
491 .toList(); | 490 .toList(); |
492 for (Int32x4 value in samples) { | 491 for (Int32x4 value in samples) { |
493 int length = buffer.length; | 492 int length = buffer.length; |
494 buffer.add(value); | 493 buffer.add(value); |
495 expect(buffer.length, equals(length + 1)); | 494 expect(buffer.length, equals(length + 1)); |
496 expect(buffer[length], equals32x4(value)); | 495 expect(buffer[length], equals32x4(value)); |
497 } | 496 } |
498 | 497 |
499 buffer.addAll(samples); // Add all the values at once. | 498 buffer.addAll(samples); // Add all the values at once. |
(...skipping 26 matching lines...) Expand all Loading... |
526 bool matches(item, Map matchState) { | 525 bool matches(item, Map matchState) { |
527 if (item is! Int32x4) return false; | 526 if (item is! Int32x4) return false; |
528 Int32x4 value = item; | 527 Int32x4 value = item; |
529 return result.x == value.x && result.y == value.y && | 528 return result.x == value.x && result.y == value.y && |
530 result.z == value.z && result.w == value.w; | 529 result.z == value.z && result.w == value.w; |
531 } | 530 } |
532 | 531 |
533 Description describe(Description description) => | 532 Description describe(Description description) => |
534 description.add('Int32x4.=='); | 533 description.add('Int32x4.=='); |
535 } | 534 } |
OLD | NEW |