1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include "rtl/strbuf.hxx"
24 #include "rtl/string.hxx"
25 #include "rtl/ustring.hxx"
26 #include <sal/macros.h>
28 namespace test
{ namespace oustring
{
30 class Convert
: public CppUnit::TestFixture
33 void convertToString();
35 CPPUNIT_TEST_SUITE(Convert
);
36 CPPUNIT_TEST(convertToString
);
37 CPPUNIT_TEST_SUITE_END();
42 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::Convert
);
46 struct TestConvertToString
48 sal_Unicode aSource
[100];
50 rtl_TextEncoding nEncoding
;
53 char const * pRelaxed
;
56 void testConvertToString(TestConvertToString
const & rTest
)
58 const rtl::OUString
aSource(rTest
.aSource
, rTest
.nLength
);
59 rtl::OString
aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
60 bool bSuccess
= aSource
.convertToString(&aStrict
, rTest
.nEncoding
,
62 rtl::OString
aRelaxed(rtl::OUStringToOString(aSource
, rTest
.nEncoding
,
65 rtl::OStringBuffer aPrefix
;
66 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM("{"));
67 for (sal_Int32 i
= 0; i
< rTest
.nLength
; ++i
)
69 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM("U+"));
70 aPrefix
.append(static_cast< sal_Int32
>(rTest
.aSource
[i
]), 16);
71 if (i
+ 1 < rTest
.nLength
)
72 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM(","));
74 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM("}, "));
75 aPrefix
.append(static_cast< sal_Int32
>(rTest
.nEncoding
));
76 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM(", 0x"));
77 aPrefix
.append(static_cast< sal_Int32
>(rTest
.nFlags
), 16);
78 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM(" -> "));
82 if (rTest
.pStrict
== 0 || !aStrict
.equals(rTest
.pStrict
))
84 rtl::OStringBuffer
aMessage(aPrefix
);
85 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("strict = \""));
86 aMessage
.append(aStrict
);
87 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("\""));
88 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
93 if (!aStrict
.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345"))))
95 rtl::OStringBuffer
aMessage(aPrefix
);
96 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("modified output"));
97 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
99 if (rTest
.pStrict
!= 0)
101 rtl::OStringBuffer
aMessage(aPrefix
);
102 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("failed"));
103 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
106 if (!aRelaxed
.equals(rTest
.pRelaxed
))
108 rtl::OStringBuffer
aMessage(aPrefix
);
109 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \""));
110 aMessage
.append(aRelaxed
);
111 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("\""));
112 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
118 void test::oustring::Convert::convertToString()
120 TestConvertToString
const aTests
[]
123 RTL_TEXTENCODING_ASCII_US
,
124 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
125 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
130 RTL_TEXTENCODING_ASCII_US
,
131 OUSTRING_TO_OSTRING_CVTFLAGS
,
134 { { 0x0041,0x0042,0x0043 },
136 RTL_TEXTENCODING_ASCII_US
,
137 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
138 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
141 { { 0x0041,0x0042,0x0043 },
143 RTL_TEXTENCODING_ASCII_US
,
144 OUSTRING_TO_OSTRING_CVTFLAGS
,
149 RTL_TEXTENCODING_ISO_2022_JP
,
150 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
151 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
154 { { 0x3001, 0xB800 },
156 RTL_TEXTENCODING_ISO_2022_JP
,
157 OUSTRING_TO_OSTRING_CVTFLAGS
,
158 "\x1b\x24\x42\x21\x22\x1b\x28\x42\x3f",
159 "\x1b\x24\x42\x21\x22\x1b\x28\x42\x3f" },
160 { { 0x0041,0x0100,0x0042 },
162 RTL_TEXTENCODING_ISO_8859_1
,
163 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
164 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
167 { { 0x0041,0x0100,0x0042 },
169 RTL_TEXTENCODING_ISO_8859_1
,
170 OUSTRING_TO_OSTRING_CVTFLAGS
,
173 for (unsigned int i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
174 testConvertToString(aTests
[i
]);
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */