| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 native byte arrays. | 5 // Dart test program for testing native byte arrays. |
| 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("ByteArrayTest.dart"); | 8 #library("ByteArrayTest.dart"); |
| 9 | 9 |
| 10 #import('dart:scalarlist'); | 10 #import('dart:scalarlist'); |
| 11 | 11 |
| 12 void testCreateByteArray() { | 12 void testCreateUint8ByteArray() { |
| 13 Uint8List byteArray; | 13 Uint8List byteArray; |
| 14 | 14 |
| 15 byteArray = new Uint8List(0); | 15 byteArray = new Uint8List(0); |
| 16 Expect.isTrue(byteArray is Uint8List); |
| 17 Expect.isFalse(byteArray is Uint8ClampedList); |
| 16 Expect.equals(0, byteArray.length); | 18 Expect.equals(0, byteArray.length); |
| 17 | 19 |
| 18 byteArray = new Uint8List(10); | 20 byteArray = new Uint8List(10); |
| 19 Expect.equals(10, byteArray.length); | 21 Expect.equals(10, byteArray.length); |
| 20 for (int i = 0; i < 10; i++) { | 22 for (int i = 0; i < 10; i++) { |
| 21 Expect.equals(0, byteArray[i]); | 23 Expect.equals(0, byteArray[i]); |
| 22 } | 24 } |
| 25 } |
| 23 | 26 |
| 27 void testCreateClampedUint8ByteArray() { |
| 28 Uint8ClampedList clampedByteArray; |
| 29 |
| 30 clampedByteArray = new Uint8ClampedList(0); |
| 31 Expect.isTrue(clampedByteArray is Uint8ClampedList); |
| 32 Expect.isFalse(clampedByteArray is Uint8List); |
| 33 Expect.equals(0, clampedByteArray.length); |
| 34 |
| 35 clampedByteArray = new Uint8ClampedList(10); |
| 36 Expect.equals(10, clampedByteArray.length); |
| 37 for (int i = 0; i < 10; i++) { |
| 38 Expect.equals(0, clampedByteArray[i]); |
| 39 } |
| 24 } | 40 } |
| 25 | 41 |
| 26 void testUnsignedByteArrayRange(bool check_throws) { | 42 void testUnsignedByteArrayRange(bool check_throws) { |
| 27 Uint8List byteArray; | 43 Uint8List byteArray; |
| 28 byteArray = new Uint8List(10); | 44 byteArray = new Uint8List(10); |
| 29 | 45 |
| 30 byteArray[1] = 255; | 46 byteArray[1] = 255; |
| 31 Expect.equals(255, byteArray[1]); | 47 Expect.equals(255, byteArray[1]); |
| 32 byteArray[1] = 0; | 48 byteArray[1] = 0; |
| 33 Expect.equals(0, byteArray[1]); | 49 Expect.equals(0, byteArray[1]); |
| 34 | 50 |
| 35 // These should eventually throw. | 51 // These should eventually throw. |
| 36 byteArray[1] = 256; | 52 byteArray[1] = 256; |
| 37 byteArray[1] = -1; | 53 byteArray[1] = -1; |
| 38 byteArray[2] = -129; | 54 byteArray[2] = -129; |
| 39 if (check_throws) { | 55 if (check_throws) { |
| 40 Expect.throws(() { | 56 Expect.throws(() { |
| 41 byteArray[1] = 1.2; | 57 byteArray[1] = 1.2; |
| 42 }); | 58 }); |
| 43 } | 59 } |
| 44 } | 60 } |
| 45 | 61 |
| 62 void testClampedUnsignedByteArrayRange(bool check_throws) { |
| 63 Uint8ClampedList byteArray; |
| 64 byteArray = new Uint8ClampedList(10); |
| 65 |
| 66 byteArray[1] = 255; |
| 67 Expect.equals(255, byteArray[1]); |
| 68 byteArray[1] = 0; |
| 69 Expect.equals(0, byteArray[1]); |
| 70 |
| 71 // These should eventually throw. |
| 72 byteArray[1] = 256; |
| 73 byteArray[2] = -129; |
| 74 Expect.equals(255, byteArray[1]); |
| 75 Expect.equals(0, byteArray[2]); |
| 76 } |
| 77 |
| 46 void testByteArrayRange(bool check_throws) { | 78 void testByteArrayRange(bool check_throws) { |
| 47 Int8List byteArray; | 79 Int8List byteArray; |
| 48 byteArray = new Int8List(10); | 80 byteArray = new Int8List(10); |
| 49 byteArray[1] = 0; | 81 byteArray[1] = 0; |
| 50 Expect.equals(0, byteArray[1]); | 82 Expect.equals(0, byteArray[1]); |
| 51 byteArray[2] = -128; | 83 byteArray[2] = -128; |
| 52 Expect.equals(-128, byteArray[2]); | 84 Expect.equals(-128, byteArray[2]); |
| 53 byteArray[3] = 127; | 85 byteArray[3] = 127; |
| 54 Expect.equals(127, byteArray[3]); | 86 Expect.equals(127, byteArray[3]); |
| 55 // This should eventually throw. | 87 // This should eventually throw. |
| 56 byteArray[0] = 128; | 88 byteArray[0] = 128; |
| 57 byteArray[4] = -129; | 89 byteArray[4] = -129; |
| 58 if (check_throws) { | 90 if (check_throws) { |
| 59 Expect.throws(() { | 91 Expect.throws(() { |
| 60 byteArray[1] = 1.2; | 92 byteArray[1] = 1.2; |
| 61 }); | 93 }); |
| 62 } | 94 } |
| 63 } | 95 } |
| 64 | 96 |
| 65 void testSetRange() { | |
| 66 Uint8List byteArray = new Uint8List(3); | |
| 67 | 97 |
| 98 void testSetRangeHelper(byteArray) { |
| 68 List<int> list = [10, 11, 12]; | 99 List<int> list = [10, 11, 12]; |
| 69 byteArray.setRange(0, 3, list); | 100 byteArray.setRange(0, 3, list); |
| 70 for (int i = 0; i < 3; i++) { | 101 for (int i = 0; i < 3; i++) { |
| 71 Expect.equals(10 + i, byteArray[i]); | 102 Expect.equals(10 + i, byteArray[i]); |
| 72 } | 103 } |
| 73 | 104 |
| 74 byteArray[0] = 20; | 105 byteArray[0] = 20; |
| 75 byteArray[1] = 21; | 106 byteArray[1] = 21; |
| 76 byteArray[2] = 22; | 107 byteArray[2] = 22; |
| 77 list.setRange(0, 3, byteArray); | 108 list.setRange(0, 3, byteArray); |
| 78 for (int i = 0; i < 3; i++) { | 109 for (int i = 0; i < 3; i++) { |
| 79 Expect.equals(20 + i, list[i]); | 110 Expect.equals(20 + i, list[i]); |
| 80 } | 111 } |
| 81 | 112 |
| 82 byteArray.setRange(1, 2, const [8, 9]); | 113 byteArray.setRange(1, 2, const [8, 9]); |
| 83 Expect.equals(20, byteArray[0]); | 114 Expect.equals(20, byteArray[0]); |
| 84 Expect.equals(8, byteArray[1]); | 115 Expect.equals(8, byteArray[1]); |
| 85 Expect.equals(9, byteArray[2]); | 116 Expect.equals(9, byteArray[2]); |
| 86 } | 117 } |
| 87 | 118 |
| 88 void testIndexOutOfRange() { | 119 |
| 89 Uint8List byteArray = new Uint8List(3); | 120 void testSetRange() { |
| 121 testSetRangeHelper(new Uint8List(3)); |
| 122 testSetRangeHelper(new Uint8ClampedList(3)); |
| 123 } |
| 124 |
| 125 void testIndexOutOfRangeHelper(byteArray) { |
| 90 List<int> list = const [0, 1, 2, 3]; | 126 List<int> list = const [0, 1, 2, 3]; |
| 91 | 127 |
| 92 Expect.throws(() { | 128 Expect.throws(() { |
| 93 byteArray.setRange(0, 4, list); | 129 byteArray.setRange(0, 4, list); |
| 94 }); | 130 }); |
| 95 | 131 |
| 96 Expect.throws(() { | 132 Expect.throws(() { |
| 97 byteArray.setRange(3, 1, list); | 133 byteArray.setRange(3, 1, list); |
| 98 }); | 134 }); |
| 99 } | 135 } |
| 100 | 136 |
| 101 void testIndexOf() { | 137 void testIndexOutOfRange() { |
| 102 var list = new Uint8List(10); | 138 testIndexOutOfRangeHelper(new Uint8List(3)); |
| 139 testIndexOutOfRangeHelper(new Uint8ClampedList(3)); |
| 140 } |
| 141 |
| 142 void testIndexOfHelper(list) { |
| 103 for (int i = 0; i < list.length; i++) { | 143 for (int i = 0; i < list.length; i++) { |
| 104 list[i] = i + 10; | 144 list[i] = i + 10; |
| 105 } | 145 } |
| 106 Expect.equals(0, list.indexOf(10)); | 146 Expect.equals(0, list.indexOf(10)); |
| 107 Expect.equals(5, list.indexOf(15)); | 147 Expect.equals(5, list.indexOf(15)); |
| 108 Expect.equals(9, list.indexOf(19)); | 148 Expect.equals(9, list.indexOf(19)); |
| 109 Expect.equals(-1, list.indexOf(20)); | 149 Expect.equals(-1, list.indexOf(20)); |
| 110 | 150 |
| 111 list = new Float32List(10); | 151 list = new Float32List(10); |
| 112 for (int i = 0; i < list.length; i++) { | 152 for (int i = 0; i < list.length; i++) { |
| 113 list[i] = i + 10.0; | 153 list[i] = i + 10.0; |
| 114 } | 154 } |
| 115 Expect.equals(0, list.indexOf(10.0)); | 155 Expect.equals(0, list.indexOf(10.0)); |
| 116 Expect.equals(5, list.indexOf(15.0)); | 156 Expect.equals(5, list.indexOf(15.0)); |
| 117 Expect.equals(9, list.indexOf(19.0)); | 157 Expect.equals(9, list.indexOf(19.0)); |
| 118 Expect.equals(-1, list.indexOf(20.0)); | 158 Expect.equals(-1, list.indexOf(20.0)); |
| 119 } | 159 } |
| 120 | 160 |
| 121 void testSubArray() { | 161 void testIndexOf() { |
| 122 var list = new Uint8List(10); | 162 testIndexOfHelper(new Uint8List(10)); |
| 163 testIndexOfHelper(new Uint8ClampedList(10)); |
| 164 } |
| 165 |
| 166 void testSubArrayHelper(list) { |
| 123 var array = list.asByteArray(); | 167 var array = list.asByteArray(); |
| 124 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); | 168 Expect.equals(0, array.subByteArray(0, 0).lengthInBytes()); |
| 125 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); | 169 Expect.equals(0, array.subByteArray(5, 0).lengthInBytes()); |
| 126 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); | 170 Expect.equals(0, array.subByteArray(10, 0).lengthInBytes()); |
| 127 Expect.equals(0, array.subByteArray(10).lengthInBytes()); | 171 Expect.equals(0, array.subByteArray(10).lengthInBytes()); |
| 128 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); | 172 Expect.equals(0, array.subByteArray(10, null).lengthInBytes()); |
| 129 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); | 173 Expect.equals(5, array.subByteArray(0, 5).lengthInBytes()); |
| 130 Expect.equals(5, array.subByteArray(5, 5).lengthInBytes()); | 174 Expect.equals(5, array.subByteArray(5, 5).lengthInBytes()); |
| 131 Expect.equals(5, array.subByteArray(5).lengthInBytes()); | 175 Expect.equals(5, array.subByteArray(5).lengthInBytes()); |
| 132 Expect.equals(5, array.subByteArray(5, null).lengthInBytes()); | 176 Expect.equals(5, array.subByteArray(5, null).lengthInBytes()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 152 assert(checkedMode = true); | 196 assert(checkedMode = true); |
| 153 if (!checkedMode) { | 197 if (!checkedMode) { |
| 154 // In checked mode these will necessarily throw a TypeError. | 198 // In checked mode these will necessarily throw a TypeError. |
| 155 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); | 199 Expect.throws(() => array.subByteArray(0, "5"), (e) => e is ArgumentError); |
| 156 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); | 200 Expect.throws(() => array.subByteArray("0", 5), (e) => e is ArgumentError); |
| 157 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); | 201 Expect.throws(() => array.subByteArray("0"), (e) => e is ArgumentError); |
| 158 } | 202 } |
| 159 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); | 203 Expect.throws(() => array.subByteArray(null), (e) => e is ArgumentError); |
| 160 } | 204 } |
| 161 | 205 |
| 206 |
| 207 void testSubArray() { |
| 208 testSubArrayHelper(new Uint8List(10)); |
| 209 testSubArrayHelper(new Uint8ClampedList(10)); |
| 210 } |
| 211 |
| 162 main() { | 212 main() { |
| 163 for (int i = 0; i < 2000; i++) { | 213 for (int i = 0; i < 2000; i++) { |
| 164 testCreateByteArray(); | 214 testCreateUint8ByteArray(); |
| 215 testCreateClampedUint8ByteArray(); |
| 165 testByteArrayRange(false); | 216 testByteArrayRange(false); |
| 166 testUnsignedByteArrayRange(false); | 217 testUnsignedByteArrayRange(false); |
| 218 testClampedUnsignedByteArrayRange(false); |
| 167 testSetRange(); | 219 testSetRange(); |
| 168 testIndexOutOfRange(); | 220 testIndexOutOfRange(); |
| 169 testIndexOf(); | 221 testIndexOf(); |
| 170 testSubArray(); | 222 testSubArray(); |
| 171 } | 223 } |
| 172 testByteArrayRange(true); | 224 testByteArrayRange(true); |
| 173 testUnsignedByteArrayRange(true); | 225 testUnsignedByteArrayRange(true); |
| 174 } | 226 } |
| 175 | 227 |
| OLD | NEW |