OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "package:collection/collection.dart"; | 5 import "package:collection/collection.dart"; |
6 import "package:test/test.dart"; | 6 import "package:test/test.dart"; |
7 | 7 |
8 void main() { | 8 void main() { |
9 List<String> strings = [ | 9 List<String> strings = [ |
10 "", | 10 "", |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 "a0a", | 50 "a0a", |
51 "a1a", | 51 "a1a", |
52 "aa", | 52 "aa", |
53 "aab", | 53 "aab", |
54 "ab", | 54 "ab", |
55 "z", | 55 "z", |
56 "{", | 56 "{", |
57 "~" | 57 "~" |
58 ]; | 58 ]; |
59 | 59 |
60 sortedBy(compare) => strings.toList()..shuffle()..sort(compare); | 60 sortedBy(compare) => strings.toList() |
| 61 ..shuffle() |
| 62 ..sort(compare); |
61 | 63 |
62 test("String.compareTo", () { | 64 test("String.compareTo", () { |
63 expect(sortedBy(null), strings); | 65 expect(sortedBy(null), strings); |
64 }); | 66 }); |
65 test("compareAsciiLowerCase", () { | 67 test("compareAsciiLowerCase", () { |
66 expect(sortedBy(compareAsciiLowerCase), | 68 expect(sortedBy(compareAsciiLowerCase), sortedBy((a, b) { |
67 sortedBy((a, b) { | 69 int delta = a.toLowerCase().compareTo(b.toLowerCase()); |
68 int delta = a.toLowerCase().compareTo(b.toLowerCase()); | 70 if (delta != 0) return delta; |
69 if (delta != 0) return delta; | 71 if (a == b) return 0; |
70 if (a == b) return 0; | 72 return a.compareTo(b); |
71 return a.compareTo(b); | 73 })); |
72 })); | |
73 }); | 74 }); |
74 test("compareAsciiUpperCase", () { | 75 test("compareAsciiUpperCase", () { |
75 expect(sortedBy(compareAsciiUpperCase), | 76 expect(sortedBy(compareAsciiUpperCase), sortedBy((a, b) { |
76 sortedBy((a, b) { | 77 int delta = a.toUpperCase().compareTo(b.toUpperCase()); |
77 int delta = a.toUpperCase().compareTo(b.toUpperCase()); | 78 if (delta != 0) return delta; |
78 if (delta != 0) return delta; | 79 if (a == b) return 0; |
79 if (a == b) return 0; | 80 return a.compareTo(b); |
80 return a.compareTo(b); | 81 })); |
81 })); | |
82 }); | 82 }); |
83 | 83 |
84 // Replace any digit sequence by ("0", value, length) as char codes. | 84 // Replace any digit sequence by ("0", value, length) as char codes. |
85 // This will sort alphabetically (by charcode) the way digits sort | 85 // This will sort alphabetically (by charcode) the way digits sort |
86 // numerically, and the leading 0 means it sorts like a digit | 86 // numerically, and the leading 0 means it sorts like a digit |
87 // compared to non-digits. | 87 // compared to non-digits. |
88 replaceNumbers(string) => string.replaceAllMapped(new RegExp(r"\d+"), (m) { | 88 replaceNumbers(String string) => |
89 var digits = m[0]; | 89 string.replaceAllMapped(new RegExp(r"\d+"), (m) { |
90 return new String.fromCharCodes([0x30, int.parse(digits), digits.length]); | 90 var digits = m[0]; |
91 }); | 91 return new String.fromCharCodes( |
| 92 [0x30, int.parse(digits), digits.length]); |
| 93 }); |
92 | 94 |
93 test("compareNatural", () { | 95 test("compareNatural", () { |
94 expect(sortedBy(compareNatural), | 96 expect(sortedBy(compareNatural), |
95 sortedBy((a, b) => replaceNumbers(a).compareTo(replaceNumbers(b)))); | 97 sortedBy((a, b) => replaceNumbers(a).compareTo(replaceNumbers(b)))); |
96 }); | 98 }); |
97 | 99 |
98 test("compareAsciiLowerCaseNatural", () { | 100 test("compareAsciiLowerCaseNatural", () { |
99 expect(sortedBy(compareAsciiLowerCaseNatural), | 101 expect(sortedBy(compareAsciiLowerCaseNatural), sortedBy((a, b) { |
100 sortedBy((a, b) { | 102 int delta = replaceNumbers(a.toLowerCase()) |
101 int delta = replaceNumbers(a.toLowerCase()).compareTo( | 103 .compareTo(replaceNumbers(b.toLowerCase())); |
102 replaceNumbers(b.toLowerCase())); | 104 if (delta != 0) return delta; |
103 if (delta != 0) return delta; | 105 if (a == b) return 0; |
104 if (a == b) return 0; | 106 return a.compareTo(b); |
105 return a.compareTo(b); | 107 })); |
106 })); | |
107 }); | 108 }); |
108 | 109 |
109 test("compareAsciiUpperCaseNatural", () { | 110 test("compareAsciiUpperCaseNatural", () { |
110 expect(sortedBy(compareAsciiUpperCaseNatural), | 111 expect(sortedBy(compareAsciiUpperCaseNatural), sortedBy((a, b) { |
111 sortedBy((a, b) { | 112 int delta = replaceNumbers(a.toUpperCase()) |
112 int delta = replaceNumbers(a.toUpperCase()).compareTo( | 113 .compareTo(replaceNumbers(b.toUpperCase())); |
113 replaceNumbers(b.toUpperCase())); | 114 if (delta != 0) return delta; |
114 if (delta != 0) return delta; | 115 if (a == b) return 0; |
115 if (a == b) return 0; | 116 return a.compareTo(b); |
116 return a.compareTo(b); | 117 })); |
117 })); | |
118 }); | 118 }); |
119 } | 119 } |
OLD | NEW |