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

#include "TestUtility.h"

#include "Matrix.h"
#include "CudaMatrix.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) {
    float* xD= new float[4]{1,2,3,4};
    Aurora::Matrix x(std::shared_ptr<float>(xD,std::default_delete<float[]>()),std::vector<int>{4});
    float* yD= new float[5]{1,4,9,16,25};
    Aurora::Matrix y(std::shared_ptr<float>(yD,std::default_delete<float[]>()),std::vector<int>{5});
    float* zD= new float[3]{22,33,56};
    Aurora::Matrix z(std::shared_ptr<float>(zD,std::default_delete<float[]>()),std::vector<int>{3});
    float* x1D= new float[3]{1.22,2.69,3.84};
    Aurora::Matrix x1(std::shared_ptr<float>(x1D,std::default_delete<float[]>()),std::vector<int>{3});
    float* y1D= new float[3]{4.5823,10.552,18};
    Aurora::Matrix y1(std::shared_ptr<float>(y1D,std::default_delete<float[]>()),std::vector<int>{3});
    float* z1D= new float[3]{22.256,40.22,48};
    Aurora::Matrix z1(std::shared_ptr<float>(z1D,std::default_delete<float[]>()),std::vector<int>{3});
    float* vD= new float[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<float>(vD,std::default_delete<float[]>()),std::vector<int>{5,4,3});

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

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

TEST_F(Function3D_Test, interpn) {
    float* xD= new float[4]{1,2,3,4};
    Aurora::Matrix x(std::shared_ptr<float>(xD,std::default_delete<float[]>()),std::vector<int>{4});
    float* yD= new float[5]{1,4,9,16,25};
    Aurora::Matrix y(std::shared_ptr<float>(yD,std::default_delete<float[]>()),std::vector<int>{5});
    float* zD= new float[3]{22,33,56};
    Aurora::Matrix z(std::shared_ptr<float>(zD,std::default_delete<float[]>()),std::vector<int>{3});
    float* x1D= new float[3]{1.22,2.69,3.84};
    Aurora::Matrix x1(std::shared_ptr<float>(x1D,std::default_delete<float[]>()),std::vector<int>{3});
    float* y1D= new float[3]{2.5823,3.552,3.888};
    Aurora::Matrix y1(std::shared_ptr<float>(y1D,std::default_delete<float[]>()),std::vector<int>{3});
    float* z1D= new float[3]{22.256,40.22,48};
    Aurora::Matrix z1(std::shared_ptr<float>(z1D,std::default_delete<float[]>()),std::vector<int>{3});
    float* v2D= new float[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<float>(v2D,std::default_delete<float[]>()),std::vector<int>{5,4});
    float* v3D= new float[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<float>(v3D,std::default_delete<float[]>()),std::vector<int>{5,4,3});

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

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

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

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

TEST_F(Function3D_Test, zerosAndonesAndsizesMKL) {
    // mklversion
    Aurora::Matrix zerosM = Aurora::zeros(3, 4, 5);
    EXPECT_EQ(60, zerosM.getDataSize());
    auto size = Aurora::size(zerosM);
    EXPECT_EQ(3,size[0]);
    EXPECT_EQ(4,size[1]);
    EXPECT_EQ(5,size[2]);
    EXPECT_EQ(3,Aurora:: size(zerosM,1));
    EXPECT_EQ(4,Aurora:: size(zerosM,2));
    EXPECT_EQ(5,Aurora:: size(zerosM,3));
    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_FLOAT_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_FLOAT_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_FLOAT_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_FLOAT_EQ(1.0, onesM.getData()[i]) << " error at index:" << i;
    }
}

TEST_F(Function3D_Test, zerosAndonesAndsizeCuda) {
    Aurora::CudaMatrix zerosM = Aurora::zerosCuda(3, 4, 5);
    auto size = Aurora::size(zerosM);
    EXPECT_EQ(3,size.getValue(0));
    EXPECT_EQ(4,size.getValue(1));
    EXPECT_EQ(5,size.getValue(2));
    EXPECT_EQ(3,Aurora:: size(zerosM,1));
    EXPECT_EQ(4,Aurora:: size(zerosM,2));
    EXPECT_EQ(5,Aurora:: size(zerosM,3));
    EXPECT_EQ(60, zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
        EXPECT_EQ(0, zerosM.getValue(i)) << " error at index:" << i;
    }
    zerosM = Aurora::zerosCuda(3, 4);
    EXPECT_EQ(12, zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
      EXPECT_EQ(0, zerosM.getValue(i)) << " error at index:" << i;
    }
    zerosM = Aurora::zerosCuda(20);
    EXPECT_EQ(400, zerosM.getDataSize());
    for (int i = 0; i < zerosM.getDataSize(); ++i) {
      EXPECT_EQ(0, zerosM.getValue(i)) << " error at index:" << i;
    }
    auto onesM = Aurora::onesCuda(9, 9, 9);
    EXPECT_EQ(729, onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
      ASSERT_FLOAT_EQ(1.0, onesM.getValue(i)) << " error at index:" << i;
    }

    onesM = Aurora::onesCuda(9, 3);
    EXPECT_EQ(27, onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
      EXPECT_FLOAT_EQ(1.0, onesM.getValue(i)) << " error at index:" << i;
    }

    onesM = Aurora::onesCuda(9);
    EXPECT_EQ(81, onesM.getDataSize());
    for (int i = 0; i < onesM.getDataSize(); ++i) {
      EXPECT_FLOAT_EQ(1.0, onesM.getValue(i)) << " error at index:" << i;
    }
}