1 | // |
2 | // UTF8StringTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "UTF8StringTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/UTF8String.h" |
15 | |
16 | |
17 | using Poco::UTF8; |
18 | |
19 | |
20 | UTF8StringTest::UTF8StringTest(const std::string& rName): CppUnit::TestCase(rName) |
21 | { |
22 | } |
23 | |
24 | |
25 | UTF8StringTest::~UTF8StringTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void UTF8StringTest::testCompare() |
31 | { |
32 | std::string a1("aaaaa" ); |
33 | std::string b1("bbbbb" ); |
34 | |
35 | assertTrue (UTF8::icompare(a1, b1) < 0); |
36 | |
37 | std::string a2("aaaaa" ); |
38 | std::string b2("BBBBB" ); |
39 | |
40 | assertTrue (UTF8::icompare(a2, b2) < 0); |
41 | |
42 | std::string a3("AAAAA" ); |
43 | std::string b3("bbbbb" ); |
44 | |
45 | assertTrue (UTF8::icompare(a3, b3) < 0); |
46 | |
47 | std::string a4("aaaaa" ); |
48 | std::string b4("AAAAA" ); |
49 | |
50 | assertTrue (UTF8::icompare(a4, b4) == 0); |
51 | |
52 | std::string a5("AAAAA" ); |
53 | std::string b5("bbbbb" ); |
54 | |
55 | assertTrue (UTF8::icompare(a5, b5) < 0); |
56 | |
57 | std::string a6("\303\274\303\266\303\244" ); // "u"o"a |
58 | std::string b6("\303\234\303\226\303\204" ); // "U"O"A |
59 | |
60 | assertTrue (UTF8::icompare(a6, b6) == 0); |
61 | } |
62 | |
63 | |
64 | void UTF8StringTest::testTransform() |
65 | { |
66 | std::string s1("abcde" ); |
67 | UTF8::toUpperInPlace(s1); |
68 | assertTrue (s1 == "ABCDE" ); |
69 | |
70 | std::string s2("aBcDe123" ); |
71 | UTF8::toUpperInPlace(s2); |
72 | assertTrue (s2 == "ABCDE123" ); |
73 | |
74 | std::string s3("\303\274\303\266\303\244" ); // "u"o"a |
75 | UTF8::toUpperInPlace(s3); |
76 | assertTrue (s3 == "\303\234\303\226\303\204" ); // "U"O"A |
77 | UTF8::toLowerInPlace(s3); |
78 | assertTrue (s3 == "\303\274\303\266\303\244" ); // "u"o"a |
79 | |
80 | // a mix of invalid sequences |
81 | std::string str = "\xC2\xE5\xF0\xF8\xE8\xED\xFB+-++" ; |
82 | assertTrue ("???+-++" == UTF8::toLower(str)); |
83 | } |
84 | |
85 | |
86 | void UTF8StringTest::testEscape() |
87 | { |
88 | std::string s1("A \t, a \v, and an \a walk into a |, and the barman says \xD0\x82" ); |
89 | |
90 | assertTrue (UTF8::escape(s1) == "A \\t, a \\v, and an \\a walk into a |, and the barman says \\u0402" ); |
91 | assertTrue (UTF8::escape(s1, true) == "A \\t, a \\u000B, and an \\u0007 walk into a |, and the barman says \\u0402" ); |
92 | } |
93 | |
94 | |
95 | void UTF8StringTest::testUnescape() |
96 | { |
97 | std::string s1("A \\t, a \\u000B, and an \\u0007 walk into a |, and the barman says \\u0402" ); |
98 | std::string s2("A \\t, a \\v, and an \\a walk into a |, and the barman says \\u0402" ); |
99 | |
100 | assertTrue (UTF8::unescape(s1) == "A \t, a \v, and an \a walk into a |, and the barman says \xD0\x82" ); |
101 | assertTrue (UTF8::unescape(s2) == "A \t, a \v, and an \a walk into a |, and the barman says \xD0\x82" ); |
102 | } |
103 | |
104 | |
105 | void UTF8StringTest::setUp() |
106 | { |
107 | } |
108 | |
109 | |
110 | void UTF8StringTest::tearDown() |
111 | { |
112 | } |
113 | |
114 | |
115 | CppUnit::Test* UTF8StringTest::suite() |
116 | { |
117 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UTF8StringTest" ); |
118 | |
119 | CppUnit_addTest(pSuite, UTF8StringTest, testCompare); |
120 | CppUnit_addTest(pSuite, UTF8StringTest, testTransform); |
121 | CppUnit_addTest(pSuite, UTF8StringTest, testEscape); |
122 | CppUnit_addTest(pSuite, UTF8StringTest, testUnescape); |
123 | |
124 | return pSuite; |
125 | } |
126 | |