// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForByte(final boolean unsigned) {
//     return requireValidSizeByte(unsigned, current().nextInt(1, Byte.SIZE + (unsigned ? 0 : 1)));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForChar() {
//     return requireValidSizeChar(current().nextInt(1, Character.SIZE));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForInt(final boolean unsigned) {
//     return requireValidSizeInt(unsigned, current().nextInt(1, Integer.SIZE + (unsigned ? 0 : 1)));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForLong(final boolean unsigned) {
//     return requireValidSizeLong(unsigned, current().nextInt(1, Long.SIZE + (unsigned ? 0 : 1)));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForShort(final boolean unsigned) {
//     return requireValidSizeShort(unsigned, current().nextInt(1, Short.SIZE + (unsigned ? 0 : 1)));
// }


import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForByte;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForChar;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForInt;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForLong;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForShort;
import static java.util.concurrent.ThreadLocalRandom.current;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.lenient;
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%
 */




/**
 * A class for unit-testing {@link AbstractBitInput} with a spy.
 *
 * @author Jin Kwon &lt;onacit_at_gmail.com&gt;
 * @see AbstractBitOutputSpyTest
 */
@ExtendWith({MockitoExtension.class})
@Slf4j
final class AbstractBitInputSpyTest {

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

    // -----------------------------------------------------------------------------------------------------------------
    @AfterEach
    void alignAfterEach() throws IOException {
        bitInput.align(current().nextInt(1, 8));
    }

    // ------------------------------------------------------------------------------------------------------------ byte

    /**
     * Tests {@link AbstractBitInput#readByte(boolean, int)} method.
     *
     * @throws IOException if an I/O error occurs.
     */
    @RepeatedTest(8)
    void testReadByte() throws IOException {
        final boolean unsigned = current().nextBoolean();
        final int size = randomSizeForByte(unsigned);
        final byte value = bitInput.readByte(unsigned, size);
        if (unsigned) {
            assertEquals(0, value >> size);
        } else {
            if (value >= 0) {
                assertEquals(0, value >> (size - 1));
            } else {
                assertEquals(-1, value >> (size - 1));
            }
        }
    }

    // ----------------------------------------------------------------------------------------------------------- short
    @RepeatedTest(8)
    void testReadShort() throws IOException {
        final boolean unsigned = current().nextBoolean();
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForByte(final boolean unsigned) {
//     return requireValidSizeByte(unsigned, current().nextInt(1, Byte.SIZE + (unsigned ? 0 : 1)));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForChar() {
//     return requireValidSizeChar(current().nextInt(1, Character.SIZE));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForInt(final boolean unsigned) {
//     return requireValidSizeInt(unsigned, current().nextInt(1, Integer.SIZE + (unsigned ? 0 : 1)));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForLong(final boolean unsigned) {
//     return requireValidSizeLong(unsigned, current().nextInt(1, Long.SIZE + (unsigned ? 0 : 1)));
// }
// 
// Path: src/test/java/com/github/jinahya/bit/io/BitIoTests.java
// static int randomSizeForShort(final boolean unsigned) {
//     return requireValidSizeShort(unsigned, current().nextInt(1, Short.SIZE + (unsigned ? 0 : 1)));
// }


// Path: src/test/java/com/github/jinahya/bit/io/AbstractBitInputSpyTest.java
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForByte;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForChar;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForInt;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForLong;
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForShort;
import static java.util.concurrent.ThreadLocalRandom.current;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.lenient;

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%
 */




/**
 * A class for unit-testing {@link AbstractBitInput} with a spy.
 *
 * @author Jin Kwon &lt;onacit_at_gmail.com&gt;
 * @see AbstractBitOutputSpyTest
 */
@ExtendWith({MockitoExtension.class})
@Slf4j
final class AbstractBitInputSpyTest {

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

    // -----------------------------------------------------------------------------------------------------------------
    @AfterEach
    void alignAfterEach() throws IOException {
        bitInput.align(current().nextInt(1, 8));
    }

    // ------------------------------------------------------------------------------------------------------------ byte

    /**
     * Tests {@link AbstractBitInput#readByte(boolean, int)} method.
     *
     * @throws IOException if an I/O error occurs.
     */
    @RepeatedTest(8)
    void testReadByte() throws IOException {
        final boolean unsigned = current().nextBoolean();
        final int size = randomSizeForByte(unsigned);
        final byte value = bitInput.readByte(unsigned, size);
        if (unsigned) {
            assertEquals(0, value >> size);
        } else {
            if (value >= 0) {
                assertEquals(0, value >> (size - 1));
            } else {
                assertEquals(-1, value >> (size - 1));
            }
        }
    }

    // ----------------------------------------------------------------------------------------------------------- short
    @RepeatedTest(8)
    void testReadShort() throws IOException {
        final boolean unsigned = current().nextBoolean();
        final int size = randomSizeForShort(unsigned);