From 1fe15b896ede0882a0970602fea9d2e823b51095 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 29 Apr 2016 22:58:58 +0200 Subject: Implement RLE and its tests --- test/ch/epfl/xblast/RunLengthEncoderTest.java | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 test/ch/epfl/xblast/RunLengthEncoderTest.java (limited to 'test') diff --git a/test/ch/epfl/xblast/RunLengthEncoderTest.java b/test/ch/epfl/xblast/RunLengthEncoderTest.java new file mode 100644 index 0000000..6880b99 --- /dev/null +++ b/test/ch/epfl/xblast/RunLengthEncoderTest.java @@ -0,0 +1,100 @@ +package ch.epfl.xblast; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Pacien TRAN-GIRARD (261948) + */ +public class RunLengthEncoderTest { + + private static final Byte[] SAMPLE_PLAIN = {10, 10, 10, 20, 20, 30, 20, 30, 40, 40, 40, 40, 40, 40}; + private static final Byte[] SAMPLE_ENCODED = {-1, 10, 20, 20, 30, 20, 30, -4, 40}; + private static final RunLengthEncoder.RunLength[] SAMPLE_RUN_LENGTHS = { + new RunLengthEncoder.RunLength(3, (byte) 10), + new RunLengthEncoder.RunLength(2, (byte) 20), + new RunLengthEncoder.RunLength(1, (byte) 30), + new RunLengthEncoder.RunLength(1, (byte) 20), + new RunLengthEncoder.RunLength(1, (byte) 30), + new RunLengthEncoder.RunLength(6, (byte) 40)}; + + @Test + public void isSampleEncoded() { + List encoded = RunLengthEncoder.encode(Arrays.asList(SAMPLE_PLAIN)); + assertEquals(Arrays.asList(SAMPLE_ENCODED), encoded); + } + + @Test + public void isSampleDecoded() { + List decoded = RunLengthEncoder.decode(Arrays.asList(SAMPLE_ENCODED)); + assertEquals(Arrays.asList(SAMPLE_PLAIN), decoded); + } + + @Test + public void isSampleRunLengthListDecoded() { + List rll = RunLengthEncoder.decodeRunLengths(Arrays.asList(SAMPLE_ENCODED)); + assertEquals(Arrays.asList(SAMPLE_RUN_LENGTHS), RunLengthEncoder.collapseRunLengths(rll)); + } + + @Test + public void isSampleRunLengthListCollapsed() { + final RunLengthEncoder.RunLength[] runLengths = { + new RunLengthEncoder.RunLength(1, (byte) 10), + new RunLengthEncoder.RunLength(2, (byte) 20), + new RunLengthEncoder.RunLength(3, (byte) 20)}; + final RunLengthEncoder.RunLength[] expectedCollapsed = { + new RunLengthEncoder.RunLength(1, (byte) 10), + new RunLengthEncoder.RunLength(5, (byte) 20)}; + + List collapsed = RunLengthEncoder.collapseRunLengths(Arrays.asList(runLengths)); + assertEquals(Arrays.asList(expectedCollapsed), collapsed); + } + + @Test + public void isSampleRunLengthExpanded() { + final RunLengthEncoder.RunLength rl = new RunLengthEncoder.RunLength(5, (byte) 0); + final Byte[] expectedExpanded = {0, 0, 0, 0, 0}; + + assertEquals(Arrays.asList(expectedExpanded), rl.expand()); + } + + @Test + public void isSampleLongRunLengthSplit() { + final RunLengthEncoder.RunLength longRL = new RunLengthEncoder.RunLength(200, (byte) 0); + final RunLengthEncoder.RunLength[] expectedSplit = { + new RunLengthEncoder.RunLength(130, (byte) 0), + new RunLengthEncoder.RunLength(70, (byte) 0)}; + + assertEquals(Arrays.asList(expectedSplit), longRL.split()); + } + + @Test + public void isSampleRunLengthEncoded() { + final RunLengthEncoder.RunLength rl = new RunLengthEncoder.RunLength(5, (byte) 0); + final Byte[] expectedEncoded = {-3, 0}; + + assertEquals(Arrays.asList(expectedEncoded), rl.encode()); + } + + @Test + public void isSampleShortRunLengthEncoded() { + final RunLengthEncoder.RunLength rl = new RunLengthEncoder.RunLength(2, (byte) 0); + final Byte[] expectedEncoded = {0, 0}; + + assertEquals(Arrays.asList(expectedEncoded), rl.encode()); + } + + @Test + public void isSampleRunLengthDecoded() { + final Byte[] encoded = {-3, 0}; + final RunLengthEncoder.RunLength expectedDecoded = new RunLengthEncoder.RunLength(5, (byte) 0); + + RunLengthEncoder.RunLength decoded = new RunLengthEncoder.RunLength(encoded[0], encoded[1]); + assertEquals(expectedDecoded, decoded); + } + +} -- cgit v1.2.3