OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 // |
| 5 // Dart test program for testing native byte arrays. |
| 6 |
| 7 void testCreateByteArray() { |
| 8 ByteArray byteArray; |
| 9 |
| 10 byteArray = new ByteArray(0); |
| 11 Expect.equals(0, byteArray.length); |
| 12 |
| 13 byteArray = new ByteArray(10); |
| 14 Expect.equals(10, byteArray.length); |
| 15 for (int i = 0; i < 10; i++) { |
| 16 Expect.equals(0, byteArray[i]); |
| 17 } |
| 18 } |
| 19 |
| 20 void testSetRange() { |
| 21 ByteArray byteArray = new ByteArray(3); |
| 22 |
| 23 List<int> list = [10, 11, 12]; |
| 24 byteArray.setRange(0, 3, list); |
| 25 for (int i = 0; i < 3; i++) { |
| 26 Expect.equals(10 + i, byteArray[i]); |
| 27 } |
| 28 |
| 29 byteArray[0] = 20; |
| 30 byteArray[1] = 21; |
| 31 byteArray[2] = 22; |
| 32 list.setRange(0, 3, byteArray); |
| 33 for (int i = 0; i < 3; i++) { |
| 34 Expect.equals(20 + i, list[i]); |
| 35 } |
| 36 } |
| 37 |
| 38 void testSetInvalidValue() { |
| 39 ByteArray byteArray = new ByteArray(1); |
| 40 |
| 41 Expect.throws(() { |
| 42 byteArray[0] = 0.0; |
| 43 }); |
| 44 |
| 45 Expect.throws(() { |
| 46 byteArray[0] = -1; |
| 47 }); |
| 48 |
| 49 Expect.throws(() { |
| 50 byteArray[0] = 256; |
| 51 }); |
| 52 } |
| 53 |
| 54 void testSetInvalidRange() { |
| 55 ByteArray byteArray = new ByteArray(3); |
| 56 List<int> list = const [-1, 256, 1024]; |
| 57 |
| 58 Expect.throws(() { |
| 59 byteArray.setRange(0, 3, list); |
| 60 }); |
| 61 |
| 62 Expect.throws(() { |
| 63 byteArray.setRange(1, 2, list); |
| 64 }); |
| 65 |
| 66 Expect.throws(() { |
| 67 byteArray.setRange(2, 1, list); |
| 68 }); |
| 69 } |
| 70 |
| 71 void testIndexOutOfRange() { |
| 72 ByteArray byteArray = new ByteArray(3); |
| 73 List<int> list = const [0, 1, 2, 3]; |
| 74 |
| 75 Expect.throws(() { |
| 76 byteArray.setRange(0, 4, list); |
| 77 }); |
| 78 |
| 79 Expect.throws(() { |
| 80 byteArray.setRange(3, 1, list); |
| 81 }); |
| 82 } |
| 83 |
| 84 main() { |
| 85 testCreateByteArray(); |
| 86 testSetRange(); |
| 87 testSetInvalidValue(); |
| 88 testSetInvalidRange(); |
| 89 testIndexOutOfRange(); |
| 90 } |
OLD | NEW |