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
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 #include "gtest/gtest.h"
28 #include <rtl/string.hxx>
29 #include <rtl/ustring.hxx>
30
31 namespace rtl_string
32 {
33
34 class getLength : public ::testing::Test
35 {
36 public:
37 }; // class getLength
38
TEST_F(getLength,getLength_000)39 TEST_F(getLength, getLength_000)
40 {
41 // Former test passed NULL; the "should not GPF" comment was wrong --
42 // rtl_string_getLength does "return pThis->length", so NULL dereferences
43 // a null pointer. Verify the empty-string boundary instead: the length
44 // of an empty rtl_String is 0.
45 rtl::OString aStr;
46 sal_Int32 nValue = rtl_string_getLength( aStr.pData );
47 ASSERT_TRUE(nValue == 0) << "the length of an empty string must be 0.";
48 }
49
TEST_F(getLength,getLength_001)50 TEST_F(getLength, getLength_001)
51 {
52 rtl::OString aStr("Test Length.");
53 sal_Int32 nValue = rtl_string_getLength( aStr.pData );
54
55 ASSERT_TRUE(aStr.getLength() == nValue) << "Length must equal getLength()";
56 ASSERT_TRUE(nValue >= 0
57 && (strlen(aStr.getStr())
58 == sal::static_int_cast< sal_uInt32 >(nValue))) << "Length must equal strlen()";
59 }
60 // -----------------------------------------------------------------------------
61
62 class newFromString : public ::testing::Test
63 {
64 public:
65 }; // class newFromString
66
67 // TEST_F(newFromString, newFromString_000)
68 // {
69 // sal_Int32 nValue = rtl_string_newFromString( NULL, NULL );
70 // // should not GPF
71 // }
72
TEST_F(newFromString,newFromString_001)73 TEST_F(newFromString, newFromString_001)
74 {
75 rtl::OString aStr("Test Length.");
76 rtl_String *pStr = NULL;
77
78 rtl_string_newFromString( &pStr, aStr.pData );
79
80 rtl::OString aNewStr(pStr);
81 ASSERT_TRUE(aStr.equals(aNewStr) == sal_True) << "Strings must be equal";
82
83 rtl_string_release(pStr);
84 }
85
86 // -----------------------------------------------------------------------------
87
88 class convertUStringToString : public ::testing::Test
89 {
90 public:
91 }; // class convertUStringToString
92
93
94 // TEST_F(convertUStringToString, newFromString_000)
95 // {
96 // sal_Int32 nValue = rtl_string_newFromString( NULL, NULL );
97 // // should not GPF
98 // }
99
TEST_F(convertUStringToString,convertUStringToString_001)100 TEST_F(convertUStringToString, convertUStringToString_001)
101 {
102 rtl::OUString suString = rtl::OUString::createFromAscii("Hello");
103 rtl::OString sString;
104 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS);
105
106 ASSERT_TRUE(bRet == sal_True && sString.equals(rtl::OString("Hello")) == sal_True) << "Strings must be equal";
107 }
108
TEST_F(convertUStringToString,convertUStringToString_002)109 TEST_F(convertUStringToString, convertUStringToString_002)
110 {
111 rtl::OString sStr("H\xE4llo");
112 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
113
114 rtl::OString sString;
115 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ISO_8859_15, OUSTRING_TO_OSTRING_CVTFLAGS);
116
117 ASSERT_TRUE(bRet == sal_True && sString.equals(rtl::OString("H\xE4llo")) == sal_True) << "Strings must be equal";
118 }
119
TEST_F(convertUStringToString,convertUStringToString_003)120 TEST_F(convertUStringToString, convertUStringToString_003)
121 {
122 rtl::OString sStr("H\xC3\xA4llo");
123 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_UTF8);
124
125 rtl::OString sString;
126 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ISO_8859_15, OUSTRING_TO_OSTRING_CVTFLAGS);
127
128 ASSERT_TRUE(bRet == sal_True && sString.equals(rtl::OString("H\xE4llo")) == sal_True) << "Strings must be equal";
129 }
130
TEST_F(convertUStringToString,convertUStringToString_004)131 TEST_F(convertUStringToString, convertUStringToString_004)
132 {
133 rtl::OString sStr("Tsch\xFC\xDF");
134 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
135 rtl::OString sString;
136
137 sal_Bool bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
138 /* sal_Bool */ bRet = rtl_convertUStringToString(&sString.pData, suString.getStr(), suString.getLength(), RTL_TEXTENCODING_ISO_8859_15, OUSTRING_TO_OSTRING_CVTFLAGS);
139 ASSERT_TRUE(bRet == sal_True && sString.equals(rtl::OString("Tsch\xFC\xDF")) == sal_True) << "Strings must be equal";
140 }
141
142 } // namespace rtl_string
143
144
main(int argc,char ** argv)145 int main(int argc, char **argv)
146 {
147 ::testing::InitGoogleTest(&argc, argv);
148 return RUN_ALL_TESTS();
149 }
150