From 83072d5f1814be4cadc5992011983d60607f1dd3 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sat, 1 Jan 2005 15:19:24 +0000 Subject: [PATCH] Added unit tests for 'utility::stringProxy' and 'utility::stringUtils'. --- ChangeLog | 3 +- SConstruct | 4 +- tests/utility/stringProxyTest.cpp | 205 ++++++++++++++++++++++++++++++ tests/utility/stringUtilsTest.cpp | 130 +++++++++++++++++++ 4 files changed, 340 insertions(+), 2 deletions(-) create mode 100644 tests/utility/stringProxyTest.cpp create mode 100644 tests/utility/stringUtilsTest.cpp diff --git a/ChangeLog b/ChangeLog index 61443f25..01e10c24 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,7 +6,8 @@ VERSION 0.6.2-cvs * Converted all C-style casts to C++-style casts. - * Added unit test for utility::md5. + * Added unit tests for utility::md5, utility::stringProxy and + utility::stringUtils. 2004-12-31 Vincent Richard diff --git a/SConstruct b/SConstruct index 754ba276..2732ec8a 100644 --- a/SConstruct +++ b/SConstruct @@ -306,7 +306,9 @@ libvmimetest_sources = [ [ 'tests/parser/mailboxTest', [ 'tests/parser/mailboxTest.cpp' ] ], [ 'tests/parser/mediaTypeTest', [ 'tests/parser/mediaTypeTest.cpp' ] ], [ 'tests/parser/textTest', [ 'tests/parser/textTest.cpp' ] ], - [ 'tests/utility/md5Test', [ 'tests/utility/md5Test.cpp' ] ] + [ 'tests/utility/md5Test', [ 'tests/utility/md5Test.cpp' ] ], + [ 'tests/utility/stringProxyTest', [ 'tests/utility/stringProxyTest.cpp' ] ], + [ 'tests/utility/stringUtilsTest', [ 'tests/utility/stringUtilsTest.cpp' ] ] ] libvmime_autotools = [ diff --git a/tests/utility/stringProxyTest.cpp b/tests/utility/stringProxyTest.cpp new file mode 100644 index 00000000..25557363 --- /dev/null +++ b/tests/utility/stringProxyTest.cpp @@ -0,0 +1,205 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "../lib/unit++/unit++.h" + +#include +#include + +#include "vmime/vmime.hpp" +#include "vmime/platforms/posix/posixHandler.hpp" + +using namespace unitpp; + + +namespace +{ + class stringProxyTest : public suite + { + void testConstruct() + { + vmime::utility::stringProxy s; + + assert_eq("1", 0, s.length()); + assert_eq("2", 0, s.start()); + assert_eq("3", 0, s.end()); + } + + void testConstruct2() + { + vmime::string str("This is a test string."); + + vmime::utility::stringProxy s1(str); + + assert_eq("1", str.length(), s1.length()); + assert_eq("2", 0, s1.start()); + assert_eq("3", str.length(), s1.end()); + + vmime::utility::stringProxy s2(str, 10); + + assert_eq("4", str.length() - 10, s2.length()); + assert_eq("5", 10, s2.start()); + assert_eq("6", str.length(), s2.end()); + + vmime::utility::stringProxy s3(str, 10, 14); + + assert_eq("7", 4, s3.length()); + assert_eq("8", 10, s3.start()); + assert_eq("9", 14, s3.end()); + + assert_eq("10", 't', *s3.it_begin()); + assert_eq("11", 'e', *(s3.it_begin() + 1)); + assert_eq("12", 's', *(s3.it_begin() + 2)); + assert_eq("13", 't', *(s3.it_begin() + 3)); + } + + void testDetach() + { + vmime::utility::stringProxy s; + s = "foo"; + + s.detach(); + + assert_eq("1", 0, s.length()); + assert_eq("2", 0, s.start()); + assert_eq("3", 0, s.end()); + } + + void testSet() + { + vmime::string str("This is a test string."); + + vmime::utility::stringProxy s1; + s1.set(str); + + assert_eq("1", str.length(), s1.length()); + assert_eq("2", 0, s1.start()); + assert_eq("3", str.length(), s1.end()); + + vmime::utility::stringProxy s2; + s2.set(str, 10); + + assert_eq("4", str.length() - 10, s2.length()); + assert_eq("5", 10, s2.start()); + assert_eq("6", str.length(), s2.end()); + + vmime::utility::stringProxy s3; + s3.set(str, 10, 14); + + assert_eq("7", 4, s3.length()); + assert_eq("8", 10, s3.start()); + assert_eq("9", 14, s3.end()); + + assert_eq("10", 't', *s3.it_begin()); + assert_eq("11", 'e', *(s3.it_begin() + 1)); + assert_eq("12", 's', *(s3.it_begin() + 2)); + assert_eq("13", 't', *(s3.it_begin() + 3)); + } + + void testExtract() + { + vmime::string str("This is a test string."); + + vmime::utility::stringProxy s1(str, 10, 14); + + std::ostringstream oss1; + vmime::utility::outputStreamAdapter osa1(oss1); + + s1.extract(osa1); + + assert_eq("1", "test", oss1.str()); + + vmime::utility::stringProxy s2(str); + + std::ostringstream oss2; + vmime::utility::outputStreamAdapter osa2(oss2); + + s2.extract(osa2); + + assert_eq("2", str, oss2.str()); + } + + void testOperatorLTLT1() + { + vmime::string str("This is a test string."); + + vmime::utility::stringProxy s1(str, 10, 14); + + std::ostringstream oss1; + oss1 << s1; + + assert_eq("1", "test", oss1.str()); + + vmime::utility::stringProxy s2(str); + + std::ostringstream oss2; + oss2 << s2; + + assert_eq("2", str, oss2.str()); + } + + void testOperatorLTLT2() + { + vmime::string str("This is a test string."); + + vmime::utility::stringProxy s1(str, 10, 14); + + std::ostringstream oss1; + vmime::utility::outputStreamAdapter osa1(oss1); + + osa1 << s1; + + assert_eq("1", "test", oss1.str()); + + vmime::utility::stringProxy s2(str); + + std::ostringstream oss2; + vmime::utility::outputStreamAdapter osa2(oss2); + + osa2 << s2; + + assert_eq("2", str, oss2.str()); + } + + public: + + stringProxyTest() : suite("vmime::utility::stringProxy") + { + // VMime initialization + vmime::platformDependant::setHandler(); + + add("Construct", testcase(this, "Construct", &stringProxyTest::testConstruct)); + add("Construct2", testcase(this, "Construct2", &stringProxyTest::testConstruct2)); + + add("Detach", testcase(this, "Detach", &stringProxyTest::testDetach)); + + add("Set", testcase(this, "Set", &stringProxyTest::testSet)); + + add("Extract", testcase(this, "Extract", &stringProxyTest::testExtract)); + + add("Operator<<(1)", testcase(this, "Operator<<(1)", &stringProxyTest::testOperatorLTLT1)); + add("Operator<<(2)", testcase(this, "Operator<<(2)", &stringProxyTest::testOperatorLTLT2)); + + suite::main().add("vmime::utility::stringProxy", this); + } + + }; + + stringProxyTest* theTest = new stringProxyTest(); +} diff --git a/tests/utility/stringUtilsTest.cpp b/tests/utility/stringUtilsTest.cpp new file mode 100644 index 00000000..76f9f36d --- /dev/null +++ b/tests/utility/stringUtilsTest.cpp @@ -0,0 +1,130 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "../lib/unit++/unit++.h" + +#include +#include + +#include "vmime/vmime.hpp" +#include "vmime/platforms/posix/posixHandler.hpp" +#include "vmime/utility/stringUtils.hpp" + +using namespace unitpp; + + +namespace +{ + class stringUtilsTest : public suite + { + void testIsStringEqualNoCase1() + { + assert_eq("1", true, vmime::stringUtils::isStringEqualNoCase(vmime::string("foo"), "foo", 3)); + assert_eq("2", true, vmime::stringUtils::isStringEqualNoCase(vmime::string("FOo"), "foo", 3)); + + assert_eq("3", false, vmime::stringUtils::isStringEqualNoCase(vmime::string("foo"), "FOo", 3)); + assert_eq("4", false, vmime::stringUtils::isStringEqualNoCase(vmime::string("foo"), "bar", 3)); + } + + void testIsStringEqualNoCase2() + { + assert_eq("1", true, vmime::stringUtils::isStringEqualNoCase(vmime::string("foo"), vmime::string("foo"))); + assert_eq("2", true, vmime::stringUtils::isStringEqualNoCase(vmime::string("FOo"), vmime::string("foo"))); + assert_eq("3", true, vmime::stringUtils::isStringEqualNoCase(vmime::string("foO"), vmime::string("FOo"))); + } + + void testIsStringEqualNoCase3() + { + vmime::string str1("FooBar"); + + assert_eq("1", true, vmime::stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "foobar", 6)); + assert_eq("2", false, vmime::stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "FooBar", 6)); + assert_eq("3", true, vmime::stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "fooBar", 3)); + assert_eq("4", false, vmime::stringUtils::isStringEqualNoCase(str1.begin(), str1.begin() + 3, "fooBar", 6)); + } + + void testToLower() + { + assert_eq("1", "foo", vmime::stringUtils::toLower("FOO")); + assert_eq("2", "foo", vmime::stringUtils::toLower("foO")); + assert_eq("3", "foo", vmime::stringUtils::toLower("foo")); + } + + void testTrim() + { + assert_eq("1", "foo", vmime::stringUtils::trim(" foo")); + assert_eq("2", "foo", vmime::stringUtils::trim("\t\tfoo")); + assert_eq("3", "foo", vmime::stringUtils::trim(" \t \tfoo")); + assert_eq("4", "foo", vmime::stringUtils::trim(" \r\n\tfoo")); + + assert_eq("5", "foo", vmime::stringUtils::trim("foo ")); + assert_eq("6", "foo", vmime::stringUtils::trim("foo\t\t")); + assert_eq("7", "foo", vmime::stringUtils::trim("foo \t \t")); + assert_eq("8", "foo", vmime::stringUtils::trim("foo \r\n\t")); + + assert_eq( "9", "foo", vmime::stringUtils::trim("foo ")); + assert_eq("10", "foo", vmime::stringUtils::trim(" foo ")); + assert_eq("11", "foo", vmime::stringUtils::trim(" foo\t\t")); + assert_eq("12", "foo", vmime::stringUtils::trim("\tfoo \r \t")); + assert_eq("13", "foo", vmime::stringUtils::trim("\r \tfoo \n\t")); + } + + void testCountASCIIChars() + { + vmime::string str1("foo"); + assert_eq("1", static_cast (3), + vmime::stringUtils::countASCIIchars(str1.begin(), str1.end())); + + vmime::string str2("f=?oo"); + assert_eq("2", static_cast (3 + 1), + vmime::stringUtils::countASCIIchars(str2.begin(), str2.end())); + + vmime::string str3("foo\x7f"); + assert_eq("3", static_cast (4), + vmime::stringUtils::countASCIIchars(str3.begin(), str3.end())); + + vmime::string str4("foo\x80"); + assert_eq("4", static_cast (3), + vmime::stringUtils::countASCIIchars(str4.begin(), str4.end())); + } + + public: + + stringUtilsTest() : suite("vmime::utility::stringUtils") + { + // VMime initialization + vmime::platformDependant::setHandler(); + + add("IsStringEqualNoCase1", testcase(this, "IsStringEqualNoCase1", &stringUtilsTest::testIsStringEqualNoCase1)); + add("IsStringEqualNoCase2", testcase(this, "IsStringEqualNoCase2", &stringUtilsTest::testIsStringEqualNoCase2)); + add("IsStringEqualNoCase3", testcase(this, "IsStringEqualNoCase3", &stringUtilsTest::testIsStringEqualNoCase3)); + + add("ToLower", testcase(this, "ToLower", &stringUtilsTest::testToLower)); + + add("Trim", testcase(this, "Trim", &stringUtilsTest::testTrim)); + + add("CountASCIIChars", testcase(this, "CountASCIIChars", &stringUtilsTest::testCountASCIIChars)); + + suite::main().add("vmime::utility::stringUtils", this); + } + + }; + + stringUtilsTest* theTest = new stringUtilsTest(); +}