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

📄 test_ncbistr.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    // NStr::PrintableString()    assert(NStr::PrintableString(kEmptyStr).empty());    assert(NStr::PrintableString("AB\\CD\nAB\rCD\vAB\tCD\'AB\"").           compare("AB\\\\CD\\nAB\\rCD\\vAB\\tCD'AB\\\"") == 0);    assert(NStr::PrintableString("A\020B" + string(1, '\0') + "CD").           compare("A\\x10B\\x00CD") == 0);    // NStr::ParseEscapes    assert(NStr::ParseEscapes(kEmptyStr).empty());    assert(NStr::ParseEscapes("AB\\\\CD\\nAB\\rCD\\vAB\\tCD'AB\\\"").           compare("AB\\CD\nAB\rCD\vAB\tCD\'AB\"") == 0);    assert(NStr::ParseEscapes("A\\x10B\\x00CD").           compare("A\020B" + string(1, '\0') + "CD") == 0);    // NStr::Compare()    NcbiCout << NcbiEndl << "NStr::Compare() tests...";    size_t j;    const SStrCompare* rec;    for (j = 0;  j < sizeof(s_StrCompare) / sizeof(s_StrCompare[0]);  j++) {        rec = &s_StrCompare[j];        string s1 = rec->s1;        TestStrings_StrCompare            (NStr::Compare(s1, rec->s2, NStr::eCase), rec->case_res);        TestStrings_StrCompare            (NStr::Compare(s1, rec->s2, NStr::eNocase), rec->nocase_res);        TestStrings_StrCompare            (NStr::Compare(s1, 0, rec->n, rec->s2, NStr::eCase),             rec->n_case_res);        TestStrings_StrCompare            (NStr::Compare(s1, 0, rec->n, rec->s2, NStr::eNocase),             rec->n_nocase_res);        string s2 = rec->s2;        TestStrings_StrCompare            (NStr::Compare(s1, s2, NStr::eCase), rec->case_res);        TestStrings_StrCompare            (NStr::Compare(s1, s2, NStr::eNocase), rec->nocase_res);        TestStrings_StrCompare            (NStr::Compare(s1, 0, rec->n, s2, NStr::eCase),             rec->n_case_res);        TestStrings_StrCompare            (NStr::Compare(s1, 0, rec->n, s2, NStr::eNocase),             rec->n_nocase_res);    }    assert(NStr::Compare("0123", 0, 2, "12") <  0);    assert(NStr::Compare("0123", 1, 2, "12") == 0);    assert(NStr::Compare("0123", 2, 2, "12") >  0);    assert(NStr::Compare("0123", 3, 2,  "3") == 0);    NcbiCout << " completed successfully!" << NcbiEndl;    NcbiCout << NcbiEndl << "NStr::Split() tests...";    static const string s_SplitStr[] = {        "ab+cd+ef",        "aaAAabBbbb",        "-abc-def--ghijk---",        "a12c3ba45acb678bc"        "nodelim",        "emptydelim",        ""    };    static const string s_SplitDelim[] = {        "+", "AB", "-", "abc", "*", "", "*"    };    static const string split_result[] = {        "ab", "cd", "ef",        "aa", "ab", "bbb",        "abc", "def", "ghijk",        "12", "3", "45", "678",        "nodelim",        "emptydelim"    };    list<string> split;    for (size_t i = 0; i < sizeof(s_SplitStr) / sizeof(s_SplitStr[0]); i++) {        NStr::Split(s_SplitStr[i], s_SplitDelim[i], split);    }    {{        int i = 0;        ITERATE(list<string>, it, split) {            assert(NStr::Compare(*it, split_result[i++]) == 0);        }    }}        NcbiCout << " completed successfully!" << NcbiEndl;        // NStr::Tokenize()    NcbiCout << NcbiEndl << "NStr::Tokenize() tests...";    static const string s_TokStr[] = {        "ab+cd+ef",        "123;45,78",        "1;",        ";1",        "emptydelim"    };    static const string s_TokDelim[] = {        "+", ";,", ";", ";", ""    };    static const string tok_result[] = {        "ab", "cd", "ef",        "123", "45", "78",        "1", "",         "", "1",        "emptydelim"    };    vector<string> tok;    for (size_t i = 0; i < sizeof(s_TokStr) / sizeof(s_TokStr[0]); ++i) {        NStr::Tokenize(s_TokStr[i], s_TokDelim[i], tok);                   }    {{        int i = 0;        ITERATE(vector<string>, it, tok) {            assert(NStr::Compare(*it, tok_result[i++]) == 0);        }    }}        NcbiCout << " completed successfully!" << NcbiEndl;    // NStr::SplitInTwo()    NcbiCout << NcbiEndl << "NStr::SplitInTwo() tests...";    static const struct {        const char* str;        const char* delim;        const char* expected_str1;        const char* expected_str2;        bool    expected_ret;    }  s_SplitInTwoTest[] = {        { "ab+cd+ef", "+", "ab", "cd+ef", true },        { "aaAAabBbbb", "AB", "aa", "AabBbbb", true },        { "aaCAabBCbbb", "ABC", "aa", "AabBCbbb", true },        { "-beg-delim-", "-", "", "beg-delim-", true },        { "end-delim:", ":", "end-delim", "", true },        { "nodelim", ".,:;-+", "nodelim", "", false },        { "emptydelim", "", "emptydelim", "", false },        { "", "emtpystring", "", "", false },        { "", "", "", "", false }    };    {{        string  string1, string2;        bool    result;        for ( size_t i = 0;               i < sizeof(s_SplitInTwoTest) / sizeof(s_SplitInTwoTest[0]);              i++) {            result = NStr::SplitInTwo(s_SplitInTwoTest[i].str,                                      s_SplitInTwoTest[i].delim,                                      string1, string2);            assert(s_SplitInTwoTest[i].expected_ret == result);            assert(s_SplitInTwoTest[i].expected_str1 == string1);            assert(s_SplitInTwoTest[i].expected_str2 == string2);        }    }}        NcbiCout << " completed successfully!" << NcbiEndl;    NcbiCout << NcbiEndl << "NStr::ToLower/ToUpper() tests...";    static const struct {        const char* orig;        const char* x_lower;        const char* x_upper;    } s_Tri[] = {        { "", "", "" },        { "a", "a", "A" },        { "4", "4", "4" },        { "B5a", "b5a", "B5A" },        { "baObaB", "baobab", "BAOBAB" },        { "B", "b", "B" },        { "B", "b", "B" }    };    static const char s_Indiff[] =        "#@+_)(*&^%/?\"':;~`'\\!\v|=-0123456789.,><{}[]\t\n\r";    {{        char indiff[sizeof(s_Indiff) + 1];        ::strcpy(indiff, s_Indiff);        assert(NStr::Compare(s_Indiff, indiff) == 0);        assert(NStr::Compare(s_Indiff, NStr::ToLower(indiff)) == 0);        ::strcpy(indiff, s_Indiff);        assert(NStr::Compare(s_Indiff, NStr::ToUpper(indiff)) == 0);        assert(NStr::Compare(s_Indiff, NStr::ToLower(indiff)) == 0);    }}    {{        string indiff;        indiff = s_Indiff;        assert(NStr::Compare(s_Indiff, indiff) == 0);        assert(NStr::Compare(s_Indiff, NStr::ToLower(indiff)) == 0);        indiff = s_Indiff;        assert(NStr::Compare(s_Indiff, NStr::ToUpper(indiff)) == 0);        assert(NStr::Compare(s_Indiff, NStr::ToLower(indiff)) == 0);    }}    for (j = 0;  j < sizeof(s_Tri) / sizeof(s_Tri[0]);  j++) {        assert(NStr::Compare(s_Tri[j].orig, s_Tri[j].x_lower, NStr::eNocase)               == 0);        assert(NStr::Compare(s_Tri[j].orig, s_Tri[j].x_upper, NStr::eNocase)               == 0);        string orig = s_Tri[j].orig;        assert(NStr::Compare(orig, s_Tri[j].x_lower, NStr::eNocase)               == 0);        assert(NStr::Compare(orig, s_Tri[j].x_upper, NStr::eNocase)               == 0);        string x_lower = s_Tri[j].x_lower;        {{            char x_str[16];            ::strcpy(x_str, s_Tri[j].orig);            assert(::strlen(x_str) < sizeof(x_str));            assert(NStr::Compare(NStr::ToLower(x_str), x_lower) == 0);            ::strcpy(x_str, s_Tri[j].orig);            assert(NStr::Compare(NStr::ToUpper(x_str), s_Tri[j].x_upper) ==0);            assert(NStr::Compare(x_lower, NStr::ToLower(x_str)) == 0);        }}        {{            string x_str;            x_lower = s_Tri[j].x_lower;            x_str = s_Tri[j].orig;            assert(NStr::Compare(NStr::ToLower(x_str), x_lower) == 0);            x_str = s_Tri[j].orig;            assert(NStr::Compare(NStr::ToUpper(x_str), s_Tri[j].x_upper) ==0);            assert(NStr::Compare(x_lower, NStr::ToLower(x_str)) == 0);        }}    }    NcbiCout << " completed successfully!" << NcbiEndl;    NcbiCout << NcbiEndl << "AStrEquiv tests...";    string as1("abcdefg ");    string as2("abcdefg ");    string as3("aBcdEfg ");    string as4("lsekfu");    assert( AStrEquiv(as1, as2, PNocase()) == true );    assert( AStrEquiv(as1, as3, PNocase()) == true );    assert( AStrEquiv(as3, as4, PNocase()) == false );    assert( AStrEquiv(as1, as2, PCase())   == true );    assert( AStrEquiv(as1, as3, PCase())   == false );    assert( AStrEquiv(as2, as4, PCase())   == false );    NcbiCout << " completed successfully!" << NcbiEndl;    NcbiCout << NcbiEndl << "Equal{Case,Nocase} tests...";    assert( NStr::EqualNocase(as1, as2) == true );    assert( NStr::EqualNocase(as1, as3) == true );    assert( NStr::EqualNocase(as3, as4) == false );    assert( NStr::EqualCase(as1, as2)   == true );    assert( NStr::EqualCase(as1, as3)   == false );    assert( NStr::EqualCase(as2, as4)   == false );    NcbiCout << " completed successfully!" << NcbiEndl;    {{        NcbiCout << NcbiEndl                  << "Testing string reference counting properties:"                 << NcbiEndl;        string s1(10, '1');        string s2(s1);        if ( s1.data() != s2.data() ) {            NcbiCout << "BAD: string reference counting is OFF" << NcbiEndl;        }        else {            NcbiCout << "GOOD: string reference counting is ON" << NcbiEndl;            for ( int i = 0; i < 4; ++i ) {                NcbiCout << "Restoring reference counting"<<NcbiEndl;                s2 = s1;                if ( s1.data() != s2.data() ) {                    NcbiCout << "BAD: cannot restore string reference " \                        "counting" << NcbiEndl;                    continue;                }                NcbiCout << "GOOD: reference counting is ON" << NcbiEndl;                const char* type = i&1? "str.begin()": "str.c_str()";                NcbiCout << "Calling "<<type<<NcbiEndl;                if ( i&1 ) {                    s2.begin();                }                else {                    s1.c_str();                }                if ( s1.data() == s2.data() ) {                    NcbiCout << "GOOD: "<< type                              <<" doesn't affect reference counting"<< NcbiEndl;                    continue;                }                NcbiCout << "OK: "<< type                          << " turns reference counting OFF" << NcbiEndl;                NcbiCout << "Restoring reference counting"<<NcbiEndl;                s2 = s1;                if ( s1.data() != s2.data() ) {                    NcbiCout << "BAD: " << type                              <<" turns reference counting OFF completely"                             << NcbiEndl;                    continue;                }                NcbiCout << "GOOD: reference counting is ON" << NcbiEndl;                if ( i&1 ) continue;                NcbiCout << "Calling " << type << " on source" << NcbiEndl;                s1.c_str();                if ( s1.data() != s2.data() ) {                    NcbiCout << "BAD: "<< type                             << " on source turns reference counting OFF"                             << NcbiEndl;                }                NcbiCout << "Calling "<< type <<" on destination" << NcbiEndl;                s2.c_str();                if ( s1.data() != s2.data() ) {                    NcbiCout << "BAD: " << type                             <<" on destination turns reference counting OFF"                             << NcbiEndl;                }            }        }    }}    NcbiCout << NcbiEndl << "NStr::FindNoCase() tests...";    assert(NStr::FindNoCase(" abcd", " xyz") == NPOS);    assert(NStr::FindNoCase(" abcd", " xyz", 0, NPOS, NStr::eLast) == NPOS);    assert(NStr::FindNoCase(" abcd", " aBc", 0, NPOS, NStr::eLast) == 0);    NcbiCout << " completed successfully!" << NcbiEndl;    NcbiCout << NcbiEndl << "TEST_NCBISTR execution completed successfully!"             << NcbiEndl << NcbiEndl << NcbiEndl;    return 0;}  /////////////////////////////////// APPLICATION OBJECT and MAIN//int main(int argc, const char* argv[] /*, const char* envp[]*/){    CTestApplication theTestApplication;    return theTestApplication.AppMain(argc, argv, 0 /*envp*/, eDS_ToMemory);}/* * ========================================================================== * $Log: test_ncbistr.cpp,v $ * Revision 1000.2  2004/06/01 19:10:19  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.28 * * Revision 6.28  2004/05/26 20:46:54  ucko * Add tests for Equal{Case,Nocase}. * * Revision 6.27  2004/05/26 19:26:29  ucko * Add a(n incomplete) set of tests for FindNoCase. * * Revision 6.26  2004/05/14 13:59:51  gorelenk * Added include of ncbi_pch.hpp * * Revision 6.25  2003/12/02 15:23:25  ucko * Add tests for NStr::ParseEscapes (inverted from tests for PrintableString) * * Revision 6.24  2003/10/01 20:43:30  ivanov * Get rid of compilation warnings; some formal code rearrangement * * Revision 6.23  2003/08/19 15:17:20  rsmith * Add NStr::SplitInTwo() function. * * Revision 6.22  2003/07/23 17:49:55  vasilche * Commented out memory usage test. * * Revision 6.21  2003/07/22 21:45:05  vasilche * Commented out memory usage test. * * Revision 6.20  2003/07/17 20:01:38  vasilche * Added test for string reference counting. * * Revision 6.19  2003/07/09 20:52:20  vasilche * Added test for string's reference counting. * * Revision 6.18  2003/03/25 22:16:11  lavr * Conform to new NUL char representation from NStr::PrintableString() * * Revision 6.17  2003/03/10 18:57:08  kuznets * iterate->ITERATE * * Revision 6.16  2003/02/27 15:34:23  lavr * Add tests for stray dots in numbers * * Revision 6.15  2003/02/26 20:35:13  siyan * Added/deleted whitespaces to conform to existing code style. * * Revision 6.14  2003/02/26 16:47:36  siyan * Added test cases to test new version of NStr::StringToUInt8. * * Revision 6.13  2003/01/21 23:55:44  vakatov * Get rid of a warning * * Revision 6.12  2003/01/14 21:17:58  kuznets * + test for NStr::Tokenize * * Revision 6.11  2003/01/13 14:48:08  kuznets * minor fix * * Revision 6.10  2003/01/10 22:17:39  kuznets * Implemented test for NStr::String2Int8 * * Revision 6.9  2003/01/10 00:08:28  vakatov * + Int8ToString(),  UInt8ToString() * * Revision 6.8  2002/10/17 16:56:02  ivanov * Added tests for 'b' and 'B' time format symbols * * Revision 6.7  2002/09/04 19:32:11  vakatov * Minor change to reflect the changed handling of '"' by NStr::PrintableString * * Revision 6.6  2002/07/15 18:17:26  gouriano * renamed CNcbiException and its descendents * * Revision 6.5  2002/07/11 14:18:29  gouriano * exceptions replaced by CNcbiException-type ones * * Revision 6.4  2002/04/16 18:49:08  ivanov * Centralize threatment of assert() in tests. * Added #include <test/test_assert.h>. CVS log moved to end of file. * * Revision 6.3  2001/08/30 00:39:58  vakatov * + NStr::StringToNumeric() * * Revision 6.2  2001/05/17 15:05:09  lavr * Typos corrected * * Revision 6.1  2001/03/26 20:34:38  vakatov * Initial revision (moved from "coretest.cpp") * * ========================================================================== */

⌨️ 快捷键说明

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