#include <gtest/gtest.h>
#include <vector>

#include "TestUtility.h"

#include "Matrix.h"
#include "Function.h"
#include "Function1D.h"
#include "Function2D.h"
#include "Function3D.h"


class Function3D_Test: public ::testing::Test{
protected:
    static void SetUpFunction3DTester(){

    }
    static void TearDownTestCase(){
    }
    void SetUp(){
    }
    void TearDown(){
    }
};

TEST_F(Function3D_Test, interp3) {
    double* xD= new double[4]{1,2,3,4};
    Aurora::Matrix x(std::shared_ptr<double>(xD,std::default_delete<double[]>()),std::vector<int>{4});
    double* yD= new double[5]{1,4,9,16,25};
    Aurora::Matrix y(std::shared_ptr<double>(yD,std::default_delete<double[]>()),std::vector<int>{5});
    double* zD= new double[3]{22,33,56};
    Aurora::Matrix z(std::shared_ptr<double>(zD,std::default_delete<double[]>()),std::vector<int>{3});
    double* x1D= new double[3]{1.22,2.69,3.84};
    Aurora::Matrix x1(std::shared_ptr<double>(x1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* y1D= new double[3]{4.5823,10.552,18};
    Aurora::Matrix y1(std::shared_ptr<double>(y1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* z1D= new double[3]{22.256,40.22,48};
    Aurora::Matrix z1(std::shared_ptr<double>(z1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* vD= new double[60]{158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25,
                               158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25,
                               158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25};
    Aurora::Matrix v(std::shared_ptr<double>(vD,std::default_delete<double[]>()),std::vector<int>{5,4,3});

    Aurora::Matrix result = interp3(x,y,z,v,x1,y1,z1,Aurora::InterpnMethod::Spline);
    EXPECT_DOUBLE_AE(result.getData()[0],303.5663);
    EXPECT_DOUBLE_AE(result.getData()[1],243.6711);
    EXPECT_DOUBLE_AE(result.getData()[2],-47.32);

    result = interp3(x,y,z,v,x1,y1,z1,Aurora::InterpnMethod::Linear);
    EXPECT_DOUBLE_AE(result.getData()[0],302.6298);
    EXPECT_DOUBLE_AE(result.getData()[1],261.1313);
    EXPECT_DOUBLE_AE(result.getData()[2],18.6089);
}

TEST_F(Function3D_Test, interpn) {
    double* xD= new double[4]{1,2,3,4};
    Aurora::Matrix x(std::shared_ptr<double>(xD,std::default_delete<double[]>()),std::vector<int>{4});
    double* yD= new double[5]{1,4,9,16,25};
    Aurora::Matrix y(std::shared_ptr<double>(yD,std::default_delete<double[]>()),std::vector<int>{5});
    double* zD= new double[3]{22,33,56};
    Aurora::Matrix z(std::shared_ptr<double>(zD,std::default_delete<double[]>()),std::vector<int>{3});
    double* x1D= new double[3]{1.22,2.69,3.84};
    Aurora::Matrix x1(std::shared_ptr<double>(x1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* y1D= new double[3]{2.5823,3.552,3.888};
    Aurora::Matrix y1(std::shared_ptr<double>(y1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* z1D= new double[3]{22.256,40.22,48};
    Aurora::Matrix z1(std::shared_ptr<double>(z1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* v2D= new double[20]{158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25};
    Aurora::Matrix v2(std::shared_ptr<double>(v2D,std::default_delete<double[]>()),std::vector<int>{5,4});
    double* v3D= new double[60]{158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25,
                                158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25,
                                158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25};
    Aurora::Matrix v3(std::shared_ptr<double>(v3D,std::default_delete<double[]>()),std::vector<int>{5,4,3});

    Aurora::Matrix result = interpn(y,x,v2,x1,y1,Aurora::InterpnMethod::Spline);
    EXPECT_DOUBLE_AE(result.getData()[0],399.2742);
    EXPECT_DOUBLE_AE(result.getData()[1],387.7651);
    EXPECT_DOUBLE_AE(result.getData()[2],135.6138);

    result = interpn(y,x,v2,x1,y1,Aurora::InterpnMethod::Linear);
    EXPECT_DOUBLE_AE(result.getData()[0],352.1727);
    EXPECT_DOUBLE_AE(result.getData()[1],269.8596);
    EXPECT_DOUBLE_AE(result.getData()[2],94.7908);

    result = interpn(y,x,z,v3,x1,y1,z1,Aurora::InterpnMethod::Spline);
    EXPECT_DOUBLE_AE(result.getData()[0],399.2742);
    EXPECT_DOUBLE_AE(result.getData()[1],387.7651);
    EXPECT_DOUBLE_AE(result.getData()[2],135.6138);

    result = interpn(y,x,z,v3,x1,y1,z1,Aurora::InterpnMethod::Linear);
    EXPECT_DOUBLE_AE(result.getData()[0],352.1727);
    EXPECT_DOUBLE_AE(result.getData()[1],269.8596);
    EXPECT_DOUBLE_AE(result.getData()[2],94.7908);
}

TEST_F(Function3D_Test, zerosAndones){

    Aurora::Matrix zerosM = Aurora::zeros( 3, 4,5);
    EXPECT_EQ(60,zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
        EXPECT_EQ(0,zerosM.getData()[i])<<" error at index："<<i;
    }
    zerosM = Aurora::zeros( 3, 4);
    EXPECT_EQ(12,zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
        EXPECT_EQ(0,zerosM.getData()[i])<<" error at index："<<i;
    }
    zerosM = Aurora::zeros( 3, 1);
    EXPECT_EQ(3,zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
        EXPECT_EQ(0,zerosM.getData()[i])<<" error at index："<<i;
    }
    zerosM = Aurora::zeros( 20);
    EXPECT_EQ(400,zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
        EXPECT_EQ(0,zerosM.getData()[i])<<" error at index："<<i;
    }
    Aurora::Matrix onesM = Aurora::ones( 9, 9,9);
    EXPECT_EQ(729,onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
        EXPECT_DOUBLE_EQ(1.0,onesM.getData()[i])<<" error at index："<<i;
    }

    onesM = Aurora::ones( 9, 9);
    EXPECT_EQ(81,onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
        EXPECT_DOUBLE_EQ(1.0,onesM.getData()[i])<<" error at index："<<i;
    }

    onesM = Aurora::ones( 9, 1);
    EXPECT_EQ(9,onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
        EXPECT_DOUBLE_EQ(1.0,onesM.getData()[i])<<" error at index："<<i;
    }

    onesM = Aurora::ones( 9);
    EXPECT_EQ(81,onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
        EXPECT_DOUBLE_EQ(1.0,onesM.getData()[i])<<" error at index："<<i;
    }

}