/src/test/java/com/wet/wired/jrc/frame/compression/RunLengthThenGZCompressorTest.java
Java | 81 lines | 56 code | 25 blank | 0 comment | 3 complexity | 048eca2260a6b9a38dbb8a21f43c719a MD5 | raw file
1package com.wet.wired.jrc.frame.compression; 2 3import java.awt.Canvas; 4import java.awt.Image; 5import java.io.ByteArrayInputStream; 6import java.io.ByteArrayOutputStream; 7import java.io.IOException; 8 9import junit.framework.TestCase; 10 11import com.wet.wired.jrc.frame.Frame; 12 13public class RunLengthThenGZCompressorTest extends TestCase { 14 15 public Frame blankFrame; 16 public Frame blueFrame; 17 public Frame newFrame; 18 19 public void setUp() { 20 blankFrame = new Frame(100,100); 21 blankFrame.setAllPixels(0xFF000000); 22 23 blueFrame = new Frame(100,100); 24 blueFrame.setAllPixels(0xFF0000FF); 25 26 newFrame = new Frame(100,100); 27 } 28 29 public void testCompressedSizeSmallerThanUncompressedSize() throws IOException { 30 RunLengthThenGZCompressor rlgz = new RunLengthThenGZCompressor(); 31 32 Frame lastFrame = blankFrame; 33 Frame currentFrame = blueFrame; 34 ByteArrayOutputStream out = new ByteArrayOutputStream(); 35 36 rlgz.compressFrame(lastFrame, currentFrame, out); 37 38 if(out.toByteArray().length > currentFrame.getDataSize()*4) { 39 fail("Compressed size greater than original size"); 40 } 41 } 42 43 public void testCompressedSizeEqualsReportedSize() throws IOException { 44 RunLengthThenGZCompressor rlgz = new RunLengthThenGZCompressor(); 45 46 Frame lastFrame = blankFrame; 47 Frame currentFrame = blueFrame; 48 ByteArrayOutputStream out = new ByteArrayOutputStream(); 49 50 int size = rlgz.compressFrame(lastFrame, currentFrame, out); 51 52 assertEquals(out.toByteArray().length, size); 53 } 54 55 public void testDecompressedFrameSameAsCompressedFrame() throws IOException { 56 RunLengthThenGZCompressor rlgz = new RunLengthThenGZCompressor(); 57 58 Frame lastFrame = blankFrame; 59 Frame currentFrame = blueFrame; 60 ByteArrayOutputStream out = new ByteArrayOutputStream(); 61 62 int size = rlgz.compressFrame(lastFrame, currentFrame, out); 63 64 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 65 rlgz.decompressFrame(in, size, lastFrame, newFrame); 66 67 assertEquals(currentFrame.getDataSize(),newFrame.getDataSize()); 68 69 for(int i=0;i<currentFrame.getDataSize();i++) { 70 if(currentFrame.getData()[i]!=newFrame.getData()[i]) { 71 fail("Decompressed data "+currentFrame.getData()[i]+" did not match "+newFrame.getData()[i]+" at index "+i); 72 } 73 } 74 } 75 76 public static Image loadImage(String fileName) { 77 78 return new Canvas().getToolkit().createImage( fileName ); 79 80 } 81}