Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/resource/compressor.cpp

raw
1 /*
2 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2021 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 3 мар. 2021 г.
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/test-fw/utest.h>
23 #include <lsp-plug.in/common/alloc.h>
24 #include <lsp-plug.in/io/Path.h>
25 #include <lsp-plug.in/io/Dir.h>
26 #include <lsp-plug.in/io/InFileStream.h>
27 #include <lsp-plug.in/io/OutFileStream.h>
28 #include <lsp-plug.in/resource/Compressor.h>
29 #include <lsp-plug.in/resource/Decompressor.h>
30 #include <lsp-plug.in/resource/BuiltinLoader.h>
31
32 using namespace lsp;
33
34 static constexpr size_t LOG_BUFFER_SIZE = 12;
35 static constexpr size_t FLUSH_LIMIT = 1 << 16;
36
37 UTEST_BEGIN("runtime.resource", compressor)
38
39 UTEST_TIMELIMIT(30)
40
41 void scan_directory(wsize_t *data_size, const io::Path *base, const io::Path *path, resource::Compressor *c)
42 {
43 io::Dir dir;
44 status_t res;
45 LSPString str;
46 io::Path child, relative;
47 io::fattr_t fattr;
48 size_t compressed = 0;
49
50 UTEST_ASSERT(dir.open(path) == STATUS_OK);
51 while ((res = dir.reads(&str, &fattr, false)) == STATUS_OK)
52 {
53 if (io::Path::is_dots(&str))
54 continue;
55
56 UTEST_ASSERT(child.set(path, &str) == STATUS_OK);
57 UTEST_ASSERT(relative.set(path, &str) == STATUS_OK);
58 UTEST_ASSERT(relative.remove_base(base) == STATUS_OK);
59
60 printf(" found entry: %s\n", relative.as_native());
61
62 if (fattr.type == io::fattr_t::FT_REGULAR)
63 {
64 io::InFileStream ifs;
65 UTEST_ASSERT(ifs.open(&child) == STATUS_OK);
66 wssize_t len = c->create_file(&relative, &ifs);
67 UTEST_ASSERT(len >= 0);
68 UTEST_ASSERT(ifs.close() == STATUS_OK);
69
70 *data_size += len;
71 compressed += len;
72 if (compressed >= FLUSH_LIMIT)
73 {
74 UTEST_ASSERT(c->flush() == STATUS_OK);
75 compressed = 0;
76 }
77 }
78 else if (fattr.type == io::fattr_t::FT_DIRECTORY)
79 {
80 UTEST_ASSERT(c->create_dir(&relative) == STATUS_OK);
81 scan_directory(data_size, base, &child, c);
82 }
83 }
84 }
85
86 void scan_resources(resource::BuiltinLoader *load, const io::Path *path, const io::Path *temp, const io::Path *rel)
87 {
88 resource::resource_t *rlist = NULL;
89 io::Path file, child, out;
90 io::OutMemoryStream oms1, oms2;
91 io::InFileStream ifs;
92 io::IInStream *irs;
93 io::OutFileStream ofs;
94
95 ssize_t items = load->enumerate(rel, &rlist);
96 UTEST_ASSERT(items >= 0);
97 UTEST_ASSERT(rlist != NULL);
98
99 for (ssize_t i=0; i<items; ++i)
100 {
101 resource::resource_t *item = &rlist[i];
102
103 UTEST_ASSERT(child.set(rel, item->name) == STATUS_OK);
104 printf(" found entry: %s\n", child.as_native());
105
106 if (item->type == resource::RES_DIR)
107 {
108 scan_resources(load, path, temp, &child);
109 continue;
110 }
111
112 // Decompress the item
113 oms1.clear();
114 UTEST_ASSERT((irs = load->read_stream(&child)) != NULL);
115 wssize_t sz1 = irs->sink(&oms1);
116 UTEST_ASSERT(sz1 >= 0);
117 UTEST_ASSERT(irs->close() == STATUS_OK);
118 delete irs;
119 UTEST_ASSERT(oms1.size() == size_t(sz1));
120 printf(" decompressed entry size: %ld bytes\n", long(sz1));
121
122 // Save the decompressed entry
123 UTEST_ASSERT(out.set(temp, rel) == STATUS_OK);
124 UTEST_ASSERT(out.append_child(item->name) == STATUS_OK);
125 printf(" saving decompressed entry as: %s\n", out.as_native());
126 UTEST_ASSERT(out.mkparent(true) == STATUS_OK);
127 UTEST_ASSERT(ofs.open(&out, io::File::FM_WRITE_NEW) == STATUS_OK);
128 wssize_t osz = ofs.write(oms1.data(), oms1.size());
129 UTEST_ASSERT(osz == sz1);
130 UTEST_ASSERT(ofs.close() == STATUS_OK);
131
132 // Read the original file
133 oms2.clear();
134 UTEST_ASSERT(file.set(path, &child) == STATUS_OK);
135 UTEST_ASSERT(ifs.open(&file) == STATUS_OK);
136 wssize_t sz2 = ifs.sink(&oms2);
137 UTEST_ASSERT(ifs.close() == STATUS_OK);
138 UTEST_ASSERT(sz2 >= 0);
139 UTEST_ASSERT(oms2.size() == size_t(sz2));
140 printf(" original entry size: %ld bytes\n", long(sz2));
141
142 // Compare the data
143 UTEST_ASSERT(sz1 == sz2);
144 UTEST_ASSERT(memcmp(oms1.data(), oms2.data(), sz1) == 0);
145 oms1.drop();
146 oms2.drop();
147 }
148
149 // Free items
150 free(rlist);
151 }
152
153 void test_compress_data(const io::Path *path, resource::Compressor *c, io::OutMemoryStream *os)
154 {
155 wsize_t data_size = 0;
156 io::Path tmp;
157 io::OutFileStream ofs;
158
159 // Scan and compress directory
160 UTEST_ASSERT(c->init(LOG_BUFFER_SIZE, os) == STATUS_OK);
161 printf("Scanning source directory...\n");
162 scan_directory(&data_size, path, path, c);
163 c->flush(); // Flush compressor if ther is some data
164
165 size_t buf_sz = os->size();
166 double ratio = double(data_size) / double(buf_sz);
167
168 printf("Command size: %d, data size: %d, ratio: %.2f\n",
169 int(os->size()),
170 int(data_size),
171 ratio
172 );
173
174 UTEST_ASSERT(tmp.fmt("%s/%s.commands", tempdir(), full_name()) > 0);
175 printf("Dumping commands to: %s\n", tmp.as_native());
176 UTEST_ASSERT(ofs.open(&tmp, io::File::FM_WRITE_NEW) == STATUS_OK);
177 ofs.write(os->data(), os->size());
178 UTEST_ASSERT(ofs.close() == STATUS_OK);
179 }
180
181 void test_decompress_data(const io::Path *path, resource::Compressor *c, io::OutMemoryStream *os)
182 {
183 resource::BuiltinLoader load;
184 io::Path rel;
185 io::Path tmp;
186
187 UTEST_ASSERT(tmp.fmt("%s/utest-%s", tempdir(), full_name()) > 0);
188 UTEST_ASSERT(load.init(os->data(), os->size(), c->entries(), c->num_entires(), LOG_BUFFER_SIZE) == STATUS_OK);
189 printf("Scanning resource registry...\n");
190 scan_resources(&load, path, &tmp, &rel);
191 }
192
193 UTEST_MAIN
194 {
195 io::Path path;
196 resource::Compressor c;
197 io::OutMemoryStream oms;
198
199 UTEST_ASSERT(path.fmt("%s/compressor", resources()) > 0);
200 printf("Resource directory: %s\n", path.as_native());
201
202 // Compress data
203 test_compress_data(&path, &c, &oms);
204
205 // Decompress data
206 test_decompress_data(&path, &c, &oms);
207
208 UTEST_ASSERT(c.close() == STATUS_OK);
209 oms.drop();
210 UTEST_ASSERT(oms.close() == STATUS_OK);
211 }
212
213 UTEST_END
214
215