Path: blob/master/test/jdk/java/lang/String/CompactString/CompareTo.java
41152 views
/*1* Copyright (c) 2015, 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 org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import static org.testng.Assert.assertEquals;2728/*29* @test30* @bug 8077559 813732631* @summary Tests Compact String. Verifies the compareTo method for String,32* StringBuilder and StringBuffer.33* @run testng/othervm -XX:+CompactStrings CompareTo34* @run testng/othervm -XX:-CompactStrings CompareTo35*/3637public class CompareTo extends CompactString {3839@DataProvider40public Object[][] provider() {41return new Object[][] {4243new Object[] { STRING_EMPTY, "A", -1 },44new Object[] { STRING_EMPTY, "\uFF21", -1 },45new Object[] { STRING_L1, "AB", -1 },46new Object[] { STRING_L1, "A", 0 },47new Object[] { STRING_L1, "a", -32 },48new Object[] { STRING_L1, "\uFF21", -65248 },49new Object[] { STRING_L2, "AB", 0 },50new Object[] { STRING_L2, "Ab", -32 },51new Object[] { STRING_L2, "AA", 1 },52new Object[] { STRING_L2, "\uFF21", -65248 },53new Object[] { STRING_L2, "A\uFF21", -65247 },54new Object[] { STRING_L4, "ABC", 1 },55new Object[] { STRING_L4, "AB", 2 },56new Object[] { STRING_L4, "ABcD", -32 },57new Object[] { STRING_L4, "ABCD\uFF21\uFF21", -2 },58new Object[] { STRING_L4, "ABCD\uFF21", -1 },59new Object[] { STRING_LLONG, "ABCDEFG\uFF21", -65241 },60new Object[] { STRING_LLONG, "AB", 6 },61new Object[] { STRING_LLONG, "ABCD", 4 },62new Object[] { STRING_LLONG, "ABCDEFGH\uFF21\uFF21", -2 },63new Object[] { STRING_U1, "\uFF21", 0 },64new Object[] { STRING_U1, "\uFF22", -1 },65new Object[] { STRING_U1, "\uFF21\uFF22", -1 },66new Object[] { STRING_U1, "A", 65248 },67new Object[] { STRING_U2, "\uFF21\uFF22", 0 },68new Object[] { STRING_U2, "\uFF22", -1 },69new Object[] { STRING_U2, "\uFF21\uFF21", 1 },70new Object[] { STRING_U2, "A", 65248 },71new Object[] { STRING_M12, "\uFF21A", 0 },72new Object[] { STRING_M12, "A\uFF21", 65248 },73new Object[] { STRING_M12, "\uFF21\uFF21", -65248 },74new Object[] { STRING_M11, "A\uFF21", 0 },75new Object[] { STRING_M11, "\uFF21A", -65248 },76new Object[] { STRING_M11, "AA", 65248 }, };77}7879@Test(dataProvider = "provider")80public void testCompareTo(String str, String anotherString, int expected) {81map.get(str)82.forEach(83(source, data) -> {84assertEquals(85data.compareTo(anotherString),86expected,87String.format(88"testing String(%s).compareTo(%s), source : %s, ",89escapeNonASCIIs(data),90escapeNonASCIIs(anotherString),91source));92});93}9495/*96* Runs the same test with StringBuilder97*/98@Test(dataProvider = "provider")99public void testStringBuilder(String str, String anotherString, int expected) {100StringBuilder another = new StringBuilder(anotherString);101map.get(str)102.forEach(103(source, data) -> {104StringBuilder sb = new StringBuilder(data);105assertEquals(106sb.compareTo(another),107expected,108String.format(109"testing StringBuilder(%s).compareTo(%s), source : %s, ",110escapeNonASCIIs(data),111escapeNonASCIIs(anotherString),112source));113});114}115116/*117* Runs the same test with StringBuffer118*/119@Test(dataProvider = "provider")120public void testStringBuffer(String str, String anotherString, int expected) {121StringBuffer another = new StringBuffer(anotherString);122map.get(str)123.forEach(124(source, data) -> {125StringBuffer sb = new StringBuffer(data);126assertEquals(127sb.compareTo(another),128expected,129String.format(130"testing StringBuffer(%s).compareTo(%s), source : %s, ",131escapeNonASCIIs(data),132escapeNonASCIIs(anotherString),133source));134});135}136}137138139