1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 #include "testshl/simpleheader.hxx"
32 #include "rtl/strbuf.hxx"
33 #include "rtl/string.h"
34 #include "rtl/string.hxx"
35 #include "rtl/textenc.h"
36 #include "rtl/ustring.hxx"
37 #include "sal/types.h"
38 
39 namespace test { namespace oustring {
40 
41 class EndsWith: public CppUnit::TestFixture
42 {
43 private:
44     void endsWith();
45 
46     CPPUNIT_TEST_SUITE(EndsWith);
47     CPPUNIT_TEST(endsWith);
48     CPPUNIT_TEST_SUITE_END();
49 };
50 
51 } }
52 
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustring::EndsWith, "alltest");
54 
55 namespace {
56 
57 void appendString(rtl::OStringBuffer & buffer, rtl::OString const & string)
58 {
59     buffer.append('"');
60     for (int i = 0; i < string.getLength(); ++i) {
61         char c = string[i];
62         if (c < ' ' || c == '"' || c == '\\' || c > '~') {
63             buffer.append('\\');
64             sal_Int32 n = static_cast< sal_Int32 >(
65                 static_cast< unsigned char >(c));
66             if (n < 16) {
67                 buffer.append('0');
68             }
69             buffer.append(n, 16);
70         } else {
71             buffer.append(c);
72         }
73     }
74     buffer.append('"');
75 }
76 
77 }
78 
79 void test::oustring::EndsWith::endsWith()
80 {
81     struct Data {
82         char const * str1;
83         sal_Int32 str1Len;
84         char const * str2;
85         sal_Int32 str2Len;
86         bool endsWith;
87     };
88     Data const data[] = {
89         { RTL_CONSTASCII_STRINGPARAM(""), RTL_CONSTASCII_STRINGPARAM(""),
90           true },
91         { RTL_CONSTASCII_STRINGPARAM("abc"), RTL_CONSTASCII_STRINGPARAM(""),
92           true },
93         { RTL_CONSTASCII_STRINGPARAM(""), RTL_CONSTASCII_STRINGPARAM("abc"),
94           false },
95         { RTL_CONSTASCII_STRINGPARAM("ABC"), RTL_CONSTASCII_STRINGPARAM("abc"),
96           true },
97         { RTL_CONSTASCII_STRINGPARAM("abcd"), RTL_CONSTASCII_STRINGPARAM("bcd"),
98           true },
99         { RTL_CONSTASCII_STRINGPARAM("bcd"), RTL_CONSTASCII_STRINGPARAM("abcd"),
100           false },
101         { RTL_CONSTASCII_STRINGPARAM("a\0b\0c"),
102           RTL_CONSTASCII_STRINGPARAM("b\0c"), true },
103         { RTL_CONSTASCII_STRINGPARAM("a\0b\0c"),
104           RTL_CONSTASCII_STRINGPARAM("b"), false } };
105     for (int i = 0; i < sizeof data / sizeof data[0]; ++i) {
106         rtl::OStringBuffer msg;
107         appendString(msg, rtl::OString(data[i].str1, data[i].str1Len));
108         msg.append(
109             RTL_CONSTASCII_STRINGPARAM(".endsWithIgnoreAsciiCaseAsciiL("));
110         appendString(msg, rtl::OString(data[i].str2, data[i].str2Len));
111         msg.append(RTL_CONSTASCII_STRINGPARAM(") == "));
112         msg.append(static_cast< sal_Bool >(data[i].endsWith));
113         CPPUNIT_ASSERT_MESSAGE(
114             msg.getStr(),
115             rtl::OUString(
116                 data[i].str1, data[i].str1Len,
117                 RTL_TEXTENCODING_ASCII_US).endsWithIgnoreAsciiCaseAsciiL(
118                     data[i].str2, data[i].str2Len)
119             == data[i].endsWith);
120     }
121 }
122