Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/io/encdec.cpp

raw
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: 9 мар. 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
23 #include <lsp-plug.in/test-fw/utest.h>
24 #include <lsp-plug.in/common/status.h>
25 #include <lsp-plug.in/stdlib/locale.h>
26 #include <lsp-plug.in/runtime/LSPString.h>
27 #include <lsp-plug.in/io/NativeFile.h>
28 #include <lsp-plug.in/io/CharsetEncoder.h>
29 #include <lsp-plug.in/io/CharsetDecoder.h>
30
31 #include <string.h>
32
33 using namespace lsp;
34 using namespace lsp::io;
35
36 // Test buffer size is a simple number, more than 0x1000
37 #define BUFFER_SIZE 4567
38
39 UTEST_BEGIN("runtime.io", encdec)
40
41 void testDecodeFile(const LSPString *src, const LSPString *dst, const char *charset)
42 {
43 NativeFile in, out;
44 CharsetDecoder decoder;
45
46 printf(" decoding %s (%s) -> %s (UTF-32)\n", src->get_native(), charset, dst->get_native());
47
48 UTEST_ASSERT(in.open(src, File::FM_READ) == STATUS_OK);
49 UTEST_ASSERT(out.open(dst, File::FM_WRITE | File::FM_CREATE | File::FM_TRUNC) == STATUS_OK);
50 UTEST_ASSERT(decoder.init(charset) == STATUS_OK);
51
52 lsp_wchar_t *obuf = new lsp_wchar_t[BUFFER_SIZE];
53 UTEST_ASSERT(obuf != NULL);
54
55 ssize_t fetched, filled;
56
57 do
58 {
59 // Is there any data on output?
60 fetched = decoder.fetch(obuf, BUFFER_SIZE);
61 if (fetched > 0)
62 {
63 ssize_t to_write = fetched * sizeof(lsp_wchar_t);
64 ssize_t written = out.write(obuf, to_write);
65 UTEST_ASSERT(written == to_write);
66 }
67 else if (fetched < 0)
68 UTEST_FAIL_MSG("decoder.fetch() failed with error %d", int(-fetched));
69
70 // Is there any data on input?
71 filled = decoder.fill(&in);
72 if ((filled < 0) && (filled != -STATUS_EOF))
73 UTEST_FAIL_MSG("decoder.fill() failed with error %d", int(-filled));
74
75 } while ((fetched > 0) || (filled > 0));
76
77 decoder.close();
78 UTEST_ASSERT(out.flush() == STATUS_OK);
79 UTEST_ASSERT(out.size() > 0);
80 UTEST_ASSERT(out.close() == STATUS_OK);
81 UTEST_ASSERT(in.close() == STATUS_OK);
82
83 delete [] obuf;
84 }
85
86 void testEncodeFile(const LSPString *src, const LSPString *dst, const char *charset)
87 {
88 NativeFile in, out;
89 CharsetEncoder encoder;
90
91 printf(" encoding %s (UTF-32) -> %s (%s)\n", src->get_native(), dst->get_native(), charset);
92
93 UTEST_ASSERT(in.open(src, File::FM_READ) == STATUS_OK);
94 UTEST_ASSERT(out.open(dst, File::FM_WRITE | File::FM_CREATE | File::FM_TRUNC) == STATUS_OK);
95 UTEST_ASSERT(encoder.init(charset) == STATUS_OK);
96
97 lsp_wchar_t *ibuf = new lsp_wchar_t[BUFFER_SIZE];
98 UTEST_ASSERT(ibuf != NULL);
99 lsp_wchar_t *ibufh = ibuf, *ibuft = ibuf;
100
101 ssize_t fetched, filled;
102
103 do
104 {
105 // Is there any data on output?
106 fetched = encoder.fetch(&out, BUFFER_SIZE);
107 if ((fetched < 0) && (fetched != -STATUS_EOF))
108 UTEST_FAIL_MSG("decoder.fetch() failed with error %d", int(-fetched));
109
110 // Do we need to fill the buffer with new data?
111 size_t count = ibuft - ibufh;
112 if (count < (BUFFER_SIZE >> 1))
113 {
114 if (count > 0)
115 ::memmove(ibuf, ibufh, count * sizeof(lsp_wchar_t));
116 ibufh = ibuf;
117 ibuft = &ibuf[count];
118
119 // Try to perform additional read
120 size_t to_read = BUFFER_SIZE - count;
121 filled = in.read(ibuft, to_read * sizeof(lsp_wchar_t));
122 if (filled > 0)
123 {
124 UTEST_ASSERT((filled % sizeof(lsp_wchar_t)) == 0);
125 count += filled / sizeof(lsp_wchar_t) ;
126 ibuft = &ibufh[count];
127 }
128 }
129
130 // Is there any data on input?
131 filled = encoder.fill(ibufh, count);
132 if (filled > 0)
133 ibufh += filled;
134 else if ((filled < 0) && (filled != -STATUS_EOF))
135 UTEST_FAIL_MSG("decoder.fill() failed with error %d", int(-filled));
136
137 } while ((fetched > 0) || (filled > 0));
138
139 encoder.close();
140 UTEST_ASSERT(out.flush() == STATUS_OK);
141 UTEST_ASSERT(out.size() > 0);
142 UTEST_ASSERT(out.close() == STATUS_OK);
143 UTEST_ASSERT(in.close() == STATUS_OK);
144
145 delete [] ibuf;
146 }
147
148 void compareFiles(const LSPString *src, const LSPString *dst)
149 {
150 NativeFile f1, f2;
151 uint8_t *b1, *b2;
152
153 printf(" comparing %s <-> %s\n", src->get_native(), dst->get_native());
154
155 UTEST_ASSERT(f1.open(src, File::FM_READ) == STATUS_OK);
156 UTEST_ASSERT(f2.open(dst, File::FM_READ) == STATUS_OK);
157 UTEST_ASSERT(f1.size() == f2.size());
158
159 b1 = new uint8_t[0x1000];
160 UTEST_ASSERT(b1 != NULL);
161 b2 = new uint8_t[0x1000];
162 UTEST_ASSERT(b2 != NULL);
163
164 while (true)
165 {
166 wssize_t offset = f1.position();
167 ssize_t read1 = f1.read(b1, 0x1000);
168 ssize_t read2 = f2.read(b2, 0x1000);
169
170 // Check read status
171 UTEST_ASSERT(read1 == read2);
172 if ((read1 == 0) || (read2 == (-STATUS_EOF)))
173 break;
174 else if (read1 < 0)
175 UTEST_FAIL_MSG("read returned %d", int(read1));
176
177 // Perform check
178 UTEST_ASSERT_MSG((::memcmp(b1, b2, read1) == 0), "Blocks offset=0x%x, size=0x%x differ", int(offset), int(read1));
179 }
180
181 UTEST_ASSERT(f1.close() == STATUS_OK);
182 UTEST_ASSERT(f2.close() == STATUS_OK);
183
184 delete [] b1;
185 delete [] b2;
186 }
187
188 void testFileCoding(const char *base, const char *src, const char *charset)
189 {
190 LSPString fenc, fdec, fsrc;
191 UTEST_ASSERT(fenc.fmt_utf8("%s" FILE_SEPARATOR_S "utest-%s-encoded.tmp", tempdir(), full_name()));
192 UTEST_ASSERT(fdec.fmt_utf8("%s" FILE_SEPARATOR_S "utest-%s-decoded.tmp", tempdir(), full_name()));
193
194 UTEST_ASSERT(fsrc.fmt_utf8("%s" FILE_SEPARATOR_S "%s" FILE_SEPARATOR_S "%s", resources(), base, src));
195 printf("Testing encoders on file %s...\n", fsrc.get_native());
196 testDecodeFile(&fsrc, &fenc, charset);
197 testEncodeFile(&fenc, &fdec, charset);
198 compareFiles(&fsrc, &fdec);
199 }
200
201 UTEST_MAIN
202 {
203 const char *base = "io" FILE_SEPARATOR_S "iconv";
204
205 testFileCoding(base, "01-de-utf16le.txt", "UTF-16LE");
206 testFileCoding(base, "01-de-utf8.txt", "UTF-8");
207 testFileCoding(base, "02-ja-utf16le.txt", "UTF-16LE");
208 testFileCoding(base, "02-ja-utf8.txt", "UTF-8");
209 testFileCoding(base, "03-ru-cp1251.txt", "CP1251");
210 testFileCoding(base, "03-ru-utf16le.txt", "UTF-16LE");
211 testFileCoding(base, "03-ru-utf8.txt", "UTF-8");
212 }
213
214 UTEST_END
215