Path: blob/master/test/jdk/java/lang/CharSequence/Comparison.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.CharBuffer;24import java.util.Iterator;25import java.util.Set;26import java.util.TreeSet;27import org.testng.Assert;28import org.testng.annotations.Test;2930/**31* @test32* @bug 813732633* @summary Test to verify the compare method for the CharSequence class.34* @run testng Comparison35*/36public class Comparison {37static char SEP = ':';3839static String[][] books = {40{"Biography", "Steve Jobs"},41{"Biography", "Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future"},42{"Law", "Law 101: Everything You Need to Know About American Law, Fourth Edition"},43{"Law", "The Tools of Argument: How the Best Lawyers Think, Argue, and Win"},44{"History", "The History Book (Big Ideas Simply Explained)"},45{"History", "A People's History of the United States"},46};4748/**49* Verifies the compare method by comparing StringBuilder objects with String50* objects.51*/52@Test53public void compareWithString() {54Set<StringBuilder> sbSet = constructSBSet();55Set<String> sSet = constructStringSet();56Iterator<StringBuilder> iSB = sbSet.iterator();57Iterator<String> iS = sSet.iterator();58while (iSB.hasNext()) {59int result = CharSequence.compare(iSB.next(), iS.next());6061Assert.assertTrue(result == 0, "Comparing item by item");62}63}6465/**66* Verify comparison between two CharSequence implementations, including String,67* StringBuffer and StringBuilder.68*69* Note: CharBuffer states that "A char buffer is not comparable to any other type of object."70*/71@Test72public void testCompare() {73StringBuilder sb1 = generateTestBuilder(65, 70, 97, 102);74StringBuilder sb2 = generateTestBuilder(65, 70, 97, 102);75StringBuilder sb3 = generateTestBuilder(65, 71, 97, 103);7677Assert.assertTrue(CharSequence.compare(sb1, sb2) == 0, "Compare between StringBuilders");78Assert.assertFalse(CharSequence.compare(sb1, sb3) == 0, "Compare between StringBuilders");7980Assert.assertTrue(CharSequence.compare(sb1, sb2.toString()) == 0, "Compare between a StringBuilder and String");81Assert.assertFalse(CharSequence.compare(sb1, sb3.toString()) == 0, "Compare between a StringBuilder and String");8283StringBuffer buf1 = generateTestBuffer(65, 70, 97, 102);84StringBuffer buf2 = generateTestBuffer(65, 70, 97, 102);85StringBuffer buf3 = generateTestBuffer(65, 71, 97, 103);8687Assert.assertTrue(CharSequence.compare(buf1, buf2) == 0, "Compare between StringBuffers");88Assert.assertFalse(CharSequence.compare(buf1, buf3) == 0, "Compare between StringBuffers");8990Assert.assertTrue(CharSequence.compare(sb1, buf2) == 0, "Compare between a StringBuilder and StringBuffer");91Assert.assertFalse(CharSequence.compare(sb1, buf3) == 0, "Compare between a StringBuilder and StringBuffer");9293CharSequence cs1 = (CharSequence)buf1;94CharSequence cs2 = (CharSequence)sb1;95@SuppressWarnings("unchecked")96int result = ((Comparable<Object>)cs1).compareTo(buf2);97Assert.assertTrue(result == 0, "Compare between a StringBuilder and StringBuffer");98}99100101private Set<String> constructStringSet() {102Set<String> sSet = new TreeSet<>();103for (String[] book : books) {104sSet.add(book[0] + SEP + book[1]);105}106return sSet;107}108109private Set<StringBuilder> constructSBSet() {110Set<StringBuilder> sbSet = new TreeSet<>();111for (String[] book : books) {112sbSet.add(new StringBuilder(book[0]).append(SEP).append(book[1]));113}114return sbSet;115}116117private static StringBuilder generateTestBuilder(int from1, int to1,118int from2, int to2) {119StringBuilder aBuffer = new StringBuilder(50);120121for (int i = from1; i < to1; i++) {122aBuffer.append((char)i);123}124for (int i = from2; i < to2; i++) {125aBuffer.append((char)i);126}127return aBuffer;128}129130private static StringBuffer generateTestBuffer(int from1, int to1,131int from2, int to2) {132StringBuffer aBuffer = new StringBuffer(50);133134for (int i = from1; i < to1; i++) {135aBuffer.append((char)i);136}137for (int i = from2; i < to2; i++) {138aBuffer.append((char)i);139}140return aBuffer;141}142}143144145