testcase_cmatrix.cpp
来自「c++ 实现的矩阵运算库」· C++ 代码 · 共 2,500 行 · 第 1/5 页
CPP
2,500 行
#include <cmath>
#include <float.h>
#include <stdio.h>
#include "TestCase_cmatrix.h"
#include "../cmatrix.h"
using namespace Zenautics;
#ifndef PI
#define PI (3.1415926535897932384626433832795) //!< better value
#endif
#ifndef TWOPI
#define TWOPI (6.283185307179586476925286766559) //!< 2.0*PI
#endif
#ifndef HALFPI
#define HALFPI (1.5707963267948966192313216916398) //!< PI/2.0
#endif
time_t t0, t1; // time_t is defined on <time.h> and <sys/types.h> as long
clock_t c0, c1; // clock_t is defined on <time.h> and <sys/types.h> as int
#define CMTX_TIMING_START() {t0 = time(NULL); c0 = clock();}
#ifndef _CRT_SECURE_NO_DEPRECATE
#define CMTX_TIMING_END(); {t1 = time(NULL); c1 = clock(); FILE* fid=NULL; if( fopen_s( &fid, "timing.txt", "a+" ) != 0 ){ printf( "\n\nUnable to open timing.txt\n\n" ); exit(1); } fprintf( fid, "%-80s: %10.1f ms\n", __FUNCTION__, (float) (c1 - c0)/CLOCKS_PER_SEC*1000.0 ); fclose(fid); }
#else
#define CMTX_TIMING_END(); {t1 = time(NULL); c1 = clock(); FILE* fid = fopen( "timing.txt", "a+" ); if( fid == NULL ) { printf( "\n\nUnable to open timing.txt\n\n" ); exit(1); } fprintf( fid, "%-80s: %10.1f ms\n", __FUNCTION__, (float) (c1 - c0)/CLOCKS_PER_SEC*1000.0 ); fclose(fid); }
#endif
TestCase_cmatrix::TestCase_cmatrix()
{
}
TestCase_cmatrix::~TestCase_cmatrix()
{
}
TestCase_cmatrix::TestCase_cmatrix( const TestCase_cmatrix& src )
{
// no use required
}
void TestCase_cmatrix::operator =( const TestCase_cmatrix& rhs )
{
//no use required
}
void TestCase_cmatrix::setUp()
{
}
void TestCase_cmatrix::tearDown()
{
}
void TestCase_cmatrix::Test_MTX_isConformalForMultiplication()
{
CMTX_TIMING_START();
unsigned nrows = 3;
unsigned ncols = 5;
MTX A;
MTX B;
BOOL result;
result = MTX_Init( &A );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &A, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_Init( &B );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &B, ncols, nrows, true );
CPPUNIT_ASSERT( result );
result = MTX_isConformalForMultiplication( &A, &B );
CPPUNIT_ASSERT( result );
result = MTX_Redim( &A, 5, 6 );
CPPUNIT_ASSERT( result );
result = MTX_isConformalForMultiplication( &A, &B );
CPPUNIT_ASSERT( result == false );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
result = MTX_Free( &B );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_isConformalForAddition()
{
CMTX_TIMING_START();
unsigned nrows = 3;
unsigned ncols = 5;
MTX A;
MTX B;
BOOL result;
result = MTX_Init( &A );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &A, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_Init( &B );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &B, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_isConformalForAddition( &A, &B );
CPPUNIT_ASSERT( result );
result = MTX_Redim( &A, 5, 6 );
CPPUNIT_ASSERT( result );
result = MTX_isConformalForAddition( &A, &B );
CPPUNIT_ASSERT( result == false );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
result = MTX_Free( &B );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_isSquare()
{
CMTX_TIMING_START();
unsigned nrows = 5;
unsigned ncols = 5;
MTX A;
BOOL result;
result = MTX_Init( &A );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &A, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_isSquare( &A );
CPPUNIT_ASSERT( result );
result = MTX_Redim( &A, 5, 6 );
CPPUNIT_ASSERT( result );
result = MTX_isSquare( &A );
CPPUNIT_ASSERT( result == false );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_isSameSize()
{
CMTX_TIMING_START();
unsigned nrows = 5;
unsigned ncols = 5;
MTX A;
MTX B;
BOOL result;
result = MTX_Init( &A );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &A, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_Init( &B );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &B, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_isSameSize( &A, &B );
CPPUNIT_ASSERT( result );
result = MTX_Redim( &A, 5, 6 );
CPPUNIT_ASSERT( result );
result = MTX_isSameSize( &A, &B );
CPPUNIT_ASSERT( result == false );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
result = MTX_Free( &B );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_Init()
{
CMTX_TIMING_START();
MTX M;
BOOL result;
result = MTX_Init( &M );
CPPUNIT_ASSERT( result );
result = MTX_isNull( &M );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_SetComment()
{
CMTX_TIMING_START();
MTX M;
BOOL result;
char *empty = NULL;
char *message = "Test Message";
result = MTX_Init( &M );
CPPUNIT_ASSERT( result );
result = MTX_SetComment( &M, empty );
CPPUNIT_ASSERT( result == false );
result = MTX_SetComment( &M, message );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( strcmp( "Test Message", M.comment ) == 0 );
result = MTX_SetComment( &M, message+1 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( strcmp( "est Message", M.comment ) == 0 );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_Free()
{
CMTX_TIMING_START();
unsigned nrows = 5;
unsigned ncols = 5;
MTX A;
MTX *B=NULL;
BOOL result;
result = MTX_Free( B );
CPPUNIT_ASSERT( result == false );
result = MTX_Init( &A );
CPPUNIT_ASSERT( result );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &A, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( A.data == NULL );
CPPUNIT_ASSERT( A.nrows == 0 && A.ncols == 0 );
result = MTX_Calloc( &A, nrows, ncols, false );
CPPUNIT_ASSERT( result );
result = MTX_Free( &A );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( A.data == NULL );
CPPUNIT_ASSERT( A.nrows == 0 && A.ncols == 0 );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_Alloc()
{
CMTX_TIMING_START();
unsigned nrows = 86400;
unsigned ncols = 5;
MTX M;
BOOL result;
result = MTX_Init( &M );
CPPUNIT_ASSERT( result );
// calloc
result = MTX_Calloc( &M, nrows, ncols, true );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( M.isReal );
CPPUNIT_ASSERT( M.ncols == ncols );
CPPUNIT_ASSERT( M.nrows == nrows );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
// calloc
result = MTX_Calloc( &M, nrows, ncols, false );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( !M.isReal );
CPPUNIT_ASSERT( M.ncols == ncols );
CPPUNIT_ASSERT( M.nrows == nrows );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
// malloc
result = MTX_Malloc( &M, nrows, ncols, true );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( M.isReal );
CPPUNIT_ASSERT( M.ncols == ncols );
CPPUNIT_ASSERT( M.nrows == nrows );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
// malloc
result = MTX_Malloc( &M, nrows, ncols, false );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( !M.isReal );
CPPUNIT_ASSERT( M.ncols == ncols );
CPPUNIT_ASSERT( M.nrows == nrows );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
result = MTX_Malloc( &M, nrows, 0, true );
CPPUNIT_ASSERT( result == false );
result = MTX_Malloc( &M, 0, ncols, true );
CPPUNIT_ASSERT( result == false );
result = MTX_Malloc( &M, nrows, 0, false );
CPPUNIT_ASSERT( result == false );
result = MTX_Malloc( &M, 0, ncols, false );
CPPUNIT_ASSERT( result == false );
// just in case
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_SetValue()
{
CMTX_TIMING_START();
MTX M;
BOOL result;
unsigned nrows = 2;
unsigned ncols = 2;
result = MTX_Init( &M );
CPPUNIT_ASSERT( result );
result = MTX_SetValue( &M, 0, 0, 1.0 );
CPPUNIT_ASSERT( result == false );
result = MTX_Calloc( &M, nrows, ncols, true );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( M.isReal );
result = MTX_SetValue( &M, 0, 0, 1.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetValue( &M, 1, 0, 2.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetValue( &M, 0, 1, 3.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetValue( &M, 1, 1, 4.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetValue( &M, 2, 2, 40.0 );
CPPUNIT_ASSERT( result == false );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.data[0][0], 1.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.data[0][1], 2.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.data[1][0], 3.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.data[1][1], 4.0, 1.0e-03 );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_SetComplexValue()
{
CMTX_TIMING_START();
MTX M;
BOOL result;
unsigned nrows = 2;
unsigned ncols = 2;
result = MTX_Init( &M );
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 0, 0, 1.0, 1.0 );
CPPUNIT_ASSERT( result == false );
result = MTX_Calloc( &M, nrows, ncols, false );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( M.isReal == false );
result = MTX_SetValue( &M, 0, 0, 1.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 1, 0, 2.0, 2.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 0, 1, 3.0, 3.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 1, 1, 4.0, 4.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 2, 2, 40.0, 50.0 );
CPPUNIT_ASSERT( result == false );
CPPUNIT_ASSERT( M.isReal == false );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][0].re, 1.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][0].im, 0.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][1].re, 2.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][1].im, 2.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][0].re, 3.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][0].im, 3.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][1].re, 4.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][1].im, 4.0, 1.0e-03 );
result = MTX_Calloc( &M, nrows, ncols, true ); // starts out real
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( M.isReal );
result = MTX_SetValue( &M, 0, 0, 1.0 ); // still real
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 1, 0, 2.0, 2.0 ); // now it's complex
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( M.isReal == false );
result = MTX_SetComplexValue( &M, 0, 1, 3.0, 3.0 );
CPPUNIT_ASSERT( result );
result = MTX_SetComplexValue( &M, 1, 1, 4.0, 4.0 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][0].re, 1.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][0].im, 0.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][1].re, 2.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[0][1].im, 2.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][0].re, 3.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][0].im, 3.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][1].re, 4.0, 1.0e-03 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( M.cplx[1][1].im, 4.0, 1.0e-03 );
result = MTX_Free( &M );
CPPUNIT_ASSERT( result );
CMTX_TIMING_END();
}
void TestCase_cmatrix::Test_MTX_Complex()
{
CMTX_TIMING_START();
MTX M;
MTX Re;
MTX Im;
BOOL result;
unsigned nrows = 2;
unsigned ncols = 2;
result = MTX_Init( &M );
CPPUNIT_ASSERT( result );
result = MTX_Init( &Re );
CPPUNIT_ASSERT( result );
result = MTX_Init( &Im );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &Re, nrows, ncols, true );
CPPUNIT_ASSERT( result );
result = MTX_Calloc( &Im, nrows, ncols, true );
CPPUNIT_ASSERT( result );
Re.data[0][0] = 1;
Re.data[0][1] = 2;
Re.data[1][0] = 3;
Re.data[1][1] = 4;
Im.data[0][0] = 4;
Im.data[0][1] = 3;
Im.data[1][0] = 2;
Im.data[1][1] = 1;
result = MTX_Complex( &M, &Re, &Im );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( !M.isReal );
CPPUNIT_ASSERT( M.cplx[0][0].re == 1 );
CPPUNIT_ASSERT( M.cplx[0][0].im == 4 );
CPPUNIT_ASSERT( M.cplx[0][1].re == 2 );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?