VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrCache.cpp@ 46208

Last change on this file since 46208 was 46208, checked in by vboxsync, 12 years ago

tstRTStrCache: check that a real strcache impl doesn't return duplicates.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/* $Id: tstRTStrCache.cpp 46208 2013-05-22 09:26:27Z vboxsync $ */
2/** @file
3 * IPRT Testcase - StrCache.
4 */
5
6/*
7 * Copyright (C) 2009-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/strcache.h>
31
32#include <iprt/asm.h>
33#include <iprt/err.h>
34#include <iprt/initterm.h>
35#include <iprt/string.h>
36#include <iprt/test.h>
37#include <iprt/thread.h>
38#include <iprt/rand.h>
39
40
41/**
42 * Basic API checks.
43 * We'll return if any of these fails.
44 */
45static void tst1(RTSTRCACHE hStrCache)
46{
47 const char *psz;
48
49 /* Simple string entering and length. */
50 RTTESTI_CHECK_RETV(psz = RTStrCacheEnter(hStrCache, "abcdefgh"));
51 RTTESTI_CHECK_RETV(strcmp(psz, "abcdefgh") == 0);
52 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("abcdefgh"));
53 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
54
55 RTTESTI_CHECK_RETV(psz = RTStrCacheEnter(hStrCache, "abcdefghijklmnopqrstuvwxyz"));
56 RTTESTI_CHECK_RETV(strcmp(psz, "abcdefghijklmnopqrstuvwxyz") == 0);
57 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("abcdefghijklmnopqrstuvwxyz"));
58 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
59
60 /* Unterminated strings. */
61 RTTESTI_CHECK_RETV(psz = RTStrCacheEnterN(hStrCache, "0123456789", 3));
62 RTTESTI_CHECK_RETV(strcmp(psz, "012") == 0);
63 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("012"));
64 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
65
66 RTTESTI_CHECK_RETV(psz = RTStrCacheEnterN(hStrCache, "0123456789abcdefghijklmnopqrstuvwxyz", 16));
67 RTTESTI_CHECK_RETV(strcmp(psz, "0123456789abcdef") == 0);
68 RTTESTI_CHECK_RETV(RTStrCacheLength(psz) == strlen("0123456789abcdef"));
69 RTTESTI_CHECK_RETV(RTStrCacheRelease(hStrCache, psz) == 0);
70
71 /* String referencing. */
72 char szTest[4096+16];
73 memset(szTest, 'a', sizeof(szTest));
74 char szTest2[4096+16];
75 memset(szTest2, 'f', sizeof(szTest));
76 for (int32_t i = 4096; i > 3; i /= 3)
77 {
78 void *pv2;
79 RTTESTI_CHECK_RETV(psz = RTStrCacheEnterN(hStrCache, szTest, i));
80 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
81 RTTESTI_CHECK(RTStrCacheRetain(psz) == 2);
82 RTTESTI_CHECK(RTStrCacheRetain(psz) == 3);
83 RTTESTI_CHECK(RTStrCacheRetain(psz) == 4);
84 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
85 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == 3);
86 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
87 RTTESTI_CHECK(RTStrCacheRetain(psz) == 4);
88 RTTESTI_CHECK(RTStrCacheRetain(psz) == 5);
89 RTTESTI_CHECK(RTStrCacheRetain(psz) == 6);
90 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == 5);
91 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == 4);
92 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz));
93
94 for (uint32_t cRefs = 3;; cRefs--)
95 {
96 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz) == cRefs);
97 if (cRefs == 0)
98 break;
99 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x cRefs=%d\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz, cRefs));
100 for (uint32_t j = 0; j < 42; j++)
101 {
102 const char *psz2;
103 RTTESTI_CHECK_RETV(psz2 = RTStrCacheEnterN(hStrCache, szTest2, i));
104 RTTESTI_CHECK_RETV(psz2 != psz);
105 RTTESTI_CHECK(RTStrCacheRelease(hStrCache, psz2) == 0);
106 RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(psz, i, 'a')) == NULL && !psz[i], ("i=%#x psz=%p off=%#x cRefs=%d\n", i, psz, (uintptr_t)pv2 - (uintptr_t)psz, cRefs));
107 }
108 }
109 }
110
111 /* Lots of allocations. */
112 memset(szTest, 'b', sizeof(szTest));
113 memset(szTest2, 'e', sizeof(szTest));
114 const char *pszTest1Rets[4096 + 16];
115 const char *pszTest2Rets[4096 + 16];
116 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
117 {
118 RTTESTI_CHECK(pszTest1Rets[i] = RTStrCacheEnterN(hStrCache, szTest, i));
119 RTTESTI_CHECK(strlen(pszTest1Rets[i]) == i);
120 RTTESTI_CHECK(pszTest2Rets[i] = RTStrCacheEnterN(hStrCache, szTest2, i));
121 RTTESTI_CHECK(strlen(pszTest2Rets[i]) == i);
122 }
123
124 if (RTStrCacheIsRealImpl())
125 {
126 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
127 {
128 uint32_t cRefs;
129 const char *psz1, *psz2;
130 RTTESTI_CHECK((psz1 = RTStrCacheEnterN(hStrCache, szTest, i)) == pszTest1Rets[i]);
131 RTTESTI_CHECK((psz2 = RTStrCacheEnterN(hStrCache, szTest2, i)) == pszTest2Rets[i]);
132 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, psz1)) == 1, ("cRefs=%#x i=%#x\n", cRefs, i));
133 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, psz2)) == 1, ("cRefs=%#x i=%#x\n", cRefs, i));
134 }
135 }
136
137 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
138 {
139 uint32_t cRefs;
140 RTTESTI_CHECK(strlen(pszTest1Rets[i]) == i);
141 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, pszTest1Rets[i])) == 0, ("cRefs=%#x i=%#x\n", cRefs, i));
142 RTTESTI_CHECK(strlen(pszTest2Rets[i]) == i);
143 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, pszTest2Rets[i])) == 0, ("cRefs=%#x i=%#x\n", cRefs, i));
144 }
145}
146
147
148int main()
149{
150 RTTEST hTest;
151 int rc = RTTestInitAndCreate("tstRTStrCache", &hTest);
152 if (rc)
153 return rc;
154 RTTestBanner(hTest);
155
156 /*
157 * Smoke tests using first the default and then a custom pool.
158 */
159 RTTestSub(hTest, "Smoke test on default cache");
160 tst1(RTSTRCACHE_DEFAULT);
161
162 RTTestSub(hTest, "Smoke test on custom cache");
163 RTSTRCACHE hStrCache;
164 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2a"), VINF_SUCCESS);
165 if (RT_SUCCESS(rc))
166 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
167 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(NIL_RTSTRCACHE), VINF_SUCCESS);
168 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
169 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
170
171 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2b"), VINF_SUCCESS);
172 if (RT_SUCCESS(rc))
173 {
174 tst1(hStrCache);
175 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
176 }
177
178 /*
179 * Summary.
180 */
181 return RTTestSummaryAndDestroy(hTest);
182}
183
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette