Home | History | Annotate | Download | only in lang
      1 /*
      2  * Copyright (C) 2011 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 package libcore.java.lang;
     18 
     19 import java.util.Arrays;
     20 
     21 public class StringBuilderTest extends junit.framework.TestCase {
     22     // See https://code.google.com/p/android/issues/detail?id=60639
     23     public void test_deleteChatAt_lastRange() {
     24         StringBuilder sb = new StringBuilder("oarFish_");
     25         sb.append('a');
     26         String oarFishA = sb.toString();
     27 
     28         sb.deleteCharAt(sb.length() - 1);
     29         sb.append('b');
     30         String oarFishB = sb.toString();
     31 
     32         assertEquals("oarFish_a", oarFishA);
     33         assertEquals("oarFish_b", oarFishB);
     34     }
     35 
     36     // See https://code.google.com/p/android/issues/detail?id=60639
     37     public void test_deleteCharAt_lastChar() {
     38         StringBuilder sb = new StringBuilder();
     39         sb.append('a');
     40         String a = sb.toString();
     41 
     42         sb.deleteCharAt(0);
     43         sb.append('b');
     44         String b = sb.toString();
     45 
     46         assertEquals("a", a);
     47         assertEquals("b", b);
     48     }
     49 
     50     // See https://code.google.com/p/android/issues/detail?id=60639
     51     public void test_delete_endsAtLastChar() {
     52         StringBuilder sb = new StringBuilder("newGuineaSinging");
     53         sb.append("Dog");
     54         String dog = sb.toString();
     55 
     56         sb.delete(sb.length() - 3, sb.length());
     57         sb.append("Cat");
     58         String cat = sb.toString();
     59 
     60         // NOTE: It's important that these asserts stay at the end of this test.
     61         // We're trying to make sure that replacing chars in the builder does not
     62         // change strings that have already been returned from it.
     63         assertEquals("newGuineaSingingDog", dog);
     64         assertEquals("newGuineaSingingCat", cat);
     65     }
     66 
     67     public void test_deleteCharAt_boundsChecks() {
     68         StringBuilder sb = new StringBuilder("yeti");
     69 
     70         try {
     71             sb.deleteCharAt(sb.length());
     72             fail();
     73         } catch (StringIndexOutOfBoundsException expected) {
     74         }
     75 
     76         try {
     77             sb.deleteCharAt(-1);
     78             fail();
     79         } catch (StringIndexOutOfBoundsException expected) {
     80         }
     81     }
     82 
     83     public void test_delete_boundsChecks() throws Exception {
     84         StringBuilder sb = new StringBuilder("yeti");
     85 
     86         // The cases below ahould not throw (even though they are clearly invalid
     87         // ranges), because we promise not to throw if start == count as long as
     88         // end >= start.
     89         sb.delete(sb.length(), sb.length() + 2);
     90         sb.delete(sb.length(), sb.length());
     91 
     92         sb.delete(2, 2);
     93         assertEquals("yeti", sb.toString());
     94 
     95         // We must throw if start > count....
     96         try {
     97             sb.delete(sb.length() + 2, sb.length() + 3);
     98             fail();
     99         } catch (StringIndexOutOfBoundsException expected) {
    100         }
    101 
    102         // ... even if the length of the range is 0.
    103         try {
    104             sb.delete(sb.length() + 2, sb.length() + 2);
    105             fail();
    106         } catch (StringIndexOutOfBoundsException expected) {
    107         }
    108 
    109         // Must throw if start < 0.
    110         try {
    111             sb.delete(-1, sb.length() -1);
    112             fail();
    113         } catch (StringIndexOutOfBoundsException expected) {
    114         }
    115 
    116         // A few commonly used specializations: sb.delete(0, 0) on an empty
    117         // builder is a particularly common pattern.
    118         StringBuilder sb2 = new StringBuilder();
    119         sb2.delete(0, sb2.length());
    120         sb2.delete(0, 12);
    121     }
    122 
    123     // We shouldn't throw if the end index is > count, we should clamp it
    124     // instead.
    125     public void test_delete_clampsEnd() throws Exception {
    126         StringBuilder sb = new StringBuilder("mogwai");
    127 
    128         sb.delete(sb.length() - 1 , sb.length() + 2);
    129         assertEquals("mogwa", sb.toString());
    130 
    131         sb.delete(sb.length() - 1, sb.length());
    132         assertEquals("mogw", sb.toString());
    133     }
    134 
    135     public void testChars() {
    136         StringBuilder s = new StringBuilder("Hello\n\tworld");
    137         int[] expected = new int[s.length()];
    138         for (int i = 0; i < s.length(); ++i) {
    139             expected[i] = (int) s.charAt(i);
    140         }
    141         assertTrue(Arrays.equals(expected, s.chars().toArray()));
    142 
    143         // Surrogate code point
    144         char high = '\uD83D', low = '\uDE02';
    145         StringBuilder surrogateCP = new StringBuilder().append(new char[]{high, low, low});
    146         assertTrue(Arrays.equals(new int[]{high, low, low}, surrogateCP.chars().toArray()));
    147     }
    148 
    149     public void testCodePoints() {
    150         StringBuilder s = new StringBuilder("Hello\n\tworld");
    151         int[] expected = new int[s.length()];
    152         for (int i = 0; i < s.length(); ++i) {
    153             expected[i] = (int) s.charAt(i);
    154         }
    155         assertTrue(Arrays.equals(expected, s.codePoints().toArray()));
    156 
    157         // Surrogate code point
    158         char high = '\uD83D', low = '\uDE02';
    159         StringBuilder surrogateCP = new StringBuilder().append(new char[]{high, low, low, '0'});
    160         assertEquals(Character.toCodePoint(high, low), surrogateCP.codePoints().toArray()[0]);
    161         assertEquals((int) low, surrogateCP.codePoints().toArray()[1]); // Unmatched surrogate.
    162         assertEquals((int) '0', surrogateCP.codePoints().toArray()[2]);
    163     }
    164 }
    165