package com.atlassian.bitbucket.mesh.util;

import java.util.List;

import static org.apache.commons.lang3.RandomStringUtils.randomAscii;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

abstract class AbstractByteSegmentTest {

    static final int MAX_BUFFER_SIZE = 4096;
    static final byte[] DELIMITER_NULL = new byte[]{(byte) 0x0};

    static void assertSegments(List<byte[]> expected, List<byte[]> actual) {
        assertEquals("Results must match testSegments", expected.size(), actual.size());

        // Compare each segment to verify content matches
        for (int index = 0; index < expected.size(); index++) {
            byte[] expectedValue = expected.get(index);
            byte[] computedValue = actual.get(index);

            assertArrayEquals(
                    String.format("Segment value [%s] [%d] did not match expected value [%s] [%d] from segment %d.",
                            new String(computedValue), computedValue.length,
                            new String(expectedValue), expectedValue.length,
                            index),
                    computedValue, expectedValue);
        }
    }

    static byte[] bytes(int count) {
        return randomAscii(count).getBytes();
    }

    static byte[] getDelimiter() {
        return DELIMITER_NULL;
    }
}
