OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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:collection/collection.dart"; |
| 6 import "package:test/test.dart"; |
| 7 |
| 8 void main() { |
| 9 List<String> strings = [ |
| 10 "", |
| 11 "\x00", |
| 12 " ", |
| 13 "+", |
| 14 "/", |
| 15 "0", |
| 16 "00", |
| 17 "000", |
| 18 "001", |
| 19 "01", |
| 20 "011", |
| 21 "1", |
| 22 "100", |
| 23 "11", |
| 24 "110", |
| 25 "9", |
| 26 ":", |
| 27 "=", |
| 28 "@", |
| 29 "A", |
| 30 "A0", |
| 31 "A000A", |
| 32 "A001A", |
| 33 "A00A", |
| 34 "A01A", |
| 35 "A0A", |
| 36 "A1A", |
| 37 "AA", |
| 38 "AAB", |
| 39 "AB", |
| 40 "Z", |
| 41 "[", |
| 42 "_", |
| 43 "`", |
| 44 "a", |
| 45 "a0", |
| 46 "a000a", |
| 47 "a001a", |
| 48 "a00a", |
| 49 "a01a", |
| 50 "a0a", |
| 51 "a1a", |
| 52 "aa", |
| 53 "aab", |
| 54 "ab", |
| 55 "z", |
| 56 "{", |
| 57 "~" |
| 58 ]; |
| 59 |
| 60 sortedBy(compare) => strings.toList()..shuffle()..sort(compare); |
| 61 |
| 62 test("String.compareTo", () { |
| 63 expect(sortedBy(null), strings); |
| 64 }); |
| 65 test("compareAsciiLowerCase", () { |
| 66 expect(sortedBy(compareAsciiLowerCase), |
| 67 sortedBy((a, b) { |
| 68 int delta = a.toLowerCase().compareTo(b.toLowerCase()); |
| 69 if (delta != 0) return delta; |
| 70 if (a == b) return 0; |
| 71 return a.compareTo(b); |
| 72 })); |
| 73 }); |
| 74 test("compareAsciiUpperCase", () { |
| 75 expect(sortedBy(compareAsciiUpperCase), |
| 76 sortedBy((a, b) { |
| 77 int delta = a.toUpperCase().compareTo(b.toUpperCase()); |
| 78 if (delta != 0) return delta; |
| 79 if (a == b) return 0; |
| 80 return a.compareTo(b); |
| 81 })); |
| 82 }); |
| 83 |
| 84 // Replace any digit sequence by ("0", value, length) as char codes. |
| 85 // This will sort alphabetically (by charcode) the way digits sort |
| 86 // numerically, and the leading 0 means it sorts like a digit |
| 87 // compared to non-digits. |
| 88 replaceNumbers(string) => string.replaceAllMapped(new RegExp(r"\d+"), (m) { |
| 89 var digits = m[0]; |
| 90 return new String.fromCharCodes([0x30, int.parse(digits), digits.length]); |
| 91 }); |
| 92 |
| 93 test("compareNatural", () { |
| 94 expect(sortedBy(compareNatural), |
| 95 sortedBy((a, b) => replaceNumbers(a).compareTo(replaceNumbers(b)))); |
| 96 }); |
| 97 |
| 98 test("compareAsciiLowerCaseNatural", () { |
| 99 expect(sortedBy(compareAsciiLowerCaseNatural), |
| 100 sortedBy((a, b) { |
| 101 int delta = replaceNumbers(a.toLowerCase()).compareTo( |
| 102 replaceNumbers(b.toLowerCase())); |
| 103 if (delta != 0) return delta; |
| 104 if (a == b) return 0; |
| 105 return a.compareTo(b); |
| 106 })); |
| 107 }); |
| 108 |
| 109 test("compareAsciiUpperCaseNatural", () { |
| 110 expect(sortedBy(compareAsciiUpperCaseNatural), |
| 111 sortedBy((a, b) { |
| 112 int delta = replaceNumbers(a.toUpperCase()).compareTo( |
| 113 replaceNumbers(b.toUpperCase())); |
| 114 if (delta != 0) return delta; |
| 115 if (a == b) return 0; |
| 116 return a.compareTo(b); |
| 117 })); |
| 118 }); |
| 119 } |
OLD | NEW |