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.util.Iterator; 25 import java.util.Set; 26 import java.util.TreeSet; 27 import org.testng.Assert; 28 import org.testng.annotations.Test; 29 30 /** 31 * @test 32 * @bug 8137326 33 * @summary Test to verify the Comparable implementation for the StringBuilder class. 34 * @run testng Comparison 35 */ 36 public class Comparison { 37 static char SEP = ':'; 38 39 static 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 }; 47 48 /** 49 * Verifies the Comparable implementation by comparing with two TreeSet that 50 * contain either StringBuilder or String. 51 */ 52 @Test 53 public void compareWithString() { 54 Set<StringBuilder> sbSet = constructSBSet(); 55 Set<String> sSet = constructStringSet(); 56 Iterator<StringBuilder> iSB = sbSet.iterator(); 57 Iterator<String> iS = sSet.iterator(); 58 while (iSB.hasNext()) { 59 String temp1 = iSB.next().toString(); 60 System.out.println(temp1); 61 String temp2 = iS.next(); 62 System.out.println(temp2); 63 64 Assert.assertTrue(temp1.equals(temp2), "Comparing item by item"); 65 } 66 67 } 68 69 /** 70 * Compares between StringBuilders 71 */ 72 @Test 73 public void testCompare() { 74 StringBuilder sb1 = generateTestBuffer(65, 70, 97, 102); 75 StringBuilder sb2 = generateTestBuffer(65, 70, 97, 102); 76 StringBuilder sb3 = generateTestBuffer(65, 71, 97, 103); 77 78 System.out.println(sb1.toString()); 79 System.out.println(sb2.toString()); 80 System.out.println(sb3.toString()); 81 Assert.assertTrue(sb1.compareTo(sb2) == 0, "Compare sb1 and sb2"); 82 Assert.assertFalse(sb1.compareTo(sb3) == 0, "Compare sb1 and sb3"); 83 } 84 85 /** 86 * Verifies that the comparison is from index 0 to length() - 1 of the two 87 * character sequences. 88 */ 89 @Test 90 public void testModifiedSequence() { 91 StringBuilder sb1 = generateTestBuffer(65, 70, 97, 102); 92 StringBuilder sb2 = generateTestBuffer(65, 70, 98, 103); 93 94 // contain different character sequences 95 Assert.assertFalse(sb1.compareTo(sb2) == 0, "Compare the sequences before truncation"); 96 97 // the first 5 characters however are the same 98 sb1.setLength(5); 99 sb2.setLength(5); 100 101 System.out.println(sb1.toString()); 102 System.out.println(sb2.toString()); 103 104 Assert.assertTrue(sb1.compareTo(sb2) == 0, "Compare sb1 and sb2"); 105 Assert.assertTrue(sb1.toString().compareTo(sb2.toString()) == 0, "Compare strings of sb1 and sb2"); 106 } 107 108 private Set<String> constructStringSet() { 109 Set<String> sSet = new TreeSet<>(); 110 for (String[] book : books) { 111 sSet.add(book[0] + SEP + book[1]); 112 } 113 return sSet; 114 } 115 116 private Set<StringBuilder> constructSBSet() { 117 Set<StringBuilder> sbSet = new TreeSet<>(); 118 for (String[] book : books) { 119 sbSet.add(new StringBuilder(book[0]).append(SEP).append(book[1])); 120 } 121 return sbSet; 122 } 123 124 private static StringBuilder generateTestBuffer(int from1, int to1, 125 int from2, int to2) { 126 StringBuilder aBuffer = new StringBuilder(50); 127 128 for (int i = from1; i < to1; i++) { 129 aBuffer.append((char)i); 130 } 131 for (int i = from2; i < to2; i++) { 132 aBuffer.append((char)i); 133 } 134 return aBuffer; 135 } 136 }