VirtualBox

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

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

strcache.cpp: Enabled it. Some tuning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: tstRTStrCache.cpp 46199 2013-05-21 19:49:19Z 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.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/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 for (uint32_t i = 1; i < RT_ELEMENTS(pszTest1Rets); i++)
125 {
126 uint32_t cRefs;
127 RTTESTI_CHECK(strlen(pszTest1Rets[i]) == i);
128 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, pszTest1Rets[i])) == 0, ("cRefs=%#x i=%#x\n", cRefs, i));
129 RTTESTI_CHECK(strlen(pszTest2Rets[i]) == i);
130 RTTESTI_CHECK_MSG((cRefs = RTStrCacheRelease(hStrCache, pszTest2Rets[i])) == 0, ("cRefs=%#x i=%#x\n", cRefs, i));
131 }
132}
133
134
135int main()
136{
137 RTTEST hTest;
138 int rc = RTTestInitAndCreate("tstRTStrCache", &hTest);
139 if (rc)
140 return rc;
141 RTTestBanner(hTest);
142
143 /*
144 * Smoke tests using first the default and then a custom pool.
145 */
146 RTTestSub(hTest, "Smoke test on default cache");
147 tst1(RTSTRCACHE_DEFAULT);
148
149 RTTestSub(hTest, "Smoke test on custom cache");
150 RTSTRCACHE hStrCache;
151 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2a"), VINF_SUCCESS);
152 if (RT_SUCCESS(rc))
153 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
154 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(NIL_RTSTRCACHE), VINF_SUCCESS);
155 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
156 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
157
158 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2b"), VINF_SUCCESS);
159 if (RT_SUCCESS(rc))
160 {
161 tst1(hStrCache);
162 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
163 }
164
165 /*
166 * Summary.
167 */
168 return RTTestSummaryAndDestroy(hTest);
169}
170
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