📄 stringtest.cpp
字号:
//
// StringTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/StringTest.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "StringTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/String.h"
using Foundation::trimLeft;
using Foundation::trimLeftInPlace;
using Foundation::trimRight;
using Foundation::trimRightInPlace;
using Foundation::trim;
using Foundation::trimInPlace;
using Foundation::toUpper;
using Foundation::toUpperInPlace;
using Foundation::toLower;
using Foundation::toLowerInPlace;
using Foundation::icompare;
using Foundation::translate;
using Foundation::translateInPlace;
using Foundation::cat;
StringTest::StringTest(const std::string& name): CppUnit::TestCase(name)
{
}
StringTest::~StringTest()
{
}
void StringTest::testTrimLeft()
{
{
std::string s = "abc";
assert (trimLeft(s) == "abc");
}
std::string s = " abc ";
assert (trimLeft(s) == "abc ");
{
std::string s = " ab c ";
assert (trimLeft(s) == "ab c ");
}
}
void StringTest::testTrimLeftInPlace()
{
{
std::string s = "abc";
assert (trimLeftInPlace(s) == "abc");
}
{
std::string s = " abc ";
assert (trimLeftInPlace(s) == "abc ");
}
{
std::string s = " ab c ";
assert (trimLeftInPlace(s) == "ab c ");
}
}
void StringTest::testTrimRight()
{
{
std::string s = "abc";
assert (trimRight(s) == "abc");
}
{
std::string s = " abc ";
assert (trimRight(s) == " abc");
}
{
std::string s = " ab c ";
assert (trimRight(s) == " ab c");
}
}
void StringTest::testTrimRightInPlace()
{
{
std::string s = "abc";
assert (trimRightInPlace(s) == "abc");
}
{
std::string s = " abc ";
assert (trimRightInPlace(s) == " abc");
}
{
std::string s = " ab c ";
assert (trimRightInPlace(s) == " ab c");
}
}
void StringTest::testTrim()
{
{
std::string s = "abc";
assert (trim(s) == "abc");
}
{
std::string s = "abc ";
assert (trim(s) == "abc");
}
{
std::string s = " ab c ";
assert (trim(s) == "ab c");
}
}
void StringTest::testTrimInPlace()
{
{
std::string s = "abc";
assert (trimInPlace(s) == "abc");
}
{
std::string s = " abc ";
assert (trimInPlace(s) == "abc");
}
{
std::string s = " ab c ";
assert (trimInPlace(s) == "ab c");
}
}
void StringTest::testToUpper()
{
{
std::string s = "abc";
assert (toUpper(s) == "ABC");
}
{
std::string s = "Abc";
assert (toUpper(s) == "ABC");
}
{
std::string s = "abc";
assert (toUpperInPlace(s) == "ABC");
}
{
std::string s = "Abc";
assert (toUpperInPlace(s) == "ABC");
}
}
void StringTest::testToLower()
{
{
std::string s = "ABC";
assert (toLower(s) == "abc");
}
{
std::string s = "aBC";
assert (toLower(s) == "abc");
}
{
std::string s = "ABC";
assert (toLowerInPlace(s) == "abc");
}
{
std::string s = "aBC";
assert (toLowerInPlace(s) == "abc");
}
}
void StringTest::testIcompare()
{
std::string s1 = "AAA";
std::string s2 = "aaa";
std::string s3 = "bbb";
std::string s4 = "cCcCc";
std::string s5;
assert (icompare(s1, s2) == 0);
assert (icompare(s1, s3) < 0);
assert (icompare(s1, s4) < 0);
assert (icompare(s3, s1) > 0);
assert (icompare(s4, s2) > 0);
assert (icompare(s2, s4) < 0);
assert (icompare(s1, s5) > 0);
assert (icompare(s5, s4) < 0);
std::string ss1 = "xxAAAzz";
std::string ss2 = "YaaaX";
std::string ss3 = "YbbbX";
assert (icompare(ss1, 2, 3, ss2, 1, 3) == 0);
assert (icompare(ss1, 2, 3, ss3, 1, 3) < 0);
assert (icompare(ss1, 2, 3, ss2, 1) == 0);
assert (icompare(ss1, 2, 3, ss3, 1) < 0);
assert (icompare(ss1, 2, 2, ss2, 1, 3) < 0);
assert (icompare(ss1, 2, 2, ss2, 1, 2) == 0);
assert (icompare(ss3, 1, 3, ss1, 2, 3) > 0);
assert (icompare(s1, s2.c_str()) == 0);
assert (icompare(s1, s3.c_str()) < 0);
assert (icompare(s1, s4.c_str()) < 0);
assert (icompare(s3, s1.c_str()) > 0);
assert (icompare(s4, s2.c_str()) > 0);
assert (icompare(s2, s4.c_str()) < 0);
assert (icompare(s1, s5.c_str()) > 0);
assert (icompare(s5, s4.c_str()) < 0);
assert (icompare(ss1, 2, 3, "aaa") == 0);
assert (icompare(ss1, 2, 2, "aaa") < 0);
assert (icompare(ss1, 2, 3, "AAA") == 0);
assert (icompare(ss1, 2, 2, "bb") < 0);
assert (icompare(ss1, 2, "aaa") > 0);
}
void StringTest::testTranslate()
{
std::string s = "aabbccdd";
assert (translate(s, "abc", "ABC") == "AABBCCdd");
assert (translate(s, "abc", "AB") == "AABBdd");
assert (translate(s, "abc", "") == "dd");
assert (translate(s, "cba", "CB") == "BBCCdd");
assert (translate(s, "", "CB") == "aabbccdd");
}
void StringTest::testTranslateInPlace()
{
std::string s = "aabbccdd";
translateInPlace(s, "abc", "ABC");
assert (s == "AABBCCdd");
}
void StringTest::testCat()
{
std::string s1("one");
std::string s2("two");
std::string s3("three");
std::string s4("four");
std::string s5("five");
std::string s6("six");
assert (cat(s1, s2) == "onetwo");
assert (cat(s1, s2, s3) == "onetwothree");
assert (cat(s1, s2, s3, s4) == "onetwothreefour");
assert (cat(s1, s2, s3, s4, s5) == "onetwothreefourfive");
assert (cat(s1, s2, s3, s4, s5, s6) == "onetwothreefourfivesix");
std::vector<std::string> vec;
assert (cat(std::string(), vec.begin(), vec.end()) == "");
assert (cat(std::string(","), vec.begin(), vec.end()) == "");
vec.push_back(s1);
assert (cat(std::string(","), vec.begin(), vec.end()) == "one");
vec.push_back(s2);
assert (cat(std::string(","), vec.begin(), vec.end()) == "one,two");
vec.push_back(s3);
assert (cat(std::string(","), vec.begin(), vec.end()) == "one,two,three");
}
void StringTest::setUp()
{
}
void StringTest::tearDown()
{
}
CppUnit::Test* StringTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StringTest");
CppUnit_addTest(pSuite, StringTest, testTrimLeft);
CppUnit_addTest(pSuite, StringTest, testTrimLeftInPlace);
CppUnit_addTest(pSuite, StringTest, testTrimRight);
CppUnit_addTest(pSuite, StringTest, testTrimInPlace);
CppUnit_addTest(pSuite, StringTest, testTrim);
CppUnit_addTest(pSuite, StringTest, testTrimRightInPlace);
CppUnit_addTest(pSuite, StringTest, testToUpper);
CppUnit_addTest(pSuite, StringTest, testToLower);
CppUnit_addTest(pSuite, StringTest, testIcompare);
CppUnit_addTest(pSuite, StringTest, testTranslate);
CppUnit_addTest(pSuite, StringTest, testTranslateInPlace);
CppUnit_addTest(pSuite, StringTest, testCat);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -