1 | /* $Id: tstRTCircBuf.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Lock free circular buffers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/circbuf.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Basic API checks.
|
---|
50 | */
|
---|
51 | static void tst1(void)
|
---|
52 | {
|
---|
53 | void *pvBuf;
|
---|
54 | size_t cbSize;
|
---|
55 |
|
---|
56 | char achTestPattern1[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
|
---|
57 | // char achTestPattern2[] = { 0x8, 0x9, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
|
---|
58 | // char achTestPattern3[] = { 0x5, 0x6, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
|
---|
59 |
|
---|
60 | /* Create */
|
---|
61 | RTTestISub("Creation");
|
---|
62 | PRTCIRCBUF pBuf;
|
---|
63 | RTTESTI_CHECK_RC(RTCircBufCreate(&pBuf, 10), VINF_SUCCESS);
|
---|
64 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 10);
|
---|
65 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 0);
|
---|
66 |
|
---|
67 | /* Full write */
|
---|
68 | RTTestISub("Full write");
|
---|
69 | RTCircBufAcquireWriteBlock(pBuf, 10, &pvBuf, &cbSize);
|
---|
70 | RTTESTI_CHECK(cbSize == 10);
|
---|
71 | memcpy(pvBuf, achTestPattern1, 10);
|
---|
72 | RTCircBufReleaseWriteBlock(pBuf, 10);
|
---|
73 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 0);
|
---|
74 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 10);
|
---|
75 | // RTTESTI_CHECK(memcmp(pBuf->pvBuf, achTestPattern1, 10) == 0); /* Check the internal state */
|
---|
76 |
|
---|
77 | /* Half read */
|
---|
78 | RTTestISub("Half read");
|
---|
79 | RTCircBufAcquireReadBlock(pBuf, 5, &pvBuf, &cbSize);
|
---|
80 | RTTESTI_CHECK(cbSize == 5);
|
---|
81 | RTTESTI_CHECK(memcmp(pvBuf, achTestPattern1, 5) == 0);
|
---|
82 | RTCircBufReleaseReadBlock(pBuf, 5);
|
---|
83 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 5);
|
---|
84 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 5);
|
---|
85 |
|
---|
86 | /* Sub write */
|
---|
87 | RTTestISub("Sub write");
|
---|
88 | RTCircBufAcquireWriteBlock(pBuf, 2, &pvBuf, &cbSize);
|
---|
89 | RTTESTI_CHECK(cbSize == 2);
|
---|
90 | memcpy(pvBuf, &achTestPattern1[8], 2);
|
---|
91 | RTCircBufReleaseWriteBlock(pBuf, 2);
|
---|
92 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 3);
|
---|
93 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 7);
|
---|
94 | // RTTESTI_CHECK(memcmp(pBuf->pvBuf, achTestPattern2, 10) == 0); /* Check the internal state */
|
---|
95 |
|
---|
96 | /* Split tests */
|
---|
97 | /* Split read */
|
---|
98 | RTTestISub("Split read");
|
---|
99 | RTCircBufAcquireReadBlock(pBuf, 7, &pvBuf, &cbSize);
|
---|
100 | RTTESTI_CHECK(cbSize == 5);
|
---|
101 | RTTESTI_CHECK(memcmp(pvBuf, &achTestPattern1[5], 5) == 0);
|
---|
102 | RTCircBufReleaseReadBlock(pBuf, 5);
|
---|
103 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 8);
|
---|
104 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 2);
|
---|
105 | RTCircBufAcquireReadBlock(pBuf, 2, &pvBuf, &cbSize);
|
---|
106 | RTTESTI_CHECK(cbSize == 2);
|
---|
107 | RTTESTI_CHECK(memcmp(pvBuf, &achTestPattern1[8], 2) == 0);
|
---|
108 | RTCircBufReleaseReadBlock(pBuf, 2);
|
---|
109 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 10);
|
---|
110 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 0);
|
---|
111 |
|
---|
112 | /* Split write */
|
---|
113 | RTTestISub("Split write");
|
---|
114 | RTCircBufAcquireWriteBlock(pBuf, 10, &pvBuf, &cbSize);
|
---|
115 | RTTESTI_CHECK(cbSize == 8);
|
---|
116 | memcpy(pvBuf, achTestPattern1, 8);
|
---|
117 | RTCircBufReleaseWriteBlock(pBuf, 8);
|
---|
118 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 2);
|
---|
119 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 8);
|
---|
120 | RTCircBufAcquireWriteBlock(pBuf, 2, &pvBuf, &cbSize);
|
---|
121 | RTTESTI_CHECK(cbSize == 2);
|
---|
122 | memcpy(pvBuf, &achTestPattern1[5], 2);
|
---|
123 | RTCircBufReleaseWriteBlock(pBuf, 2);
|
---|
124 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 0);
|
---|
125 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 10);
|
---|
126 | // RTTESTI_CHECK(memcmp(pBuf->pvBuf, achTestPattern3, 10) == 0); /* Check the internal state */
|
---|
127 |
|
---|
128 | /* Destroy */
|
---|
129 | RTCircBufDestroy(pBuf);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | int main()
|
---|
134 | {
|
---|
135 | RTTEST hTest;
|
---|
136 | int rc = RTTestInitAndCreate("tstRTCircBuf", &hTest);
|
---|
137 | if (rc)
|
---|
138 | return rc;
|
---|
139 | RTTestBanner(hTest);
|
---|
140 |
|
---|
141 | tst1();
|
---|
142 |
|
---|
143 | return RTTestSummaryAndDestroy(hTest);
|
---|
144 | }
|
---|
145 |
|
---|