VirtualBox

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

Last change on this file since 39837 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: tstRTStrCache.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - StrCache.
4 */
5
6/*
7 * Copyright (C) 2009 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(NIL_RTSTRCACHE, psz) == 5);
91 RTTESTI_CHECK(RTStrCacheRelease(NIL_RTSTRCACHE, 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
112
113int main()
114{
115 RTTEST hTest;
116 int rc = RTTestInitAndCreate("tstRTStrCache", &hTest);
117 if (rc)
118 return rc;
119 RTTestBanner(hTest);
120
121 /*
122 * Smoke tests using first the default and then a custom pool.
123 */
124 RTTestSub(hTest, "Smoke test on default cache");
125 tst1(RTSTRCACHE_DEFAULT);
126
127 RTTestSub(hTest, "Smoke test on custom cache");
128 RTSTRCACHE hStrCache;
129 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2a"), VINF_SUCCESS);
130 if (RT_SUCCESS(rc))
131 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
132 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(NIL_RTSTRCACHE), VINF_SUCCESS);
133 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
134 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(RTSTRCACHE_DEFAULT), VINF_SUCCESS);
135
136 RTTESTI_CHECK_RC(rc = RTStrCacheCreate(&hStrCache, "test 2b"), VINF_SUCCESS);
137 if (RT_SUCCESS(rc))
138 {
139 tst1(hStrCache);
140 RTTESTI_CHECK_RC(rc = RTStrCacheDestroy(hStrCache), VINF_SUCCESS);
141 }
142
143 /*
144 * Summary.
145 */
146 return RTTestSummaryAndDestroy(hTest);
147}
148
Note: See TracBrowser for help on using the repository browser.

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