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 17 matching lines...) Expand all Loading... |
28 | 28 |
29 testInt32x4Buffer(intSamples); | 29 testInt32x4Buffer(intSamples); |
30 | 30 |
31 List roundedFloatSamples = floatSamples.map(roundToFloat).toList(); | 31 List roundedFloatSamples = floatSamples.map(roundToFloat).toList(); |
32 testFloatBuffer(32, roundedFloatSamples, | 32 testFloatBuffer(32, roundedFloatSamples, |
33 () => new Float32Buffer(), | 33 () => new Float32Buffer(), |
34 roundToFloat); | 34 roundToFloat); |
35 testFloatBuffer(64, doubleSamples, () => new Float64Buffer(), (x) => x); | 35 testFloatBuffer(64, doubleSamples, () => new Float64Buffer(), (x) => x); |
36 | 36 |
37 testFloat32x4Buffer(roundedFloatSamples); | 37 testFloat32x4Buffer(roundedFloatSamples); |
| 38 |
| 39 group("addAll", () { |
| 40 for (var type in ['a list', 'an iterable']) { |
| 41 group("with $type", () { |
| 42 var source; |
| 43 var buffer; |
| 44 setUp(() { |
| 45 source = [1, 2, 3, 4, 5]; |
| 46 if (type == 'an iterable') source = source.reversed.toList().reversed; |
| 47 buffer = new Uint8Buffer(); |
| 48 }); |
| 49 |
| 50 test("adds values to the buffer", () { |
| 51 buffer.addAll(source, 1, 4); |
| 52 expect(buffer, equals([2, 3, 4])); |
| 53 |
| 54 buffer.addAll(source, 4); |
| 55 expect(buffer, equals([2, 3, 4, 5])); |
| 56 |
| 57 buffer.addAll(source, 0, 1); |
| 58 expect(buffer, equals([2, 3, 4, 5, 1])); |
| 59 }); |
| 60 |
| 61 test("does nothing for empty slices", () { |
| 62 buffer.addAll([6, 7, 8, 9, 10]); |
| 63 |
| 64 buffer.addAll(source, 0, 0); |
| 65 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 66 |
| 67 buffer.addAll(source, 3, 3); |
| 68 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 69 |
| 70 buffer.addAll(source, 5); |
| 71 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 72 |
| 73 buffer.addAll(source, 5, 5); |
| 74 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 75 }); |
| 76 |
| 77 test("throws errors for invalid start and end", () { |
| 78 expect(() => buffer.addAll(source, -1), throwsRangeError); |
| 79 expect(() => buffer.addAll(source, -1, 2), throwsRangeError); |
| 80 expect(() => buffer.addAll(source, 10), throwsStateError); |
| 81 expect(() => buffer.addAll(source, 10, 11), throwsStateError); |
| 82 expect(() => buffer.addAll(source, 3, 2), throwsRangeError); |
| 83 expect(() => buffer.addAll(source, 3, 10), throwsStateError); |
| 84 expect(() => buffer.addAll(source, 3, -1), throwsRangeError); |
| 85 }); |
| 86 }); |
| 87 } |
| 88 }); |
| 89 |
| 90 group("insertAll", () { |
| 91 for (var type in ['a list', 'an iterable']) { |
| 92 group("with $type", () { |
| 93 var source; |
| 94 var buffer; |
| 95 setUp(() { |
| 96 source = [1, 2, 3, 4, 5]; |
| 97 if (type == 'an iterable') source = source.reversed.toList().reversed; |
| 98 buffer = new Uint8Buffer()..addAll([6, 7, 8, 9, 10]); |
| 99 }); |
| 100 |
| 101 test("inserts values into the buffer", () { |
| 102 buffer.insertAll(0, source, 1, 4); |
| 103 expect(buffer, equals([2, 3, 4, 6, 7, 8, 9, 10])); |
| 104 |
| 105 buffer.insertAll(3, source, 4); |
| 106 expect(buffer, equals([2, 3, 4, 5, 6, 7, 8, 9, 10])); |
| 107 |
| 108 buffer.insertAll(5, source, 0, 1); |
| 109 expect(buffer, equals([2, 3, 4, 5, 6, 1, 7, 8, 9, 10])); |
| 110 }); |
| 111 |
| 112 test("does nothing for empty slices", () { |
| 113 buffer.insertAll(1, source, 0, 0); |
| 114 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 115 |
| 116 buffer.insertAll(2, source, 3, 3); |
| 117 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 118 |
| 119 buffer.insertAll(3, source, 5); |
| 120 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 121 |
| 122 buffer.insertAll(4, source, 5, 5); |
| 123 expect(buffer, equals([6, 7, 8, 9, 10])); |
| 124 }); |
| 125 |
| 126 test("throws errors for invalid start and end", () { |
| 127 expect(() => buffer.insertAll(-1, source), throwsRangeError); |
| 128 expect(() => buffer.insertAll(6, source), throwsRangeError); |
| 129 expect(() => buffer.insertAll(1, source, -1), throwsRangeError); |
| 130 expect(() => buffer.insertAll(2, source, -1, 2), throwsRangeError); |
| 131 expect(() => buffer.insertAll(3, source, 10), throwsStateError); |
| 132 expect(() => buffer.insertAll(4, source, 10, 11), throwsStateError); |
| 133 expect(() => buffer.insertAll(5, source, 3, 2), throwsRangeError); |
| 134 expect(() => buffer.insertAll(1, source, 3, 10), throwsStateError); |
| 135 expect(() => buffer.insertAll(2, source, 3, -1), throwsRangeError); |
| 136 }); |
| 137 }); |
| 138 } |
| 139 }); |
38 } | 140 } |
39 | 141 |
40 double roundToFloat(double value) { | 142 double roundToFloat(double value) { |
41 return (new Float32List(1)..[0] = value)[0]; | 143 return (new Float32List(1)..[0] = value)[0]; |
42 } | 144 } |
43 | 145 |
44 typedef int Rounder(int value); | 146 typedef int Rounder(int value); |
45 | 147 |
46 Rounder roundUint(bits) { | 148 Rounder roundUint(bits) { |
47 int halfbits = (1 << (bits ~/ 2)) - 1; | 149 int halfbits = (1 << (bits ~/ 2)) - 1; |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 bool matches(item, Map matchState) { | 519 bool matches(item, Map matchState) { |
418 if (item is! Int32x4) return false; | 520 if (item is! Int32x4) return false; |
419 Int32x4 value = item; | 521 Int32x4 value = item; |
420 return result.x == value.x && result.y == value.y && | 522 return result.x == value.x && result.y == value.y && |
421 result.z == value.z && result.w == value.w; | 523 result.z == value.z && result.w == value.w; |
422 } | 524 } |
423 | 525 |
424 Description describe(Description description) => | 526 Description describe(Description description) => |
425 description.add('Int32x4.=='); | 527 description.add('Int32x4.=='); |
426 } | 528 } |
OLD | NEW |