1/*
2 * Copyright (C) 2011 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
17package com.android.inputmethod.compat;
18
19import android.test.AndroidTestCase;
20
21public class ArraysCompatUtilsTests extends AndroidTestCase {
22    // See {@link tests.api.java.util.ArraysTest}.
23    private static final int ARRAY_SIZE = 100;
24    private final int[] mIntArray = new int[ARRAY_SIZE];
25
26    @Override
27    protected void setUp() throws Exception {
28        super.setUp();
29        for (int counter = 0; counter < ARRAY_SIZE; counter++) {
30            mIntArray[counter] = counter;
31        }
32    }
33
34    public void testEmptyArray() {
35        final int index = ArraysCompatUtils.binarySearch(mIntArray, 0, 0, 0);
36        assertEquals("empty", ~0, index);
37        final int compat = ArraysCompatUtils.compatBinarySearch(mIntArray, 0, 0, 0);
38        assertEquals("empty compat", ~0, compat);
39    }
40
41    public void testEmptyRangeArray() {
42        final int mid = ARRAY_SIZE / 3;
43        final int index = ArraysCompatUtils.binarySearch(mIntArray, mid, mid, 1);
44        assertEquals("empty", ~mid, index);
45        final int compat = ArraysCompatUtils.compatBinarySearch(mIntArray, mid, mid, 1);
46        assertEquals("empty compat", ~mid, compat);
47    }
48
49    public void testFind() {
50        for (int counter = 0; counter < ARRAY_SIZE; counter++) {
51            final int index = ArraysCompatUtils.binarySearch(mIntArray, 0, ARRAY_SIZE, counter);
52            assertEquals("found", counter, index);
53        }
54        for (int counter = 0; counter < ARRAY_SIZE; counter++) {
55            final int compat = ArraysCompatUtils.compatBinarySearch(
56                    mIntArray, 0, ARRAY_SIZE, counter);
57            assertEquals("found compat", counter, compat);
58        }
59    }
60
61    public void testFindNegative() {
62        final int offset = ARRAY_SIZE / 2;
63        for (int counter = 0; counter < ARRAY_SIZE; counter++) {
64            mIntArray[counter] -= offset;
65        }
66        for (int counter = 0; counter < ARRAY_SIZE; counter++) {
67            final int index = ArraysCompatUtils.binarySearch(
68                    mIntArray, 0, ARRAY_SIZE, counter - offset);
69            assertEquals("found", counter, index);
70        }
71        for (int counter = 0; counter < ARRAY_SIZE; counter++) {
72            final int compat = ArraysCompatUtils.compatBinarySearch(
73                    mIntArray, 0, ARRAY_SIZE, counter - offset);
74            assertEquals("found compat", counter, compat);
75        }
76    }
77
78    public void testNotFountAtTop() {
79        final int index = ArraysCompatUtils.binarySearch(mIntArray, 0, ARRAY_SIZE, -1);
80        assertEquals("not found top", ~0, index);
81        final int compat = ArraysCompatUtils.compatBinarySearch(
82                    mIntArray, 0, ARRAY_SIZE, -1);
83        assertEquals("not found top compat", ~0, compat);
84    }
85
86    public void testNotFountAtEnd() {
87        final int index = ArraysCompatUtils.binarySearch(mIntArray, 0, ARRAY_SIZE, ARRAY_SIZE);
88        assertEquals("not found end", ~ARRAY_SIZE, index);
89        final int compat = ArraysCompatUtils.compatBinarySearch(
90                    mIntArray, 0, ARRAY_SIZE, ARRAY_SIZE);
91        assertEquals("not found end compat", ~ARRAY_SIZE, compat);
92    }
93
94    public void testNotFountAtMid() {
95        final int mid = ARRAY_SIZE / 3;
96        mIntArray[mid] = mIntArray[mid + 1];
97        final int index = ArraysCompatUtils.binarySearch(mIntArray, 0, ARRAY_SIZE, mid);
98        assertEquals("not found mid", ~mid, index);
99        final int compat = ArraysCompatUtils.compatBinarySearch(
100                    mIntArray, 0, ARRAY_SIZE, mid);
101        assertEquals("not found mid compat", ~mid, compat);
102    }
103}
104