|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright (c) 2014, Arvid Norberg |
| 4 | +All rights reserved. |
| 5 | +
|
| 6 | +Redistribution and use in source and binary forms, with or without |
| 7 | +modification, are permitted provided that the following conditions |
| 8 | +are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in |
| 14 | + the documentation and/or other materials provided with the distribution. |
| 15 | + * Neither the name of the author nor the names of its |
| 16 | + contributors may be used to endorse or promote products derived |
| 17 | + from this software without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +POSSIBILITY OF SUCH DAMAGE. |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +#include "test.hpp" |
| 34 | +#include "libtorrent/utf8.hpp" |
| 35 | +#include "libtorrent/ConvertUTF.h" |
| 36 | +#include "setup_transfer.hpp" // for load_file |
| 37 | +#include "file.hpp" // for combine_path |
| 38 | + |
| 39 | +#include <vector> |
| 40 | + |
| 41 | +using namespace libtorrent; |
| 42 | + |
| 43 | +int test_main() |
| 44 | +{ |
| 45 | + std::vector<char> utf8_source; |
| 46 | + error_code ec; |
| 47 | + load_file(combine_path("..", "utf8_test.txt"), utf8_source, ec, 1000000); |
| 48 | + if (ec) fprintf(stderr, "failed to open file: (%d) %s\n", ec.value() |
| 49 | + , ec.message().c_str()); |
| 50 | + TEST_CHECK(!ec); |
| 51 | + |
| 52 | + // test lower level conversions |
| 53 | + |
| 54 | + // utf8 -> utf16 -> utf32 -> utf8 |
| 55 | + { |
| 56 | + std::vector<UTF16> utf16(utf8_source.size()); |
| 57 | + UTF8 const* in8 = (UTF8 const*)&utf8_source[0]; |
| 58 | + UTF16* out16 = &utf16[0]; |
| 59 | + ConversionResult ret = ConvertUTF8toUTF16(&in8, in8 + utf8_source.size() |
| 60 | + , &out16, out16 + utf16.size(), strictConversion); |
| 61 | + |
| 62 | + TEST_EQUAL(ret, conversionOK); |
| 63 | + |
| 64 | + std::vector<UTF32> utf32(utf8_source.size()); |
| 65 | + UTF16 const* in16 = &utf16[0]; |
| 66 | + UTF32* out32 = &utf32[0]; |
| 67 | + ret = ConvertUTF16toUTF32(&in16, out16 |
| 68 | + , &out32, out32 + utf32.size(), strictConversion); |
| 69 | + |
| 70 | + TEST_EQUAL(ret, conversionOK); |
| 71 | + |
| 72 | + std::vector<UTF8> utf8(utf8_source.size()); |
| 73 | + UTF32 const* in32 = &utf32[0]; |
| 74 | + UTF8* out8 = &utf8[0]; |
| 75 | + ret = ConvertUTF32toUTF8(&in32, out32 |
| 76 | + , &out8, out8 + utf8.size(), strictConversion); |
| 77 | + |
| 78 | + TEST_EQUAL(ret, conversionOK); |
| 79 | + TEST_EQUAL(out8 - &utf8[0], utf8_source.size()); |
| 80 | + TEST_CHECK(std::equal(&utf8[0], out8, (UTF8 const*)&utf8_source[0])); |
| 81 | + } |
| 82 | + |
| 83 | + // utf8 -> utf32 -> utf16 -> utf8 |
| 84 | + { |
| 85 | + std::vector<UTF32> utf32(utf8_source.size()); |
| 86 | + UTF8 const* in8 = (UTF8 const*)&utf8_source[0]; |
| 87 | + UTF32* out32 = &utf32[0]; |
| 88 | + ConversionResult ret = ConvertUTF8toUTF32(&in8, in8 + utf8_source.size() |
| 89 | + , &out32, out32 + utf32.size(), strictConversion); |
| 90 | + |
| 91 | + TEST_EQUAL(ret, conversionOK); |
| 92 | + |
| 93 | + std::vector<UTF16> utf16(utf8_source.size()); |
| 94 | + UTF32 const* in32 = &utf32[0]; |
| 95 | + UTF16* out16 = &utf16[0]; |
| 96 | + ret = ConvertUTF32toUTF16(&in32, out32 |
| 97 | + , &out16, out16 + utf16.size(), strictConversion); |
| 98 | + |
| 99 | + TEST_EQUAL(ret, conversionOK); |
| 100 | + |
| 101 | + std::vector<UTF8> utf8(utf8_source.size()); |
| 102 | + UTF16 const* in16 = &utf16[0]; |
| 103 | + UTF8* out8 = &utf8[0]; |
| 104 | + ret = ConvertUTF16toUTF8(&in16, out16 |
| 105 | + , &out8, out8 + utf8.size(), strictConversion); |
| 106 | + |
| 107 | + TEST_EQUAL(ret, conversionOK); |
| 108 | + TEST_EQUAL(out8 - &utf8[0], utf8_source.size()); |
| 109 | + TEST_CHECK(std::equal(&utf8[0], out8, (UTF8 const*)&utf8_source[0])); |
| 110 | + } |
| 111 | + |
| 112 | + // test higher level conversions |
| 113 | + |
| 114 | + std::string utf8; |
| 115 | + std::copy(utf8_source.begin(), utf8_source.end(), std::back_inserter(utf8)); |
| 116 | + |
| 117 | + std::wstring wide; |
| 118 | + utf8_conv_result_t ret = utf8_wchar(utf8, wide); |
| 119 | + TEST_EQUAL(ret, conversion_ok); |
| 120 | + |
| 121 | + std::string identity; |
| 122 | + ret = wchar_utf8(wide, identity); |
| 123 | + TEST_EQUAL(ret, conversion_ok); |
| 124 | + |
| 125 | + TEST_EQUAL(utf8, identity); |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
0 commit comments