Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2014 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 android.support.v7.widget;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.support.test.filters.SmallTest;
     24 
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 import org.junit.runners.JUnit4;
     29 
     30 import java.util.ArrayList;
     31 import java.util.Collections;
     32 import java.util.HashSet;
     33 import java.util.Set;
     34 
     35 @SmallTest
     36 @RunWith(JUnit4.class)
     37 public class BucketTest {
     38 
     39     ChildHelper.Bucket mBucket;
     40 
     41     ArrayList<Integer> mArr;
     42 
     43     Set<Integer> mSet;
     44 
     45     int max = 0;
     46 
     47     @Before
     48     public void setUp() throws Exception {
     49         mBucket = new ChildHelper.Bucket();
     50         mArr = new ArrayList<>();
     51         Collections.addAll(mArr, 0, 1, 2, 3, 4, 5, 6, 10, 12, 13, 21, 22, 122, 14, 44, 29, 205, 19);
     52         for (int i = 1; i < 4; i++) {
     53             mArr.add(i * (ChildHelper.Bucket.BITS_PER_WORD - 1));
     54             mArr.add(i * (ChildHelper.Bucket.BITS_PER_WORD));
     55             mArr.add(i * (ChildHelper.Bucket.BITS_PER_WORD + 1));
     56             mArr.add(i * ChildHelper.Bucket.BITS_PER_WORD - 1);
     57             mArr.add(i * ChildHelper.Bucket.BITS_PER_WORD);
     58             mArr.add(i * ChildHelper.Bucket.BITS_PER_WORD + 1);
     59         }
     60 
     61         mSet = new HashSet<>();
     62         max = 0;
     63         for (int i = mArr.size() - 1; i >= 0; i--) {
     64             if (!mSet.add(mArr.get(i))) {
     65                 mArr.remove(i);
     66             }
     67             max = Math.max(max, i);
     68         }
     69     }
     70 
     71 
     72     @Test
     73     public void setClear() {
     74         for (int i : mArr) {
     75             mBucket.set(i);
     76             assertTrue(mBucket.get(i));
     77         }
     78         for (int i = 0; i < max + 100; i++) {
     79             assertEquals(mBucket.get(i), mSet.contains(i));
     80         }
     81 
     82         for (int i : mArr) {
     83             mBucket.clear(i);
     84             assertFalse(mBucket.get(i));
     85         }
     86 
     87         for (int i = 0; i < max + 100; i++) {
     88             assertFalse(mBucket.get(i));
     89         }
     90     }
     91 
     92 
     93     @Test
     94     public void remove() {
     95         for (int i : mArr) {
     96             mBucket.reset();
     97             for (int j : mArr) {
     98                 mBucket.set(j);
     99             }
    100             mBucket.remove(i);
    101             for (int j : mArr) {
    102                 if (i == j) {
    103                     continue;
    104                 }
    105                 if (j < i) {
    106                     assertTrue(mBucket.get(j));
    107                 } else {
    108                     assertEquals(mSet.contains(j + 1), mBucket.get(j));
    109                 }
    110             }
    111         }
    112     }
    113 
    114     @Test
    115     public void insert() {
    116         for (int i : mArr) {
    117             for (boolean val : new boolean[]{true, false}) {
    118                 mBucket.reset();
    119                 for (int j : mArr) {
    120                     mBucket.set(j);
    121                 }
    122                 mBucket.insert(i, val);
    123                 assertEquals(mBucket.get(i), val);
    124                 for (int j : mArr) {
    125                     if (j < i) {
    126                         assertTrue(mBucket.get(j));
    127                     } else if (j == i) {
    128                         assertEquals(mBucket.get(j), val);
    129                     } else {
    130                         assertEquals(mSet.contains(j - 1), mBucket.get(j));
    131                         assertTrue(mBucket.get(j + 1));
    132                     }
    133                 }
    134             }
    135         }
    136     }
    137 
    138 
    139     @Test
    140     public void countOnesBefore() {
    141         assertEquals(mBucket.countOnesBefore(0), 0);
    142         for (int i : mArr) {
    143             mBucket.set(i);
    144             max = Math.max(i, max);
    145         }
    146         assertEquals(mBucket.countOnesBefore(0), 0);
    147         for (int i = 0; i < max + 200; i++) {
    148             int count = 0;
    149             for (int j : mArr) {
    150                 if (j < i) {
    151                     count++;
    152                 }
    153             }
    154             assertEquals(count, mBucket.countOnesBefore(i));
    155         }
    156     }
    157 }
    158