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
32  * @summary Tests Compact String. This one is for String.concat.
33  * @run testng/othervm -XX:+CompactStrings Concat
34  * @run testng/othervm -XX:-CompactStrings Concat
35  */
36 
37 public class Concat extends CompactString {
38 
39     @DataProvider
provider()40     public Object[][] provider() {
41         return new Object[][] {
42                 new Object[] { STRING_EMPTY, "ABC", "ABC" },
43                 new Object[] { STRING_EMPTY,
44                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
45                         "ABC\uFF21\uFF22\uFF23DEF" },
46                 new Object[] {
47                         STRING_EMPTY,
48                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
49                                 "\uFF24\uFF25\uFF26"),
50                         "\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
51                 new Object[] { STRING_L1,
52                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
53                         "AABC\uFF21\uFF22\uFF23DEF" },
54                 new Object[] {
55                         STRING_L1,
56                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
57                                 "\uFF24\uFF25\uFF26"),
58                         "A\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
59                 new Object[] { STRING_L2,
60                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
61                         "ABABC\uFF21\uFF22\uFF23DEF" },
62                 new Object[] {
63                         STRING_L2,
64                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
65                                 "\uFF24\uFF25\uFF26"),
66                         "AB\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
67                 new Object[] { STRING_L4,
68                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
69                         "ABCDABC\uFF21\uFF22\uFF23DEF" },
70                 new Object[] {
71                         STRING_L4,
72                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
73                                 "\uFF24\uFF25\uFF26"),
74                         "ABCD\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
75                 new Object[] { STRING_LLONG,
76                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
77                         "ABCDEFGHABC\uFF21\uFF22\uFF23DEF" },
78                 new Object[] {
79                         STRING_LLONG,
80                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
81                                 "\uFF24\uFF25\uFF26"),
82                         "ABCDEFGH\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
83                 new Object[] { STRING_U1,
84                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
85                         "\uFF21ABC\uFF21\uFF22\uFF23DEF" },
86                 new Object[] {
87                         STRING_U1,
88                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
89                                 "\uFF24\uFF25\uFF26"),
90                         "\uFF21\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
91                 new Object[] { STRING_U2,
92                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
93                         "\uFF21\uFF22ABC\uFF21\uFF22\uFF23DEF" },
94                 new Object[] {
95                         STRING_U2,
96                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
97                                 "\uFF24\uFF25\uFF26"),
98                         "\uFF21\uFF22\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
99                 new Object[] { STRING_M12,
100                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
101                         "\uFF21AABC\uFF21\uFF22\uFF23DEF" },
102                 new Object[] {
103                         STRING_M12,
104                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
105                                 "\uFF24\uFF25\uFF26"),
106                         "\uFF21A\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" },
107                 new Object[] { STRING_M11,
108                         "ABC".concat("\uFF21\uFF22\uFF23").concat("DEF"),
109                         "A\uFF21ABC\uFF21\uFF22\uFF23DEF" },
110                 new Object[] {
111                         STRING_M11,
112                         "\uFF21\uFF22\uFF23".concat("ABC").concat(
113                                 "\uFF24\uFF25\uFF26"),
114                         "A\uFF21\uFF21\uFF22\uFF23ABC\uFF24\uFF25\uFF26" }, };
115     }
116 
117     @Test(dataProvider = "provider")
testConcat(String str, String anotherString, String expected)118     public void testConcat(String str, String anotherString, String expected) {
119         map.get(str)
120                 .forEach(
121                         (source, data) -> {
122                             assertEquals(
123                                     data.concat(anotherString),
124                                     expected,
125                                     String.format(
126                                             "testing String(%s).concat(%s), source : %s, ",
127                                             escapeNonASCIIs(data),
128                                             escapeNonASCIIs(anotherString),
129                                             source));
130                         });
131     }
132 }
133