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