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 "gtest/gtest.h"
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
36
37 namespace {
38
appendString(rtl::OStringBuffer & buffer,rtl::OString const & string)39 void appendString(rtl::OStringBuffer & buffer, rtl::OString const & string)
40 {
41 buffer.append('"');
42 for (int i = 0; i < string.getLength(); ++i) {
43 char c = string[i];
44 if (c < ' ' || c == '"' || c == '\\' || c > '~') {
45 buffer.append('\\');
46 sal_Int32 n = static_cast< sal_Int32 >(
47 static_cast< unsigned char >(c));
48 if (n < 16) {
49 buffer.append('0');
50 }
51 buffer.append(n, 16);
52 } else {
53 buffer.append(c);
54 }
55 }
56 buffer.append('"');
57 }
58
59 }
60
61
62
63 namespace test { namespace oustring {
64
65 class EndsWith: public ::testing::Test
66 {
67 };
68
TEST_F(EndsWith,endsWith)69 TEST_F(EndsWith, endsWith)
70 {
71 struct Data {
72 char const * str1;
73 sal_Int32 str1Len;
74 char const * str2;
75 sal_Int32 str2Len;
76 bool endsWith;
77 };
78 Data const data[] = {
79 { RTL_CONSTASCII_STRINGPARAM(""), RTL_CONSTASCII_STRINGPARAM(""),
80 true },
81 { RTL_CONSTASCII_STRINGPARAM("abc"), RTL_CONSTASCII_STRINGPARAM(""),
82 true },
83 { RTL_CONSTASCII_STRINGPARAM(""), RTL_CONSTASCII_STRINGPARAM("abc"),
84 false },
85 { RTL_CONSTASCII_STRINGPARAM("ABC"), RTL_CONSTASCII_STRINGPARAM("abc"),
86 true },
87 { RTL_CONSTASCII_STRINGPARAM("abcd"), RTL_CONSTASCII_STRINGPARAM("bcd"),
88 true },
89 { RTL_CONSTASCII_STRINGPARAM("bcd"), RTL_CONSTASCII_STRINGPARAM("abcd"),
90 false },
91 { RTL_CONSTASCII_STRINGPARAM("a\0b\0c"),
92 RTL_CONSTASCII_STRINGPARAM("b\0c"), true },
93 { RTL_CONSTASCII_STRINGPARAM("a\0b\0c"),
94 RTL_CONSTASCII_STRINGPARAM("b"), false } };
95 for (int i = 0; i < sizeof data / sizeof data[0]; ++i) {
96 rtl::OStringBuffer msg;
97 appendString(msg, rtl::OString(data[i].str1, data[i].str1Len));
98 msg.append(
99 RTL_CONSTASCII_STRINGPARAM(".endsWithIgnoreAsciiCaseAsciiL("));
100 appendString(msg, rtl::OString(data[i].str2, data[i].str2Len));
101 msg.append(RTL_CONSTASCII_STRINGPARAM(") == "));
102 msg.append(static_cast< sal_Bool >(data[i].endsWith));
103 ASSERT_TRUE(
104 rtl::OUString(
105 data[i].str1, data[i].str1Len,
106 RTL_TEXTENCODING_ASCII_US).endsWithIgnoreAsciiCaseAsciiL(
107 data[i].str2, data[i].str2Len)
108 == data[i].endsWith) << msg.getStr();
109 }
110 }
111
112 } }
113