176 lines
5.9 KiB
C++
176 lines
5.9 KiB
C++
#include <gtest/gtest.h>
|
||
#include "Matrix.h"
|
||
#include "Function.h"
|
||
#include "Function1D.h"
|
||
|
||
|
||
class FunctionTester:public ::testing::Test{
|
||
protected:
|
||
static void SetUpFunctionTester(){
|
||
|
||
}
|
||
static void TearDownTestCase(){
|
||
}
|
||
void SetUp(){
|
||
}
|
||
void TearDown(){
|
||
}
|
||
};
|
||
|
||
double fourDecimalRound(double src){
|
||
return round(src*10000.0)/10000.0;
|
||
}
|
||
|
||
TEST_F(FunctionTester, matrixSlice) {
|
||
double * dataA =Aurora::malloc(8);
|
||
double * dataB =Aurora::malloc(8);
|
||
for (int i = 0; i < 8; ++i) {
|
||
dataA[i]=(double)(i-3);
|
||
dataB[i]=(double)(i+2);
|
||
}
|
||
Aurora::Matrix A = Aurora::Matrix::New(dataA,2,2,2);
|
||
printf("A:\r\n");
|
||
A.printf();
|
||
Aurora::Matrix B = Aurora::Matrix::New(dataB,2,2,2);
|
||
printf("B:\r\n");
|
||
B.printf();
|
||
A(Aurora::$,Aurora::$,1) = B(Aurora::$,Aurora::$,0);
|
||
printf("New A:\r\n");
|
||
A.printf();
|
||
printf("New B:\r\n");
|
||
B.printf();
|
||
}
|
||
|
||
TEST_F(FunctionTester, RawDataMatrix) {
|
||
double * dataA =new double[8];
|
||
double * dataB =new double[8];
|
||
for (int i = 0; i < 8; ++i) {
|
||
dataA[i]=(double)(i-3);
|
||
dataB[i]=(double)(i+2);
|
||
}
|
||
Aurora::Matrix A = Aurora::Matrix::fromRawData(dataA,2,2,2);
|
||
printf("A:\r\n");
|
||
A.printf();
|
||
Aurora::Matrix B = Aurora::Matrix::copyFromRawData(dataB,2,2,2);
|
||
delete [] dataB;
|
||
printf("B:\r\n");
|
||
B.printf();
|
||
A(Aurora::$,Aurora::$,1) = B(Aurora::$,Aurora::$,0);
|
||
printf("New A:\r\n");
|
||
A.printf();
|
||
printf("New B:\r\n");
|
||
B.printf();
|
||
}
|
||
|
||
TEST_F(FunctionTester, 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);
|
||
printf("A:\r\n");
|
||
A.printf();
|
||
Aurora::Matrix B = Aurora::Matrix::New(dataB,3,3);
|
||
printf("B:\r\n");
|
||
B.printf();
|
||
printf("sign(A):\r\n");
|
||
sign(A*B).printf();
|
||
}
|
||
|
||
TEST_F(FunctionTester, matrix) {
|
||
double * dataA =Aurora::malloc(9);
|
||
double * dataB =Aurora::malloc(9);
|
||
for (int i = 0; i < 9; ++i) {
|
||
dataA[i]=(double)(i+1);
|
||
dataB[i]=(double)(i+2);
|
||
}
|
||
Aurora::Matrix A = Aurora::Matrix::New(dataA,3,3);
|
||
A.printf();
|
||
Aurora::Matrix B = Aurora::Matrix::New(dataB,3,3);
|
||
printf("B:\r\n");
|
||
B.printf();
|
||
printf("A*B:\r\n");
|
||
(A*B).printf();
|
||
printf("A*B+A:\r\n");
|
||
(A*B+A).printf();
|
||
A = (A*B+A)+3.;
|
||
A.printf();
|
||
}
|
||
|
||
|
||
TEST_F(FunctionTester, immse){
|
||
double dataA[9]={1,2,3,4,5,6,7,8,9};
|
||
double dataB[9]={10,20,30,40,50,40,30,20,10};
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(Aurora::immse(dataA,dataB,9)),698.3333)<<" immse error;";
|
||
}
|
||
|
||
TEST_F(FunctionTester, polyval){
|
||
double dataP[3]={3,2,1};
|
||
double dataX[3]={5,7,9};
|
||
double*resultP = Aurora::polyval(dataX,dataP,3);
|
||
EXPECT_DOUBLE_EQ(86., resultP[0])<<" polyval error;";
|
||
EXPECT_DOUBLE_EQ(162., resultP[1])<<" polyval error;";
|
||
EXPECT_DOUBLE_EQ(262., resultP[2])<<" polyval error;";
|
||
delete [] resultP;
|
||
}
|
||
|
||
TEST_F(FunctionTester, std){
|
||
double dataMA[9]={1, 2, 3, 2, 2, 6, 3, 3, 6};
|
||
double* resultStd = Aurora::std(3, 3, dataMA);
|
||
EXPECT_DOUBLE_EQ(1.0, resultStd[0])<<" std error index 0";
|
||
EXPECT_DOUBLE_EQ(2.3094, fourDecimalRound(resultStd[1]))<<" std error index 1";
|
||
EXPECT_DOUBLE_EQ(1.7321, fourDecimalRound(resultStd[2]))<<" std error index 2";
|
||
delete [] resultStd;
|
||
}
|
||
|
||
TEST_F(FunctionTester, fftAndComplexAndIfft){
|
||
double input[10]{1,1,0,2,2,0,1,1,0,2};
|
||
std::complex<double>* complexInput = Aurora::complex(10,input);
|
||
//复数化后,实部不变,虚部全为0
|
||
EXPECT_DOUBLE_EQ(complexInput[1].real(),1.0)<<" complex error";
|
||
EXPECT_DOUBLE_EQ(complexInput[1].imag(),0)<<" complex error";
|
||
std::complex<double>* result = Aurora::fft(10,complexInput);
|
||
delete [] complexInput;
|
||
//检验fft结果与matlab是否对应
|
||
EXPECT_DOUBLE_EQ(0.0729, fourDecimalRound(result[1].real()))<<" fft result value error";
|
||
EXPECT_DOUBLE_EQ(2.4899, fourDecimalRound(result[2].imag()))<<" fft result value error";
|
||
//检验fft的结果是否共轭
|
||
EXPECT_DOUBLE_EQ(0, result[4].imag()+result[6].imag())<<" fft result conjugate error";
|
||
EXPECT_DOUBLE_EQ(0, result[4].real()-result[6].real())<<" fft result conjugate error";
|
||
std::complex<double>* ifftResult = Aurora::ifft(10,result);
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(ifftResult[1].real()),1.0)<<" ifft result real value error";
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(ifftResult[1].imag()),0)<<" ifft result imag value error";
|
||
delete [] result;
|
||
delete [] ifftResult;
|
||
}
|
||
|
||
TEST_F(FunctionTester, inv){
|
||
//默认是column major排布数据
|
||
double dataMA[9]={2, 0, 2, 2, 3, 0, 3, 3, 3};
|
||
double* result = Aurora::inv(3,dataMA);
|
||
EXPECT_DOUBLE_EQ(0.75, fourDecimalRound(result[0]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(0.5, fourDecimalRound(result[1]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[2]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[3]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(.0, fourDecimalRound(result[4]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(0.3333, fourDecimalRound(result[5]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(-0.25, fourDecimalRound(result[6]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[7]))<<" inv value error";
|
||
EXPECT_DOUBLE_EQ(0.5, fourDecimalRound(result[8]))<<" inv value error";
|
||
delete [] result;
|
||
}
|
||
|
||
TEST_F(FunctionTester, hilbert) {
|
||
double input[10]{1,1,0,2,2,0,1,1,0,2};
|
||
auto result = Aurora::hilbert(10,input);
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(result[1].real()),1.0)<<" hilbert result real value error";
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(result[1].imag()),0.3249)<<" hilbert result imag value error";
|
||
delete [] result;
|
||
result = Aurora::hilbert(9,input);
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(result[1].real()),1.0)<<" hilbert result real value error";
|
||
EXPECT_DOUBLE_EQ(fourDecimalRound(result[1].imag()),0.4253)<<" hilbert result imag value error";
|
||
}
|
||
|