Mercurial > thymian
comparison 3rdparty/vmime/tests/utility/stringUtilsTest.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 // | |
| 2 // VMime library (http://www.vmime.org) | |
| 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org> | |
| 4 // | |
| 5 // This program is free software; you can redistribute it and/or | |
| 6 // modify it under the terms of the GNU General Public License as | |
| 7 // published by the Free Software Foundation; either version 3 of | |
| 8 // the License, or (at your option) any later version. | |
| 9 // | |
| 10 // This program is distributed in the hope that it will be useful, | |
| 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 // General Public License for more details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU General Public License along | |
| 16 // with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 // | |
| 19 // Linking this library statically or dynamically with other modules is making | |
| 20 // a combined work based on this library. Thus, the terms and conditions of | |
| 21 // the GNU General Public License cover the whole combination. | |
| 22 // | |
| 23 | |
| 24 #include "tests/testUtils.hpp" | |
| 25 | |
| 26 #include "vmime/utility/stringUtils.hpp" | |
| 27 | |
| 28 | |
| 29 VMIME_TEST_SUITE_BEGIN(stringUtilsTest) | |
| 30 | |
| 31 VMIME_TEST_LIST_BEGIN | |
| 32 VMIME_TEST(testMakeStringFromBytes) | |
| 33 VMIME_TEST(testAppendBytesToString) | |
| 34 | |
| 35 VMIME_TEST(testIsStringEqualNoCase1) | |
| 36 VMIME_TEST(testIsStringEqualNoCase2) | |
| 37 VMIME_TEST(testIsStringEqualNoCase3) | |
| 38 | |
| 39 VMIME_TEST(testToLower) | |
| 40 | |
| 41 VMIME_TEST(testTrim) | |
| 42 | |
| 43 VMIME_TEST(testCountASCIIChars) | |
| 44 | |
| 45 VMIME_TEST(testUnquote) | |
| 46 VMIME_TEST_LIST_END | |
| 47 | |
| 48 | |
| 49 typedef vmime::utility::stringUtils stringUtils; | |
| 50 | |
| 51 | |
| 52 void testMakeStringFromBytes() | |
| 53 { | |
| 54 vmime::byte_t bytes[] = { 0x12, 0x34, 0x56, 0x78 }; | |
| 55 vmime::string str = vmime::utility::stringUtils::makeStringFromBytes(bytes, 3); | |
| 56 | |
| 57 VASSERT_EQ("length", 3, str.length()); | |
| 58 VASSERT_EQ("byte1", '\x12', str[0]); | |
| 59 VASSERT_EQ("byte2", '\x34', str[1]); | |
| 60 VASSERT_EQ("byte3", '\x56', str[2]); | |
| 61 } | |
| 62 | |
| 63 void testAppendBytesToString() | |
| 64 { | |
| 65 vmime::byte_t bytes[] = { 0x42, 0x56, 0x12, 0x00, 'f', 'o', 'o' }; | |
| 66 | |
| 67 vmime::string str = "test"; | |
| 68 vmime::utility::stringUtils::appendBytesToString(str, bytes, 7); | |
| 69 | |
| 70 VASSERT_EQ("length", 4 + 7, str.length()); | |
| 71 VASSERT_EQ("byte1", 't', str[0]); | |
| 72 VASSERT_EQ("byte2", 'e', str[1]); | |
| 73 VASSERT_EQ("byte3", 's', str[2]); | |
| 74 VASSERT_EQ("byte4", 't', str[3]); | |
| 75 VASSERT_EQ("byte5", '\x42', str[4]); | |
| 76 VASSERT_EQ("byte6", '\x56', str[5]); | |
| 77 VASSERT_EQ("byte7", '\x12', str[6]); | |
| 78 VASSERT_EQ("byte8", '\0', str[7]); | |
| 79 VASSERT_EQ("byte9", 'f', str[8]); | |
| 80 VASSERT_EQ("byte10", 'o', str[9]); | |
| 81 VASSERT_EQ("byte11", 'o', str[10]); | |
| 82 } | |
| 83 | |
| 84 void testIsStringEqualNoCase1() | |
| 85 { | |
| 86 VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(vmime::string("foo"), "foo", 3)); | |
| 87 VASSERT_EQ("2", true, stringUtils::isStringEqualNoCase(vmime::string("FOo"), "foo", 3)); | |
| 88 | |
| 89 VASSERT_EQ("3", false, stringUtils::isStringEqualNoCase(vmime::string("foo"), "FOo", 3)); | |
| 90 VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(vmime::string("foo"), "bar", 3)); | |
| 91 } | |
| 92 | |
| 93 void testIsStringEqualNoCase2() | |
| 94 { | |
| 95 VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(vmime::string("foo"), vmime::string("foo"))); | |
| 96 VASSERT_EQ("2", true, stringUtils::isStringEqualNoCase(vmime::string("FOo"), vmime::string("foo"))); | |
| 97 VASSERT_EQ("3", true, stringUtils::isStringEqualNoCase(vmime::string("foO"), vmime::string("FOo"))); | |
| 98 } | |
| 99 | |
| 100 void testIsStringEqualNoCase3() | |
| 101 { | |
| 102 vmime::string str1("FooBar"); | |
| 103 | |
| 104 VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "foobar", 6)); | |
| 105 VASSERT_EQ("2", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "FooBar", 6)); | |
| 106 VASSERT_EQ("3", true, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "fooBar", 3)); | |
| 107 VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.begin() + 3, "fooBar", 6)); | |
| 108 } | |
| 109 | |
| 110 void testToLower() | |
| 111 { | |
| 112 VASSERT_EQ("1", "foo", stringUtils::toLower("FOO")); | |
| 113 VASSERT_EQ("2", "foo", stringUtils::toLower("foO")); | |
| 114 VASSERT_EQ("3", "foo", stringUtils::toLower("foo")); | |
| 115 } | |
| 116 | |
| 117 void testTrim() | |
| 118 { | |
| 119 VASSERT_EQ("1", "foo", stringUtils::trim(" foo")); | |
| 120 VASSERT_EQ("2", "foo", stringUtils::trim("\t\tfoo")); | |
| 121 VASSERT_EQ("3", "foo", stringUtils::trim(" \t \tfoo")); | |
| 122 VASSERT_EQ("4", "foo", stringUtils::trim(" \r\n\tfoo")); | |
| 123 | |
| 124 VASSERT_EQ("5", "foo", stringUtils::trim("foo ")); | |
| 125 VASSERT_EQ("6", "foo", stringUtils::trim("foo\t\t")); | |
| 126 VASSERT_EQ("7", "foo", stringUtils::trim("foo \t \t")); | |
| 127 VASSERT_EQ("8", "foo", stringUtils::trim("foo \r\n\t")); | |
| 128 | |
| 129 VASSERT_EQ( "9", "foo", stringUtils::trim("foo ")); | |
| 130 VASSERT_EQ("10", "foo", stringUtils::trim(" foo ")); | |
| 131 VASSERT_EQ("11", "foo", stringUtils::trim(" foo\t\t")); | |
| 132 VASSERT_EQ("12", "foo", stringUtils::trim("\tfoo \r \t")); | |
| 133 VASSERT_EQ("13", "foo", stringUtils::trim("\r \tfoo \n\t")); | |
| 134 } | |
| 135 | |
| 136 void testCountASCIIChars() | |
| 137 { | |
| 138 vmime::string str1("foo"); | |
| 139 VASSERT_EQ("1", static_cast <vmime::size_t>(3), | |
| 140 stringUtils::countASCIIchars(str1.begin(), str1.end())); | |
| 141 | |
| 142 vmime::string str2("f=?oo"); | |
| 143 VASSERT_EQ("2", static_cast <vmime::size_t>(3 + 1), | |
| 144 stringUtils::countASCIIchars(str2.begin(), str2.end())); | |
| 145 | |
| 146 vmime::string str3("foo\x7f"); | |
| 147 VASSERT_EQ("3", static_cast <vmime::size_t>(4), | |
| 148 stringUtils::countASCIIchars(str3.begin(), str3.end())); | |
| 149 | |
| 150 vmime::string str4("foo\x80"); | |
| 151 VASSERT_EQ("4", static_cast <vmime::size_t>(3), | |
| 152 stringUtils::countASCIIchars(str4.begin(), str4.end())); | |
| 153 } | |
| 154 | |
| 155 void testUnquote() | |
| 156 { | |
| 157 VASSERT_EQ("1", "quoted", stringUtils::unquote("\"quoted\"")); // "quoted" | |
| 158 VASSERT_EQ("2", "\"not quoted", stringUtils::unquote("\"not quoted")); // "not quoted | |
| 159 VASSERT_EQ("3", "not quoted\"", stringUtils::unquote("not quoted\"")); // not quoted" | |
| 160 VASSERT_EQ("4", "quoted with \"escape\"", stringUtils::unquote("\"quoted with \\\"escape\\\"\"")); // "quoted with \"escape\"" | |
| 161 } | |
| 162 | |
| 163 VMIME_TEST_SUITE_END | |
| 164 |
