Home | History | Annotate | Download | only in src
      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;
     18 
     19 /**
     20  * more string tests
     21  */
     22 public class Main {
     23     public static void main(String args[]) {
     24         String test = "0123456789";
     25         String test1 = new String("0123456789");    // different object
     26         String test2 = new String("0123456780");    // different value
     27         String offset = new String("xxx0123456789yyy");
     28         String sub = offset.substring(3, 13);
     29         Object blah = new Object();
     30 
     31         Assert.assertTrue(test.equals(test));
     32         Assert.assertTrue(test.equals(test1));
     33         Assert.assertFalse(test.equals(test2));
     34 
     35         Assert.assertEquals(test.compareTo(test1), 0);
     36         Assert.assertTrue(test1.compareTo(test2) > 0);
     37         Assert.assertTrue(test2.compareTo(test1) < 0);
     38 
     39         /* compare string with a nonzero offset, in left/right side */
     40         Assert.assertEquals(test.compareTo(sub), 0);
     41         Assert.assertEquals(sub.compareTo(test), 0);
     42         Assert.assertTrue(test.equals(sub));
     43         Assert.assertTrue(sub.equals(test));
     44         /* same base, one is a substring */
     45         Assert.assertFalse(offset.equals(sub));
     46         Assert.assertFalse(sub.equals(offset));
     47         /* wrong class */
     48         Assert.assertFalse(test.equals(blah));
     49 
     50         /* null ptr - throw */
     51         try {
     52             test.compareTo(null);
     53             Assert.fail("didn't get expected npe");
     54         } catch (NullPointerException npe) {
     55             System.out.println("Got expected npe");
     56         }
     57         /* null ptr - ok */
     58         Assert.assertFalse(test.equals(null));
     59 
     60         test = test.substring(1);
     61         Assert.assertTrue(test.equals("123456789"));
     62         Assert.assertFalse(test.equals(test1));
     63 
     64         test = test.substring(1);
     65         Assert.assertTrue(test.equals("23456789"));
     66 
     67         test = test.substring(1);
     68         Assert.assertTrue(test.equals("3456789"));
     69 
     70         test = test.substring(1);
     71         Assert.assertTrue(test.equals("456789"));
     72 
     73         test = test.substring(3,5);
     74         Assert.assertTrue(test.equals("78"));
     75 
     76         test = "this/is/a/path";
     77         String[] strings = test.split("/");
     78         Assert.assertEquals(4, strings.length);
     79 
     80         Assert.assertEquals("this is a path", test.replaceAll("/", " "));
     81         Assert.assertEquals("this is a path", test.replace("/", " "));
     82     }
     83 }
     84