Path: blob/master/test/jdk/java/lang/StringBuilder/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.util.Iterator;24import java.util.Set;25import java.util.TreeSet;26import org.testng.Assert;27import org.testng.annotations.Test;2829/**30* @test31* @bug 813732632* @summary Test to verify the Comparable implementation for the StringBuilder class.33* @run testng Comparison34*/35public class Comparison {36static char SEP = ':';3738static String[][] books = {39{"Biography", "Steve Jobs"},40{"Biography", "Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future"},41{"Law", "Law 101: Everything You Need to Know About American Law, Fourth Edition"},42{"Law", "The Tools of Argument: How the Best Lawyers Think, Argue, and Win"},43{"History", "The History Book (Big Ideas Simply Explained)"},44{"History", "A People's History of the United States"},45};4647/**48* Verifies the Comparable implementation by comparing with two TreeSet that49* contain either StringBuilder or String.50*/51@Test52public void compareWithString() {53Set<StringBuilder> sbSet = constructSBSet();54Set<String> sSet = constructStringSet();55Iterator<StringBuilder> iSB = sbSet.iterator();56Iterator<String> iS = sSet.iterator();57while (iSB.hasNext()) {58String temp1 = iSB.next().toString();59System.out.println(temp1);60String temp2 = iS.next();61System.out.println(temp2);6263Assert.assertTrue(temp1.equals(temp2), "Comparing item by item");64}6566}6768/**69* Compares between StringBuilders70*/71@Test72public void testCompare() {73StringBuilder sb1 = generateTestBuffer(65, 70, 97, 102);74StringBuilder sb2 = generateTestBuffer(65, 70, 97, 102);75StringBuilder sb3 = generateTestBuffer(65, 71, 97, 103);7677System.out.println(sb1.toString());78System.out.println(sb2.toString());79System.out.println(sb3.toString());80Assert.assertTrue(sb1.compareTo(sb2) == 0, "Compare sb1 and sb2");81Assert.assertFalse(sb1.compareTo(sb3) == 0, "Compare sb1 and sb3");82}8384/**85* Verifies that the comparison is from index 0 to length() - 1 of the two86* character sequences.87*/88@Test89public void testModifiedSequence() {90StringBuilder sb1 = generateTestBuffer(65, 70, 97, 102);91StringBuilder sb2 = generateTestBuffer(65, 70, 98, 103);9293// contain different character sequences94Assert.assertFalse(sb1.compareTo(sb2) == 0, "Compare the sequences before truncation");9596// the first 5 characters however are the same97sb1.setLength(5);98sb2.setLength(5);99100System.out.println(sb1.toString());101System.out.println(sb2.toString());102103Assert.assertTrue(sb1.compareTo(sb2) == 0, "Compare sb1 and sb2");104Assert.assertTrue(sb1.toString().compareTo(sb2.toString()) == 0, "Compare strings of sb1 and sb2");105}106107private Set<String> constructStringSet() {108Set<String> sSet = new TreeSet<>();109for (String[] book : books) {110sSet.add(book[0] + SEP + book[1]);111}112return sSet;113}114115private Set<StringBuilder> constructSBSet() {116Set<StringBuilder> sbSet = new TreeSet<>();117for (String[] book : books) {118sbSet.add(new StringBuilder(book[0]).append(SEP).append(book[1]));119}120return sbSet;121}122123private static StringBuilder generateTestBuffer(int from1, int to1,124int from2, int to2) {125StringBuilder aBuffer = new StringBuilder(50);126127for (int i = from1; i < to1; i++) {128aBuffer.append((char)i);129}130for (int i = from2; i < to2; i++) {131aBuffer.append((char)i);132}133return aBuffer;134}135}136137138