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