Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 package com.android.internal.util;
     17 
     18 import android.test.suitebuilder.annotation.SmallTest;
     19 import junit.framework.TestCase;
     20 
     21 import java.util.ArrayList;
     22 import java.util.Arrays;
     23 import java.util.Collections;
     24 
     25 public class ArrayUtilsTest extends TestCase {
     26 
     27     @SmallTest
     28     public void testUnstableRemoveIf() throws Exception {
     29         java.util.function.Predicate<Object> isNull = new java.util.function.Predicate<Object>() {
     30             @Override
     31             public boolean test(Object o) {
     32                 return o == null;
     33             }
     34         };
     35 
     36         final Object a = new Object();
     37         final Object b = new Object();
     38         final Object c = new Object();
     39 
     40         ArrayList<Object> collection = null;
     41         assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
     42 
     43         collection = new ArrayList<>();
     44         assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
     45 
     46         collection = new ArrayList<>(Collections.singletonList(a));
     47         assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
     48         assertEquals(1, collection.size());
     49         assertTrue(collection.contains(a));
     50 
     51         collection = new ArrayList<>(Collections.singletonList(null));
     52         assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
     53         assertEquals(0, collection.size());
     54 
     55         collection = new ArrayList<>(Arrays.asList(a, b));
     56         assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
     57         assertEquals(2, collection.size());
     58         assertTrue(collection.contains(a));
     59         assertTrue(collection.contains(b));
     60 
     61         collection = new ArrayList<>(Arrays.asList(a, null));
     62         assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
     63         assertEquals(1, collection.size());
     64         assertTrue(collection.contains(a));
     65 
     66         collection = new ArrayList<>(Arrays.asList(null, a));
     67         assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
     68         assertEquals(1, collection.size());
     69         assertTrue(collection.contains(a));
     70 
     71         collection = new ArrayList<>(Arrays.asList(null, null));
     72         assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
     73         assertEquals(0, collection.size());
     74 
     75         collection = new ArrayList<>(Arrays.asList(a, b, c));
     76         assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
     77         assertEquals(3, collection.size());
     78         assertTrue(collection.contains(a));
     79         assertTrue(collection.contains(b));
     80         assertTrue(collection.contains(c));
     81 
     82         collection = new ArrayList<>(Arrays.asList(a, b, null));
     83         assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
     84         assertEquals(2, collection.size());
     85         assertTrue(collection.contains(a));
     86         assertTrue(collection.contains(b));
     87 
     88         collection = new ArrayList<>(Arrays.asList(a, null, b));
     89         assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
     90         assertEquals(2, collection.size());
     91         assertTrue(collection.contains(a));
     92         assertTrue(collection.contains(b));
     93 
     94         collection = new ArrayList<>(Arrays.asList(null, a, b));
     95         assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
     96         assertEquals(2, collection.size());
     97         assertTrue(collection.contains(a));
     98         assertTrue(collection.contains(b));
     99 
    100         collection = new ArrayList<>(Arrays.asList(a, null, null));
    101         assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
    102         assertEquals(1, collection.size());
    103         assertTrue(collection.contains(a));
    104 
    105         collection = new ArrayList<>(Arrays.asList(null, null, a));
    106         assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
    107         assertEquals(1, collection.size());
    108         assertTrue(collection.contains(a));
    109 
    110         collection = new ArrayList<>(Arrays.asList(null, a, null));
    111         assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
    112         assertEquals(1, collection.size());
    113         assertTrue(collection.contains(a));
    114 
    115         collection = new ArrayList<>(Arrays.asList(null, null, null));
    116         assertEquals(3, ArrayUtils.unstableRemoveIf(collection, isNull));
    117         assertEquals(0, collection.size());
    118     }
    119 }
    120