1 | /* $Id: tstRTCircBuf.cpp 33286 2010-10-21 09:48:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Lock free circular buffers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/circbuf.h>
|
---|
31 |
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/test.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Basic API checks.
|
---|
39 | */
|
---|
40 | static void tst1(void)
|
---|
41 | {
|
---|
42 | void *pvBuf = NULL;
|
---|
43 | size_t cbSize = 0;
|
---|
44 |
|
---|
45 | char pcTestPattern1[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
|
---|
46 | char pcTestPattern2[] = { 0x8, 0x9, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
|
---|
47 | char pcTestPattern3[] = { 0x5, 0x6, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
|
---|
48 |
|
---|
49 | /* Create */
|
---|
50 | RTTestISub("Creation");
|
---|
51 | PRTCIRCBUF pBuf;
|
---|
52 | RTTESTI_CHECK_RC(RTCircBufCreate(&pBuf, 10), VINF_SUCCESS);
|
---|
53 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 10);
|
---|
54 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 0);
|
---|
55 |
|
---|
56 | /* Full write */
|
---|
57 | RTTestISub("Full write");
|
---|
58 | RTCircBufAcquireWriteBlock(pBuf, 10, &pvBuf, &cbSize);
|
---|
59 | RTTESTI_CHECK(cbSize == 10);
|
---|
60 | memcpy(pvBuf, pcTestPattern1, 10);
|
---|
61 | RTCircBufReleaseWriteBlock(pBuf, 10);
|
---|
62 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 0);
|
---|
63 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 10);
|
---|
64 | RTTESTI_CHECK(memcmp(pBuf->pvBuf, pcTestPattern1, 10) == 0); /* Check the internal state */
|
---|
65 |
|
---|
66 | /* Half read */
|
---|
67 | RTTestISub("Half read");
|
---|
68 | RTCircBufAcquireReadBlock(pBuf, 5, &pvBuf, &cbSize);
|
---|
69 | RTTESTI_CHECK(cbSize == 5);
|
---|
70 | RTTESTI_CHECK(memcmp(pvBuf, pcTestPattern1, 5) == 0);
|
---|
71 | RTCircBufReleaseReadBlock(pBuf, 5);
|
---|
72 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 5);
|
---|
73 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 5);
|
---|
74 |
|
---|
75 | /* Sub write */
|
---|
76 | RTTestISub("Sub write");
|
---|
77 | RTCircBufAcquireWriteBlock(pBuf, 2, &pvBuf, &cbSize);
|
---|
78 | RTTESTI_CHECK(cbSize == 2);
|
---|
79 | memcpy(pvBuf, &pcTestPattern1[8], 2);
|
---|
80 | RTCircBufReleaseWriteBlock(pBuf, 2);
|
---|
81 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 3);
|
---|
82 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 7);
|
---|
83 | RTTESTI_CHECK(memcmp(pBuf->pvBuf, pcTestPattern2, 10) == 0); /* Check the internal state */
|
---|
84 |
|
---|
85 | /* Split tests */
|
---|
86 | /* Split read */
|
---|
87 | RTTestISub("Split read");
|
---|
88 | RTCircBufAcquireReadBlock(pBuf, 7, &pvBuf, &cbSize);
|
---|
89 | RTTESTI_CHECK(cbSize == 5);
|
---|
90 | RTTESTI_CHECK(memcmp(pvBuf, &pcTestPattern1[5], 5) == 0);
|
---|
91 | RTCircBufReleaseReadBlock(pBuf, 5);
|
---|
92 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 8);
|
---|
93 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 2);
|
---|
94 | RTCircBufAcquireReadBlock(pBuf, 2, &pvBuf, &cbSize);
|
---|
95 | RTTESTI_CHECK(cbSize == 2);
|
---|
96 | RTTESTI_CHECK(memcmp(pvBuf, &pcTestPattern1[8], 2) == 0);
|
---|
97 | RTCircBufReleaseReadBlock(pBuf, 2);
|
---|
98 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 10);
|
---|
99 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 0);
|
---|
100 |
|
---|
101 | /* Split write */
|
---|
102 | RTTestISub("Split write");
|
---|
103 | RTCircBufAcquireWriteBlock(pBuf, 10, &pvBuf, &cbSize);
|
---|
104 | RTTESTI_CHECK(cbSize == 8);
|
---|
105 | memcpy(pvBuf, pcTestPattern1, 8);
|
---|
106 | RTCircBufReleaseWriteBlock(pBuf, 8);
|
---|
107 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 2);
|
---|
108 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 8);
|
---|
109 | RTCircBufAcquireWriteBlock(pBuf, 2, &pvBuf, &cbSize);
|
---|
110 | RTTESTI_CHECK(cbSize == 2);
|
---|
111 | memcpy(pvBuf, &pcTestPattern1[5], 2);
|
---|
112 | RTCircBufReleaseWriteBlock(pBuf, 2);
|
---|
113 | RTTESTI_CHECK(RTCircBufFree(pBuf) == 0);
|
---|
114 | RTTESTI_CHECK(RTCircBufUsed(pBuf) == 10);
|
---|
115 | RTTESTI_CHECK(memcmp(pBuf->pvBuf, pcTestPattern3, 10) == 0); /* Check the internal state */
|
---|
116 |
|
---|
117 | /* Destroy */
|
---|
118 | RTCircBufDestroy(pBuf);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | int main()
|
---|
123 | {
|
---|
124 | RTTEST hTest;
|
---|
125 | int rc = RTTestInitAndCreate("tstRTCircBuf", &hTest);
|
---|
126 | if (rc)
|
---|
127 | return rc;
|
---|
128 | RTTestBanner(hTest);
|
---|
129 |
|
---|
130 | tst1();
|
---|
131 |
|
---|
132 | return RTTestSummaryAndDestroy(hTest);
|
---|
133 | }
|
---|
134 |
|
---|