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