Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: test/comparators_test.dart

Issue 1412293008: Add string comparators that can compare ASCII strings ignoring case. (Closed) Base URL: https://github.com/dart-lang/collection.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/src/comparators.dart ('K') | « lib/src/comparators.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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",
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", length, value) as char codes.
81 // This will sort alphabetically (by charcode) the way digits sort
82 // lexicographically, 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, digits.length, int.parse(digits)]);
87 });
88
89 test("compareWithNumbers", () {
90 expect(sortedBy(compareWithNumbers),
91 sortedBy((a, b) => replaceNumbers(a).compareTo(replaceNumbers(b))));
92 });
93
94 test("compareAsciiLowerCaseWithNumbers", () {
95 expect(sortedBy(compareAsciiLowerCaseWithNumbers),
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("compareAsciiUpperCaseWithNumbers", () {
106 expect(sortedBy(compareAsciiUpperCaseWithNumbers),
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 }
OLDNEW
« lib/src/comparators.dart ('K') | « lib/src/comparators.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698