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

#include "MatlabReader.h"
#include "TestUtility.h"

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


class Function1D_Test:public ::testing::Test{
protected:
    static void SetUpFunction1DTester(){

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

};

TEST_F(Function1D_Test, sign) {
    double * dataA =Aurora::malloc(9);
    double * dataB =Aurora::malloc(9);
    for (int i = 0; i < 9; ++i) {
        dataA[i]=(double)(i-3);
        dataB[i]=(double)(i+2);
    }
    Aurora::Matrix A = Aurora::Matrix::New(dataA,3,3);
    Aurora::Matrix B = Aurora::Matrix::New(dataB,3,3);
    auto C = sign(A*B);
    double * result = C.getData();
    EXPECT_EQ(-1, result[0]);
    EXPECT_EQ(0, result[3]);
    EXPECT_EQ(1, result[4]);
    C = sign(A);
    result = C.getData();
    EXPECT_EQ(-1, result[0]);
    EXPECT_EQ(0, result[3]);
    EXPECT_EQ(1, result[4]);
}

TEST_F(Function1D_Test, repmat) {
    //input 1D repmat 2D
    double* input = new double[10]{1,2,3,4,5,6,7,8,9,10};
    Aurora::Matrix m(std::shared_ptr<double>(input,std::default_delete<double[]>()),std::vector<int>{10});
    EXPECT_DOUBLE_EQ(repmat(m,1,1).getData()[7],8);
    EXPECT_DOUBLE_EQ(repmat(m,2,3).getDataSize(),60);
    EXPECT_DOUBLE_EQ(repmat(m,2,3).getData()[22],3);
    EXPECT_DOUBLE_EQ(repmat(m,2,3).getDimSize(0),20);
    EXPECT_DOUBLE_EQ(repmat(m,2,3).getDimSize(1),3);
    EXPECT_DOUBLE_EQ(repmat(m,2,3).getDimSize(2),1);

    //input 2D repmat 2D
    double* input2 = new double[10]{1,2,3,4,5,6,7,8,9,10};
    Aurora::Matrix m2(std::shared_ptr<double>(input2,std::default_delete<double[]>()),std::vector<int>{5,2});
    EXPECT_DOUBLE_EQ(repmat(m2,1,1).getData()[7],8);
    EXPECT_DOUBLE_EQ(repmat(m2,2,3).getDataSize(),60);
    EXPECT_DOUBLE_EQ(repmat(m2,2,3).getData()[22],3);
    EXPECT_DOUBLE_EQ(repmat(m2,2,3).getDimSize(0),10);
    EXPECT_DOUBLE_EQ(repmat(m2,2,3).getDimSize(1),6);
    EXPECT_DOUBLE_EQ(repmat(m2,2,3).getDimSize(2),1);

    //input 1D repmat 3D
    double* input3 = new double[10]{1,2,3,4,5,6,7,8,9,10};
    Aurora::Matrix m3(std::shared_ptr<double>(input3,std::default_delete<double[]>()),std::vector<int>{10});
    EXPECT_DOUBLE_EQ(repmat(m3,1,1,1).getData()[7],8);
    EXPECT_DOUBLE_EQ(repmat(m3,2,3,4).getDataSize(),240);
    EXPECT_DOUBLE_EQ(repmat(m3,2,3,4).getData()[22],3);
    EXPECT_DOUBLE_EQ(repmat(m3,2,3,4).getDimSize(0),20);
    EXPECT_DOUBLE_EQ(repmat(m3,2,3,4).getDimSize(1),3);
    EXPECT_DOUBLE_EQ(repmat(m3,2,3,4).getDimSize(2),4);

    //input 2D repmat 3D
    double* input4 = new double[10]{1,2,3,4,5,6,7,8,9,10};
    Aurora::Matrix m4(std::shared_ptr<double>(input4,std::default_delete<double[]>()),std::vector<int>{5,2});
    EXPECT_DOUBLE_EQ(repmat(m4,1,1).getData()[7],8);
    EXPECT_DOUBLE_EQ(repmat(m4,2,3,4).getDataSize(),240);
    EXPECT_DOUBLE_EQ(repmat(m4,2,3,4).getData()[22],3);
    EXPECT_DOUBLE_EQ(repmat(m4,2,3,4).getDimSize(0),10);
    EXPECT_DOUBLE_EQ(repmat(m4,2,3,4).getDimSize(1),6);
    EXPECT_DOUBLE_EQ(repmat(m4,2,3,4).getDimSize(2),4);

}

TEST_F(Function1D_Test, interp1) {
    double* xD= new double[5]{1,2,3,4,5};
    Aurora::Matrix x(std::shared_ptr<double>(xD,std::default_delete<double[]>()),std::vector<int>{5});
    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* 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});
    Aurora::Matrix result = interp1(x,y,x1,Aurora::InterpnMethod::Spline);
    EXPECT_DOUBLE_EQ(result.getData()[0],1.4884);
    EXPECT_DOUBLE_EQ(result.getData()[1],7.2361);
    EXPECT_DOUBLE_EQ(result.getData()[2],14.7456);

    result = interp1(x,y,x1,Aurora::InterpnMethod::Linear);
    EXPECT_DOUBLE_EQ(result.getData()[0],1.66);
    EXPECT_DOUBLE_EQ(result.getData()[1],7.45);
    EXPECT_DOUBLE_EQ(result.getData()[2],14.88);
}

TEST_F(Function1D_Test, polyval){
    // double *dataP = new double[3]{3,2,1};
    double *dataP = new double[6]{2.78786e-9, -1.398845e-6, 3.287156e-4, -5.799136e-2, 5.038813, 1.402385e3};
    // double *dataX = new double[3]{5,7,9};
    double *dataX = new double[1]{24.570274572648690};

    
    auto matrixP = Aurora::Matrix::fromRawData(dataP,6);
    auto matrixX = Aurora::Matrix::fromRawData(dataX,1);
    auto result = Aurora::polyval(matrixP,matrixX);
    EXPECT_DOUBLE_EQ(1495.5717310876246, result.getData()[0]);
    // EXPECT_DOUBLE_EQ(162., result.getData()[1]);
    // EXPECT_DOUBLE_EQ(262., result.getData()[2]);
}

TEST_F(Function1D_Test, complexAndEtc){
    //complex
    {
        double *dataP =new double[8]{3,2,1,6, 7, 8 , 19, 13};
        auto matrixAN = Aurora::Matrix::fromRawData(dataP,2,4);
        auto matrixBC = Aurora::complex(matrixAN);
        EXPECT_EQ(8, matrixBC.getDataSize());
        EXPECT_EQ(Aurora::Complex, matrixBC.getValueType());
        EXPECT_DOUBLE_EQ(3, matrixBC.getData()[0]);
        EXPECT_DOUBLE_EQ(0, matrixBC.getData()[3]);
    }
    //complex & real & imag
    {
        double *dataP =new double[8]{3,2,1,6, 7, 8 , 19, 13};
        auto matrixAC = Aurora::Matrix::fromRawData(dataP,2,2,0,Aurora::Complex);
        auto matrixCN = Aurora::real(matrixAC);
        EXPECT_EQ(4, matrixCN.getDataSize());
        EXPECT_EQ(Aurora::Normal, matrixCN.getValueType());
        EXPECT_DOUBLE_EQ(3, matrixCN.getData()[0]);
        EXPECT_DOUBLE_EQ(19, matrixCN.getData()[3]);

        auto matrixDN = Aurora::imag(matrixAC);
        EXPECT_EQ(4, matrixDN.getDataSize());
        EXPECT_EQ(Aurora::Normal, matrixDN.getValueType());
        EXPECT_DOUBLE_EQ(2, matrixDN.getData()[0]);
        EXPECT_DOUBLE_EQ(13, matrixDN.getData()[3]);
    }
}

TEST_F(Function1D_Test, ceilAndRound) {
    double *dataP = new double[3]{3.1, 2.5, 1.8};
    auto matrixA = Aurora::Matrix::fromRawData(dataP, 3);
    auto matrixB = Aurora::ceil(matrixA);
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(4, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(3, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[2]);
    matrixB = Aurora::ceil(matrixA * 0.5) ;
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[2]);

    matrixB = Aurora::round(matrixA);
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(3, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(3, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[2]);

    matrixB = Aurora::round(matrixA * 0.5);
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[2]);
}

TEST_F(Function1D_Test, absAndSqrt) {
    double *dataP = new double[3]{1, 4, -3};
    auto matrixA = Aurora::Matrix::fromRawData(dataP, 3);
    auto matrixB = Aurora::abs(matrixA);
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(4, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(3, matrixB.getData()[2]);
    matrixB = Aurora::sqrt(matrixB);
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(1.7321, fourDecimalRound(matrixB.getData()[2]));
    matrixB = Aurora::sqrt(Aurora::abs(matrixA*-1));
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[1]);
    EXPECT_DOUBLE_EQ(1.7321, fourDecimalRound(matrixB.getData()[2]));
    matrixB = Aurora::sqrt(matrixA);
    EXPECT_EQ(3, matrixB.getDataSize());
    EXPECT_EQ(Aurora::Normal, matrixB.getValueType());
    EXPECT_DOUBLE_EQ(1, matrixB.getData()[0]);
    EXPECT_DOUBLE_EQ(2, matrixB.getData()[1]);
    EXPECT_TRUE(isnanf(matrixB.getData()[2]));
}

TEST_F(Function1D_Test, log) {
    double *dataP = new double[3]{1, 4, 6};
    auto matrixA = Aurora::Matrix::fromRawData(dataP, 3);
    auto result = Aurora::log(matrixA);
    EXPECT_DOUBLE_AE(result.getData()[0],0);
    EXPECT_DOUBLE_AE(result.getData()[1],1.3863);
    EXPECT_DOUBLE_AE(result.getData()[2],1.7918);

    result = Aurora::log(matrixA,10);
    EXPECT_DOUBLE_AE(result.getData()[0],0);
    EXPECT_DOUBLE_AE(result.getData()[1],0.6021);
    EXPECT_DOUBLE_AE(result.getData()[2],0.7782);
}

TEST_F(Function1D_Test, exp) {
    double *data1 = new double[4]{1,2,3,4};
    auto matrix1 = Aurora::Matrix::fromRawData(data1, 4,1,1);
    auto result = Aurora::exp(matrix1);
    EXPECT_DOUBLE_AE(result.getData()[0],2.7183);
    EXPECT_DOUBLE_AE(result.getData()[1],7.3891);
    EXPECT_DOUBLE_AE(result.getData()[2],20.0855);
    EXPECT_DOUBLE_AE(result.getData()[3],54.5982);

    double *data2 = new double[4]{1,2,3,4};
    auto matrix2 = Aurora::Matrix::fromRawData(data2, 2,1,1,Aurora::Complex);
    result = Aurora::exp(matrix2);
    EXPECT_DOUBLE_AE(result.getData()[0],-1.1312);
    EXPECT_DOUBLE_AE(result.getData()[1],2.4717);
    EXPECT_DOUBLE_AE(result.getData()[2],-13.1288);
    EXPECT_DOUBLE_AE(result.getData()[3],-15.2008);
}

TEST_F(Function1D_Test, mod) {
    double *data = new double[3]{1.22,3.23,23.2};
    auto matrix = Aurora::Matrix::fromRawData(data, 3);
    auto result = Aurora::mod(matrix,1.2);
    EXPECT_DOUBLE_AE(result.getData()[0],0.02);
    EXPECT_DOUBLE_AE(result.getData()[1],0.83);
    EXPECT_DOUBLE_AE(result.getData()[2],0.4);
}

TEST_F(Function1D_Test, acos) {
    double *data = new double[3]{0.02,0.83,0.4,};
    auto matrix = Aurora::Matrix::fromRawData(data, 3);
    auto result = Aurora::acos(matrix);
    EXPECT_DOUBLE_AE(result.getData()[0],1.5508);
    EXPECT_DOUBLE_AE(result.getData()[1],0.5917);
    EXPECT_DOUBLE_AE(result.getData()[2],1.1593);
}

TEST_F(Function1D_Test, acosd) {
    double *data = new double[3]{0.02,0.83,0.4,};
    auto matrix = Aurora::Matrix::fromRawData(data, 3);
    auto result = Aurora::acosd(matrix);
    EXPECT_DOUBLE_AE(result.getData()[0],88.854);
    EXPECT_DOUBLE_AE(result.getData()[1],33.9013);
    EXPECT_DOUBLE_AE(result.getData()[2],66.4218);
}

TEST_F(Function1D_Test, conj) {
    double *data = new double[6]{0.02,1,0.83,-1,0.4,2};
    auto matrix = Aurora::Matrix::fromRawData(data, 3,1,1,Aurora::Complex);
    auto result = Aurora::conj(matrix);
    EXPECT_DOUBLE_AE(result.getData()[0],0.02);
    EXPECT_DOUBLE_AE(result.getData()[1],-1);
    EXPECT_DOUBLE_AE(result.getData()[2],0.83);
    EXPECT_DOUBLE_AE(result.getData()[3],1);
    EXPECT_DOUBLE_AE(result.getData()[4],0.4);
    EXPECT_DOUBLE_AE(result.getData()[5],-2);
    EXPECT_DOUBLE_AE((double)result.getValueType(), (double)Aurora::Complex);

    data = new double[6]{0.02,1,0.83,-1,0.4,2};
    matrix = Aurora::Matrix::fromRawData(data, 6,1,1);
    result = Aurora::conj(matrix);
    EXPECT_DOUBLE_AE(result.getData()[0],0.02);
    EXPECT_DOUBLE_AE(result.getData()[1],1);
    EXPECT_DOUBLE_AE(result.getData()[2],0.83);
    EXPECT_DOUBLE_AE(result.getData()[3],-1);
    EXPECT_DOUBLE_AE(result.getData()[4],0.4);
    EXPECT_DOUBLE_AE(result.getData()[5],2);
    EXPECT_DOUBLE_AE((double)result.getValueType(), (double)Aurora::Normal);
}

TEST_F(Function1D_Test, norm) {
    //1Dim
    double *data = new double[3]{1,2,-3};
    auto matrix = Aurora::Matrix::fromRawData(data, 3);
    auto result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
    EXPECT_DOUBLE_AE(result,6);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm2);
    EXPECT_DOUBLE_AE(result,3.74166);
    result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
    EXPECT_DOUBLE_AE(result,3.74166);

    //2Dims
    data = new double[8]{1,2,-3,6,7,9,22.3,-8.6};
    matrix = Aurora::Matrix::fromRawData(data, 4,2);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
    EXPECT_DOUBLE_AE(result,46.9);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm2);
    EXPECT_DOUBLE_AE(result,26.7284);
    result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
    EXPECT_DOUBLE_AE(result,27.4089);

    //1Dim Complex
    data = new double[6]{1,2,-3,4,5,-6};
    matrix = Aurora::Matrix::fromRawData(data, 3,1,1,Aurora::Complex);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
    EXPECT_DOUBLE_AE(result,15.0463);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm2);
    EXPECT_DOUBLE_AE(result,9.5394);
    result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
    EXPECT_DOUBLE_AE(result,9.5394);

    //2Dims Complex
    data = new double[12]{1,2,-3,4,5,-6,7,8,9,22,24,25};
    matrix = Aurora::Matrix::fromRawData(data, 3,2,1,Aurora::Complex);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
    EXPECT_DOUBLE_AE(result,69.0553);
    result = Aurora::norm(matrix,Aurora::NormMethod::Norm2);
    EXPECT_DOUBLE_AE(result,43.5314);
    result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
    EXPECT_DOUBLE_AE(result,44.3847);
}

TEST_F(Function1D_Test, transpose) {
    double *data = new double[6]{1,2,3,4,5,6};
    auto matrix = Aurora::Matrix::fromRawData(data, 3,2);
    auto result = Aurora::transpose(matrix);
    EXPECT_DOUBLE_EQ(result.getData()[0],1);
    EXPECT_DOUBLE_EQ(result.getData()[1],4);
    EXPECT_DOUBLE_EQ(result.getData()[2],2);
    EXPECT_DOUBLE_EQ(result.getData()[3],5);
    EXPECT_DOUBLE_EQ(result.getData()[4],3);
    EXPECT_DOUBLE_EQ(result.getData()[5],6);
    EXPECT_DOUBLE_EQ(result.getDimSize(0),2);
    EXPECT_DOUBLE_EQ(result.getDimSize(1),3);

    data = new double[12]{1,2,3,4,5,6,7,8,9,10,11,12};
    matrix = Aurora::Matrix::fromRawData(data, 3,2,1,Aurora::Complex);
    result = Aurora::transpose(matrix);
    EXPECT_DOUBLE_EQ(result.getData()[0],1);
    EXPECT_DOUBLE_EQ(result.getData()[1],2);
    EXPECT_DOUBLE_EQ(result.getData()[2],7);
    EXPECT_DOUBLE_EQ(result.getData()[3],8);
    EXPECT_DOUBLE_EQ(result.getData()[4],3);
    EXPECT_DOUBLE_EQ(result.getData()[5],4);
    EXPECT_DOUBLE_EQ(result.getData()[6],9);
    EXPECT_DOUBLE_EQ(result.getData()[7],10);
    EXPECT_DOUBLE_EQ(result.getData()[8],5);
    EXPECT_DOUBLE_EQ(result.getData()[9],6);
    EXPECT_DOUBLE_EQ(result.getData()[10],11);
    EXPECT_DOUBLE_EQ(result.getData()[11],12);
    EXPECT_DOUBLE_EQ(result.getDimSize(0),2);
    EXPECT_DOUBLE_EQ(result.getDimSize(1),3);
    EXPECT_DOUBLE_EQ(result.getDimSize(0),2);
    EXPECT_DOUBLE_EQ(result.getDimSize(1),3);
}

TEST_F(Function1D_Test, horzcat) {
    double *data1 = new double[6]{1,2,3,4,5,6};
    auto matrix1 = Aurora::Matrix::fromRawData(data1, 3,2);
    double *data2 = new double[9]{7,8,9,10,11,12,13,14,15};
    auto matrix2 = Aurora::Matrix::fromRawData(data2, 3,3);
    auto result = Aurora::horzcat(matrix1,matrix2);
    EXPECT_DOUBLE_EQ(result.getData()[0],1);
    EXPECT_DOUBLE_EQ(result.getData()[1],2);
    EXPECT_DOUBLE_EQ(result.getData()[10],11);
    EXPECT_DOUBLE_EQ(result.getData()[14],15);
    EXPECT_DOUBLE_EQ(result.getDimSize(0),3);
    EXPECT_DOUBLE_EQ(result.getDimSize(1),5);

    data1 = new double[6]{1,2,3,4,5,6};
    matrix1 = Aurora::Matrix::fromRawData(data1, 3,1,1,Aurora::Complex);
    data2 = new double[6]{7,8,9,10,11,12};
    matrix2 = Aurora::Matrix::fromRawData(data2, 3,1,1,Aurora::Complex);
    result = Aurora::horzcat(matrix1,matrix2);
    EXPECT_DOUBLE_EQ(result.getData()[0],1);
    EXPECT_DOUBLE_EQ(result.getData()[1],2);
    EXPECT_DOUBLE_EQ(result.getData()[8],5);
    EXPECT_DOUBLE_EQ(result.getData()[9],6);
    EXPECT_DOUBLE_EQ(result.getDimSize(0),3);
    EXPECT_DOUBLE_EQ(result.getDimSize(1),2);
}

TEST_F(Function1D_Test, vecnrom) {
    //1Dim
    double *data = new double[3]{1,2,-3};
    auto matrix = Aurora::Matrix::fromRawData(data, 3);
    auto result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
    EXPECT_DOUBLE_AE(result.getData()[0],6);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
    EXPECT_DOUBLE_AE(result.getData()[0],3.74166);

    //2Dims
    data = new double[8]{1,2,-3,6,7,9,22.3,-8.6};
    matrix = Aurora::Matrix::fromRawData(data, 4,2);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
    EXPECT_DOUBLE_AE(result.getData()[0],12);
    EXPECT_DOUBLE_AE(result.getData()[1],46.9);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
    EXPECT_DOUBLE_AE(result.getData()[0],7.0711);
    EXPECT_DOUBLE_AE(result.getData()[1],26.4811);

    //1Dim Complex
    data = new double[6]{1,2,-3,4,5,-6};
    matrix = Aurora::Matrix::fromRawData(data, 3,1,1,Aurora::Complex);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
    EXPECT_DOUBLE_AE(result.getData()[0],15.0463);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
    EXPECT_DOUBLE_AE(result.getData()[0],9.5394);

    //2Dims Complex
    data = new double[12]{1,2,-3,4,5,-6,7,8,9,22,24,25};
    matrix = Aurora::Matrix::fromRawData(data, 3,2,1,Aurora::Complex);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
    EXPECT_DOUBLE_AE(result.getData()[0],15.0463);
    EXPECT_DOUBLE_AE(result.getData()[1],69.0553);
    result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
    EXPECT_DOUBLE_AE(result.getData()[0],9.5394);
    EXPECT_DOUBLE_AE(result.getData()[1],43.3474);
}

TEST_F(Function1D_Test, linspace) {
    auto result = Aurora::linspace(-5,5,7);
    EXPECT_DOUBLE_AE(result.getData()[0],-5);
    EXPECT_DOUBLE_AE(result.getData()[1],-3.3333);
    EXPECT_DOUBLE_AE(result.getData()[2],-1.6667);
    EXPECT_DOUBLE_AE(result.getData()[3],0);
    EXPECT_DOUBLE_AE(result.getData()[4],1.6667);
    EXPECT_DOUBLE_AE(result.getData()[5], 3.3333);
    EXPECT_DOUBLE_AE(result.getData()[6], 5.0000);
}

TEST_F(Function1D_Test, auroraUnion) {
    double* data1 = new double[9]{3,3,2,2,2,1,4,4,7};
    auto matrix1 = Aurora::Matrix::fromRawData(data1, 9,1,1);
    double* data2 = new double[8]{6,6,7,7,8,1,2};
    auto matrix2 = Aurora::Matrix::fromRawData(data2, 7,1,1);

    auto result = Aurora::auroraUnion(matrix1, matrix2);
    EXPECT_DOUBLE_AE(result.getData()[0],1);
    EXPECT_DOUBLE_AE(result.getData()[1],2);
    EXPECT_DOUBLE_AE(result.getData()[2],3);
    EXPECT_DOUBLE_AE(result.getData()[3],4);
    EXPECT_DOUBLE_AE(result.getData()[4],6);
    EXPECT_DOUBLE_AE(result.getData()[5],7);
    EXPECT_DOUBLE_AE(result.getData()[6],8);
}

TEST_F(Function1D_Test, intersect) {
    double* data1 = new double[9]{3,3,2,2,2,1,4,4,7};
    auto matrix1 = Aurora::Matrix::fromRawData(data1, 9,1,1);
    double* data2 = new double[8]{6,6,7,7,8,1,2};
    auto matrix2 = Aurora::Matrix::fromRawData(data2, 7,1,1);

    auto result = Aurora::intersect(matrix1, matrix2);
    EXPECT_DOUBLE_AE(result.getData()[0],1);
    EXPECT_DOUBLE_AE(result.getData()[1],2);
    EXPECT_DOUBLE_AE(result.getData()[2],7);

    Aurora::Matrix ia;
    result = Aurora::intersect(matrix1, matrix2, ia);
    EXPECT_DOUBLE_AE(result.getData()[0],1);
    EXPECT_DOUBLE_AE(result.getData()[1],2);
    EXPECT_DOUBLE_AE(result.getData()[2],7);
    EXPECT_DOUBLE_AE(ia.getData()[0],6);
    EXPECT_DOUBLE_AE(ia.getData()[1],3);
    EXPECT_DOUBLE_AE(ia.getData()[2],9);
}

TEST_F(Function1D_Test, reshape) {
    double* data = new double[9]{3,3,2,2,2,1,4,4,7};
    auto matrix = Aurora::Matrix::fromRawData(data, 9,1,1);
    auto result = Aurora::reshape(matrix,3,3,1);
    EXPECT_DOUBLE_AE(result.getDimSize(0),3);
    EXPECT_DOUBLE_AE(result.getDimSize(1),3);
    EXPECT_DOUBLE_AE(result.getDimSize(2),1);
    result = Aurora::reshape(matrix,3,1,3);
    EXPECT_DOUBLE_AE(result.getDimSize(0),3);
    EXPECT_DOUBLE_AE(result.getDimSize(1),1);
    EXPECT_DOUBLE_AE(result.getDimSize(2),3);
    result = Aurora::reshape(matrix,1,3,3);
    EXPECT_DOUBLE_AE(result.getDimSize(0),1);
    EXPECT_DOUBLE_AE(result.getDimSize(1),3);
    EXPECT_DOUBLE_AE(result.getDimSize(2),3);
}

TEST_F(Function1D_Test, padding) {
    double *input =  new double[18]{10,2,1,3,4,4,16,3,1,2,15,-2,1,-3,4,-4,1,-3};
    auto ma = Aurora::Matrix::fromRawData(input,18,1,1);
    Aurora::padding(ma,20,1);
    auto result = ma.getData();

}

TEST_F(Function1D_Test, convertfp16tofloat) {
    MatlabReader m("/home/krad/TestData/convertReal.mat");

    size_t count = 0;
    auto input = m.readint16("input",count);
    for(int i = 0; i<1000; i++){
        auto resultM = Aurora::convertfp16tofloat(input.get(),count,1);
    }
    auto resultM = Aurora::convertfp16tofloat(input.get(),count,1);
    auto result = resultM.getData();
    auto output = m.read("output");
    for (size_t i = 0; i<count; i++) {
        EXPECT_EQ(result[i], output.getData()[i])<<"index:"<<i<<",input:"<<input.get()[i];
    }
}

TEST_F(Function1D_Test, nanAndFinite){
    double n=std::nan("");
    double infinite=std::numeric_limits<double>::infinity();

    double* data = new double[9]{infinite,3,n,2,2,1,4,4,7};
    auto matrix = Aurora::Matrix::fromRawData(data, 9,1,1);

    auto r = Aurora::isnan(matrix);
    EXPECT_DOUBLE_EQ(r.getData()[0],0);
    EXPECT_DOUBLE_EQ(r.getData()[1],0);
    EXPECT_DOUBLE_EQ(r.getData()[2],1);

    r = Aurora::isfinite(matrix);
    EXPECT_DOUBLE_EQ(r.getData()[0],0);
    EXPECT_DOUBLE_EQ(r.getData()[1],1);
    EXPECT_DOUBLE_EQ(r.getData()[2],0);

    Aurora::nantoval(matrix,1);
    EXPECT_DOUBLE_EQ(matrix.getData()[2],1);

    double* data2 = new double[1]{n};
    auto matrix2 = Aurora::Matrix::fromRawData(data, 1,1,1);
    r = Aurora::isnan(matrix2);
    EXPECT_DOUBLE_EQ(r.getData()[0],1);

    r = Aurora::isfinite(matrix2);
    EXPECT_DOUBLE_EQ(r.getData()[0],0);

    Aurora::nantoval(matrix2,1);
    EXPECT_DOUBLE_EQ(matrix.getData()[0],1);
}
