| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import junit.framework.Assert; |
| Vladimir Marko | 85bef97 | 2017-02-17 10:18:57 +0000 | [diff] [blame] | 18 | import java.util.Locale; |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * more string tests |
| 22 | */ |
| 23 | public class Main { |
| Vladimir Marko | cb64b85 | 2018-11-28 10:54:48 +0000 | [diff] [blame] | 24 | public static void main(String args[]) { |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 25 | String test = "0123456789"; |
| 26 | String test1 = new String("0123456789"); // different object |
| 27 | String test2 = new String("0123456780"); // different value |
| 28 | String offset = new String("xxx0123456789yyy"); |
| 29 | String sub = offset.substring(3, 13); |
| 30 | Object blah = new Object(); |
| 31 | |
| 32 | Assert.assertTrue(test.equals(test)); |
| 33 | Assert.assertTrue(test.equals(test1)); |
| 34 | Assert.assertFalse(test.equals(test2)); |
| 35 | |
| 36 | Assert.assertEquals(test.compareTo(test1), 0); |
| 37 | Assert.assertTrue(test1.compareTo(test2) > 0); |
| 38 | Assert.assertTrue(test2.compareTo(test1) < 0); |
| 39 | |
| Alexei Zavjalov | 4554bfd | 2014-02-26 17:28:35 +0700 | [diff] [blame] | 40 | Assert.assertEquals("".compareTo(""), 0); |
| 41 | Assert.assertTrue(test.compareTo("") > 0); |
| 42 | Assert.assertTrue("".compareTo(test) < 0); |
| 43 | |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 44 | /* compare string with a nonzero offset, in left/right side */ |
| 45 | Assert.assertEquals(test.compareTo(sub), 0); |
| 46 | Assert.assertEquals(sub.compareTo(test), 0); |
| 47 | Assert.assertTrue(test.equals(sub)); |
| 48 | Assert.assertTrue(sub.equals(test)); |
| 49 | /* same base, one is a substring */ |
| 50 | Assert.assertFalse(offset.equals(sub)); |
| 51 | Assert.assertFalse(sub.equals(offset)); |
| 52 | /* wrong class */ |
| 53 | Assert.assertFalse(test.equals(blah)); |
| 54 | |
| 55 | /* null ptr - throw */ |
| 56 | try { |
| 57 | test.compareTo(null); |
| 58 | Assert.fail("didn't get expected npe"); |
| 59 | } catch (NullPointerException npe) { |
| 60 | System.out.println("Got expected npe"); |
| 61 | } |
| 62 | /* null ptr - ok */ |
| 63 | Assert.assertFalse(test.equals(null)); |
| 64 | |
| 65 | test = test.substring(1); |
| 66 | Assert.assertTrue(test.equals("123456789")); |
| 67 | Assert.assertFalse(test.equals(test1)); |
| 68 | |
| 69 | test = test.substring(1); |
| 70 | Assert.assertTrue(test.equals("23456789")); |
| 71 | |
| 72 | test = test.substring(1); |
| 73 | Assert.assertTrue(test.equals("3456789")); |
| 74 | |
| 75 | test = test.substring(1); |
| 76 | Assert.assertTrue(test.equals("456789")); |
| 77 | |
| 78 | test = test.substring(3,5); |
| 79 | Assert.assertTrue(test.equals("78")); |
| 80 | |
| 81 | test = "this/is/a/path"; |
| 82 | String[] strings = test.split("/"); |
| 83 | Assert.assertEquals(4, strings.length); |
| 84 | |
| 85 | Assert.assertEquals("this is a path", test.replaceAll("/", " ")); |
| 86 | Assert.assertEquals("this is a path", test.replace("/", " ")); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 87 | |
| Vladimir Marko | cb64b85 | 2018-11-28 10:54:48 +0000 | [diff] [blame] | 88 | String result = new String(new char[] { 'O', 'K' }); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 89 | System.out.println(result); |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 90 | |
| 91 | testCompareToAndEquals(); |
| 92 | testIndexOf(); |
| Vladimir Marko | 16850ae | 2016-12-09 14:01:02 +0000 | [diff] [blame] | 93 | |
| 94 | String s0_0 = "\u0000"; |
| 95 | String s0_1 = new String(s0_0); |
| 96 | String s0_2 = new String(new char[] { '\u0000' }); |
| 97 | String s0_3 = s0_0 + ""; |
| 98 | System.out.println( |
| 99 | " " + $noinline$equals(s0_0, s0_0) + |
| 100 | " " + $noinline$equals(s0_0, s0_1) + |
| 101 | " " + $noinline$equals(s0_0, s0_2) + |
| 102 | " " + $noinline$equals(s0_0, s0_3)); |
| 103 | System.out.println( |
| 104 | " " + $noinline$equals(s0_1, s0_0) + |
| 105 | " " + $noinline$equals(s0_1, s0_1) + |
| 106 | " " + $noinline$equals(s0_1, s0_2) + |
| 107 | " " + $noinline$equals(s0_1, s0_3)); |
| 108 | System.out.println( |
| 109 | " " + $noinline$equals(s0_2, s0_0) + |
| 110 | " " + $noinline$equals(s0_2, s0_1) + |
| 111 | " " + $noinline$equals(s0_2, s0_2) + |
| 112 | " " + $noinline$equals(s0_2, s0_3)); |
| 113 | System.out.println( |
| 114 | " " + $noinline$equals(s0_3, s0_0) + |
| 115 | " " + $noinline$equals(s0_3, s0_1) + |
| 116 | " " + $noinline$equals(s0_3, s0_2) + |
| 117 | " " + $noinline$equals(s0_3, s0_3)); |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 118 | |
| 119 | testEqualsConstString(); |
| 120 | testConstStringEquals(); |
| Vladimir Marko | 7629548 | 2020-10-26 12:28:37 +0000 | [diff] [blame] | 121 | testStringConcat(); |
| Vladimir Marko | 85bef97 | 2017-02-17 10:18:57 +0000 | [diff] [blame] | 122 | |
| 123 | // Regression tests for String.setCharAt() breaking string compression invariants. |
| 124 | Locale en_US = new Locale("en", "US"); |
| 125 | Assert.assertEquals("I", /* Small latin dotless i */ "\u0131".toUpperCase()); |
| 126 | Assert.assertEquals("abc", "a\u0131c".replace('\u0131', 'b')); |
| 127 | Assert.assertEquals("a\u0131c", "abc".replace('b', '\u0131')); |
| Vladimir Marko | 26ec3ca | 2017-03-14 13:37:14 +0000 | [diff] [blame] | 128 | |
| 129 | // Regression test for scratch register exhaustion in String.equals() intrinsic on arm64. |
| 130 | Assert.assertFalse(result.equals("Very long constant string, so that the known constant count field cannot be embedded in a CMP immediate instruction on arm64. Since it can hold 12-bit values, optionally shifted left by 12, let's go somewhere over 2^12, i.e. 4096. That should trigger the bug with or without string compression. 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/")); |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 131 | } |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 132 | |
| 133 | public static void testCompareToAndEquals() { |
| 134 | String[] strings = { |
| 135 | // Special: empty string. |
| 136 | "", |
| 137 | // Category 0, ASCII strings: |
| 138 | // "0123456789abcdef".substring(0, index + 1) |
| 139 | "0", |
| 140 | "01", |
| 141 | "012", |
| 142 | "0123", |
| 143 | "01234", |
| 144 | "012345", |
| 145 | "0123456", |
| 146 | "01234567", |
| 147 | "012345678", |
| 148 | "0123456789", |
| 149 | "0123456789a", |
| 150 | "0123456789ab", |
| 151 | "0123456789abc", |
| 152 | "0123456789abcd", |
| 153 | "0123456789abcde", |
| 154 | "0123456789abcdef", |
| 155 | // Category 1, ASCII strings: |
| 156 | // "0123456789abcdef".substring(0, index) + "x" |
| 157 | "x", |
| 158 | "0x", |
| 159 | "01x", |
| 160 | "012x", |
| 161 | "0123x", |
| 162 | "01234x", |
| 163 | "012345x", |
| 164 | "0123456x", |
| 165 | "01234567x", |
| 166 | "012345678x", |
| 167 | "0123456789x", |
| 168 | "0123456789ax", |
| 169 | "0123456789abx", |
| 170 | "0123456789abcx", |
| 171 | "0123456789abcdx", |
| 172 | "0123456789abcdex", |
| 173 | // Category 2, ASCII strings, |
| 174 | // "0123456789abcdef".substring(0, index) + "x" + |
| 175 | // "0123456789abcdef".substring(index + 1) |
| 176 | "x123456789abcdef", |
| 177 | "0x23456789abcdef", |
| 178 | "01x3456789abcdef", |
| 179 | "012x456789abcdef", |
| 180 | "0123x56789abcdef", |
| 181 | "01234x6789abcdef", |
| 182 | "012345x789abcdef", |
| 183 | "0123456x89abcdef", |
| 184 | "01234567x9abcdef", |
| 185 | "012345678xabcdef", |
| 186 | "0123456789xbcdef", |
| 187 | "0123456789axcdef", |
| 188 | "0123456789abxdef", |
| 189 | "0123456789abcxef", |
| 190 | "0123456789abcdxf", |
| 191 | "0123456789abcdex", |
| 192 | // Category 3, ASCII strings: |
| 193 | // "z" + "0123456789abcdef".substring(1, index + 1) |
| 194 | "z", |
| 195 | "z1", |
| 196 | "z12", |
| 197 | "z123", |
| 198 | "z1234", |
| 199 | "z12345", |
| 200 | "z123456", |
| 201 | "z1234567", |
| 202 | "z12345678", |
| 203 | "z123456789", |
| 204 | "z123456789a", |
| 205 | "z123456789ab", |
| 206 | "z123456789abc", |
| 207 | "z123456789abcd", |
| 208 | "z123456789abcde", |
| 209 | "z123456789abcdef", |
| 210 | // Category 4, non-ASCII strings: |
| 211 | // "0123456789abcdef".substring(0, index) + "\u0440" |
| 212 | "\u0440", |
| 213 | "0\u0440", |
| 214 | "01\u0440", |
| 215 | "012\u0440", |
| 216 | "0123\u0440", |
| 217 | "01234\u0440", |
| 218 | "012345\u0440", |
| 219 | "0123456\u0440", |
| 220 | "01234567\u0440", |
| 221 | "012345678\u0440", |
| 222 | "0123456789\u0440", |
| 223 | "0123456789a\u0440", |
| 224 | "0123456789ab\u0440", |
| 225 | "0123456789abc\u0440", |
| 226 | "0123456789abcd\u0440", |
| 227 | "0123456789abcde\u0440", |
| 228 | // Category 5, non-ASCII strings: |
| 229 | // "0123456789abcdef".substring(0, index) + "\u0440" + |
| 230 | // "0123456789abcdef".substring(index + 1) |
| 231 | "\u0440123456789abcdef", |
| 232 | "0\u044023456789abcdef", |
| 233 | "01\u04403456789abcdef", |
| 234 | "012\u0440456789abcdef", |
| 235 | "0123\u044056789abcdef", |
| 236 | "01234\u04406789abcdef", |
| 237 | "012345\u0440789abcdef", |
| 238 | "0123456\u044089abcdef", |
| 239 | "01234567\u04409abcdef", |
| 240 | "012345678\u0440abcdef", |
| 241 | "0123456789\u0440bcdef", |
| 242 | "0123456789a\u0440cdef", |
| 243 | "0123456789ab\u0440def", |
| 244 | "0123456789abc\u0440ef", |
| 245 | "0123456789abcd\u0440f", |
| 246 | "0123456789abcde\u0440", |
| 247 | // Category 6, ASCII strings: |
| 248 | // "\u0443" + "0123456789abcdef".substring(1, index + 1) |
| 249 | "\u0443", |
| 250 | "\u04431", |
| 251 | "\u044312", |
| 252 | "\u0443123", |
| 253 | "\u04431234", |
| 254 | "\u044312345", |
| 255 | "\u0443123456", |
| 256 | "\u04431234567", |
| 257 | "\u044312345678", |
| 258 | "\u0443123456789", |
| 259 | "\u0443123456789a", |
| 260 | "\u0443123456789ab", |
| 261 | "\u0443123456789abc", |
| 262 | "\u0443123456789abcd", |
| 263 | "\u0443123456789abcde", |
| 264 | "\u0443123456789abcdef", |
| 265 | // Category 7, non-ASCII strings: |
| 266 | // "0123456789abcdef".substring(0, index) + "\u0482" |
| 267 | "\u0482", |
| 268 | "0\u0482", |
| 269 | "01\u0482", |
| 270 | "012\u0482", |
| 271 | "0123\u0482", |
| 272 | "01234\u0482", |
| 273 | "012345\u0482", |
| 274 | "0123456\u0482", |
| 275 | "01234567\u0482", |
| 276 | "012345678\u0482", |
| 277 | "0123456789\u0482", |
| 278 | "0123456789a\u0482", |
| 279 | "0123456789ab\u0482", |
| 280 | "0123456789abc\u0482", |
| 281 | "0123456789abcd\u0482", |
| 282 | "0123456789abcde\u0482", |
| 283 | // Category 8, non-ASCII strings: |
| 284 | // "0123456789abcdef".substring(0, index) + "\u0482" + |
| 285 | // "0123456789abcdef".substring(index + 1) |
| 286 | "\u0482123456789abcdef", |
| 287 | "0\u048223456789abcdef", |
| 288 | "01\u04823456789abcdef", |
| 289 | "012\u0482456789abcdef", |
| 290 | "0123\u048256789abcdef", |
| 291 | "01234\u04826789abcdef", |
| 292 | "012345\u0482789abcdef", |
| 293 | "0123456\u048289abcdef", |
| 294 | "01234567\u04829abcdef", |
| 295 | "012345678\u0482abcdef", |
| 296 | "0123456789\u0482bcdef", |
| 297 | "0123456789a\u0482cdef", |
| 298 | "0123456789ab\u0482def", |
| 299 | "0123456789abc\u0482ef", |
| 300 | "0123456789abcd\u0482f", |
| 301 | "0123456789abcde\u0482", |
| 302 | // Category 9, ASCII strings: |
| 303 | // "\u0489" + "0123456789abcdef".substring(1, index + 1) |
| 304 | "\u0489", |
| 305 | "\u04891", |
| 306 | "\u048912", |
| 307 | "\u0489123", |
| 308 | "\u04891234", |
| 309 | "\u048912345", |
| 310 | "\u0489123456", |
| 311 | "\u04891234567", |
| 312 | "\u048912345678", |
| 313 | "\u0489123456789", |
| 314 | "\u0489123456789a", |
| 315 | "\u0489123456789ab", |
| 316 | "\u0489123456789abc", |
| 317 | "\u0489123456789abcd", |
| 318 | "\u0489123456789abcde", |
| 319 | "\u0489123456789abcdef", |
| 320 | }; |
| 321 | int length = strings.length; |
| 322 | Assert.assertEquals(1 + 16 * 10, length); |
| 323 | for (int i = 0; i != length; ++i) { |
| 324 | String lhs = strings[i]; |
| 325 | for (int j = 0; j != length; ++j) { |
| 326 | String rhs = strings[j]; |
| 327 | int result = $noinline$compareTo(lhs, rhs); |
| 328 | final int expected; |
| 329 | if (i == 0 || j == 0 || i == j) { |
| 330 | // One of the strings is empty or the strings are the same. |
| 331 | expected = lhs.length() - rhs.length(); |
| 332 | } else { |
| 333 | int i_category = (i - 1) / 16; |
| 334 | int i_index = (i - 1) % 16; |
| 335 | int j_category = (j - 1) / 16; |
| 336 | int j_index = (j - 1) % 16; |
| 337 | int min_ij_index = (i_index < j_index) ? i_index : j_index; |
| 338 | if (i_category == j_category) { |
| 339 | switch (i_category) { |
| 340 | case 0: case 3: case 6: case 9: |
| 341 | // Differs in length. |
| 342 | expected = lhs.length() - rhs.length(); |
| 343 | break; |
| 344 | case 1: case 2: case 4: case 5: case 7: case 8: |
| 345 | // Differs in charAt(min_ij_index). |
| 346 | expected = lhs.charAt(min_ij_index) - rhs.charAt(min_ij_index); |
| 347 | break; |
| 348 | default: throw new Error("Unexpected category."); |
| 349 | } |
| 350 | } else if (i_category == 3 || i_category == 6 || i_category == 9 || |
| 351 | j_category == 3 || j_category == 6 || j_category == 9) { |
| 352 | // In these categories, charAt(0) differs from other categories' strings. |
| 353 | expected = lhs.charAt(0) - rhs.charAt(0); |
| 354 | } else if (// Category 0 string is a prefix to any longer string in |
| 355 | // remaining categories. |
| 356 | (i_category == 0 && i_index < j_index) || |
| 357 | (j_category == 0 && j_index < i_index) || |
| 358 | // Category 2 string is a prefix to category 3 string at the same |
| 359 | // index. Similar for categories 4 and 5 and also 7 and 8. |
| 360 | // This includes matching last strings of these pairs of categories. |
| 361 | (i_index == j_index && |
| 362 | ((i_category == 1 && j_category == 2) || |
| 363 | (i_category == 2 && j_category == 1) || |
| 364 | (i_category == 4 && j_category == 5) || |
| 365 | (i_category == 5 && j_category == 4) || |
| 366 | (i_category == 7 && j_category == 8) || |
| 367 | (i_category == 8 && j_category == 7)))) { |
| 368 | // Differs in length. |
| 369 | expected = lhs.length() - rhs.length(); |
| 370 | } else { |
| 371 | // The remaining cases differ in charAt(min_ij_index), the characters |
| 372 | // before that are "0123456789abcdef".substring(0, min_ij_index). |
| 373 | for (int k = 0; k < min_ij_index; ++k) { |
| 374 | Assert.assertEquals("0123456789abcdef".charAt(k), lhs.charAt(k)); |
| 375 | Assert.assertEquals("0123456789abcdef".charAt(k), rhs.charAt(k)); |
| 376 | } |
| 377 | expected = lhs.charAt(min_ij_index) - rhs.charAt(min_ij_index); |
| 378 | Assert.assertFalse(expected == 0); |
| 379 | } |
| 380 | } |
| 381 | if (expected != result) { |
| 382 | throw new Error( |
| 383 | "Mismatch at i=" + i + ", j=" + j + ", expected=" + expected + |
| 384 | ", result=" + result); |
| 385 | } |
| 386 | boolean equalsExpected = |
| 387 | (i == j) || |
| 388 | // Last string in categories 1 and 2. |
| 389 | (i == 32 && j == 48) || (i == 48 && j == 32) || |
| 390 | // Last string in categories 4 and 5. |
| 391 | (i == 80 && j == 96) || (i == 96 && j == 80) || |
| 392 | // Last string in categories 7 and 8. |
| 393 | (i == 128 && j == 144) || (i == 144 && j == 128); |
| 394 | Assert.assertEquals(equalsExpected, $noinline$equals(lhs, rhs)); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | try { |
| 399 | $noinline$compareTo("", null); |
| 400 | Assert.fail(); |
| 401 | } catch (NullPointerException expected) { |
| 402 | } |
| 403 | try { |
| 404 | $noinline$compareTo(null, ""); |
| 405 | Assert.fail(); |
| 406 | } catch (NullPointerException expected) { |
| 407 | } |
| 408 | |
| 409 | Assert.assertFalse($noinline$equals("", null)); |
| 410 | try { |
| 411 | $noinline$equals(null, ""); |
| 412 | Assert.fail(); |
| 413 | } catch (NullPointerException expected) { |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | public static void testIndexOf() { |
| 418 | String[] prefixes = { |
| 419 | "", |
| 420 | "0", |
| 421 | "01", |
| 422 | "012", |
| 423 | "0123", |
| 424 | "01234", |
| 425 | "012345", |
| 426 | "0123456", |
| 427 | "01234567", |
| 428 | "012345678", |
| 429 | "0123456789", |
| 430 | "0123456789a", |
| 431 | "0123456789ab", |
| 432 | "0123456789abc", |
| 433 | "0123456789abcd", |
| 434 | "0123456789abcdef", |
| 435 | }; |
| 436 | String[] cores = { |
| 437 | "", |
| 438 | "x", |
| 439 | "xx", |
| 440 | "xxx", |
| 441 | "xxxx", |
| 442 | "xxxxx", |
| 443 | "xxxxxx", |
| 444 | "xxxxxxx", |
| 445 | "xxxxxxxx", |
| 446 | "xzx", |
| 447 | "xxzx", |
| 448 | "xxxzx", |
| 449 | "xxxxzx", |
| 450 | "xxxxxzx", |
| 451 | "xxxxxxzx", |
| 452 | "xxxxxxxzx", |
| 453 | "xxxxxxxxzx", |
| 454 | "\u0440", |
| 455 | "\u0440\u0440", |
| 456 | "\u0440\u0440\u0440", |
| 457 | "\u0440\u0440\u0440\u0440", |
| 458 | "\u0440\u0440\u0440\u0440\u0440", |
| 459 | "\u0440\u0440\u0440\u0440\u0440\u0440", |
| 460 | "\u0440\u0440\u0440\u0440\u0440\u0440\u0440", |
| 461 | "\u0440\u0440\u0440\u0440\u0440\u0440\u0440\u0440", |
| 462 | "\u0440z\u0440", |
| 463 | "\u0440\u0440z\u0440", |
| 464 | "\u0440\u0440\u0440z\u0440", |
| 465 | "\u0440\u0440\u0440\u0440z\u0440", |
| 466 | "\u0440\u0440\u0440\u0440\u0440z\u0440", |
| 467 | "\u0440\u0440\u0440\u0440\u0440\u0440z\u0440", |
| 468 | "\u0440\u0440\u0440\u0440\u0440\u0440\u0440z\u0440", |
| 469 | "\u0440\u0440\u0440\u0440\u0440\u0440\u0440\u0440z\u0440", |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 470 | "\u0000", |
| 471 | "\u0000\u0000", |
| 472 | "\u0000\u0000\u0000", |
| 473 | "\u0000\u0000\u0000\u0000", |
| 474 | "\u0000\u0000\u0000\u0000\u0000", |
| 475 | "\u0000\u0000\u0000\u0000\u0000\u0000", |
| 476 | "\u0000\u0000\u0000\u0000\u0000\u0000\u0000", |
| 477 | "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000", |
| 478 | "\u0000z\u0000", |
| 479 | "\u0000\u0000z\u0000", |
| 480 | "\u0000\u0000\u0000z\u0000", |
| 481 | "\u0000\u0000\u0000\u0000z\u0000", |
| 482 | "\u0000\u0000\u0000\u0000\u0000z\u0000", |
| 483 | "\u0000\u0000\u0000\u0000\u0000\u0000z\u0000", |
| 484 | "\u0000\u0000\u0000\u0000\u0000\u0000\u0000z\u0000", |
| 485 | "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000z\u0000", |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 486 | }; |
| 487 | String[] suffixes = { |
| 488 | "", |
| 489 | "y", |
| 490 | "yy", |
| 491 | "yyy", |
| 492 | "yyyy", |
| 493 | "yyyyy", |
| 494 | "yyyyyy", |
| 495 | "yyyyyyy", |
| 496 | "yyyyyyyy", |
| 497 | "\u0441", |
| 498 | "y\u0441", |
| 499 | "yy\u0441", |
| 500 | "yyy\u0441", |
| 501 | "yyyy\u0441", |
| 502 | "yyyyy\u0441", |
| 503 | "yyyyyy\u0441", |
| 504 | "yyyyyyy\u0441", |
| 505 | "yyyyyyyy\u0441", |
| 506 | }; |
| 507 | for (String p : prefixes) { |
| 508 | for (String c : cores) { |
| 509 | for (String s : suffixes) { |
| 510 | String full = p + c + s; |
| 511 | int expX = (c.isEmpty() || c.charAt(0) != 'x') ? -1 : p.length(); |
| 512 | int exp0440 = (c.isEmpty() || c.charAt(0) != '\u0440') ? -1 : p.length(); |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 513 | int exp0000 = (c.isEmpty() || c.charAt(0) != '\u0000') ? -1 : p.length(); |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 514 | Assert.assertEquals(expX, $noinline$indexOf(full, 'x')); |
| 515 | Assert.assertEquals(exp0440, $noinline$indexOf(full, '\u0440')); |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 516 | Assert.assertEquals(exp0000, $noinline$indexOf(full, '\u0000')); |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 517 | Assert.assertEquals(expX, $noinline$indexOf(full, 'x', -1)); |
| 518 | Assert.assertEquals(exp0440, $noinline$indexOf(full, '\u0440', -1)); |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 519 | Assert.assertEquals(exp0000, $noinline$indexOf(full, '\u0000', -1)); |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 520 | Assert.assertEquals(-1, $noinline$indexOf(full, 'x', full.length() + 1)); |
| 521 | Assert.assertEquals(-1, $noinline$indexOf(full, '\u0440', full.length() + 1)); |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 522 | Assert.assertEquals(-1, $noinline$indexOf(full, '\u0000', full.length() + 1)); |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 523 | for (int from = 0; from != full.length(); ++from) { |
| 524 | final int eX; |
| 525 | final int e0440; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 526 | final int e0000; |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 527 | if (from <= p.length()) { |
| 528 | eX = expX; |
| 529 | e0440 = exp0440; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 530 | e0000 = exp0000; |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 531 | } else if (from >= p.length() + c.length()) { |
| 532 | eX = -1; |
| 533 | e0440 = -1; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 534 | e0000 = -1; |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 535 | } else if (full.charAt(from) == 'z') { |
| 536 | eX = (full.charAt(from + 1) != 'x') ? -1 : from + 1; |
| 537 | e0440 = (full.charAt(from + 1) != '\u0440') ? -1 : from + 1; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 538 | e0000 = (full.charAt(from + 1) != '\u0000') ? -1 : from + 1; |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 539 | } else { |
| 540 | eX = (full.charAt(from) != 'x') ? -1 : from; |
| 541 | e0440 = (full.charAt(from) != '\u0440') ? -1 : from; |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 542 | e0000 = (full.charAt(from) != '\u0000') ? -1 : from; |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 543 | } |
| 544 | Assert.assertEquals(eX, $noinline$indexOf(full, 'x', from)); |
| 545 | Assert.assertEquals(e0440, $noinline$indexOf(full, '\u0440', from)); |
| Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 546 | Assert.assertEquals(e0000, $noinline$indexOf(full, '\u0000', from)); |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 553 | public static void testEqualsConstString() { |
| 554 | Assert.assertTrue($noinline$equalsConstString0("")); |
| 555 | Assert.assertFalse($noinline$equalsConstString0("1")); |
| 556 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 557 | Assert.assertTrue($noinline$equalsConstString3("012")); |
| 558 | Assert.assertFalse($noinline$equalsConstString3("01")); |
| 559 | Assert.assertFalse($noinline$equalsConstString3("0123")); |
| 560 | Assert.assertFalse($noinline$equalsConstString3("01x")); |
| 561 | Assert.assertFalse($noinline$equalsConstString3("01\u0440")); |
| 562 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 563 | Assert.assertTrue($noinline$equalsConstString7("0123456")); |
| 564 | Assert.assertFalse($noinline$equalsConstString7("012345")); |
| 565 | Assert.assertFalse($noinline$equalsConstString7("01234567")); |
| 566 | Assert.assertFalse($noinline$equalsConstString7("012345x")); |
| 567 | Assert.assertFalse($noinline$equalsConstString7("012345\u0440")); |
| 568 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 569 | Assert.assertTrue($noinline$equalsConstString12("012345678901")); |
| 570 | Assert.assertFalse($noinline$equalsConstString12("01234567890")); |
| 571 | Assert.assertFalse($noinline$equalsConstString12("0123456789012")); |
| 572 | Assert.assertFalse($noinline$equalsConstString12("01234567890x")); |
| 573 | Assert.assertFalse($noinline$equalsConstString12("01234567890\u0440")); |
| 574 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 575 | Assert.assertTrue($noinline$equalsConstString14("01234567890123")); |
| 576 | Assert.assertFalse($noinline$equalsConstString14("0123456789012")); |
| 577 | Assert.assertFalse($noinline$equalsConstString14("012345678901234")); |
| 578 | Assert.assertFalse($noinline$equalsConstString14("0123456789012x")); |
| 579 | Assert.assertFalse($noinline$equalsConstString14("0123456789012\u0440")); |
| 580 | |
| 581 | Assert.assertTrue($noinline$equalsConstString24("012345678901234567890123")); |
| 582 | Assert.assertFalse($noinline$equalsConstString24("01234567890123456789012")); |
| 583 | Assert.assertFalse($noinline$equalsConstString24("0123456789012345678901234")); |
| 584 | Assert.assertFalse($noinline$equalsConstString24("01234567890123456789012x")); |
| 585 | Assert.assertFalse($noinline$equalsConstString24("01234567890123456789012\u0440")); |
| 586 | |
| 587 | Assert.assertTrue($noinline$equalsConstString29("01234567890123456789012345678")); |
| 588 | Assert.assertFalse($noinline$equalsConstString29("0123456789012345678901234567")); |
| 589 | Assert.assertFalse($noinline$equalsConstString29("012345678901234567890123456789")); |
| 590 | Assert.assertFalse($noinline$equalsConstString29("0123456789012345678901234567x")); |
| 591 | Assert.assertFalse($noinline$equalsConstString29("0123456789012345678901234567\u0440")); |
| 592 | |
| 593 | Assert.assertTrue($noinline$equalsConstString35("01234567890123456789012345678901234")); |
| 594 | Assert.assertFalse($noinline$equalsConstString35("0123456789012345678901234567890123")); |
| 595 | Assert.assertFalse($noinline$equalsConstString35("012345678901234567890123456789012345")); |
| 596 | Assert.assertFalse($noinline$equalsConstString35("0123456789012345678901234567890123x")); |
| 597 | Assert.assertFalse( |
| 598 | $noinline$equalsConstString35("0123456789012345678901234567890123\u0440")); |
| 599 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 600 | Assert.assertTrue($noinline$equalsConstNonAsciiString3("\u044012")); |
| 601 | Assert.assertFalse($noinline$equalsConstNonAsciiString3("\u04401")); |
| 602 | Assert.assertFalse($noinline$equalsConstNonAsciiString3("\u0440123")); |
| 603 | Assert.assertFalse($noinline$equalsConstNonAsciiString3("\u04401x")); |
| 604 | Assert.assertFalse($noinline$equalsConstNonAsciiString3("012")); |
| 605 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 606 | Assert.assertTrue($noinline$equalsConstNonAsciiString7("\u0440123456")); |
| 607 | Assert.assertFalse($noinline$equalsConstNonAsciiString7("\u044012345")); |
| 608 | Assert.assertFalse($noinline$equalsConstNonAsciiString7("\u04401234567")); |
| 609 | Assert.assertFalse($noinline$equalsConstNonAsciiString7("\u044012345x")); |
| 610 | Assert.assertFalse($noinline$equalsConstNonAsciiString7("0123456")); |
| 611 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 612 | Assert.assertTrue($noinline$equalsConstNonAsciiString12("\u044012345678901")); |
| 613 | Assert.assertFalse($noinline$equalsConstNonAsciiString12("\u04401234567890")); |
| 614 | Assert.assertFalse($noinline$equalsConstNonAsciiString12("\u0440123456789012")); |
| 615 | Assert.assertFalse($noinline$equalsConstNonAsciiString12("\u04401234567890x")); |
| 616 | Assert.assertFalse($noinline$equalsConstNonAsciiString12("012345678901")); |
| 617 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 618 | Assert.assertTrue($noinline$equalsConstNonAsciiString14("\u04401234567890123")); |
| 619 | Assert.assertFalse($noinline$equalsConstNonAsciiString14("\u0440123456789012")); |
| 620 | Assert.assertFalse($noinline$equalsConstNonAsciiString14("\u044012345678901234")); |
| 621 | Assert.assertFalse($noinline$equalsConstNonAsciiString14("\u0440123456789012x")); |
| 622 | Assert.assertFalse($noinline$equalsConstNonAsciiString14("01234567890123")); |
| 623 | |
| 624 | Assert.assertTrue($noinline$equalsConstNonAsciiString24("\u044012345678901234567890123")); |
| 625 | Assert.assertFalse($noinline$equalsConstNonAsciiString24("\u04401234567890123456789012")); |
| 626 | Assert.assertFalse($noinline$equalsConstNonAsciiString24("\u0440123456789012345678901234")); |
| 627 | Assert.assertFalse($noinline$equalsConstNonAsciiString24("\u04401234567890123456789012x")); |
| 628 | Assert.assertFalse($noinline$equalsConstNonAsciiString24("\012345678901234567890123")); |
| 629 | |
| 630 | Assert.assertTrue( |
| 631 | $noinline$equalsConstNonAsciiString29("\u04401234567890123456789012345678")); |
| 632 | Assert.assertFalse( |
| 633 | $noinline$equalsConstNonAsciiString29("\u0440123456789012345678901234567")); |
| 634 | Assert.assertFalse( |
| 635 | $noinline$equalsConstNonAsciiString29("\u044012345678901234567890123456789")); |
| 636 | Assert.assertFalse( |
| 637 | $noinline$equalsConstNonAsciiString29("\u0440123456789012345678901234567x")); |
| 638 | Assert.assertFalse($noinline$equalsConstNonAsciiString29("01234567890123456789012345678")); |
| 639 | |
| 640 | Assert.assertTrue( |
| 641 | $noinline$equalsConstNonAsciiString35("\u04401234567890123456789012345678901234")); |
| 642 | Assert.assertFalse( |
| 643 | $noinline$equalsConstNonAsciiString35("\u0440123456789012345678901234567890123")); |
| 644 | Assert.assertFalse( |
| 645 | $noinline$equalsConstNonAsciiString35("\u044012345678901234567890123456789012345")); |
| 646 | Assert.assertFalse( |
| 647 | $noinline$equalsConstNonAsciiString35("\u0440123456789012345678901234567890123x")); |
| 648 | Assert.assertFalse( |
| 649 | $noinline$equalsConstNonAsciiString35("01234567890123456789012345678901234")); |
| 650 | } |
| 651 | |
| 652 | public static void testConstStringEquals() { |
| 653 | Assert.assertTrue($noinline$constString0Equals("")); |
| 654 | Assert.assertFalse($noinline$constString0Equals("1")); |
| 655 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 656 | Assert.assertTrue($noinline$constString3Equals("012")); |
| 657 | Assert.assertFalse($noinline$constString3Equals("01")); |
| 658 | Assert.assertFalse($noinline$constString3Equals("0123")); |
| 659 | Assert.assertFalse($noinline$constString3Equals("01x")); |
| 660 | Assert.assertFalse($noinline$constString3Equals("01\u0440")); |
| 661 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 662 | Assert.assertTrue($noinline$constString7Equals("0123456")); |
| 663 | Assert.assertFalse($noinline$constString7Equals("012345")); |
| 664 | Assert.assertFalse($noinline$constString7Equals("01234567")); |
| 665 | Assert.assertFalse($noinline$constString7Equals("012345x")); |
| 666 | Assert.assertFalse($noinline$constString7Equals("012345\u0440")); |
| 667 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 668 | Assert.assertTrue($noinline$constString12Equals("012345678901")); |
| 669 | Assert.assertFalse($noinline$constString12Equals("01234567890")); |
| 670 | Assert.assertFalse($noinline$constString12Equals("0123456789012")); |
| 671 | Assert.assertFalse($noinline$constString12Equals("01234567890x")); |
| 672 | Assert.assertFalse($noinline$constString12Equals("01234567890\u0440")); |
| 673 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 674 | Assert.assertTrue($noinline$constString14Equals("01234567890123")); |
| 675 | Assert.assertFalse($noinline$constString14Equals("0123456789012")); |
| 676 | Assert.assertFalse($noinline$constString14Equals("012345678901234")); |
| 677 | Assert.assertFalse($noinline$constString14Equals("0123456789012x")); |
| 678 | Assert.assertFalse($noinline$constString14Equals("0123456789012\u0440")); |
| 679 | |
| 680 | Assert.assertTrue($noinline$constString24Equals("012345678901234567890123")); |
| 681 | Assert.assertFalse($noinline$constString24Equals("01234567890123456789012")); |
| 682 | Assert.assertFalse($noinline$constString24Equals("0123456789012345678901234")); |
| 683 | Assert.assertFalse($noinline$constString24Equals("01234567890123456789012x")); |
| 684 | Assert.assertFalse($noinline$constString24Equals("01234567890123456789012\u0440")); |
| 685 | |
| 686 | Assert.assertTrue($noinline$constString29Equals("01234567890123456789012345678")); |
| 687 | Assert.assertFalse($noinline$constString29Equals("0123456789012345678901234567")); |
| 688 | Assert.assertFalse($noinline$constString29Equals("012345678901234567890123456789")); |
| 689 | Assert.assertFalse($noinline$constString29Equals("0123456789012345678901234567x")); |
| 690 | Assert.assertFalse($noinline$constString29Equals("0123456789012345678901234567\u0440")); |
| 691 | |
| 692 | Assert.assertTrue($noinline$constString35Equals("01234567890123456789012345678901234")); |
| 693 | Assert.assertFalse($noinline$constString35Equals("0123456789012345678901234567890123")); |
| 694 | Assert.assertFalse($noinline$constString35Equals("012345678901234567890123456789012345")); |
| 695 | Assert.assertFalse($noinline$constString35Equals("0123456789012345678901234567890123x")); |
| 696 | Assert.assertFalse( |
| 697 | $noinline$constString35Equals("0123456789012345678901234567890123\u0040")); |
| 698 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 699 | Assert.assertTrue($noinline$constNonAsciiString3Equals("\u044012")); |
| 700 | Assert.assertFalse($noinline$constNonAsciiString3Equals("\u04401")); |
| 701 | Assert.assertFalse($noinline$constNonAsciiString3Equals("\u0440123")); |
| 702 | Assert.assertFalse($noinline$constNonAsciiString3Equals("\u04401x")); |
| 703 | Assert.assertFalse($noinline$constNonAsciiString3Equals("0123456")); |
| 704 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 705 | Assert.assertTrue($noinline$constNonAsciiString7Equals("\u0440123456")); |
| 706 | Assert.assertFalse($noinline$constNonAsciiString7Equals("\u044012345")); |
| 707 | Assert.assertFalse($noinline$constNonAsciiString7Equals("\u04401234567")); |
| 708 | Assert.assertFalse($noinline$constNonAsciiString7Equals("\u044012345x")); |
| 709 | Assert.assertFalse($noinline$constNonAsciiString7Equals("0123456")); |
| 710 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 711 | Assert.assertTrue($noinline$constNonAsciiString12Equals("\u044012345678901")); |
| 712 | Assert.assertFalse($noinline$constNonAsciiString12Equals("\u04401234567890")); |
| 713 | Assert.assertFalse($noinline$constNonAsciiString12Equals("\u0440123456789012")); |
| 714 | Assert.assertFalse($noinline$constNonAsciiString12Equals("\u04401234567890x")); |
| 715 | Assert.assertFalse($noinline$constNonAsciiString12Equals("012345678901")); |
| 716 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 717 | Assert.assertTrue($noinline$constNonAsciiString14Equals("\u04401234567890123")); |
| 718 | Assert.assertFalse($noinline$constNonAsciiString14Equals("\u0440123456789012")); |
| 719 | Assert.assertFalse($noinline$constNonAsciiString14Equals("\u044012345678901234")); |
| 720 | Assert.assertFalse($noinline$constNonAsciiString14Equals("\u0440123456789012x")); |
| 721 | Assert.assertFalse($noinline$constNonAsciiString14Equals("01234567890123")); |
| 722 | |
| 723 | Assert.assertTrue($noinline$constNonAsciiString24Equals("\u044012345678901234567890123")); |
| 724 | Assert.assertFalse($noinline$constNonAsciiString24Equals("\u04401234567890123456789012")); |
| 725 | Assert.assertFalse($noinline$constNonAsciiString24Equals("\u0440123456789012345678901234")); |
| 726 | Assert.assertFalse($noinline$constNonAsciiString24Equals("\u04401234567890123456789012x")); |
| 727 | Assert.assertFalse($noinline$constNonAsciiString24Equals("\012345678901234567890123")); |
| 728 | |
| 729 | Assert.assertTrue( |
| 730 | $noinline$constNonAsciiString29Equals("\u04401234567890123456789012345678")); |
| 731 | Assert.assertFalse( |
| 732 | $noinline$constNonAsciiString29Equals("\u0440123456789012345678901234567")); |
| 733 | Assert.assertFalse( |
| 734 | $noinline$constNonAsciiString29Equals("\u044012345678901234567890123456789")); |
| 735 | Assert.assertFalse( |
| 736 | $noinline$constNonAsciiString29Equals("\u0440123456789012345678901234567x")); |
| 737 | Assert.assertFalse($noinline$constNonAsciiString29Equals("01234567890123456789012345678")); |
| 738 | |
| 739 | Assert.assertTrue( |
| 740 | $noinline$constNonAsciiString35Equals("\u04401234567890123456789012345678901234")); |
| 741 | Assert.assertFalse( |
| 742 | $noinline$constNonAsciiString35Equals("\u0440123456789012345678901234567890123")); |
| 743 | Assert.assertFalse( |
| 744 | $noinline$constNonAsciiString35Equals("\u044012345678901234567890123456789012345")); |
| 745 | Assert.assertFalse( |
| 746 | $noinline$constNonAsciiString35Equals("\u0440123456789012345678901234567890123x")); |
| 747 | Assert.assertFalse( |
| 748 | $noinline$constNonAsciiString35Equals("01234567890123456789012345678901234")); |
| Vladimir Marko | a44c445 | 2017-07-13 18:49:35 +0100 | [diff] [blame] | 749 | |
| 750 | // Regression test for incorrectly creating an uncompressed string when the |
| 751 | // string should be compressed. Only the low 8 bits are relevant but the whole |
| 752 | // `hibyte` was erroneously tested. Bug: 63661357 |
| 753 | Assert.assertTrue("A".equals(new String(new byte[] { (byte)'A' }, /* hibyte */ 0x100))); |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| Vladimir Marko | 7629548 | 2020-10-26 12:28:37 +0000 | [diff] [blame] | 756 | public static void testStringConcat() { |
| 757 | Assert.assertEquals("abcxyzw", "abc".concat("xyzw")); |
| 758 | Assert.assertEquals("abc\u0440", "abc".concat("\u0440")); |
| 759 | Assert.assertEquals("\u0440xyzw", "\u0440".concat("xyzw")); |
| 760 | Assert.assertEquals("abc\u0440xyzw\u0440", "abc\u0440".concat("xyzw\u0440")); |
| 761 | } |
| 762 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 763 | public static boolean $noinline$equalsConstString0(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 764 | return s.equals(""); |
| 765 | } |
| 766 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 767 | public static boolean $noinline$equalsConstString3(String s) { |
| 768 | return s.equals("012"); |
| 769 | } |
| 770 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 771 | public static boolean $noinline$equalsConstString7(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 772 | return s.equals("0123456"); |
| 773 | } |
| 774 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 775 | public static boolean $noinline$equalsConstString12(String s) { |
| 776 | return s.equals("012345678901"); |
| 777 | } |
| 778 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 779 | public static boolean $noinline$equalsConstString14(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 780 | return s.equals("01234567890123"); |
| 781 | } |
| 782 | |
| 783 | public static boolean $noinline$equalsConstString24(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 784 | return s.equals("012345678901234567890123"); |
| 785 | } |
| 786 | |
| 787 | public static boolean $noinline$equalsConstString29(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 788 | return s.equals("01234567890123456789012345678"); |
| 789 | } |
| 790 | |
| 791 | public static boolean $noinline$equalsConstString35(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 792 | return s.equals("01234567890123456789012345678901234"); |
| 793 | } |
| 794 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 795 | public static boolean $noinline$equalsConstNonAsciiString3(String s) { |
| 796 | return s.equals("\u044012"); |
| 797 | } |
| 798 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 799 | public static boolean $noinline$equalsConstNonAsciiString7(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 800 | return s.equals("\u0440123456"); |
| 801 | } |
| 802 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 803 | public static boolean $noinline$equalsConstNonAsciiString12(String s) { |
| 804 | return s.equals("\u044012345678901"); |
| 805 | } |
| 806 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 807 | public static boolean $noinline$equalsConstNonAsciiString14(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 808 | return s.equals("\u04401234567890123"); |
| 809 | } |
| 810 | |
| 811 | public static boolean $noinline$equalsConstNonAsciiString24(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 812 | return s.equals("\u044012345678901234567890123"); |
| 813 | } |
| 814 | |
| 815 | public static boolean $noinline$equalsConstNonAsciiString29(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 816 | return s.equals("\u04401234567890123456789012345678"); |
| 817 | } |
| 818 | |
| 819 | public static boolean $noinline$equalsConstNonAsciiString35(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 820 | return s.equals("\u04401234567890123456789012345678901234"); |
| 821 | } |
| 822 | |
| 823 | public static boolean $noinline$constString0Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 824 | return s.equals(""); |
| 825 | } |
| 826 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 827 | public static boolean $noinline$constString3Equals(String s) { |
| 828 | return "012".equals(s); |
| 829 | } |
| 830 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 831 | public static boolean $noinline$constString7Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 832 | return "0123456".equals(s); |
| 833 | } |
| 834 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 835 | public static boolean $noinline$constString12Equals(String s) { |
| 836 | return "012345678901".equals(s); |
| 837 | } |
| 838 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 839 | public static boolean $noinline$constString14Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 840 | return "01234567890123".equals(s); |
| 841 | } |
| 842 | |
| 843 | public static boolean $noinline$constString24Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 844 | return "012345678901234567890123".equals(s); |
| 845 | } |
| 846 | |
| 847 | public static boolean $noinline$constString29Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 848 | return "01234567890123456789012345678".equals(s); |
| 849 | } |
| 850 | |
| 851 | public static boolean $noinline$constString35Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 852 | return "01234567890123456789012345678901234".equals(s); |
| 853 | } |
| 854 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 855 | public static boolean $noinline$constNonAsciiString3Equals(String s) { |
| 856 | return "\u044012".equals(s); |
| 857 | } |
| 858 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 859 | public static boolean $noinline$constNonAsciiString7Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 860 | return "\u0440123456".equals(s); |
| 861 | } |
| 862 | |
| Vladimir Marko | 984519c | 2017-08-23 10:45:29 +0100 | [diff] [blame] | 863 | public static boolean $noinline$constNonAsciiString12Equals(String s) { |
| 864 | return "\u044012345678901".equals(s); |
| 865 | } |
| 866 | |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 867 | public static boolean $noinline$constNonAsciiString14Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 868 | return "\u04401234567890123".equals(s); |
| 869 | } |
| 870 | |
| 871 | public static boolean $noinline$constNonAsciiString24Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 872 | return "\u044012345678901234567890123".equals(s); |
| 873 | } |
| 874 | |
| 875 | public static boolean $noinline$constNonAsciiString29Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 876 | return "\u04401234567890123456789012345678".equals(s); |
| 877 | } |
| 878 | |
| 879 | public static boolean $noinline$constNonAsciiString35Equals(String s) { |
| Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 880 | return "\u04401234567890123456789012345678901234".equals(s); |
| 881 | } |
| 882 | |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 883 | public static int $noinline$compareTo(String lhs, String rhs) { |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 884 | return lhs.compareTo(rhs); |
| 885 | } |
| 886 | |
| 887 | public static boolean $noinline$equals(String lhs, String rhs) { |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 888 | return lhs.equals(rhs); |
| 889 | } |
| 890 | |
| 891 | public static int $noinline$indexOf(String lhs, int ch) { |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 892 | return lhs.indexOf(ch); |
| 893 | } |
| 894 | |
| 895 | public static int $noinline$indexOf(String lhs, int ch, int fromIndex) { |
| Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 896 | return lhs.indexOf(ch, fromIndex); |
| 897 | } |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 898 | } |