Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 25 июл. 2025 г.
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/resource/buffer.h>
24
25 using namespace lsp;
26
27 static constexpr size_t LOG_BUF_SIZE = 5;
28
29 UTEST_BEGIN("runtime.resource", dbuffer)
30
31 UTEST_MAIN
32 {
33 resource::dbuffer_t buf;
34 uint8_t tmp[0x20];
35
36 printf("Testing decompression buffer");
37
38 UTEST_ASSERT(buf.init(LOG_BUF_SIZE) == STATUS_OK);
39
40 // Check that we can not extract data from empty buffer
41 // buffer state: ???? ???? ???? ???? ???? ???? ???? ????
42 // ^
43 UTEST_ASSERT(buf.size() == 0);
44 UTEST_ASSERT(buf.extract(tmp, 0, 1) == STATUS_UNDERFLOW);
45
46 // Append single byte
47 // buffer state: a??? ???? ???? ???? ???? ???? ???? ????
48 // ^
49 buf.append('a');
50 UTEST_ASSERT(buf.size() == 1);
51 UTEST_ASSERT(buf.extract(tmp, 0, 1) == STATUS_OK);
52 UTEST_ASSERT(tmp[0] == 'a');
53 UTEST_ASSERT(buf.extract(tmp, 0, 2) == STATUS_UNDERFLOW);
54 UTEST_ASSERT(buf.extract(tmp, 1, 1) == STATUS_UNDERFLOW);
55
56 // Append small sequence of bytes
57 // buffer state: abcd efgh ???? ???? ???? ???? ???? ????
58 // ^
59 buf.append("bcdefgh", 7);
60 UTEST_ASSERT(buf.size() == 8);
61 UTEST_ASSERT(buf.extract(tmp, 0, 1) == STATUS_OK);
62 UTEST_ASSERT(tmp[0] == 'h');
63 UTEST_ASSERT(buf.extract(tmp, 6, 1) == STATUS_OK);
64 UTEST_ASSERT(tmp[0] == 'b');
65 UTEST_ASSERT(buf.extract(tmp, 7, 8) == STATUS_OK);
66 UTEST_ASSERT(memcmp(tmp, "abcdefgh", 8) == 0);
67 UTEST_ASSERT(buf.extract(tmp, 3, 4) == STATUS_OK);
68 UTEST_ASSERT(memcmp(tmp, "efgh", 4) == 0);
69 UTEST_ASSERT(buf.extract(tmp, 3, 8) == STATUS_UNDERFLOW);
70 UTEST_ASSERT(buf.extract(tmp, 7, 9) == STATUS_UNDERFLOW);
71
72 // Append yet another small sequence of bytes
73 // buffer state: abcd efgh abc0 1234 ???? ???? ???? ????
74 // ^
75 buf.append("abc01234", 8);
76 UTEST_ASSERT(buf.size() == 16);
77 UTEST_ASSERT(buf.extract(tmp, 7, 1) == STATUS_OK);
78 UTEST_ASSERT(tmp[0] == 'a');
79 UTEST_ASSERT(buf.extract(tmp, 0, 1) == STATUS_OK);
80 UTEST_ASSERT(tmp[0] == '4');
81 UTEST_ASSERT(buf.extract(tmp, 7, 8) == STATUS_OK);
82 UTEST_ASSERT(memcmp(tmp, "abc01234", 8) == 0);
83 UTEST_ASSERT(buf.extract(tmp, 15, 16) == STATUS_OK);
84 UTEST_ASSERT(memcmp(tmp, "abcdefghabc01234", 16) == 0);
85
86 // Append more data to the buffer
87 // buffer state: abcd efgh abc0 1234 0123 4567 abcd efg?
88 // ^
89 buf.append("01234567abcdefg", 15);
90 UTEST_ASSERT(buf.size() == 31);
91 UTEST_ASSERT(buf.extract(tmp, 0, 1) == STATUS_OK);
92 UTEST_ASSERT(tmp[0] == 'g');
93 UTEST_ASSERT(buf.extract(tmp, 7, 8) == STATUS_OK);
94 UTEST_ASSERT(memcmp(tmp, "7abcdefg", 8) == 0);
95 UTEST_ASSERT(buf.extract(tmp, 15, 16) == STATUS_OK);
96 UTEST_ASSERT(memcmp(tmp, "401234567abcdefg", 16) == 0);
97
98 // Fill the buffer with overlap
99 // buffer state: 0123 4567 abc0 1234 0123 4567 abcd efgh
100 // ^
101 buf.append("h01234567", 9);
102 UTEST_ASSERT(buf.size() == 32);
103 UTEST_ASSERT(buf.extract(tmp, 31, 32) == STATUS_OK);
104 UTEST_ASSERT(memcmp(tmp, "abc0123401234567abcdefgh01234567", 32) == 0);
105 UTEST_ASSERT(buf.extract(tmp, 7, 8) == STATUS_OK);
106 UTEST_ASSERT(memcmp(tmp, "01234567", 8) == 0);
107
108 // Fully overwrite the buffer
109 // buffer state: ABCD EFGH IJKL MNOP QRST UVWX YZ01 2345
110 // ^
111 buf.append("blablablaABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 41);
112 UTEST_ASSERT(buf.size() == 32);
113 UTEST_ASSERT(buf.extract(tmp, 31, 32) == STATUS_OK);
114 UTEST_ASSERT(memcmp(tmp, "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 32) == 0);
115 }
116
117 UTEST_END
118
119