| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 6 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/io/InSequence.h> |
| 23 |
#include <lsp-plug.in/test-fw/utest.h> |
| 24 |
#include <lsp-plug.in/common/status.h> |
| 25 |
#include <lsp-plug.in/stdlib/string.h> |
| 26 |
#include <lsp-plug.in/io/NativeFile.h> |
| 27 |
#include <lsp-plug.in/io/OutSequence.h> |
| 28 |
|
| 29 |
// Test buffer size is a simple number, more than 0x1000 |
| 30 |
#define BUFFER_SIZE 4567 |
| 31 |
|
| 32 |
using namespace lsp; |
| 33 |
using namespace lsp::io; |
| 34 |
|
| 35 |
UTEST_BEGIN("runtime.io", sequence) |
| 36 |
|
| 37 |
void testDecodeFile(const LSPString *src, const LSPString *dst, const char *charset) |
| 38 |
{ |
| 39 |
InSequence in; |
| 40 |
NativeFile out; |
| 41 |
|
| 42 |
printf(" decoding %s (%s) -> %s (UTF-32)\n", src->get_native(), charset, dst->get_native()); |
| 43 |
|
| 44 |
UTEST_ASSERT(in.open(src, charset) == STATUS_OK); |
| 45 |
UTEST_ASSERT(out.open(dst, File::FM_WRITE | File::FM_CREATE | File::FM_TRUNC) == STATUS_OK); |
| 46 |
|
| 47 |
lsp_wchar_t *buf = new lsp_wchar_t[BUFFER_SIZE]; |
| 48 |
UTEST_ASSERT(buf != NULL); |
| 49 |
|
| 50 |
while (true) |
| 51 |
{ |
| 52 |
// Read characters |
| 53 |
ssize_t nread = in.read(buf, BUFFER_SIZE); |
| 54 |
if (nread <= 0) |
| 55 |
{ |
| 56 |
if ((nread == 0) || (nread == -STATUS_EOF)) |
| 57 |
break; |
| 58 |
UTEST_FAIL_MSG("read() returned status: %d", -int(nread)); |
| 59 |
} |
| 60 |
|
| 61 |
// Write characters |
| 62 |
ssize_t written = out.write(buf, nread * sizeof(lsp_wchar_t)); |
| 63 |
UTEST_ASSERT(written == ssize_t(nread * sizeof(lsp_wchar_t))); |
| 64 |
} |
| 65 |
|
| 66 |
UTEST_ASSERT(out.close() == STATUS_OK); |
| 67 |
UTEST_ASSERT(in.close() == STATUS_OK); |
| 68 |
|
| 69 |
delete [] buf; |
| 70 |
} |
| 71 |
|
| 72 |
void testEncodeFile(const LSPString *src, const LSPString *dst, const char *charset) |
| 73 |
{ |
| 74 |
NativeFile in; |
| 75 |
OutSequence out; |
| 76 |
|
| 77 |
printf(" encoding %s (UTF-32) -> %s (%s)\n", src->get_native(), dst->get_native(), charset); |
| 78 |
|
| 79 |
UTEST_ASSERT(in.open(src, File::FM_READ) == STATUS_OK); |
| 80 |
UTEST_ASSERT(out.open(dst, File::FM_WRITE | File::FM_CREATE | File::FM_TRUNC, charset) == STATUS_OK); |
| 81 |
|
| 82 |
lsp_wchar_t *buf = new lsp_wchar_t[BUFFER_SIZE]; |
| 83 |
UTEST_ASSERT(buf != NULL); |
| 84 |
|
| 85 |
while (true) |
| 86 |
{ |
| 87 |
// Read characters |
| 88 |
ssize_t nread = in.read(buf, BUFFER_SIZE * sizeof(lsp_wchar_t)); |
| 89 |
if (nread <= 0) |
| 90 |
{ |
| 91 |
if ((nread == 0) || (nread == -STATUS_EOF)) |
| 92 |
break; |
| 93 |
UTEST_FAIL_MSG("read() returned status: %d", -int(nread)); |
| 94 |
} |
| 95 |
else |
| 96 |
UTEST_ASSERT((nread % sizeof(lsp_wchar_t)) == 0); |
| 97 |
|
| 98 |
// Do we need to shift the buffer? |
| 99 |
status_t res = out.write(buf, nread / sizeof(lsp_wchar_t)); |
| 100 |
UTEST_ASSERT(res == STATUS_OK); |
| 101 |
} |
| 102 |
|
| 103 |
UTEST_ASSERT(out.close() == STATUS_OK); |
| 104 |
UTEST_ASSERT(in.close() == STATUS_OK); |
| 105 |
|
| 106 |
delete [] buf; |
| 107 |
} |
| 108 |
|
| 109 |
void compareFiles(const LSPString *src, const LSPString *dst) |
| 110 |
{ |
| 111 |
NativeFile f1, f2; |
| 112 |
uint8_t *b1, *b2; |
| 113 |
|
| 114 |
printf(" comparing %s <-> %s\n", src->get_native(), dst->get_native()); |
| 115 |
|
| 116 |
UTEST_ASSERT(f1.open(src, File::FM_READ) == STATUS_OK); |
| 117 |
UTEST_ASSERT(f2.open(dst, File::FM_READ) == STATUS_OK); |
| 118 |
UTEST_ASSERT(f1.size() == f2.size()); |
| 119 |
|
| 120 |
b1 = new uint8_t[0x1000]; |
| 121 |
UTEST_ASSERT(b1 != NULL); |
| 122 |
b2 = new uint8_t[0x1000]; |
| 123 |
UTEST_ASSERT(b2 != NULL); |
| 124 |
|
| 125 |
while (true) |
| 126 |
{ |
| 127 |
wssize_t offset = f1.position(); |
| 128 |
ssize_t read1 = f1.read(b1, 0x1000); |
| 129 |
ssize_t read2 = f2.read(b2, 0x1000); |
| 130 |
|
| 131 |
// Check read status |
| 132 |
UTEST_ASSERT(read1 == read2); |
| 133 |
if ((read1 == 0) || (read2 == (-STATUS_EOF))) |
| 134 |
break; |
| 135 |
else if (read1 < 0) |
| 136 |
UTEST_FAIL_MSG("read returned %d", int(read1)); |
| 137 |
|
| 138 |
// Perform check |
| 139 |
UTEST_ASSERT_MSG((::memcmp(b1, b2, read1) == 0), "Blocks offset=0x%x, size=0x%x differ", int(offset), int(read1)); |
| 140 |
} |
| 141 |
|
| 142 |
UTEST_ASSERT(f1.close() == STATUS_OK); |
| 143 |
UTEST_ASSERT(f2.close() == STATUS_OK); |
| 144 |
|
| 145 |
delete [] b1; |
| 146 |
delete [] b2; |
| 147 |
} |
| 148 |
|
| 149 |
void testFileCoding(const char *base, const char *src, const char *charset) |
| 150 |
{ |
| 151 |
LSPString fenc, fdec, fsrc; |
| 152 |
UTEST_ASSERT(fenc.fmt_utf8("%s" FILE_SEPARATOR_S "utest-%s-encoded.tmp", tempdir(), full_name())); |
| 153 |
UTEST_ASSERT(fdec.fmt_utf8("%s" FILE_SEPARATOR_S "utest-%s-decoded.tmp", tempdir(), full_name())); |
| 154 |
|
| 155 |
UTEST_ASSERT(fsrc.fmt_utf8("%s" FILE_SEPARATOR_S "%s" FILE_SEPARATOR_S "%s", resources(), base, src)); |
| 156 |
printf("Testing encoders on file %s...\n", fsrc.get_native()); |
| 157 |
testDecodeFile(&fsrc, &fenc, charset); |
| 158 |
testEncodeFile(&fenc, &fdec, charset); |
| 159 |
compareFiles(&fsrc, &fdec); |
| 160 |
} |
| 161 |
|
| 162 |
UTEST_MAIN |
| 163 |
{ |
| 164 |
const char *base = "io" FILE_SEPARATOR_S "iconv"; |
| 165 |
|
| 166 |
testFileCoding(base, "01-de-utf16le.txt", "UTF-16LE"); |
| 167 |
testFileCoding(base, "01-de-utf8.txt", "UTF-8"); |
| 168 |
testFileCoding(base, "02-ja-utf16le.txt", "UTF-16LE"); |
| 169 |
testFileCoding(base, "02-ja-utf8.txt", "UTF-8"); |
| 170 |
testFileCoding(base, "03-ru-cp1251.txt", "CP1251"); |
| 171 |
testFileCoding(base, "03-ru-utf16le.txt", "UTF-16LE"); |
| 172 |
testFileCoding(base, "03-ru-utf8.txt", "UTF-8"); |
| 173 |
} |
| 174 |
UTEST_END |