OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 import "dart:typed_data"; |
| 7 |
| 8 main() { |
| 9 iter(count, [values]) => values is List |
| 10 ? new Iterable.generate(count, (x) => values[x]) |
| 11 : new Iterable.generate(count, (x) => values); |
| 12 test(expect, iter, [start = 0, end]) { |
| 13 Expect.equals(expect, new String.fromCharCodes(iter, start, end)); |
| 14 } |
| 15 testThrows(iterable, [start = 0, end]) { |
| 16 Expect.throws(() { new String.fromCharCodes(iterable, start, end); }); |
| 17 } |
| 18 |
| 19 test("", iter(0)); |
| 20 test("", []); |
| 21 test("", const []); |
| 22 test("", new List(0)); |
| 23 test("", new Uint8List(0)); |
| 24 test("", new Uint16List(0)); |
| 25 test("", new Uint32List(0)); |
| 26 test("", "".codeUnits); |
| 27 |
| 28 test("\x00", iter(1, 0)); |
| 29 test("\x00", [0]); |
| 30 test("\x00", const [0]); |
| 31 test("\x00", new List(1)..[0]=0); |
| 32 test("\x00", new Uint8List(1)); |
| 33 test("\x00", new Uint16List(1)); |
| 34 test("\x00", new Uint32List(1)); |
| 35 test("\x00", "\x00".codeUnits); |
| 36 |
| 37 test("\xff", iter(1, 255)); |
| 38 test("\xFF", [255]); |
| 39 test("\xFF", const [255]); |
| 40 test("\xFF", new List(1)..[0]=255); |
| 41 test("\xFF", new Uint8List(1)..[0] = 255); |
| 42 test("\xFF", new Uint16List(1)..[0] = 255); |
| 43 test("\xFF", new Uint32List(1)..[0] = 255); |
| 44 test("\xFF", "\xFF".codeUnits); |
| 45 |
| 46 test("\u0100", iter(1, 256)); |
| 47 test("\u0100", [256]); |
| 48 test("\u0100", const [256]); |
| 49 test("\u0100", new List(1)..[0]=256); |
| 50 test("\u0100", new Uint16List(1)..[0] = 256); |
| 51 test("\u0100", new Uint32List(1)..[0] = 256); |
| 52 test("\u0100", "\u0100".codeUnits); |
| 53 |
| 54 test("\uffff", iter(1, 65535)); |
| 55 test("\uffff", [65535]); |
| 56 test("\uffff", const [65535]); |
| 57 test("\uffff", new List(1)..[0]=65535); |
| 58 test("\uffff", new Uint16List(1)..[0] = 65535); |
| 59 test("\uffff", new Uint32List(1)..[0] = 65535); |
| 60 test("\uffff", "\uffff".codeUnits); |
| 61 |
| 62 test("\u{10000}", iter(1, 65536)); |
| 63 test("\u{10000}", [65536]); |
| 64 test("\u{10000}", const [65536]); |
| 65 test("\u{10000}", new List(1)..[0]=65536); |
| 66 test("\u{10000}", new Uint32List(1)..[0]=65536); |
| 67 test("\u{10000}", "\u{10000}".codeUnits); |
| 68 |
| 69 test("\u{10FFFF}", iter(1, 0x10FFFF)); |
| 70 test("\u{10FFFF}", [0x10FFFF]); |
| 71 test("\u{10FFFF}", const [0x10FFFF]); |
| 72 test("\u{10FFFF}", new List(1)..[0]=0x10FFFF); |
| 73 test("\u{10FFFF}", new Uint32List(1)..[0] = 0x10FFFF); |
| 74 |
| 75 test("\u{10ffff}", iter(2, [0xDBFF, 0xDFFF])); |
| 76 test("\u{10ffff}", [0xDBFF, 0xDFFF]); |
| 77 test("\u{10ffff}", const [0xDBFF, 0xDFFF]); |
| 78 test("\u{10ffff}", new List(2)..[0] = 0xDBFF..[1] = 0xDFFF); |
| 79 test("\u{10ffff}", new Uint16List(2)..[0] = 0xDBFF..[1] = 0xDFFF); |
| 80 test("\u{10ffff}", new Uint32List(2)..[0] = 0xDBFF..[1] = 0xDFFF); |
| 81 test("\u{10FFFF}", "\u{10FFFF}".codeUnits); |
| 82 |
| 83 var leadSurrogate = "\u{10ffff}"[0]; |
| 84 test(leadSurrogate, iter(1, 0xDBFF)); |
| 85 test(leadSurrogate, [0xDBFF]); |
| 86 test(leadSurrogate, const [0xDBFF]); |
| 87 test(leadSurrogate, new List(1)..[0]=0xDBFF); |
| 88 test(leadSurrogate, new Uint16List(1)..[0] = 0xDBFF); |
| 89 test(leadSurrogate, new Uint32List(1)..[0] = 0xDBFF); |
| 90 test(leadSurrogate, leadSurrogate.codeUnits); |
| 91 |
| 92 var tailSurrogate = "\u{10ffff}"[1]; |
| 93 test(tailSurrogate, iter(1, 0xDFFF)); |
| 94 test(tailSurrogate, [0xDFFF]); |
| 95 test(tailSurrogate, const [0xDFFF]); |
| 96 test(tailSurrogate, new List(1)..[0]=0xDFFF); |
| 97 test(tailSurrogate, new Uint16List(1)..[0] = 0xDFFF); |
| 98 test(tailSurrogate, new Uint32List(1)..[0] = 0xDFFF); |
| 99 test(tailSurrogate, tailSurrogate.codeUnits); |
| 100 |
| 101 testThrows(null); |
| 102 testThrows("not an iterable"); |
| 103 testThrows(42); |
| 104 testThrows([-1]); |
| 105 testThrows(new List(1)..[0] = -1); |
| 106 testThrows(const [-1]); |
| 107 //testThrows(new Int8List(1)..[0] = -1); |
| 108 testThrows(new Int16List(1)..[0] = -1); |
| 109 testThrows(new Int32List(1)..[0] = -1); |
| 110 testThrows([0x110000]); |
| 111 testThrows(new List(1)..[0] = 0x110000); |
| 112 testThrows(const [0x110000]); |
| 113 testThrows(new Int32List(1)..[0] = 0x110000); |
| 114 |
| 115 // Check start/end |
| 116 var list = const[0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]; |
| 117 for (var iterable in [ |
| 118 iter(list.length, list), |
| 119 list.toList(growable: true), |
| 120 list.toList(growable: false), |
| 121 list, |
| 122 new Uint8List(8)..setRange(0, 8, list), |
| 123 new Uint16List(8)..setRange(0, 8, list), |
| 124 new Uint32List(8)..setRange(0, 8, list), |
| 125 "ABCDEFGH".codeUnits, |
| 126 ]) { |
| 127 test("ABCDEFGH", iterable); |
| 128 // start varies, end is null. |
| 129 test("ABCDEFGH", iterable, 0); |
| 130 test("BCDEFGH", iterable, 1); |
| 131 test("H", iterable, 7); |
| 132 test("", iterable, 8); |
| 133 test("", iterable, 9); |
| 134 // start = 0, end varies. |
| 135 test("ABCDEFGH", iterable, 0); |
| 136 test("A", iterable, 0, 1); |
| 137 test("AB", iterable, 0, 2); |
| 138 test("ABCDEFG", iterable, 0, 7); |
| 139 test("ABCDEFGH", iterable, 0, 8); |
| 140 test("ABCDEFGH", iterable, 0, 9); |
| 141 test("", iterable, 0, 0); |
| 142 test("", iterable, 0, -1); |
| 143 // Both varying. |
| 144 test("ABCDEFGH", iterable, 0, 8); |
| 145 test("ABCDEFGH", iterable, 0, 9); |
| 146 test("AB", iterable, 0, 2); |
| 147 test("GH", iterable, 6, 8); |
| 148 test("DE", iterable, 3, 5); |
| 149 test("", iterable, 3, 3); |
| 150 test("", iterable, 5, 3); |
| 151 test("", iterable, 4, -1); |
| 152 test("", iterable, 8, -1); |
| 153 test("", iterable, 0, -1); |
| 154 test("", iterable, 9, 9); |
| 155 } |
| 156 // Can split surrogates in input, but not a single big code point. |
| 157 test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1); |
| 158 test(tailSurrogate, [0xDBFF, 0xDFFF], 1); |
| 159 test("\u{10FFFF}", [0x10FFFF], 0, 1); |
| 160 |
| 161 // Test varying slices of the code units of a string. |
| 162 testSubstring(string) { |
| 163 var codes = string.codeUnits; |
| 164 int length = string.length; |
| 165 for (var iterable in [ |
| 166 iter(length, codes), |
| 167 codes.toList(growable: true), |
| 168 codes.toList(growable: false), |
| 169 new Uint16List(length)..setRange(0, length, codes), |
| 170 new Int32List(length)..setRange(0, length, codes), |
| 171 new Uint32List(length)..setRange(0, length, codes), |
| 172 codes, |
| 173 ]) { |
| 174 var newString = new String.fromCharCodes(iterable); |
| 175 Expect.equals(string, newString); |
| 176 for (int i = 0; i < length; i = i * 2 + 1) { |
| 177 test(string.substring(i), iterable, i); |
| 178 test(string.substring(0, i), iterable, 0, i); |
| 179 for (int j = 0; i + j < length; j = j * 2 + 1) { |
| 180 test(string.substring(i, i + j), iterable, i, i + j); |
| 181 } |
| 182 } |
| 183 Expect.equals(string, new String.fromCharCodes(iterable, 0, length + 1)); |
| 184 } |
| 185 } |
| 186 |
| 187 testSubstring(""); |
| 188 testSubstring("ABCDEFGH"); |
| 189 // length > 128 |
| 190 testSubstring("ABCDEFGH" * 33); |
| 191 testSubstring("\x00" * 357); |
| 192 // length > 128 and non-ASCII. |
| 193 testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37); |
| 194 } |
OLD | NEW |