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