testmath.cpp

来自「ncbi源码」· C++ 代码 · 共 625 行 · 第 1/2 页

CPP
625
字号
/* * =========================================================================== * PRODUCTION $Log: testmath.cpp,v $ * PRODUCTION Revision 1000.2  2004/06/01 20:50:11  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5 * PRODUCTION * =========================================================================== *//*  $Id: testmath.cpp,v 1000.2 2004/06/01 20:50:11 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors:  Mike DiCuccio * * File Description: * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbiargs.hpp>#include <corelib/ncbienv.hpp>#include <corelib/ncbitime.hpp>#include <gui/math/vect3.hpp>#include <gui/math/matrix3.hpp>#include <gui/math/matrix4.hpp>#include <util/math/matrix.hpp>USING_SCOPE(ncbi);///////////////////////////////////////////////////////////////////////////////  CTestmathApp::class CTestmathApp : public CNcbiApplication{public:    CTestmathApp();private:    virtual void Init(void);    virtual int  Run(void);    virtual void Exit(void);};CTestmathApp::CTestmathApp(){}///////////////////////////////////////////////////////////////////////////////  Init test for all different types of argumentsvoid CTestmathApp::Init(void){    // Create command - line argument descriptions class    auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);    // Specify USAGE context    arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),                              "Math library test app");    arg_desc->AddDefaultKey("iters", "Iterations",                            "Number of iterations for timing tests",                            CArgDescriptions::eInteger,#ifdef _DEBUG                            "5"#else                            "500"#endif                           );    // Setup arg.descriptions for this application    SetupArgDescriptions(arg_desc.release());}////// test vector additionvoid testVectorAdd(){    cout << "testing vector addition...";    cout.flush();    {{         CVect3<float> vec0(1.0f, 1.0f, 1.0f);         CVect3<float> vec1(2.0f, 2.0f, 2.0f);         vec0 = vec0 + vec1;         _ASSERT(vec0[0] == 3.0f);         _ASSERT(vec0[1] == 3.0f);         _ASSERT(vec0[2] == 3.0f);         if (vec0[0] != 3.0f  ||             vec0[1] != 3.0f  ||             vec0[2] != 3.0f) {             string msg("vector addition of CVect3<> incorrect");             NCBI_THROW(CException, eUnknown, msg);         }     }}    {{         CVect4<float> vec0(1.0f, 1.0f, 1.0f, 1.0f);         CVect4<float> vec1(2.0f, 2.0f, 2.0f, 2.0f);         vec0 = vec0 + vec1;         _ASSERT(vec0[0] == 3.0f);         _ASSERT(vec0[1] == 3.0f);         _ASSERT(vec0[2] == 3.0f);         _ASSERT(vec0[3] == 3.0f);         if (vec0[0] != 3.0f  ||             vec0[1] != 3.0f  ||             vec0[2] != 3.0f  ||             vec0[3] != 3.0f) {             string msg("vector addition of CVect4<> incorrect");             NCBI_THROW(CException, eUnknown, msg);         }     }}    cout << "passed." << endl;}////// test matrix subtractionvoid testVectorSubtract(){    cout << "testing vector subtraction...";    cout.flush();    {{         CVect3<float> vec0(1.0f, 1.0f, 1.0f);         CVect3<float> vec1(2.0f, 2.0f, 2.0f);         vec0 = vec0 - vec1;         _ASSERT(vec0[0] == -1.0f);         _ASSERT(vec0[1] == -1.0f);         _ASSERT(vec0[2] == -1.0f);         if (vec0[0] != -1.0f  ||             vec0[1] != -1.0f  ||             vec0[2] != -1.0f) {             string msg("vector subtraction of CVect3<> incorrect");             NCBI_THROW(CException, eUnknown, msg);         }     }}    {{         CVect4<float> vec0(1.0f, 1.0f, 1.0f, 1.0f);         CVect4<float> vec1(2.0f, 2.0f, 2.0f, 2.0f);         vec0 = vec0 - vec1;         _ASSERT(vec0[0] == -1.0f);         _ASSERT(vec0[1] == -1.0f);         _ASSERT(vec0[2] == -1.0f);         _ASSERT(vec0[3] == -1.0f);         if (vec0[0] != -1.0f  ||             vec0[1] != -1.0f  ||             vec0[2] != -1.0f  ||             vec0[3] != -1.0f) {             string msg("vector subtraction of CVect4<> incorrect");             NCBI_THROW(CException, eUnknown, msg);         }     }}    cout << "passed." << endl;}////// test matrix additionvoid testMatrixAdd(){    cout << "testing matrix addition...";    cout.flush();    for (size_t r = 3;  r < 10;  ++r) {        for (size_t c = 3;  c < 10;  ++c) {            CNcbiMatrix<float> mat0(r, c, 1);            CNcbiMatrix<float> mat1(r, c, 2);            mat0 = mat0 + mat1;            ITERATE (CNcbiMatrix<float>::TData, iter, mat0.GetData()) {                _ASSERT(*iter == 3);                if (*iter != 3) {                    string msg("matrix addition of ");                    msg += NStr::IntToString(r) + "x";                    msg += NStr::IntToString(c) + " matrix incorrect";                    NCBI_THROW(CException, eUnknown, msg);                }            }        }    }    cout << "passed." << endl;}////// test matrix subtractionvoid testMatrixSubtract(){    cout << "testing matrix subtraction...";    cout.flush();    for (size_t r = 3;  r < 10;  ++r) {        for (size_t c = 3;  c < 10;  ++c) {            CNcbiMatrix<float> mat0(r, c, 1);            CNcbiMatrix<float> mat1(r, c, 2);            mat0 = mat0 - mat1;            ITERATE (CNcbiMatrix<float>::TData, iter, mat0.GetData()) {                _ASSERT(*iter == -1);                if (*iter != -1) {                    string msg("matrix subtraction of ");                    msg += NStr::IntToString(r) + "x";                    msg += NStr::IntToString(c) + " matrix incorrect";                    NCBI_THROW(CException, eUnknown, msg);                }            }        }    }    cout << "passed." << endl;}////// test vecrix multiplicationvoid testMatrixMult(){    cout << "testing matrix multiplication...";    cout.flush();    for (size_t r = 3;  r < 10;  ++r) {        for (size_t c = 3;  c < 10;  ++c) {            const float lhs_val = 1;            const float rhs_val = 2;            CNcbiMatrix<float> mat0(r, c, lhs_val);            CNcbiMatrix<float> mat1(c, r, rhs_val);            mat0 = mat0 * mat1;            float val = 0;            for (size_t cc = 0;  cc < c;  ++cc) {                val += lhs_val * rhs_val;            }            ITERATE (CNcbiMatrix<float>::TData, iter, mat0.GetData()) {                _ASSERT(*iter == val);                if (*iter != val) {                    string msg("matrix multiplication of ");                    msg += NStr::IntToString(r) + "x";                    msg += NStr::IntToString(c) + " matrix incorrect";                    NCBI_THROW(CException, eUnknown, msg);                }            }        }    }    cout << "passed." << endl;}////// test determinant calculationsvoid testDeterminant(){    CNcbiMatrix<float> mat4(4,4);    /**      mat4[ 0] =  1;      mat4[ 1] =  2;      mat4[ 2] =  0;      mat4[ 3] = -2;      mat4[ 4] =  0;      mat4[ 5] =  0;      mat4[ 6] =  2;      mat4[ 7] = -1;      mat4[ 8] =  0;      mat4[ 9] = -1;      mat4[10] =  1;      mat4[11] =  0;      mat4[12] =  1;      mat4[13] =  3;      mat4[14] =  4;      mat4[15] =  1;     **/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?