1 package com.diasparsoftware.gsbase.test; 2 3 import java.util.*; 4 5 import junit.framework.*; 6 import junitx.framework.ArrayAssert; 7 8 import com.diasparsoftware.gsbase.StreamUtil; 9 10 public class FlattenByteSegmentTest extends TestCase { 11 private byte[] expectedByteArray; 12 private List byteSegments; 13 private int eachSegmentButLastLength; 14 private int lastSegmentLength; 15 16 public FlattenByteSegmentTest( 17 String testName, 18 byte[] expectedByteArray, 19 List byteSegments, 20 int eachSegmentButLastLength, 21 int lastSegmentLength) { 22 23 super(testName); 24 25 this.expectedByteArray = expectedByteArray; 26 this.byteSegments = byteSegments; 27 this.eachSegmentButLastLength = eachSegmentButLastLength; 28 this.lastSegmentLength = lastSegmentLength; 29 } 30 31 public static Test suite() { 32 TestSuite suite = new TestSuite("Flatten Byte Segment Tests"); 33 34 suite.addTest( 35 new FlattenByteSegmentTest( 36 "empty", 37 new byte[0], 38 Collections.EMPTY_LIST, 39 0, 40 0)); 41 42 suite.addTest( 43 new FlattenByteSegmentTest( 44 "single, stripped segment", 45 new byte[] { 12 }, 46 Collections.singletonList(new byte[] { 12 }), 47 0, 48 1)); 49 50 byte[] singlePaddedRow = new byte[] { 12, 0 }; 51 52 suite.addTest( 53 new FlattenByteSegmentTest( 54 "single, padded segment", 55 new byte[] { 12 }, 56 Collections.singletonList(singlePaddedRow), 57 0, 58 1)); 59 60 byte[] fullRow = new byte[] { 12, 13, 12, 13, 12, 13 }; 61 byte[] fullRowThenSinglePaddedRow = 62 new byte[] { 12, 13, 12, 13, 12, 13, 12 }; 63 64 suite.addTest( 65 new FlattenByteSegmentTest( 66 "full segment, then single padded segment", 67 fullRowThenSinglePaddedRow, 68 Arrays.asList( 69 new Object[] { fullRow, singlePaddedRow }), 70 fullRow.length, 71 1)); 72 73 byte[] twoFullRowsThenSinglePaddedRow = 74 new byte[] { 75 12, 76 13, 77 12, 78 13, 79 12, 80 13, 81 12, 82 13, 83 12, 84 13, 85 12, 86 13, 87 12 }; 88 89 suite.addTest( 90 new FlattenByteSegmentTest( 91 "2 full segments, then single padded segment", 92 twoFullRowsThenSinglePaddedRow, 93 Arrays.asList( 94 new Object[] { fullRow, fullRow, singlePaddedRow }), 95 fullRow.length, 96 1)); 97 98 return suite; 99 } 100 101 protected void runTest() { 102 ArrayAssert.assertEquals( 103 expectedByteArray, 104 StreamUtil.flattenByteSegments( 105 byteSegments, 106 eachSegmentButLastLength, 107 lastSegmentLength)); 108 } 109 }