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