Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/resource/cbuffer.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", cbuffer)
30
31 UTEST_MAIN
32 {
33 resource::cbuffer_t buf;
34 size_t offset = 0, length = 0;
35
36 printf("Testing compression buffer");
37
38 UTEST_ASSERT(buf.init(LOG_BUF_SIZE) == STATUS_OK);
39
40 // Check that we can not find value in empty buffer
41 // buffer state: ???? ???? ???? ???? ???? ???? ???? ????
42 // ^
43 UTEST_ASSERT(buf.size() == 0);
44 length = buf.lookup(&offset, "a", 1);
45 UTEST_ASSERT(length == 0);
46 length = buf.lookup(&offset, "b", 1);
47 UTEST_ASSERT(length == 0);
48
49 // Append single byte
50 // buffer state: a??? ???? ???? ???? ???? ???? ???? ????
51 // ^
52 buf.append('a');
53 UTEST_ASSERT(buf.size() == 1);
54 length = buf.lookup(&offset, "a", 1);
55 UTEST_ASSERT(length == 1);
56 UTEST_ASSERT(offset == 0);
57 length = buf.lookup(&offset, "b", 1);
58 UTEST_ASSERT(length == 0);
59
60 // Append small sequence of bytes
61 // buffer state: abcd efgh ???? ???? ???? ???? ???? ????
62 // ^
63 buf.append("bcdefgh", 7);
64 UTEST_ASSERT(buf.size() == 8);
65 length = buf.lookup(&offset, "a", 1);
66 UTEST_ASSERT(length == 1);
67 UTEST_ASSERT(offset == 7);
68 length = buf.lookup(&offset, "b", 1);
69 UTEST_ASSERT(length == 1);
70 UTEST_ASSERT(offset == 6);
71 length = buf.lookup(&offset, "ab", 2);
72 UTEST_ASSERT(length == 2);
73 UTEST_ASSERT(offset == 7);
74
75 // Append yet another small sequence of bytes
76 // buffer state: abcd efgh abc0 1234 ???? ???? ???? ????
77 // ^
78 buf.append("abc01234", 8);
79 UTEST_ASSERT(buf.size() == 16);
80 length = buf.lookup(&offset, "a", 1);
81 UTEST_ASSERT(length == 1);
82 UTEST_ASSERT(offset == 7);
83 length = buf.lookup(&offset, "b", 1);
84 UTEST_ASSERT(length == 1);
85 UTEST_ASSERT(offset == 6);
86 length = buf.lookup(&offset, "d", 1);
87 UTEST_ASSERT(length == 1);
88 UTEST_ASSERT(offset == 12);
89 length = buf.lookup(&offset, "abc", 3);
90 UTEST_ASSERT(length == 3);
91 UTEST_ASSERT(offset == 7);
92 length = buf.lookup(&offset, "abcd", 4);
93 UTEST_ASSERT(length == 4);
94 UTEST_ASSERT(offset == 15);
95
96 // Append more data to the buffer
97 // buffer state: abcd efgh abc0 1234 0123 4567 abcd efg?
98 // ^
99 buf.append("01234567abcdefg", 15);
100 UTEST_ASSERT(buf.size() == 31);
101 length = buf.lookup(&offset, "a", 1);
102 UTEST_ASSERT(length == 1);
103 UTEST_ASSERT(offset == 6);
104 length = buf.lookup(&offset, "b", 1);
105 UTEST_ASSERT(length == 1);
106 UTEST_ASSERT(offset == 5);
107 length = buf.lookup(&offset, "abc", 3);
108 UTEST_ASSERT(length == 3);
109 UTEST_ASSERT(offset == 6);
110 length = buf.lookup(&offset, "abcd", 4);
111 UTEST_ASSERT(length == 4);
112 UTEST_ASSERT(offset == 6);
113 length = buf.lookup(&offset, "abcdefghabc", 11);
114 UTEST_ASSERT(length == 11);
115 UTEST_ASSERT(offset == 30);
116 length = buf.lookup(&offset, "abcdefghabcdefgh", 16);
117 UTEST_ASSERT(length == 11);
118 UTEST_ASSERT(offset == 30);
119
120 // Fill the buffer with overlap
121 // buffer state: 0123 4567 abc0 1234 0123 4567 abcd efgh
122 // ^
123 buf.append("h01234567", 9);
124 UTEST_ASSERT(buf.size() == 32);
125 length = buf.lookup(&offset, "a", 1);
126 UTEST_ASSERT(length == 1);
127 UTEST_ASSERT(offset == 15);
128 length = buf.lookup(&offset, "b", 1);
129 UTEST_ASSERT(length == 1);
130 UTEST_ASSERT(offset == 14);
131 length = buf.lookup(&offset, "abc", 3);
132 UTEST_ASSERT(length == 3);
133 UTEST_ASSERT(offset == 15);
134 length = buf.lookup(&offset, "abcd", 4);
135 UTEST_ASSERT(length == 4);
136 UTEST_ASSERT(offset == 15);
137 length = buf.lookup(&offset, "defgK", 5);
138 UTEST_ASSERT(length == 4);
139 UTEST_ASSERT(offset == 12);
140 length = buf.lookup(&offset, "abcdefghabc", 11);
141 UTEST_ASSERT(length == 8);
142 UTEST_ASSERT(offset == 15);
143 length = buf.lookup(&offset, "abcdefghabcdefgh", 16);
144 UTEST_ASSERT(length == 8);
145 UTEST_ASSERT(offset == 15);
146 length = buf.lookup(&offset, "234567", 6);
147 UTEST_ASSERT(length == 6);
148 UTEST_ASSERT(offset == 5);
149 length = buf.lookup(&offset, "234567abc", 9);
150 UTEST_ASSERT(length == 9);
151 UTEST_ASSERT(offset == 21);
152 length = buf.lookup(&offset, "234567abcd", 10);
153 UTEST_ASSERT(length == 10);
154 UTEST_ASSERT(offset == 21);
155
156 // Fully overwrite the buffer
157 // buffer state: ABCD EFGH IJKL MNOP QRST UVWX YZ01 2345
158 // ^
159 buf.append("blablablaABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 41);
160 UTEST_ASSERT(buf.size() == 32);
161 length = buf.lookup(&offset, "ABCD", 4);
162 UTEST_ASSERT(length == 4);
163 UTEST_ASSERT(offset == 31);
164 length = buf.lookup(&offset, "EFGHIJKL", 8);
165 UTEST_ASSERT(length == 8);
166 UTEST_ASSERT(offset == 27);
167 length = buf.lookup(&offset, "MNOPQRSTUVWX1234", 16);
168 UTEST_ASSERT(length == 12);
169 UTEST_ASSERT(offset == 19);
170 length = buf.lookup(&offset, "YZ012345ABCD", 12);
171 UTEST_ASSERT(length == 8);
172 UTEST_ASSERT(offset == 7);
173 }
174
175 UTEST_END
176
177
178