package tests;


import java.io.IOException;
import java.util.Random;
import base.DataMatrix;
import junit.framework.TestCase;

/** Creates several DataMatrix objects, writes them to temp dir and re-reads them */
public class DataMatrixReadWriteTest extends TestCase {
	
	
	public void testDataMatrixReadWrite() throws IOException {
		try{
			Random randGen = new Random();
			
			String fileName = System.getProperty("java.io.tmpdir") + "/dataMatrixIntTest.h5";
			System.out.println(fileName);
			
			/* Setup 3D matrix of ints with different types of coordinates */
			byte xvec[] = new byte[]{ 10, 11, 12, 13, 14, 15 };
			double yvec[] = new double[]{ 0.00, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07 };
			String zvec[] = new String[]{ "first", "second", "third" };
		
			int intTest[][][] = new int[6][8][3];
			
			for(int i=0;i<6;i++){
				for(int j=0;j<8;j++){
					for(int k=0;k<3;k++){
						intTest[i][j][k] = randGen.nextInt(65536);						
					}
				}
			}
			
			/* Create DataMatrix Object and write to disk */
			DataMatrix intDM = new DataMatrix(intTest); //create rank 3 matrix			
			intDM.setCoords(2, zvec);
			intDM.setCoords(1, yvec);
			intDM.setCoords(0, xvec);
			intDM.disableCompression();
			intDM.writeToHD5(fileName);
			
			/* Read it back */
			DataMatrix reReadDM = new DataMatrix(fileName);

			//check z coords
			String z[] = (String[])reReadDM.getCoords(2);
			for(int i=0;i<z.length;i++)
				assertTrue("Integer Matrix z Coord Readback", z[i].equals(zvec[i]));

			//check y coords
			double y[] = (double[])reReadDM.getCoords(1);
			for(int i=0;i<y.length;i++)
				assertTrue("Integer Matrix y Coord Readback", y[i] == yvec[i]);

			//check x coords
			byte x[] = (byte[])reReadDM.getCoords(0);
			for(int i=0;i<x.length;i++)
				assertTrue("Integer Matrix x Coord Readback", x[i] == xvec[i]);
			
			//check data
			int iData[][][] = (int[][][])reReadDM.getData();
			assertTrue("Integer Matrix Readback rank check", reReadDM.getRank() == 3);
			
			for(int i=0;i<iData.length;i++)
				for(int j=0;j<iData[i].length;j++)
					for(int k=0;k<iData[i][j].length;k++)
						assertTrue("Integer Matrix Readback data check", iData[i][j][k] == intTest[i][j][k]);
				
			
			/* Setup string matrix with no coords and write to disk */
			
			fileName = System.getProperty("java.io.tmpdir") + "/dataMatrixStrTest.h5";
			
			String strTest[][] = new String[][]{ {"cow", "pig"}, { "turnip", "potato" } };
			DataMatrix strTestDM = new DataMatrix(strTest);
			strTestDM.writeToHD5(fileName);
			
			/* Read it back and check the data */
			reReadDM = new DataMatrix(fileName);
			assertTrue("String Matrix Readback rank check", reReadDM.getRank() == 2);
			String strData[][] = (String[][])reReadDM.getData();
			
			for(int i=0;i<strData.length;i++)
				for(int j=0;j<strData[i].length;j++)
					assertTrue("String Matrix Readback data check", 
							strTest[i][j].equals(strData[i][j]) );
			
			/* Check that the coord arrays are empty */			
			assertTrue("String Matrix Readback y coords check", reReadDM.getCoords(0) == null);
			assertTrue("String Matrix Readback x coords check", reReadDM.getCoords(1) == null);
				
						
		}catch(RuntimeException e){
			e.printStackTrace(); //print it 
			throw e; //and re-throw it
		}

	}

}