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.hxx" 34 #include "rtl/ustring.hxx" 35 36 namespace test { namespace oustring { 37 38 class Convert: public CppUnit::TestFixture 39 { 40 private: 41 void convertToString(); 42 43 CPPUNIT_TEST_SUITE(Convert); 44 CPPUNIT_TEST(convertToString); 45 CPPUNIT_TEST_SUITE_END(); 46 }; 47 48 } } 49 50 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustring::Convert, "alltest"); 51 52 namespace { 53 54 struct TestConvertToString 55 { 56 sal_Unicode aSource[100]; 57 sal_Int32 nLength; 58 rtl_TextEncoding nEncoding; 59 sal_uInt32 nFlags; 60 char const * pStrict; 61 char const * pRelaxed; 62 }; 63 64 void testConvertToString(TestConvertToString const & rTest) 65 { 66 const rtl::OUString aSource(rTest.aSource, rTest.nLength); 67 rtl::OString aStrict(RTL_CONSTASCII_STRINGPARAM("12345")); 68 bool bSuccess = aSource.convertToString(&aStrict, rTest.nEncoding, 69 rTest.nFlags); 70 rtl::OString aRelaxed(rtl::OUStringToOString(aSource, rTest.nEncoding, 71 rTest.nFlags)); 72 73 rtl::OStringBuffer aPrefix; 74 aPrefix.append(RTL_CONSTASCII_STRINGPARAM("{")); 75 for (sal_Int32 i = 0; i < rTest.nLength; ++i) 76 { 77 aPrefix.append(RTL_CONSTASCII_STRINGPARAM("U+")); 78 aPrefix.append(static_cast< sal_Int32 >(rTest.aSource[i]), 16); 79 if (i + 1 < rTest.nLength) 80 aPrefix.append(RTL_CONSTASCII_STRINGPARAM(",")); 81 } 82 aPrefix.append(RTL_CONSTASCII_STRINGPARAM("}, ")); 83 aPrefix.append(static_cast< sal_Int32 >(rTest.nEncoding)); 84 aPrefix.append(RTL_CONSTASCII_STRINGPARAM(", 0x")); 85 aPrefix.append(static_cast< sal_Int32 >(rTest.nFlags), 16); 86 aPrefix.append(RTL_CONSTASCII_STRINGPARAM(" -> ")); 87 88 if (bSuccess) 89 { 90 if (rTest.pStrict == 0 || !aStrict.equals(rTest.pStrict)) 91 { 92 rtl::OStringBuffer aMessage(aPrefix); 93 aMessage.append(RTL_CONSTASCII_STRINGPARAM("strict = \"")); 94 aMessage.append(aStrict); 95 aMessage.append(RTL_CONSTASCII_STRINGPARAM("\"")); 96 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false); 97 } 98 } 99 else 100 { 101 if (!aStrict.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345")))) 102 { 103 rtl::OStringBuffer aMessage(aPrefix); 104 aMessage.append(RTL_CONSTASCII_STRINGPARAM("modified output")); 105 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false); 106 } 107 if (rTest.pStrict != 0) 108 { 109 rtl::OStringBuffer aMessage(aPrefix); 110 aMessage.append(RTL_CONSTASCII_STRINGPARAM("failed")); 111 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false); 112 } 113 } 114 if (!aRelaxed.equals(rTest.pRelaxed)) 115 { 116 rtl::OStringBuffer aMessage(aPrefix); 117 aMessage.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \"")); 118 aMessage.append(aRelaxed); 119 aMessage.append(RTL_CONSTASCII_STRINGPARAM("\"")); 120 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false); 121 } 122 } 123 124 } 125 126 void test::oustring::Convert::convertToString() 127 { 128 TestConvertToString const aTests[] 129 = { { { 0 }, 130 0, 131 RTL_TEXTENCODING_ASCII_US, 132 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR 133 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR, 134 "", 135 "" }, 136 { { 0 }, 137 0, 138 RTL_TEXTENCODING_ASCII_US, 139 OUSTRING_TO_OSTRING_CVTFLAGS, 140 "", 141 "" }, 142 { { 0x0041,0x0042,0x0043 }, 143 3, 144 RTL_TEXTENCODING_ASCII_US, 145 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR 146 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR, 147 "ABC", 148 "ABC" }, 149 { { 0x0041,0x0042,0x0043 }, 150 3, 151 RTL_TEXTENCODING_ASCII_US, 152 OUSTRING_TO_OSTRING_CVTFLAGS, 153 "ABC", 154 "ABC" }, 155 { { 0xB800 }, 156 1, 157 RTL_TEXTENCODING_ISO_2022_JP, 158 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR 159 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR, 160 0, 161 "" }, 162 // the next also tests that a short source produces a long target: 163 { { 0xB800 }, 164 1, 165 RTL_TEXTENCODING_ISO_2022_JP, 166 OUSTRING_TO_OSTRING_CVTFLAGS, 167 "\x1B(B?", 168 "\x1B(B?" }, 169 { { 0x0041,0x0100,0x0042 }, 170 3, 171 RTL_TEXTENCODING_ISO_8859_1, 172 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR 173 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR, 174 0, 175 "A" }, 176 { { 0x0041,0x0100,0x0042 }, 177 3, 178 RTL_TEXTENCODING_ISO_8859_1, 179 OUSTRING_TO_OSTRING_CVTFLAGS, 180 "A?B", 181 "A?B" } }; 182 for (unsigned int i = 0; i < sizeof aTests / sizeof aTests[0]; ++i) 183 testConvertToString(aTests[i]); 184 } 185