Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/CharSequence/Comparison.java
41149 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.nio.CharBuffer;
25
import java.util.Iterator;
26
import java.util.Set;
27
import java.util.TreeSet;
28
import org.testng.Assert;
29
import org.testng.annotations.Test;
30
31
/**
32
* @test
33
* @bug 8137326
34
* @summary Test to verify the compare method for the CharSequence class.
35
* @run testng Comparison
36
*/
37
public class Comparison {
38
static char SEP = ':';
39
40
static String[][] books = {
41
{"Biography", "Steve Jobs"},
42
{"Biography", "Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future"},
43
{"Law", "Law 101: Everything You Need to Know About American Law, Fourth Edition"},
44
{"Law", "The Tools of Argument: How the Best Lawyers Think, Argue, and Win"},
45
{"History", "The History Book (Big Ideas Simply Explained)"},
46
{"History", "A People's History of the United States"},
47
};
48
49
/**
50
* Verifies the compare method by comparing StringBuilder objects with String
51
* objects.
52
*/
53
@Test
54
public void compareWithString() {
55
Set<StringBuilder> sbSet = constructSBSet();
56
Set<String> sSet = constructStringSet();
57
Iterator<StringBuilder> iSB = sbSet.iterator();
58
Iterator<String> iS = sSet.iterator();
59
while (iSB.hasNext()) {
60
int result = CharSequence.compare(iSB.next(), iS.next());
61
62
Assert.assertTrue(result == 0, "Comparing item by item");
63
}
64
}
65
66
/**
67
* Verify comparison between two CharSequence implementations, including String,
68
* StringBuffer and StringBuilder.
69
*
70
* Note: CharBuffer states that "A char buffer is not comparable to any other type of object."
71
*/
72
@Test
73
public void testCompare() {
74
StringBuilder sb1 = generateTestBuilder(65, 70, 97, 102);
75
StringBuilder sb2 = generateTestBuilder(65, 70, 97, 102);
76
StringBuilder sb3 = generateTestBuilder(65, 71, 97, 103);
77
78
Assert.assertTrue(CharSequence.compare(sb1, sb2) == 0, "Compare between StringBuilders");
79
Assert.assertFalse(CharSequence.compare(sb1, sb3) == 0, "Compare between StringBuilders");
80
81
Assert.assertTrue(CharSequence.compare(sb1, sb2.toString()) == 0, "Compare between a StringBuilder and String");
82
Assert.assertFalse(CharSequence.compare(sb1, sb3.toString()) == 0, "Compare between a StringBuilder and String");
83
84
StringBuffer buf1 = generateTestBuffer(65, 70, 97, 102);
85
StringBuffer buf2 = generateTestBuffer(65, 70, 97, 102);
86
StringBuffer buf3 = generateTestBuffer(65, 71, 97, 103);
87
88
Assert.assertTrue(CharSequence.compare(buf1, buf2) == 0, "Compare between StringBuffers");
89
Assert.assertFalse(CharSequence.compare(buf1, buf3) == 0, "Compare between StringBuffers");
90
91
Assert.assertTrue(CharSequence.compare(sb1, buf2) == 0, "Compare between a StringBuilder and StringBuffer");
92
Assert.assertFalse(CharSequence.compare(sb1, buf3) == 0, "Compare between a StringBuilder and StringBuffer");
93
94
CharSequence cs1 = (CharSequence)buf1;
95
CharSequence cs2 = (CharSequence)sb1;
96
@SuppressWarnings("unchecked")
97
int result = ((Comparable<Object>)cs1).compareTo(buf2);
98
Assert.assertTrue(result == 0, "Compare between a StringBuilder and StringBuffer");
99
}
100
101
102
private Set<String> constructStringSet() {
103
Set<String> sSet = new TreeSet<>();
104
for (String[] book : books) {
105
sSet.add(book[0] + SEP + book[1]);
106
}
107
return sSet;
108
}
109
110
private Set<StringBuilder> constructSBSet() {
111
Set<StringBuilder> sbSet = new TreeSet<>();
112
for (String[] book : books) {
113
sbSet.add(new StringBuilder(book[0]).append(SEP).append(book[1]));
114
}
115
return sbSet;
116
}
117
118
private static StringBuilder generateTestBuilder(int from1, int to1,
119
int from2, int to2) {
120
StringBuilder aBuffer = new StringBuilder(50);
121
122
for (int i = from1; i < to1; i++) {
123
aBuffer.append((char)i);
124
}
125
for (int i = from2; i < to2; i++) {
126
aBuffer.append((char)i);
127
}
128
return aBuffer;
129
}
130
131
private static StringBuffer generateTestBuffer(int from1, int to1,
132
int from2, int to2) {
133
StringBuffer aBuffer = new StringBuffer(50);
134
135
for (int i = from1; i < to1; i++) {
136
aBuffer.append((char)i);
137
}
138
for (int i = from2; i < to2; i++) {
139
aBuffer.append((char)i);
140
}
141
return aBuffer;
142
}
143
}
144
145