ferencd@0: // ferencd@0: // VMime library (http://www.vmime.org) ferencd@0: // Copyright (C) 2002-2013 Vincent Richard ferencd@0: // ferencd@0: // This program is free software; you can redistribute it and/or ferencd@0: // modify it under the terms of the GNU General Public License as ferencd@0: // published by the Free Software Foundation; either version 3 of ferencd@0: // the License, or (at your option) any later version. ferencd@0: // ferencd@0: // This program is distributed in the hope that it will be useful, ferencd@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ferencd@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ferencd@0: // General Public License for more details. ferencd@0: // ferencd@0: // You should have received a copy of the GNU General Public License along ferencd@0: // with this program; if not, write to the Free Software Foundation, Inc., ferencd@0: // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ferencd@0: // ferencd@0: // Linking this library statically or dynamically with other modules is making ferencd@0: // a combined work based on this library. Thus, the terms and conditions of ferencd@0: // the GNU General Public License cover the whole combination. ferencd@0: // ferencd@0: ferencd@0: #include "tests/testUtils.hpp" ferencd@0: ferencd@0: #include "vmime/utility/stringUtils.hpp" ferencd@0: ferencd@0: ferencd@0: VMIME_TEST_SUITE_BEGIN(stringUtilsTest) ferencd@0: ferencd@0: VMIME_TEST_LIST_BEGIN ferencd@0: VMIME_TEST(testMakeStringFromBytes) ferencd@0: VMIME_TEST(testAppendBytesToString) ferencd@0: ferencd@0: VMIME_TEST(testIsStringEqualNoCase1) ferencd@0: VMIME_TEST(testIsStringEqualNoCase2) ferencd@0: VMIME_TEST(testIsStringEqualNoCase3) ferencd@0: ferencd@0: VMIME_TEST(testToLower) ferencd@0: ferencd@0: VMIME_TEST(testTrim) ferencd@0: ferencd@0: VMIME_TEST(testCountASCIIChars) ferencd@0: ferencd@0: VMIME_TEST(testUnquote) ferencd@0: VMIME_TEST_LIST_END ferencd@0: ferencd@0: ferencd@0: typedef vmime::utility::stringUtils stringUtils; ferencd@0: ferencd@0: ferencd@0: void testMakeStringFromBytes() ferencd@0: { ferencd@0: vmime::byte_t bytes[] = { 0x12, 0x34, 0x56, 0x78 }; ferencd@0: vmime::string str = vmime::utility::stringUtils::makeStringFromBytes(bytes, 3); ferencd@0: ferencd@0: VASSERT_EQ("length", 3, str.length()); ferencd@0: VASSERT_EQ("byte1", '\x12', str[0]); ferencd@0: VASSERT_EQ("byte2", '\x34', str[1]); ferencd@0: VASSERT_EQ("byte3", '\x56', str[2]); ferencd@0: } ferencd@0: ferencd@0: void testAppendBytesToString() ferencd@0: { ferencd@0: vmime::byte_t bytes[] = { 0x42, 0x56, 0x12, 0x00, 'f', 'o', 'o' }; ferencd@0: ferencd@0: vmime::string str = "test"; ferencd@0: vmime::utility::stringUtils::appendBytesToString(str, bytes, 7); ferencd@0: ferencd@0: VASSERT_EQ("length", 4 + 7, str.length()); ferencd@0: VASSERT_EQ("byte1", 't', str[0]); ferencd@0: VASSERT_EQ("byte2", 'e', str[1]); ferencd@0: VASSERT_EQ("byte3", 's', str[2]); ferencd@0: VASSERT_EQ("byte4", 't', str[3]); ferencd@0: VASSERT_EQ("byte5", '\x42', str[4]); ferencd@0: VASSERT_EQ("byte6", '\x56', str[5]); ferencd@0: VASSERT_EQ("byte7", '\x12', str[6]); ferencd@0: VASSERT_EQ("byte8", '\0', str[7]); ferencd@0: VASSERT_EQ("byte9", 'f', str[8]); ferencd@0: VASSERT_EQ("byte10", 'o', str[9]); ferencd@0: VASSERT_EQ("byte11", 'o', str[10]); ferencd@0: } ferencd@0: ferencd@0: void testIsStringEqualNoCase1() ferencd@0: { ferencd@0: VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(vmime::string("foo"), "foo", 3)); ferencd@0: VASSERT_EQ("2", true, stringUtils::isStringEqualNoCase(vmime::string("FOo"), "foo", 3)); ferencd@0: ferencd@0: VASSERT_EQ("3", false, stringUtils::isStringEqualNoCase(vmime::string("foo"), "FOo", 3)); ferencd@0: VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(vmime::string("foo"), "bar", 3)); ferencd@0: } ferencd@0: ferencd@0: void testIsStringEqualNoCase2() ferencd@0: { ferencd@0: VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(vmime::string("foo"), vmime::string("foo"))); ferencd@0: VASSERT_EQ("2", true, stringUtils::isStringEqualNoCase(vmime::string("FOo"), vmime::string("foo"))); ferencd@0: VASSERT_EQ("3", true, stringUtils::isStringEqualNoCase(vmime::string("foO"), vmime::string("FOo"))); ferencd@0: } ferencd@0: ferencd@0: void testIsStringEqualNoCase3() ferencd@0: { ferencd@0: vmime::string str1("FooBar"); ferencd@0: ferencd@0: VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "foobar", 6)); ferencd@0: VASSERT_EQ("2", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "FooBar", 6)); ferencd@0: VASSERT_EQ("3", true, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "fooBar", 3)); ferencd@0: VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.begin() + 3, "fooBar", 6)); ferencd@0: } ferencd@0: ferencd@0: void testToLower() ferencd@0: { ferencd@0: VASSERT_EQ("1", "foo", stringUtils::toLower("FOO")); ferencd@0: VASSERT_EQ("2", "foo", stringUtils::toLower("foO")); ferencd@0: VASSERT_EQ("3", "foo", stringUtils::toLower("foo")); ferencd@0: } ferencd@0: ferencd@0: void testTrim() ferencd@0: { ferencd@0: VASSERT_EQ("1", "foo", stringUtils::trim(" foo")); ferencd@0: VASSERT_EQ("2", "foo", stringUtils::trim("\t\tfoo")); ferencd@0: VASSERT_EQ("3", "foo", stringUtils::trim(" \t \tfoo")); ferencd@0: VASSERT_EQ("4", "foo", stringUtils::trim(" \r\n\tfoo")); ferencd@0: ferencd@0: VASSERT_EQ("5", "foo", stringUtils::trim("foo ")); ferencd@0: VASSERT_EQ("6", "foo", stringUtils::trim("foo\t\t")); ferencd@0: VASSERT_EQ("7", "foo", stringUtils::trim("foo \t \t")); ferencd@0: VASSERT_EQ("8", "foo", stringUtils::trim("foo \r\n\t")); ferencd@0: ferencd@0: VASSERT_EQ( "9", "foo", stringUtils::trim("foo ")); ferencd@0: VASSERT_EQ("10", "foo", stringUtils::trim(" foo ")); ferencd@0: VASSERT_EQ("11", "foo", stringUtils::trim(" foo\t\t")); ferencd@0: VASSERT_EQ("12", "foo", stringUtils::trim("\tfoo \r \t")); ferencd@0: VASSERT_EQ("13", "foo", stringUtils::trim("\r \tfoo \n\t")); ferencd@0: } ferencd@0: ferencd@0: void testCountASCIIChars() ferencd@0: { ferencd@0: vmime::string str1("foo"); ferencd@0: VASSERT_EQ("1", static_cast (3), ferencd@0: stringUtils::countASCIIchars(str1.begin(), str1.end())); ferencd@0: ferencd@0: vmime::string str2("f=?oo"); ferencd@0: VASSERT_EQ("2", static_cast (3 + 1), ferencd@0: stringUtils::countASCIIchars(str2.begin(), str2.end())); ferencd@0: ferencd@0: vmime::string str3("foo\x7f"); ferencd@0: VASSERT_EQ("3", static_cast (4), ferencd@0: stringUtils::countASCIIchars(str3.begin(), str3.end())); ferencd@0: ferencd@0: vmime::string str4("foo\x80"); ferencd@0: VASSERT_EQ("4", static_cast (3), ferencd@0: stringUtils::countASCIIchars(str4.begin(), str4.end())); ferencd@0: } ferencd@0: ferencd@0: void testUnquote() ferencd@0: { ferencd@0: VASSERT_EQ("1", "quoted", stringUtils::unquote("\"quoted\"")); // "quoted" ferencd@0: VASSERT_EQ("2", "\"not quoted", stringUtils::unquote("\"not quoted")); // "not quoted ferencd@0: VASSERT_EQ("3", "not quoted\"", stringUtils::unquote("not quoted\"")); // not quoted" ferencd@0: VASSERT_EQ("4", "quoted with \"escape\"", stringUtils::unquote("\"quoted with \\\"escape\\\"\"")); // "quoted with \"escape\"" ferencd@0: } ferencd@0: ferencd@0: VMIME_TEST_SUITE_END ferencd@0: