package org.deeplearning4j.exceptions;

import org.datavec.api.records.reader.SequenceRecordReader;
import org.datavec.api.records.reader.impl.collection.CollectionRecordReader;
import org.datavec.api.records.reader.impl.collection.CollectionSequenceRecordReader;
import org.datavec.api.writable.DoubleWritable;
import org.datavec.api.writable.IntWritable;
import org.datavec.api.writable.Writable;
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
import org.deeplearning4j.datasets.datavec.SequenceRecordReaderDataSetIterator;
import org.deeplearning4j.exception.DL4JException;
import org.junit.Test;
import org.nd4j.linalg.dataset.api.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import static junit.framework.TestCase.fail;

/**
 * Created by Alex on 14/11/2016.
 */
public class TestRecordReaders {

    @Test
    public void testClassIndexOutsideOfRangeRRDSI() {

        Collection<Collection<Writable>> c = new ArrayList<>();
        c.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
        c.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(2)));

        CollectionRecordReader crr = new CollectionRecordReader(c);

        RecordReaderDataSetIterator iter = new RecordReaderDataSetIterator(crr, 2, 1, 2);

        try {
            DataSet ds = iter.next();
            fail("Expected exception");
        } catch (DL4JException e) {
            System.out.println("testClassIndexOutsideOfRange(): " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    @Test
    public void testClassIndexOutsideOfRangeRRMDSI() {

        Collection<Collection<Collection<Writable>>> c = new ArrayList<>();
        Collection<Collection<Writable>> seq1 = new ArrayList<>();
        seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
        seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(1)));
        c.add(seq1);

        Collection<Collection<Writable>> seq2 = new ArrayList<>();
        seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
        seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(2)));
        c.add(seq2);

        CollectionSequenceRecordReader csrr = new CollectionSequenceRecordReader(c);
        DataSetIterator dsi = new SequenceRecordReaderDataSetIterator(csrr, 2, 2, 1);

        try {
            DataSet ds = dsi.next();
            fail("Expected exception");
        } catch (DL4JException e) {
            System.out.println("testClassIndexOutsideOfRangeRRMDSI(): " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    @Test
    public void testClassIndexOutsideOfRangeRRMDSI_MultipleReaders() {

        Collection<Collection<Collection<Writable>>> c1 = new ArrayList<>();
        Collection<Collection<Writable>> seq1 = new ArrayList<>();
        seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
        seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
        c1.add(seq1);

        Collection<Collection<Writable>> seq2 = new ArrayList<>();
        seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
        seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
        c1.add(seq2);

        Collection<Collection<Collection<Writable>>> c2 = new ArrayList<>();
        Collection<Collection<Writable>> seq1a = new ArrayList<>();
        seq1a.add(Arrays.<Writable>asList(new IntWritable(0)));
        seq1a.add(Arrays.<Writable>asList(new IntWritable(1)));
        c2.add(seq1a);

        Collection<Collection<Writable>> seq2a = new ArrayList<>();
        seq2a.add(Arrays.<Writable>asList(new IntWritable(0)));
        seq2a.add(Arrays.<Writable>asList(new IntWritable(2)));
        c2.add(seq2a);

        CollectionSequenceRecordReader csrr = new CollectionSequenceRecordReader(c1);
        CollectionSequenceRecordReader csrrLabels = new CollectionSequenceRecordReader(c2);
        DataSetIterator dsi = new SequenceRecordReaderDataSetIterator(csrr, csrrLabels, 2, 2);

        try {
            DataSet ds = dsi.next();
            fail("Expected exception");
        } catch (DL4JException e) {
            System.out.println("testClassIndexOutsideOfRangeRRMDSI_MultipleReaders(): " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

}

@@ -722,16 +722,53 @@
 terator;
+%0Aimport org.nd4j.linalg.factory.Nd4j;
 %0A%0Aimport
@@ -888,16 +888,61 @@
 se.fail;
+%0Aimport static org.junit.Assert.assertEquals;
 %0A%0A/**%0A *
@@ -1068,32 +1068,190 @@
 RangeRRDSI() %7B%0A%0A
+        //In previous implementations: this would throw an exception. Now: Skip invalid records%0A        // i.e., expect only the first record to be returned%0A%0A
         Collecti
@@ -1349,33 +1349,33 @@
 oubleWritable(0.
-0
+5
 ), new IntWritab
@@ -1432,33 +1432,33 @@
  DoubleWritable(
-0
+1
 .0), new IntWrit
@@ -1629,34 +1629,16 @@
 1, 2);%0A%0A
-        try %7B%0A    
         
@@ -1659,36 +1659,33 @@
 ter.next();%0A
-    
+%0A
         
 fail(%22Expect
@@ -1676,251 +1676,146 @@
     
-fail(%22Expected exception%22);%0A        %7D catch (DL4JException e) %7B%0A            System.out.println(%22testClassIndexOutsideOfRange(): %22 + e.getMessage());%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            fail();%0A        %7D
+assertEquals(Nd4j.create(new double%5B%5D%7B0.5%7D), ds.getFeatures());%0A        assertEquals(Nd4j.create(new double%5B%5D%7B1.0, 0.0%7D), ds.getLabels());
 %0A   

Update TestRecordReaders to reflect new RecordReaderDataSetIterator behaviour