1 /* 2 * Copyright (C) 2010 The Guava Authors 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 package com.google.common.base; 18 19 import com.google.common.annotations.GwtCompatible; 20 21 import junit.framework.TestCase; 22 23 /** 24 * Unit test for {@link Strings}. 25 * 26 * @author Kevin Bourrillion 27 */ 28 @GwtCompatible(emulated = true) 29 public class StringsTest extends TestCase { 30 public void testNullToEmpty() { 31 assertEquals("", Strings.nullToEmpty(null)); 32 assertEquals("", Strings.nullToEmpty("")); 33 assertEquals("a", Strings.nullToEmpty("a")); 34 } 35 36 public void testEmptyToNull() { 37 assertNull(Strings.emptyToNull(null)); 38 assertNull(Strings.emptyToNull("")); 39 assertEquals("a", Strings.emptyToNull("a")); 40 } 41 42 public void testIsNullOrEmpty() { 43 assertTrue(Strings.isNullOrEmpty(null)); 44 assertTrue(Strings.isNullOrEmpty("")); 45 assertFalse(Strings.isNullOrEmpty("a")); 46 } 47 48 public void testPadStart_noPadding() { 49 assertSame("", Strings.padStart("", 0, '-')); 50 assertSame("x", Strings.padStart("x", 0, '-')); 51 assertSame("x", Strings.padStart("x", 1, '-')); 52 assertSame("xx", Strings.padStart("xx", 0, '-')); 53 assertSame("xx", Strings.padStart("xx", 2, '-')); 54 } 55 56 public void testPadStart_somePadding() { 57 assertEquals("-", Strings.padStart("", 1, '-')); 58 assertEquals("--", Strings.padStart("", 2, '-')); 59 assertEquals("-x", Strings.padStart("x", 2, '-')); 60 assertEquals("--x", Strings.padStart("x", 3, '-')); 61 assertEquals("-xx", Strings.padStart("xx", 3, '-')); 62 } 63 64 public void testPadStart_negativeMinLength() { 65 assertSame("x", Strings.padStart("x", -1, '-')); 66 } 67 68 // TODO: could remove if we got NPT working in GWT somehow 69 public void testPadStart_null() { 70 try { 71 Strings.padStart(null, 5, '0'); 72 fail(); 73 } catch (NullPointerException expected) { 74 } 75 } 76 77 public void testPadEnd_noPadding() { 78 assertSame("", Strings.padEnd("", 0, '-')); 79 assertSame("x", Strings.padEnd("x", 0, '-')); 80 assertSame("x", Strings.padEnd("x", 1, '-')); 81 assertSame("xx", Strings.padEnd("xx", 0, '-')); 82 assertSame("xx", Strings.padEnd("xx", 2, '-')); 83 } 84 85 public void testPadEnd_somePadding() { 86 assertEquals("-", Strings.padEnd("", 1, '-')); 87 assertEquals("--", Strings.padEnd("", 2, '-')); 88 assertEquals("x-", Strings.padEnd("x", 2, '-')); 89 assertEquals("x--", Strings.padEnd("x", 3, '-')); 90 assertEquals("xx-", Strings.padEnd("xx", 3, '-')); 91 } 92 93 public void testPadEnd_negativeMinLength() { 94 assertSame("x", Strings.padEnd("x", -1, '-')); 95 } 96 97 // TODO: could remove if we got NPT working in GWT somehow 98 public void testPadEnd_null() { 99 try { 100 Strings.padEnd(null, 5, '0'); 101 fail(); 102 } catch (NullPointerException expected) { 103 } 104 } 105 106 public void testRepeat() { 107 String input = "20"; 108 assertEquals("", Strings.repeat(input, 0)); 109 assertEquals("20", Strings.repeat(input, 1)); 110 assertEquals("2020", Strings.repeat(input, 2)); 111 assertEquals("202020", Strings.repeat(input, 3)); 112 113 assertEquals("", Strings.repeat("", 4)); 114 115 for (int i = 0; i < 100; ++i) { 116 assertEquals(2 * i, Strings.repeat(input, i).length()); 117 } 118 119 try { 120 Strings.repeat("x", -1); 121 fail(); 122 } catch (IllegalArgumentException expected) { 123 } 124 try { 125 // Massive string 126 Strings.repeat("12345678", (1 << 30) + 3); 127 fail(); 128 } catch (ArrayIndexOutOfBoundsException expected) { 129 } 130 } 131 132 // TODO: could remove if we got NPT working in GWT somehow 133 public void testRepeat_null() { 134 try { 135 Strings.repeat(null, 5); 136 fail(); 137 } catch (NullPointerException expected) { 138 } 139 } 140 141 public void testCommonPrefix() { 142 assertEquals("", Strings.commonPrefix("", "")); 143 assertEquals("", Strings.commonPrefix("abc", "")); 144 assertEquals("", Strings.commonPrefix("", "abc")); 145 assertEquals("", Strings.commonPrefix("abcde", "xyz")); 146 assertEquals("", Strings.commonPrefix("xyz", "abcde")); 147 assertEquals("", Strings.commonPrefix("xyz", "abcxyz")); 148 assertEquals("a", Strings.commonPrefix("abc", "aaaaa")); 149 assertEquals("aa", Strings.commonPrefix("aa", "aaaaa")); 150 assertEquals("abc", 151 Strings.commonPrefix(new StringBuffer("abcdef"), "abcxyz")); 152 153 // Identical valid surrogate pairs. 154 assertEquals("abc\uD8AB\uDCAB", 155 Strings.commonPrefix("abc\uD8AB\uDCABdef", "abc\uD8AB\uDCABxyz")); 156 // Differing valid surrogate pairs. 157 assertEquals("abc", 158 Strings.commonPrefix("abc\uD8AB\uDCABdef", "abc\uD8AB\uDCACxyz")); 159 // One invalid pair. 160 assertEquals("abc", 161 Strings.commonPrefix("abc\uD8AB\uDCABdef", "abc\uD8AB\uD8ABxyz")); 162 // Two identical invalid pairs. 163 assertEquals("abc\uD8AB\uD8AC", 164 Strings.commonPrefix("abc\uD8AB\uD8ACdef", "abc\uD8AB\uD8ACxyz")); 165 // Two differing invalid pairs. 166 assertEquals("abc\uD8AB", 167 Strings.commonPrefix("abc\uD8AB\uD8ABdef", "abc\uD8AB\uD8ACxyz")); 168 // One orphan high surrogate. 169 assertEquals("", Strings.commonPrefix("\uD8AB\uDCAB", "\uD8AB")); 170 // Two orphan high surrogates. 171 assertEquals("\uD8AB", Strings.commonPrefix("\uD8AB", "\uD8AB")); 172 } 173 174 public void testCommonSuffix() { 175 assertEquals("", Strings.commonSuffix("", "")); 176 assertEquals("", Strings.commonSuffix("abc", "")); 177 assertEquals("", Strings.commonSuffix("", "abc")); 178 assertEquals("", Strings.commonSuffix("abcde", "xyz")); 179 assertEquals("", Strings.commonSuffix("xyz", "abcde")); 180 assertEquals("", Strings.commonSuffix("xyz", "xyzabc")); 181 assertEquals("c", Strings.commonSuffix("abc", "ccccc")); 182 assertEquals("aa", Strings.commonSuffix("aa", "aaaaa")); 183 assertEquals("abc", 184 Strings.commonSuffix(new StringBuffer("xyzabc"), "xxxabc")); 185 186 // Identical valid surrogate pairs. 187 assertEquals("\uD8AB\uDCABdef", 188 Strings.commonSuffix("abc\uD8AB\uDCABdef", "xyz\uD8AB\uDCABdef")); 189 // Differing valid surrogate pairs. 190 assertEquals("def", 191 Strings.commonSuffix("abc\uD8AB\uDCABdef", "abc\uD8AC\uDCABdef")); 192 // One invalid pair. 193 assertEquals("def", 194 Strings.commonSuffix("abc\uD8AB\uDCABdef", "xyz\uDCAB\uDCABdef")); 195 // Two identical invalid pairs. 196 assertEquals("\uD8AB\uD8ABdef", 197 Strings.commonSuffix("abc\uD8AB\uD8ABdef", "xyz\uD8AB\uD8ABdef")); 198 // Two differing invalid pairs. 199 assertEquals("\uDCABdef", 200 Strings.commonSuffix("abc\uDCAB\uDCABdef", "abc\uDCAC\uDCABdef")); 201 // One orphan low surrogate. 202 assertEquals("", Strings.commonSuffix("x\uD8AB\uDCAB", "\uDCAB")); 203 // Two orphan low surrogates. 204 assertEquals("\uDCAB", Strings.commonSuffix("\uDCAB", "\uDCAB")); 205 } 206 207 public void testValidSurrogatePairAt() { 208 assertTrue(Strings.validSurrogatePairAt("\uD8AB\uDCAB", 0)); 209 assertTrue(Strings.validSurrogatePairAt("abc\uD8AB\uDCAB", 3)); 210 assertTrue(Strings.validSurrogatePairAt("abc\uD8AB\uDCABxyz", 3)); 211 assertFalse(Strings.validSurrogatePairAt("\uD8AB\uD8AB", 0)); 212 assertFalse(Strings.validSurrogatePairAt("\uDCAB\uDCAB", 0)); 213 assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", -1)); 214 assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", 1)); 215 assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", -2)); 216 assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", 2)); 217 assertFalse(Strings.validSurrogatePairAt("x\uDCAB", 0)); 218 assertFalse(Strings.validSurrogatePairAt("\uD8ABx", 0)); 219 } 220 } 221 222