package org.encog.app.quant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.encog.app.quant.normalize.NormalizationAction;
import org.encog.app.quant.normalize.NormalizeCSV;
import org.encog.util.csv.CSVFormat;

public class TestNormalizeCSV extends TestCase {

	public static final String INPUT_NAME = "test.csv";
	public static final String OUTPUT_NAME = "test2.csv";

    public void generateTestFileHeadings(boolean header) throws IOException
    {
    	PrintWriter tw = new PrintWriter(new FileWriter(INPUT_NAME));

        if (header)
        {
            tw.println("a,b,c,d,e");
        }
        tw.println("one,1,1,2,3");
        tw.println("two,2,2,3,4");
        tw.println("three,3,3,4,5");
        tw.println("four,3,4,5,6");
        tw.println("five,2,5,6,7");
        tw.println("six,1,6,7,8");

        // close the stream
        tw.close();
    }

    public void testNormCSVHeaders() throws IOException
    {
        generateTestFileHeadings(true);
        NormalizeCSV norm = new NormalizeCSV();
        norm.analyze(INPUT_NAME, true, CSVFormat.ENGLISH);
        norm.getStats().getStats()[2].makePassThrough();
        norm.getStats().getStats()[3].setAction( NormalizationAction.Ignore);
        norm.getStats().getStats()[4].setAction( NormalizationAction.Ignore);
        norm.normalize(OUTPUT_NAME);

        BufferedReader tr = new BufferedReader(new FileReader(OUTPUT_NAME));
        Assert.assertEquals("\"a\",\"b\",\"c\"",tr.readLine());
        Assert.assertEquals("\"one\",-1,\"1\"",tr.readLine());
        Assert.assertEquals("\"two\",0,\"2\"",tr.readLine());
        tr.close();

        (new File(INPUT_NAME)).delete();
		(new File(OUTPUT_NAME)).delete();
    }

    public void testNormCSVNoHeaders() throws IOException
    {
        generateTestFileHeadings(false);
        NormalizeCSV norm = new NormalizeCSV();
        norm.analyze(INPUT_NAME, false, CSVFormat.ENGLISH);
        norm.setProduceOutputHeaders(false);
        norm.getStats().getStats()[2].makePassThrough();
        norm.getStats().getStats()[3].setAction( NormalizationAction.Ignore);
        norm.getStats().getStats()[4].setAction( NormalizationAction.Ignore);
        norm.normalize(OUTPUT_NAME);

        BufferedReader tr = new BufferedReader(new FileReader(OUTPUT_NAME));
        Assert.assertEquals("\"one\",-1,\"1\"",tr.readLine());
        Assert.assertEquals("\"two\",0,\"2\"",tr.readLine());
        tr.close();

        (new File(INPUT_NAME)).delete();
		(new File(OUTPUT_NAME)).delete();
    }

	
}

@@ -841,17 +841,17 @@
 one,1,1,
-2
+a
 ,3%22);%0D%0A 
@@ -877,17 +877,17 @@
 two,2,2,
-3
+a
 ,4%22);%0D%0A 
@@ -915,19 +915,19 @@
 ree,3,3,
-4,5
+b,4
 %22);%0D%0A   
@@ -952,17 +952,17 @@
 our,3,4,
-5
+b
 ,6%22);%0D%0A 
@@ -989,17 +989,17 @@
 ive,2,5,
-6
+c
 ,7%22);%0D%0A 
@@ -1025,17 +1025,17 @@
 six,1,6,
-7
+c
 ,8%22);%0D%0A%0D
@@ -1148,32 +1148,73 @@
 Exception%0D%0A    %7B
+    %09%0D%0A    %09String%5B%5D cls = %7B%22a%22,%22b%22,%22c%22%7D;
 %0D%0A        genera
@@ -1281,32 +1281,63 @@
 ormalizeCSV();%0D%0A
+        norm.setPrecision(4);%0D%0A
         norm.ana
@@ -1472,33 +1472,33 @@
 tStats()%5B3%5D.
-setAction
+makeClass
 ( Normalizat
@@ -1499,38 +1499,56 @@
 alizationAction.
-Ignore
+Equilateral, cls, 1, -1 
 );%0D%0A        norm
@@ -1778,16 +1778,32 @@
 %5C%22,%5C%22c%5C%22
+,%5C%22d-0%5C%22,%5C%22d-1%5C%22
 %22,tr.rea
@@ -1850,32 +1850,36 @@
 %5C%22one%5C%22,-1,%5C%221%5C%22
+,0,0
 %22,tr.readLine())
@@ -1917,32 +1917,36 @@
 %22%5C%22two%5C%22,0,%5C%222%5C%22
+,0,0
 %22,tr.readLine())

Update unit tests