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