Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 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 android.test.MoreAsserts;
     20 
     21 import java.util.Arrays;
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * Tests for {@link ArrayUtils}
     26  */
     27 public class ArrayUtilsTest extends TestCase {
     28     public void testContains() throws Exception {
     29         final Object A = new Object();
     30         final Object B = new Object();
     31         final Object C = new Object();
     32         final Object D = new Object();
     33 
     34         assertTrue(ArrayUtils.contains(new Object[] { A, B, C }, A));
     35         assertTrue(ArrayUtils.contains(new Object[] { A, B, C }, B));
     36         assertTrue(ArrayUtils.contains(new Object[] { A, B, C }, C));
     37         assertTrue(ArrayUtils.contains(new Object[] { A, null, C }, null));
     38 
     39         assertFalse(ArrayUtils.contains(new Object[] { A, B, C }, null));
     40         assertFalse(ArrayUtils.contains(new Object[] { }, null));
     41         assertFalse(ArrayUtils.contains(new Object[] { null }, A));
     42     }
     43 
     44     public void testIndexOf() throws Exception {
     45         final Object A = new Object();
     46         final Object B = new Object();
     47         final Object C = new Object();
     48         final Object D = new Object();
     49 
     50         assertEquals(0, ArrayUtils.indexOf(new Object[] { A, B, C }, A));
     51         assertEquals(1, ArrayUtils.indexOf(new Object[] { A, B, C }, B));
     52         assertEquals(2, ArrayUtils.indexOf(new Object[] { A, B, C }, C));
     53         assertEquals(-1, ArrayUtils.indexOf(new Object[] { A, B, C }, D));
     54 
     55         assertEquals(-1, ArrayUtils.indexOf(new Object[] { A, B, C }, null));
     56         assertEquals(-1, ArrayUtils.indexOf(new Object[] { }, A));
     57         assertEquals(-1, ArrayUtils.indexOf(new Object[] { }, null));
     58 
     59         assertEquals(0, ArrayUtils.indexOf(new Object[] { null, null }, null));
     60         assertEquals(1, ArrayUtils.indexOf(new Object[] { A, null, B }, null));
     61         assertEquals(2, ArrayUtils.indexOf(new Object[] { A, null, B }, B));
     62     }
     63 
     64     public void testContainsAll() throws Exception {
     65         final Object A = new Object();
     66         final Object B = new Object();
     67         final Object C = new Object();
     68 
     69         assertTrue(ArrayUtils.containsAll(new Object[] { C, B, A }, new Object[] { A, B, C }));
     70         assertTrue(ArrayUtils.containsAll(new Object[] { A, B }, new Object[] { A }));
     71         assertTrue(ArrayUtils.containsAll(new Object[] { A }, new Object[] { A }));
     72         assertTrue(ArrayUtils.containsAll(new Object[] { A }, new Object[] { }));
     73         assertTrue(ArrayUtils.containsAll(new Object[] { }, new Object[] { }));
     74         assertTrue(ArrayUtils.containsAll(new Object[] { null }, new Object[] { }));
     75         assertTrue(ArrayUtils.containsAll(new Object[] { null }, new Object[] { null }));
     76         assertTrue(ArrayUtils.containsAll(new Object[] { A, null, C }, new Object[] { C, null }));
     77 
     78         assertFalse(ArrayUtils.containsAll(new Object[] { }, new Object[] { A }));
     79         assertFalse(ArrayUtils.containsAll(new Object[] { B }, new Object[] { A }));
     80         assertFalse(ArrayUtils.containsAll(new Object[] { }, new Object[] { null }));
     81         assertFalse(ArrayUtils.containsAll(new Object[] { A }, new Object[] { null }));
     82     }
     83 
     84     public void testContainsInt() throws Exception {
     85         assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 1));
     86         assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 2));
     87         assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 3));
     88 
     89         assertFalse(ArrayUtils.contains(new int[] { 1, 2, 3 }, 0));
     90         assertFalse(ArrayUtils.contains(new int[] { 1, 2, 3 }, 4));
     91         assertFalse(ArrayUtils.contains(new int[] { }, 2));
     92     }
     93 
     94     public void testAppendInt() throws Exception {
     95         MoreAsserts.assertEquals(new int[] { 1 },
     96                 ArrayUtils.appendInt(null, 1));
     97         MoreAsserts.assertEquals(new int[] { 1 },
     98                 ArrayUtils.appendInt(new int[] { }, 1));
     99         MoreAsserts.assertEquals(new int[] { 1, 2 },
    100                 ArrayUtils.appendInt(new int[] { 1 }, 2));
    101         MoreAsserts.assertEquals(new int[] { 1, 2 },
    102                 ArrayUtils.appendInt(new int[] { 1, 2 }, 1));
    103     }
    104 
    105     public void testRemoveInt() throws Exception {
    106         assertNull(ArrayUtils.removeInt(null, 1));
    107         MoreAsserts.assertEquals(new int[] { },
    108                 ArrayUtils.removeInt(new int[] { }, 1));
    109         MoreAsserts.assertEquals(new int[] { 1, 2, 3, },
    110                 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 4));
    111         MoreAsserts.assertEquals(new int[] { 2, 3, },
    112                 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 1));
    113         MoreAsserts.assertEquals(new int[] { 1, 3, },
    114                 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 2));
    115         MoreAsserts.assertEquals(new int[] { 1, 2, },
    116                 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 3));
    117         MoreAsserts.assertEquals(new int[] { 2, 3, 1 },
    118                 ArrayUtils.removeInt(new int[] { 1, 2, 3, 1 }, 1));
    119     }
    120 
    121     public void testContainsLong() throws Exception {
    122         assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 1));
    123         assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 2));
    124         assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 3));
    125 
    126         assertFalse(ArrayUtils.contains(new long[] { 1, 2, 3 }, 0));
    127         assertFalse(ArrayUtils.contains(new long[] { 1, 2, 3 }, 4));
    128         assertFalse(ArrayUtils.contains(new long[] { }, 2));
    129     }
    130 
    131     public void testAppendLong() throws Exception {
    132         MoreAsserts.assertEquals(new long[] { 1 },
    133                 ArrayUtils.appendLong(null, 1));
    134         MoreAsserts.assertEquals(new long[] { 1 },
    135                 ArrayUtils.appendLong(new long[] { }, 1));
    136         MoreAsserts.assertEquals(new long[] { 1, 2 },
    137                 ArrayUtils.appendLong(new long[] { 1 }, 2));
    138         MoreAsserts.assertEquals(new long[] { 1, 2 },
    139                 ArrayUtils.appendLong(new long[] { 1, 2 }, 1));
    140     }
    141 
    142     public void testRemoveLong() throws Exception {
    143         assertNull(ArrayUtils.removeLong(null, 1));
    144         MoreAsserts.assertEquals(new long[] { },
    145                 ArrayUtils.removeLong(new long[] { }, 1));
    146         MoreAsserts.assertEquals(new long[] { 1, 2, 3, },
    147                 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 4));
    148         MoreAsserts.assertEquals(new long[] { 2, 3, },
    149                 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 1));
    150         MoreAsserts.assertEquals(new long[] { 1, 3, },
    151                 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 2));
    152         MoreAsserts.assertEquals(new long[] { 1, 2, },
    153                 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 3));
    154         MoreAsserts.assertEquals(new long[] { 2, 3, 1 },
    155                 ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1));
    156     }
    157 
    158 }
    159