// 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 byte randomValueForByte(final boolean unsigned, final int size) {
//     final byte value;
//     if (unsigned) {
//         value = (byte) (current().nextInt() >>> (Integer.SIZE - size));
//         assertTrue(value >= 0);
//     } else {
//         value = (byte) (current().nextInt() >> (Integer.SIZE - size));
//         assertEquals(value >> size, value >= 0 ? 0 : -1);
//     }
//     return value;
// }


import static com.github.jinahya.bit.io.BitIoTests.randomSizeForByte;
import static com.github.jinahya.bit.io.BitIoTests.randomValueForByte;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.ThreadLocalRandom.current;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
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 javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.io.IOException;
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%
 */




/**
 * An abstract class for testing subclasses of {@link BitOutput}.
 *
 * @param <T> bit output type parameter
 * @see BitInputTest
 * @see AbstractBitOutputTest
 */
@Slf4j
abstract class BitOutputTest<T extends BitOutput> {

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

    /**
     * Creates a new instance with given bit output class.
     *
     * @param bitOutputClass the bit output class to test.
     * @see #bitOutputClass
     */
    BitOutputTest(final Class<T> bitOutputClass) {
        super();
        this.bitOutputClass = requireNonNull(bitOutputClass, "bitOutputClass is null");
    }

    // -----------------------------------------------------------------------------------------------------------------
    @BeforeEach
    void select() {
        bitOutput = bitOutputInstance.select(bitOutputClass).get();
    }

    @AfterEach
    void align() throws IOException {
        final long bits = bitOutput.align(current().nextInt(1, 16));
        assertTrue(bits >= 0L);
    }

    // --------------------------------------------------------------------------------------------------------- boolean

    /**
     * Tests {@link BitOutput#writeBoolean(boolean)}.
     *
     * @throws IOException if an I/O error occurs.
     */
    @RepeatedTest(8)
    void testWriteBoolean() throws IOException {
        bitOutput.writeBoolean(current().nextBoolean());
    }

    // ------------------------------------------------------------------------------------------------------------ byte
    @RepeatedTest(8)
    void testWriteByte() 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 byte randomValueForByte(final boolean unsigned, final int size) {
//     final byte value;
//     if (unsigned) {
//         value = (byte) (current().nextInt() >>> (Integer.SIZE - size));
//         assertTrue(value >= 0);
//     } else {
//         value = (byte) (current().nextInt() >> (Integer.SIZE - size));
//         assertEquals(value >> size, value >= 0 ? 0 : -1);
//     }
//     return value;
// }


// Path: src/test/java/com/github/jinahya/bit/io/BitOutputTest.java
import static com.github.jinahya.bit.io.BitIoTests.randomSizeForByte;
import static com.github.jinahya.bit.io.BitIoTests.randomValueForByte;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.ThreadLocalRandom.current;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
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 javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.io.IOException;

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




/**
 * An abstract class for testing subclasses of {@link BitOutput}.
 *
 * @param <T> bit output type parameter
 * @see BitInputTest
 * @see AbstractBitOutputTest
 */
@Slf4j
abstract class BitOutputTest<T extends BitOutput> {

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

    /**
     * Creates a new instance with given bit output class.
     *
     * @param bitOutputClass the bit output class to test.
     * @see #bitOutputClass
     */
    BitOutputTest(final Class<T> bitOutputClass) {
        super();
        this.bitOutputClass = requireNonNull(bitOutputClass, "bitOutputClass is null");
    }

    // -----------------------------------------------------------------------------------------------------------------
    @BeforeEach
    void select() {
        bitOutput = bitOutputInstance.select(bitOutputClass).get();
    }

    @AfterEach
    void align() throws IOException {
        final long bits = bitOutput.align(current().nextInt(1, 16));
        assertTrue(bits >= 0L);
    }

    // --------------------------------------------------------------------------------------------------------- boolean

    /**
     * Tests {@link BitOutput#writeBoolean(boolean)}.
     *
     * @throws IOException if an I/O error occurs.
     */
    @RepeatedTest(8)
    void testWriteBoolean() throws IOException {
        bitOutput.writeBoolean(current().nextBoolean());
    }

    // ------------------------------------------------------------------------------------------------------------ byte
    @RepeatedTest(8)
    void testWriteByte() throws IOException {
        final boolean unsigned = current().nextBoolean();
        final int size = randomSizeForByte(unsigned);