/*
 *  ******************************************************************************
 *  *
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Apache License, Version 2.0 which is available at
 *  * https://www.apache.org/licenses/LICENSE-2.0.
 *  *
 *  *  See the NOTICE file distributed with this work for additional
 *  *  information regarding copyright ownership.
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *  * License for the specific language governing permissions and limitations
 *  * under the License.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  *****************************************************************************
 */

package org.eclipse.deeplearning4j.dl4jcore.datasets.iterator;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.deeplearning4j.BaseDL4JTest;
import org.deeplearning4j.datasets.iterator.JointMultiDataSetIterator;
import org.eclipse.deeplearning4j.dl4jcore.datasets.iterator.tools.DataSetGenerator;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.nd4j.common.tests.tags.NativeTag;
import org.nd4j.common.tests.tags.TagNames;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@NativeTag
@Tag(TagNames.FILE_IO)
public class JointMultiDataSetIteratorTests extends BaseDL4JTest {

    @Test ()
    @Timeout(20000L)
    public void testJMDSI_1() {
        val iter0 = new DataSetGenerator(32, new int[]{3, 3}, new int[]{2, 2});
        val iter1 = new DataSetGenerator(32, new int[]{3, 3, 3}, new int[]{2, 2, 2});
        val iter2 = new DataSetGenerator(32, new int[]{3, 3, 3, 3}, new int[]{2, 2, 2, 2});

        val mdsi = new JointMultiDataSetIterator(iter0, iter1, iter2);

        int cnt = 0;
        while (mdsi.hasNext()) {
            val mds = mdsi.next();

            assertNotNull(mds);

            val f = mds.getFeatures();
            val l = mds.getLabels();

            val fm = mds.getFeaturesMaskArrays();
            val lm = mds.getLabelsMaskArrays();

            assertNotNull(f);
            assertNotNull(l);
            assertNull(fm);
            assertNull(lm);

            assertArrayEquals(new long[]{3, 3}, f[0].shape());
            assertArrayEquals(new long[]{3, 3, 3}, f[1].shape());
            assertArrayEquals(new long[]{3, 3, 3, 3}, f[2].shape());

            assertEquals(cnt, (int) f[0].getDouble(0));
            assertEquals(cnt, (int) f[1].getDouble(0));
            assertEquals(cnt, (int) f[2].getDouble(0));

            assertArrayEquals(new long[]{2, 2}, l[0].shape());
            assertArrayEquals(new long[]{2, 2, 2}, l[1].shape());
            assertArrayEquals(new long[]{2, 2, 2, 2}, l[2].shape());

            cnt++;
        }

        assertEquals(32, cnt);
    }


    @Test ()
    @Timeout(20000L)
    public void testJMDSI_2() {
        val iter0 = new DataSetGenerator(32, new int[]{3, 3}, new int[]{2, 2});
        val iter1 = new DataSetGenerator(32, new int[]{3, 3, 3}, new int[]{2, 2, 2});
        val iter2 = new DataSetGenerator(32, new int[]{3, 3, 3, 3}, new int[]{2, 2, 2, 2});

        val mdsi = new JointMultiDataSetIterator(1, iter0, iter1, iter2);

        int cnt = 0;
        while (mdsi.hasNext()) {
            val mds = mdsi.next();

            assertNotNull(mds);

            val f = mds.getFeatures();
            val l = mds.getLabels();

            val fm = mds.getFeaturesMaskArrays();
            val lm = mds.getLabelsMaskArrays();

            assertNotNull(f);
            assertNotNull(l);
            assertNull(fm);
            assertNull(lm);

            assertArrayEquals(new long[]{3, 3}, f[0].shape());
            assertArrayEquals(new long[]{3, 3, 3}, f[1].shape());
            assertArrayEquals(new long[]{3, 3, 3, 3}, f[2].shape());

            assertEquals(cnt, (int) f[0].getDouble(0));
            assertEquals(cnt, (int) f[1].getDouble(0));
            assertEquals(cnt, (int) f[2].getDouble(0));

            assertEquals(1, l.length);

            assertArrayEquals(new long[]{2, 2, 2}, l[0].shape());
            assertEquals(cnt, (int) l[0].getDouble(0));

            cnt++;
        }

        assertEquals(32, cnt);
    }
}
