Home | History | Annotate | Download | only in util
      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 package com.android.internal.util;
     18 
     19 import com.android.internal.util.CharSequences;
     20 import static com.android.internal.util.CharSequences.forAsciiBytes;
     21 import junit.framework.TestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 public class CharSequencesTest extends TestCase {
     25 
     26     @SmallTest
     27     public void testCharSequences() {
     28         String s = "Crazy Bob";
     29         byte[] bytes = s.getBytes();
     30 
     31         String copy = toString(forAsciiBytes(bytes));
     32         assertTrue(s.equals(copy));
     33 
     34         copy = toString(forAsciiBytes(bytes, 0, s.length()));
     35         assertTrue(s.equals(copy));
     36 
     37         String crazy = toString(forAsciiBytes(bytes, 0, 5));
     38         assertTrue("Crazy".equals(crazy));
     39 
     40         String a = toString(forAsciiBytes(bytes, 0, 3).subSequence(2, 3));
     41         assertTrue("a".equals(a));
     42 
     43         String empty = toString(forAsciiBytes(bytes, 0, 3).subSequence(3, 3));
     44         assertTrue("".equals(empty));
     45 
     46         assertTrue(CharSequences.equals("bob", "bob"));
     47         assertFalse(CharSequences.equals("b", "bob"));
     48         assertFalse(CharSequences.equals("", "bob"));
     49     }
     50 
     51     /**
     52      * Converts a CharSequence to a string the slow way. Useful for testing
     53      * a CharSequence implementation.
     54      */
     55     static String toString(CharSequence charSequence) {
     56         return new StringBuilder().append(charSequence).toString();
     57     }
     58 }
     59