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