package ru.r2cloud.jradio.util;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class DeinterleaveTest {

	@Test
	public void testDeinterleaveBits() {
		byte[] data = new byte[] { 0b0010_0101, 0b0101_1100 };
		byte[] result = Deinterleave.deinterleaveBits(data, 8, data.length);
		assertEquals(data.length, result.length);
		assertEquals(0b0100_0010, result[0]);
		assertEquals(0b0011_1110, result[1]);
	}

	@Test
	public void testDeinterleaveFecBits() {
		byte[] data = new byte[] { 0b0010_0101, 0b0101_1100 };
		byte[] result = Deinterleave.deinterleaveBits(data, 4, 3);
		assertEquals(3, result.length);
		assertEquals(0b0000_0001, result[0]);
		assertEquals(0b0000_0010, result[1]);
		assertEquals(0b0000_1101, result[2]);
	}

	@Test
	public void testDeinterleaveUnpacked() {
		byte[] data = new byte[] { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
				1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,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