⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_ncbistr.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: test_ncbistr.cpp,v $ * PRODUCTION Revision 1000.2  2004/06/01 19:10:19  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.28 * PRODUCTION * =========================================================================== *//*  $Id: test_ncbistr.cpp,v 1000.2 2004/06/01 19:10:19 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. * * =========================================================================== * * Author:  Denis Vakatov * * File Description: *   TEST for:  NCBI C++ core string-related API * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbienv.hpp>#include <corelib/ncbireg.hpp>#include <algorithm>#include <test/test_assert.h>  /* This header must go last */// This is to use the ANSI C++ standard templates without the "std::" prefix// and to use NCBI C++ entities without the "ncbi::" prefixUSING_NCBI_SCOPE;/////////////////////////////////// Utilities//static void TestStrings_StrCompare(int expr_res, int valid_res){    int res = expr_res > 0 ? 1 :        expr_res == 0 ? 0 : -1;    assert(res == valid_res);}typedef struct {    const char* s1;    const char* s2;    int case_res;      /* -1, 0, 1 */    int nocase_res;    /* -1, 0, 1 */    SIZE_TYPE n;     int n_case_res;    /* -1, 0, 1 */    int n_nocase_res;  /* -1, 0, 1 */} SStrCompare;static const SStrCompare s_StrCompare[] = {    { "", "",  0, 0,  0,     0, 0 },    { "", "",  0, 0,  NPOS,  0, 0 },    { "", "",  0, 0,  10,    0, 0 },    { "", "",  0, 0,  1,     0, 0 },    { "a", "",  1, 1,  0,     0, 0 },    { "a", "",  1, 1,  1,     1, 1 },    { "a", "",  1, 1,  2,     1, 1 },    { "a", "",  1, 1,  NPOS,  1, 1 },    { "", "bb",  -1, -1,  0,     -1, -1 },    { "", "bb",  -1, -1,  1,     -1, -1 },    { "", "bb",  -1, -1,  2,     -1, -1 },    { "", "bb",  -1, -1,  3,     -1, -1 },    { "", "bb",  -1, -1,  NPOS,  -1, -1 },    { "ba", "bb",  -1, -1,  0,     -1, -1 },    { "ba", "bb",  -1, -1,  1,     -1, -1 },    { "ba", "b",    1,  1,  1,      0,  0 },    { "ba", "bb",  -1, -1,  2,     -1, -1 },    { "ba", "bb",  -1, -1,  3,     -1, -1 },    { "ba", "bb",  -1, -1,  NPOS,  -1, -1 },    { "a", "A",  1, 0,  0,    -1, -1 },    { "a", "A",  1, 0,  1,     1,  0 },    { "a", "A",  1, 0,  2,     1,  0 },    { "a", "A",  1, 0,  NPOS,  1,  0 },    { "A", "a",  -1, 0,  0,     -1, -1 },    { "A", "a",  -1, 0,  1,     -1,  0 },    { "A", "a",  -1, 0,  2,     -1,  0 },    { "A", "a",  -1, 0,  NPOS,  -1,  0 },    { "ba", "ba1",  -1, -1,  0,     -1, -1 },    { "ba", "ba1",  -1, -1,  1,     -1, -1 },    { "ba", "ba1",  -1, -1,  2,     -1, -1 },    { "bA", "ba",   -1,  0,  2,     -1,  0 },    { "ba", "ba1",  -1, -1,  3,     -1, -1 },    { "ba", "ba1",  -1, -1,  NPOS,  -1, -1 },    { "ba1", "ba",  1, 1,  0,    -1, -1 },    { "ba1", "ba",  1, 1,  1,    -1, -1 },    { "ba1", "ba",  1, 1,  2,     0,  0 },    { "ba",  "bA",  1, 0,  2,     1,  0 },    { "ba1", "ba",  1, 1,  3,     1,  1 },    { "ba1", "ba",  1, 1,  NPOS,  1,  1 },    { "ba1", "ba",  1, 1,  NPOS,  1,  1 }};typedef struct {    const char* pchar;    // String input    int         base;     // Radix base     Uint8       expected; // Expected value} SRadixTest;/////////////////////////////////// Test application//class CTestApplication : public CNcbiApplication{public:    void Init(void);    int Run(void);};void CTestApplication::Init(void){    // Set err.-posting and tracing to maximum    SetDiagTrace(eDT_Enable);    SetDiagPostFlag(eDPF_All);    SetDiagPostLevel(eDiag_Info);}//#define TEST_MEMORY_USAGE#ifdef TEST_MEMORY_USAGEenum ContainerType {    eVector,    eList,    eSet,    eRef};void DumpSize(void){    char buffer[1024];    NcbiCout << "Press enter..." << flush;    gets(buffer);}void TestAllocate(ContainerType cont, int size, int count){    switch ( cont ) {    case eVector:    {{        NcbiCout << "Allocating " << size <<            " vectors<int> with " << count << " elements" << NcbiEndl;        DumpSize();        vector<int>* v = new vector<int>[size];        for ( int i = 0; i < size; ++i ) {            for ( int j = 0; j < count; ++j ) {                v[i].push_back(j);            }        }        NcbiCout << "Allocated." << NcbiEndl;        DumpSize();        delete[] v;        break;    }}    case eList:    {{        NcbiCout << "Allocating " << size <<            " list<int> with " << count << " elements" << NcbiEndl;        DumpSize();        list<int>* v = new list<int>[size];        for ( int i = 0; i < size; ++i ) {            for ( int j = 0; j < count; ++j ) {                v[i].push_back(j);            }        }        NcbiCout << "Allocated." << NcbiEndl;        DumpSize();        delete[] v;        break;    }}    case eRef:    {{        NcbiCout << "Allocating " << size <<            " CRef<CObject> with " << count << " elements" << NcbiEndl;        DumpSize();        CRef<CObject>* v = new CRef<CObject>[size];        for ( int i = 0; i < size; ++i ) {            for ( int j = 0; j < count; ++j ) {                v[i].Reset(new CObject);            }        }        NcbiCout << "Allocated." << NcbiEndl;        DumpSize();        delete[] v;        break;    }}    }}void Test(ContainerType cont){    for ( int j = 0; j <= 2; ++j ) {        TestAllocate(cont, 100000, j);    }    for ( int j = 0; j <= 2; ++j ) {        TestAllocate(cont, 1000000, j);    }}#endifint CTestApplication::Run(void){#ifdef TEST_MEMORY_USAGE    Test(eRef);#endif    static const string s_Strings[] = {        "",        ".",        "..",        ".0",        ".0.",        "..0",        ".01",        "1.",        "1.1",        "1.1.",        "1..",        "-2147483649",        "-2147483648",        "-1",        "0",        "2147483647",        "2147483648",        "4294967295",        "4294967296",        " 123 ",        "-324",        " 567",        "+890",        "zzz"    };    NcbiCout << "Test NCBISTR:" << NcbiEndl;    const size_t count = sizeof(s_Strings) / sizeof(s_Strings[0]);    //        CExceptionReporterStream reporter(cerr);    //        CExceptionReporter::SetDefault(&reporter);    //        CExceptionReporter::EnableDefault(false);    //        CExceptionReporter::EnableDefault(true);    //        CExceptionReporter::SetDefault(0);        SetupDiag(eDS_ToStdout);    /*          CExceptionReporter::EnableDefault(true);    cerr << endl;    NCBI_REPORT_EXCEPTION(    "****** default reporter (stream) ******",e);    CExceptionReporter::SetDefault(0);    cerr << endl;    NCBI_REPORT_EXCEPTION(    "****** default reporter (diag) ******",e);    */        for (size_t i = 0;  i < count;  ++i) {        const string& str = s_Strings[i];        NcbiCout << "\n*** Checking string '" << str << "'***" << NcbiEndl;        {{            int value = NStr::StringToNumeric(str);            NcbiCout << "numeric value: " << value << ", toString: '"                     << NStr::IntToString(value) << "'" << NcbiEndl;        }}        try {            int value = NStr::StringToInt(str);            NcbiCout << "int value: " << value << ", toString: '"                     << NStr::IntToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }        try {            unsigned int value = NStr::StringToUInt(str);            NcbiCout << "unsigned int value: " << value << ", toString: '"                     << NStr::UIntToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }        try {            long value = NStr::StringToLong(str);            NcbiCout << "long value: " << value << ", toString: '"                     << NStr::IntToString(value) << "'" << NcbiEndl;            NcbiCout << "      Int8ToString: '"                     << NStr::Int8ToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }        try {            unsigned long value = NStr::StringToULong(str);            NcbiCout << "unsigned long value: " << value << ", toString: '"                     << NStr::UIntToString(value) << "'" << NcbiEndl;            NcbiCout << "      UInt8ToString: '"                     << NStr::UInt8ToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }        try {            double value = NStr::StringToDouble(str);            NcbiCout << "double value: " << value << ", toString: '"                     << NStr::DoubleToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }                try {            Int8 value = NStr::StringToInt8(str);            NcbiCout << "Int8 value: " << (int)value << ", toString: '"                     << NStr::Int8ToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }        try {            Uint8 value = NStr::StringToUInt8(str);            NcbiCout << "Uint8 value: " << (unsigned)value << ", toString: '"                     << NStr::UInt8ToString(value) << "'" << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }            }    // Writing separate tests for StringToUInt8 because we    // need to test for different radix values such as 2, 8, and 16    // and s_Strings presents string samples with radix of 10 only.    //    static const SRadixTest s_RadixTests[] = {        {"A", 16, 10},        {"B9", 16, 185},        {"C5D", 16, 3165},        {"FFFF", 16, 65535},        {"17ABCDEF", 16, 397135343},        {"BADBADBA", 16, 3134959034U},        {"7", 8, 7},        {"17", 8, 15},        {"177", 8, 127},        {"0123", 8, 83},        {"01234567", 8, 342391},        {"0", 2, 0},        {"1", 2, 1},        {"10", 2, 2},        {"11", 2, 3},        {"100", 2, 4},        {"101", 2, 5},         {"110", 2, 6},        {"111", 2, 7},        // Invalid values come next        {"10ABCDEFGH", 16, 0},        {"12345A", 10, 0},        {"012345678", 8, 0},        {"012", 2, 0}    };    for (size_t i = 0;           i < sizeof(s_RadixTests)/sizeof(s_RadixTests[0]);         ++i) {        const string& str = s_RadixTests[i].pchar;        int base = s_RadixTests[i].base;        Uint8 expected = s_RadixTests[i].expected;        NcbiCout << "Checking numeric string: '" << str                  << "': with base " << base << NcbiEndl;        try {            Uint8 value = NStr::StringToUInt8(str, base);            NcbiCout << "Uint8 value: " << ((unsigned)value)                     << ", Expected: " << (unsigned)expected << NcbiEndl;        }        catch (CException& e) {            NCBI_REPORT_EXCEPTION("TestStrings",e);        }    }     NcbiCout << NcbiEndl << "NStr::Replace() tests...";    string src("aaabbbaaccczzcccXX");    string dst;    string search("ccc");    string replace("RrR");    NStr::Replace(src, search, replace, dst);    assert(dst == "aaabbbaaRrRzzRrRXX");    search = "a";    replace = "W";    NStr::Replace(src, search, replace, dst, 6, 1);    assert(dst == "aaabbbWaccczzcccXX");        search = "bbb";    replace = "BBB";    NStr::Replace(src, search, replace, dst, 50);    assert(dst == "aaabbbaaccczzcccXX");    search = "ggg";    replace = "no";    dst = NStr::Replace(src, search, replace);    assert(dst == "aaabbbaaccczzcccXX");    search = "a";    replace = "A";    dst = NStr::Replace(src, search, replace);    assert(dst == "AAAbbbAAccczzcccXX");    search = "X";    replace = "x";    dst = NStr::Replace(src, search, replace, src.size() - 1);    assert(dst == "aaabbbaaccczzcccXx");    NcbiCout << " completed successfully!" << NcbiEndl;

⌨️ 快捷键说明

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