/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package libcore.libcore.util;

import static libcore.util.ArrayUtils.throwsIfOutOfBounds;

import org.junit.Test;

public class ArrayUtilsTest {
    @Test
    public void testThrowsIfOutOfBounds_passesWhenRangeInsideArray() {
        throwsIfOutOfBounds(10, 2, 6);
    }

    @Test
    public void testThrowsIfOutOfBounds_passesWhenRangeIsWholeArray() {
        throwsIfOutOfBounds(10, 0, 10);
    }

    @Test
    public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtStart() {
        throwsIfOutOfBounds(10, 0, 0);
    }

    @Test
    public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtEnd() {
        throwsIfOutOfBounds(10, 10, 0);
    }

    @Test
    public void testThrowsIfOutOfBounds_passesWhenEmptyArray() {
        throwsIfOutOfBounds(0, 0, 0);
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void testThrowsIfOutOfBounds_failsWhenRangeStartNegative() {
        throwsIfOutOfBounds(10, -1, 5);
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void testThrowsIfOutOfBounds_failsWhenCountNegative() {
        throwsIfOutOfBounds(10, 5, -1);
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void testThrowsIfOutOfBounds_failsWhenRangeStartTooHigh() {
        throwsIfOutOfBounds(10, 11, 0);
    }


    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void testThrowsIfOutOfBounds_failsWhenRangeEndTooHigh() {
        throwsIfOutOfBounds(10, 5, 6);
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void testThrowsIfOutOfBounds_failsWhenLengthNegative() {
        throwsIfOutOfBounds(-1, 0, 0);
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void testThrowsIfOutOfBounds_failsWhenOverflowRangeEndTooHigh() {
        throwsIfOutOfBounds(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
    }
}
