package com.github.jinahya.bit.io;

/*-
 * #%L
 * bit-io
 * %%
 * Copyright (C) 2014 - 2019 Jinahya, Inc.
 * %%
 * 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.
 * #L%
 */

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;

import static java.util.concurrent.ThreadLocalRandom.current;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

@ExtendWith({MockitoExtension.class})
@Slf4j
public class AbstractBitInputSpyTest {

    // -----------------------------------------------------------------------------------------------------------------
    @BeforeEach
    void stubRead() throws IOException {
        when(bitInput.read()).thenReturn(current().nextInt(0, 256));
    }

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Tests {@link BitInput#readByte(boolean, int)} method.
     *
     * @throws IOException if an I/O error occurs.
     */
    @RepeatedTest(8)
    public void testReadByte() throws IOException {
        final boolean unsigned = current().nextBoolean();
        final int size = current().nextInt(1, Byte.SIZE + (unsigned ? 0 : 1));
        final byte value = bitInput.readByte(unsigned, size);
        if (unsigned) {
            assertTrue(value >= 0);
        }
    }

    /**
     * Tests {@link BitInput#readShort(boolean, int)} method.
     *
     * @throws IOException if an I/O error occurs.
     */
    @RepeatedTest(8)
    public void testReadShort() throws IOException {
        final boolean unsigned = current().nextBoolean();
        final int size = current().nextInt(1, Short.SIZE + (unsigned ? 0 : 1));
        final short value = bitInput.readShort(unsigned, size);
        if (unsigned) {
            assertTrue(value >= 0);
        }
    }

    // -----------------------------------------------------------------------------------------------------------------
    @Spy
    private AbstractBitInput bitInput;
}

@@ -842,16 +842,174 @@
 ndWith;%0A
+import org.junit.jupiter.params.ParameterizedTest;%0Aimport org.junit.jupiter.params.provider.Arguments;%0Aimport org.junit.jupiter.params.provider.MethodSource;%0A
 import o
@@ -1024,16 +1024,16 @@
 to.Spy;%0A
-
 import o
@@ -1099,24 +1099,91 @@
 IOException;
+%0Aimport java.util.stream.IntStream;%0Aimport java.util.stream.Stream;
 %0A%0Aimport sta
@@ -1238,69 +1238,8 @@
 nt;%0A
-import static org.junit.jupiter.api.Assertions.assertEquals;%0A
 impo
@@ -1419,16 +1419,292 @@
 Test %7B%0A%0A
+    // -----------------------------------------------------------------------------------------------------------------%0A    private static Stream%3CArguments%3E sourceUnsigned() %7B%0A        return IntStream.range(0, 16).mapToObj(i -%3E Arguments.of(current().nextBoolean()));%0A    %7D%0A%0A
     // -
@@ -2895,32 +2895,32 @@
 gned ? 0 : 1));%0A
-
         final sh
@@ -3036,32 +3036,565 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Tests %7B@link BitInput#readShort(boolean, int)%7D method.%0A     *%0A     * @param unsigned a flag for unsigned.%0A     * @throws IOException if an I/O error occurs.%0A     */%0A    @MethodSource(%7B%22sourceUnsigned%22%7D)%0A    @ParameterizedTest%0A    public void testReadInt(final boolean unsigned) throws IOException %7B%0A        final int size = current().nextInt(1, Integer.SIZE + (unsigned ? 0 : 1));%0A        final int value = bitInput.readInt(unsigned, size);%0A        if (unsigned) %7B%0A            assertTrue(value %3E= 0);%0A        %7D%0A    %7D%0A%0A
     // ---------

Add a test case