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