1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 #include "testshl/simpleheader.hxx"
28 #include "rtl/strbuf.hxx"
29 #include "rtl/string.h"
30 #include "rtl/string.hxx"
31 #include "rtl/textenc.h"
32 #include "rtl/ustring.hxx"
33 #include "sal/types.h"
34 
35 namespace test { namespace oustring {
36 
37 class EndsWith: public CppUnit::TestFixture
38 {
39 private:
40     void endsWith();
41 
42     CPPUNIT_TEST_SUITE(EndsWith);
43     CPPUNIT_TEST(endsWith);
44     CPPUNIT_TEST_SUITE_END();
45 };
46 
47 } }
48 
49 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustring::EndsWith, "alltest");
50 
51 namespace {
52 
appendString(rtl::OStringBuffer & buffer,rtl::OString const & string)53 void appendString(rtl::OStringBuffer & buffer, rtl::OString const & string)
54 {
55     buffer.append('"');
56     for (int i = 0; i < string.getLength(); ++i) {
57         char c = string[i];
58         if (c < ' ' || c == '"' || c == '\\' || c > '~') {
59             buffer.append('\\');
60             sal_Int32 n = static_cast< sal_Int32 >(
61                 static_cast< unsigned char >(c));
62             if (n < 16) {
63                 buffer.append('0');
64             }
65             buffer.append(n, 16);
66         } else {
67             buffer.append(c);
68         }
69     }
70     buffer.append('"');
71 }
72 
73 }
74 
endsWith()75 void test::oustring::EndsWith::endsWith()
76 {
77     struct Data {
78         char const * str1;
79         sal_Int32 str1Len;
80         char const * str2;
81         sal_Int32 str2Len;
82         bool endsWith;
83     };
84     Data const data[] = {
85         { RTL_CONSTASCII_STRINGPARAM(""), RTL_CONSTASCII_STRINGPARAM(""),
86           true },
87         { RTL_CONSTASCII_STRINGPARAM("abc"), RTL_CONSTASCII_STRINGPARAM(""),
88           true },
89         { RTL_CONSTASCII_STRINGPARAM(""), RTL_CONSTASCII_STRINGPARAM("abc"),
90           false },
91         { RTL_CONSTASCII_STRINGPARAM("ABC"), RTL_CONSTASCII_STRINGPARAM("abc"),
92           true },
93         { RTL_CONSTASCII_STRINGPARAM("abcd"), RTL_CONSTASCII_STRINGPARAM("bcd"),
94           true },
95         { RTL_CONSTASCII_STRINGPARAM("bcd"), RTL_CONSTASCII_STRINGPARAM("abcd"),
96           false },
97         { RTL_CONSTASCII_STRINGPARAM("a\0b\0c"),
98           RTL_CONSTASCII_STRINGPARAM("b\0c"), true },
99         { RTL_CONSTASCII_STRINGPARAM("a\0b\0c"),
100           RTL_CONSTASCII_STRINGPARAM("b"), false } };
101     for (int i = 0; i < sizeof data / sizeof data[0]; ++i) {
102         rtl::OStringBuffer msg;
103         appendString(msg, rtl::OString(data[i].str1, data[i].str1Len));
104         msg.append(
105             RTL_CONSTASCII_STRINGPARAM(".endsWithIgnoreAsciiCaseAsciiL("));
106         appendString(msg, rtl::OString(data[i].str2, data[i].str2Len));
107         msg.append(RTL_CONSTASCII_STRINGPARAM(") == "));
108         msg.append(static_cast< sal_Bool >(data[i].endsWith));
109         CPPUNIT_ASSERT_MESSAGE(
110             msg.getStr(),
111             rtl::OUString(
112                 data[i].str1, data[i].str1Len,
113                 RTL_TEXTENCODING_ASCII_US).endsWithIgnoreAsciiCaseAsciiL(
114                     data[i].str2, data[i].str2Len)
115             == data[i].endsWith);
116     }
117 }
118