package me.doubledutch.lazyjson.compressor;

import java.io.File;
import org.junit.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.ArrayList;
import java.net.*;
import java.nio.ByteBuffer;

import me.doubledutch.lazyjson.*;


public class CompressionTest{
	@Test
	public void testInOut() throws Exception{
		String str="{\"foo\":42}";
		Compressor c=new Compressor("./ctest",1000,0);
		byte[] out=c.compress(str);
		// System.out.println(out.length+" vs "+str.length());
		String str2=c.decompress(out);
		assertEquals(str,str2);
	}

	@Test
	public void testSet() throws Exception{
		Compressor c=new Compressor("./ctest",1000,3);
		List<byte[]> list=new ArrayList<byte[]>();
		for(int i=0;i<100;i++){
			String str="{\"foo\":"+i+"}";
			list.add(c.compress(str));
		}
		for(int i=0;i<100;i++){
			String str=c.decompress(list.get(i));
			LazyObject obj=new LazyObject(str);
			assertEquals(i,obj.getInt("foo"));			
		}
	}

	@Test
	public void testBadCompression() throws Exception{
		String str="{\"foo\":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]}";
		Compressor c=new Compressor("./ctest",1000,0);
		byte[] out=c.compress(str);
		// System.out.println(out.length+" vs "+str.length());
		String str2=c.decompress(out);
		assertEquals(str,str2);
	}

	@Test
	public void testCommitAndReload() throws Exception{
		String str="{\"foo\":42,\"bar\":\"Hello World!\"}";
		Compressor c=new Compressor("./ctest",10,0);
		byte[] out=c.compress(str);
		c.commit();
		c=new Compressor("./ctest",10,0);
		// System.out.println(out.length+" vs "+str.length());
		String str2=c.decompress(out);
		assertEquals(str,str2);
		File ftest=new File("./ctest.templates");
		ftest.delete();
		ftest=new File("./ctest.dictionary");
		ftest.delete();
	}
}
@@ -548,32 +548,363 @@
 (str,str2);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testDecompress() throws Exception%7B%0A%09%09String str=%22%7B%5C%22foo%5C%22:42%7D%22;%0A%09%09Compressor c=new Compressor(%22./ctest%22,1000,0);%0A%09%09byte%5B%5D out=c.compress(str);%0A%09%09// System.out.println(out.length+%22 vs %22+str.length());%0A%09%09LazyObject obj=c.decompressObject(out);%0A%09%09// String str2=c.decompress(out);%0A%09%09// assertEquals(str,str2);%0A%09%7D%0A%0A
 %09@Test%0A%09public v

Add decompress test