/*
 *  ******************************************************************************
 *  *
 *  *
 *  * 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.nd4j.linalg.api.indexing.shape;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.nd4j.common.tests.tags.NativeTag;
import org.nd4j.common.tests.tags.TagNames;
import org.nd4j.linalg.BaseNd4jTestWithBackends;
import org.nd4j.linalg.factory.Nd4jBackend;
import org.nd4j.linalg.indexing.INDArrayIndex;
import org.nd4j.linalg.indexing.Indices;
import org.nd4j.linalg.indexing.NDArrayIndex;

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

/**
 * @author Adam Gibson
 */
@Tag(TagNames.NDARRAY_INDEXING)
@NativeTag
public class IndexShapeTests extends BaseNd4jTestWithBackends {

    private int[] shape = {1, 1, 2, 1, 3, 4, 5, 1};

    @ParameterizedTest
    @MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
    public void testSinglePoint(Nd4jBackend backend) {
        /*
        Assumes all indexes are filled out.
        Test simple general point case
         */
        int[] assertion = {2, 1, 4, 5, 1};
        INDArrayIndex[] indexes = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all()};

        int[] testShape = Indices.shape(shape, indexes);
        assertArrayEquals(assertion, testShape);

        int[] secondAssertion = {1, 2, 1, 5, 1};
        INDArrayIndex[] otherCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.point(0)

        };

        assertArrayEquals(secondAssertion, Indices.shape(shape, otherCase));


        int[] thridAssertion = {1, 2, 1, 4, 5, 1};
        INDArrayIndex[] thirdCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.point(0),

        };
        assertArrayEquals(thridAssertion, Indices.shape(shape, thirdCase));

    }

    @ParameterizedTest
    @MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
    public void testInterval(Nd4jBackend backend) {
        int[] basicAssertion = {1, 1, 1, 1, 3, 1, 2, 1};
        INDArrayIndex[] basicTest = {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 1),
                        NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(1, 2),
                        NDArrayIndex.interval(2, 4), NDArrayIndex.all()};
        assertArrayEquals(basicAssertion, Indices.shape(shape, basicTest));

    }


    @ParameterizedTest
    @MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
    public void testNewAxis(Nd4jBackend backend) {
        //normal prepend
        int[] prependAssertion = {1, 1, 1, 1, 2, 1, 3, 4, 5, 1};
        INDArrayIndex[] prependTest = {NDArrayIndex.newAxis(), NDArrayIndex.newAxis(), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),

        };

        assertArrayEquals(prependAssertion, Indices.shape(shape, prependTest));

        //test setting for particular indexes.
        //when an all is encountered before a new axis,
        //it is assumed that new axis must occur at the destination
        //where the new axis was specified
        int[] addToMiddle = {1, 1, 2, 1, 1, 1, 3, 4, 5, 1};
        INDArrayIndex[] setInMiddleTest = {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),
                        NDArrayIndex.newAxis(), NDArrayIndex.newAxis(), NDArrayIndex.all(), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),};
        assertArrayEquals(addToMiddle, Indices.shape(shape, setInMiddleTest));

        //test prepending AND adding to middle
        int[] prependAndAddToMiddleAssertion = {1, 1, 1, 1, 2, 1, 1, 1, 3, 4, 5, 1};

        INDArrayIndex[] prependAndMiddle = {NDArrayIndex.newAxis(), NDArrayIndex.newAxis(), NDArrayIndex.all(),
                        NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.newAxis(), NDArrayIndex.newAxis(),
                        NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),
                        NDArrayIndex.all(),};
        assertArrayEquals(prependAndAddToMiddleAssertion, Indices.shape(shape, prependAndMiddle));

    }


    @Override
    public char ordering() {
        return 'f';
    }
}
