Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 22 февр. 2023 г.
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/test-fw/ByteBuffer.h>
24 #include <lsp-plug.in/io/InSharedMemoryStream.h>
25
26 UTEST_BEGIN("runtime.io", insharedmemorystream)
27
28 void test_simple_share()
29 {
30 ByteBuffer src(0x100);
31 ByteBuffer dst1(0x100);
32 ByteBuffer dst2(0x100);
33 src.randomize();
34 dst1.randomize();
35 dst2.randomize();
36
37 // Do in limited context
38 {
39 io::InSharedMemoryStream a(src.data<void>(), src.size());
40 UTEST_ASSERT(a.references() == 1);
41
42 io::InSharedMemoryStream b = a;
43 UTEST_ASSERT(a.references() == 2);
44 UTEST_ASSERT(b.references() == 2);
45
46 UTEST_ASSERT(a.avail() == ssize_t(src.size()));
47 UTEST_ASSERT(b.avail() == ssize_t(src.size()));
48
49 // Read first (step 1)
50 UTEST_ASSERT(a.read(dst1.data<void>(a.position()), 0x20) == 0x20);
51 UTEST_ASSERT(a.avail() == ssize_t(src.size() - 0x20));
52 UTEST_ASSERT(b.avail() == ssize_t(src.size()));
53 UTEST_ASSERT(a.position() == 0x20);
54 UTEST_ASSERT(b.position() == 0x00);
55
56 // Read second (step 1)
57 UTEST_ASSERT(b.read(dst2.data<void>(b.position()), 0xe0) == 0xe0);
58 UTEST_ASSERT(a.avail() == ssize_t(src.size() - 0x20));
59 UTEST_ASSERT(b.avail() == ssize_t(src.size() - 0xe0));
60 UTEST_ASSERT(a.position() == 0x20);
61 UTEST_ASSERT(b.position() == 0xe0);
62
63 // Read first (step 2)
64 UTEST_ASSERT(a.read(dst1.data<void>(a.position()), 0x100) == 0xe0);
65 UTEST_ASSERT(a.avail() == 0);
66 UTEST_ASSERT(b.avail() == ssize_t(src.size() - 0xe0));
67 UTEST_ASSERT(a.position() == 0x100);
68 UTEST_ASSERT(b.position() == 0xe0);
69
70 // Read second (step 2)
71 UTEST_ASSERT(b.read(dst2.data<void>(b.position()), 0x100) == 0x20);
72 UTEST_ASSERT(a.avail() == 0);
73 UTEST_ASSERT(b.avail() == 0);
74 UTEST_ASSERT(a.position() == 0x100);
75 UTEST_ASSERT(b.position() == 0x100);
76
77 // Read first (step 3)
78 UTEST_ASSERT(a.read(dst1.data<void>(a.position()), 0x100) == -STATUS_EOF);
79 UTEST_ASSERT(a.avail() == 0);
80 UTEST_ASSERT(b.avail() == 0);
81 UTEST_ASSERT(a.position() == 0x100);
82 UTEST_ASSERT(b.position() == 0x100);
83
84 // Read second (step 3)
85 UTEST_ASSERT(b.read(dst2.data<void>(b.position()), 0x100) == -STATUS_EOF);
86 UTEST_ASSERT(a.avail() == 0);
87 UTEST_ASSERT(b.avail() == 0);
88 UTEST_ASSERT(a.position() == 0x100);
89 UTEST_ASSERT(b.position() == 0x100);
90 }
91
92 // Check buffers
93 UTEST_ASSERT(!src.corrupted());
94 UTEST_ASSERT(!dst1.corrupted());
95 UTEST_ASSERT(!dst2.corrupted());
96 UTEST_ASSERT(src.equals(dst1));
97 UTEST_ASSERT(src.equals(dst2));
98 }
99
100 UTEST_MAIN
101 {
102 test_simple_share();
103 }
104 UTEST_END
105
106
107
108
109
110
111
112