xref: /AOO41X/main/sal/qa/rtl/ostring/rtl_string.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 #include <testshl/simpleheader.hxx>
32 
33 namespace rtl_string
34 {
35 
36     class getLength : public CppUnit::TestFixture
37     {
38     public:
39 
40         void getLength_000()
41             {
42                 rtl_string_getLength( NULL );
43                 // should not GPF
44             }
45 
46         void getLength_001()
47             {
48                 rtl::OString aStr("Test Length.");
49                 sal_Int32 nValue = rtl_string_getLength( aStr.pData );
50 
51                 CPPUNIT_ASSERT_MESSAGE("Length must equal getLength()", aStr.getLength() == nValue);
52                 CPPUNIT_ASSERT_MESSAGE(
53                     "Length must equal strlen()",
54                     nValue >= 0
55                     && (strlen(aStr.getStr())
56                         == sal::static_int_cast< sal_uInt32 >(nValue)));
57             }
58         // Change the following lines only, if you add, remove or rename
59         // member functions of the current class,
60         // because these macros are need by auto register mechanism.
61 
62         CPPUNIT_TEST_SUITE(getLength);
63         CPPUNIT_TEST(getLength_000);
64         CPPUNIT_TEST(getLength_001);
65         CPPUNIT_TEST_SUITE_END();
66     }; // class getLength
67 
68 // -----------------------------------------------------------------------------
69 
70     class newFromString : public CppUnit::TestFixture
71     {
72     public:
73 
74         // void newFromString_000()
75         //     {
76         //         sal_Int32 nValue = rtl_string_newFromString( NULL, NULL );
77         //         // should not GPF
78         //     }
79 
80         void newFromString_001()
81             {
82                 rtl::OString aStr("Test Length.");
83                 rtl_String *pStr = NULL;
84 
85                 rtl_string_newFromString( &pStr, aStr.pData );
86 
87                 rtl::OString aNewStr(pStr);
88                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", aStr.equals(aNewStr) == sal_True);
89 
90                 rtl_string_release(pStr);
91             }
92         // Change the following lines only, if you add, remove or rename
93         // member functions of the current class,
94         // because these macros are need by auto register mechanism.
95 
96         CPPUNIT_TEST_SUITE(newFromString);
97         // CPPUNIT_TEST(newFromString_000);
98         CPPUNIT_TEST(newFromString_001);
99         CPPUNIT_TEST_SUITE_END();
100     }; // class newFromString
101 
102     // -----------------------------------------------------------------------------
103 
104     class convertUStringToString : public CppUnit::TestFixture
105     {
106     public:
107 
108         // void newFromString_000()
109         //     {
110         //         sal_Int32 nValue = rtl_string_newFromString( NULL, NULL );
111         //         // should not GPF
112         //     }
113 
114         void convertUStringToString_001()
115             {
116                 rtl::OUString suString = rtl::OUString::createFromAscii("Hello");
117                 rtl::OString sString;
118                 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS);
119 
120                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", bRet == sal_True && sString.equals(rtl::OString("Hello")) == sal_True);
121             }
122 
123         void convertUStringToString_002()
124             {
125                 rtl::OString sStr("H\xE4llo");
126                 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
127 
128                 rtl::OString sString;
129                 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ISO_8859_15, OUSTRING_TO_OSTRING_CVTFLAGS);
130 
131                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", bRet == sal_True && sString.equals(rtl::OString("H\xE4llo")) == sal_True);
132             }
133 
134         void convertUStringToString_003()
135             {
136                 rtl::OString sStr("H\xC3\xA4llo");
137                 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_UTF8);
138 
139                 rtl::OString sString;
140                 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ISO_8859_15, OUSTRING_TO_OSTRING_CVTFLAGS);
141 
142                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", bRet == sal_True && sString.equals(rtl::OString("H\xE4llo")) == sal_True);
143             }
144 
145         void convertUStringToString_004()
146             {
147                 rtl::OString sStr("Tsch\xFC\xDF");
148                 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
149                 rtl::OString sString;
150 
151                 sal_Bool       bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
152                 /* sal_Bool */ bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ISO_8859_15, OUSTRING_TO_OSTRING_CVTFLAGS);
153                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", bRet == sal_True && sString.equals(rtl::OString("Tsch\xFC\xDF")) == sal_True);
154             }
155 
156 
157 
158         // Change the following lines only, if you add, remove or rename
159         // member functions of the current class,
160         // because these macros are need by auto register mechanism.
161 
162         CPPUNIT_TEST_SUITE(convertUStringToString);
163         CPPUNIT_TEST(convertUStringToString_001);
164         CPPUNIT_TEST(convertUStringToString_002);
165         CPPUNIT_TEST(convertUStringToString_003);
166         CPPUNIT_TEST(convertUStringToString_004);
167         CPPUNIT_TEST_SUITE_END();
168     }; // class convertUStringToString
169 
170 
171 
172 } // namespace rtl_string
173 
174 // -----------------------------------------------------------------------------
175 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_string::getLength, "rtl_string");
176 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_string::newFromString, "rtl_string");
177 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_string::convertUStringToString, "rtl_string");
178 
179 // -----------------------------------------------------------------------------
180 
181 // this macro creates an empty function, which will called by the RegisterAllFunctions()
182 // to let the user the possibility to also register some functions by hand.
183 NOADDITIONAL;
184