VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstMemAutoPtr.cpp@ 11016

Last change on this file since 11016 was 11016, checked in by vboxsync, 16 years ago

IPRT: Added tstMemAutoPtr - a testcase for RTMemAutoPtr.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: tstMemAutoPtr.cpp 11016 2008-07-30 21:55:23Z vboxsync $ */
2/** @file
3 * IPRT - Testcase the RTMemAutoPtr template.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/mem.h>
35#include <iprt/stream.h>
36#include <iprt/initterm.h>
37#include <iprt/string.h>
38
39
40/*******************************************************************************
41* Structures and Typedefs *
42*******************************************************************************/
43typedef struct TSTMEMAUTOPTRSTRUCT
44{
45 uint32_t a;
46 uint32_t b;
47 uint32_t c;
48} TSTMEMAUTOPTRSTRUCT;
49
50
51/*******************************************************************************
52* Global Variables *
53*******************************************************************************/
54static unsigned g_cErrors = 0;
55static unsigned g_cFrees;
56
57
58
59template <class T>
60void tstMemAutoPtrDestructorCounter(T *aMem)
61{
62 if (aMem == NULL)
63 {
64 RTPrintf("tstMemAutoPtr(%d): Destructor called with NILL handle!\n");
65 g_cErrors++;
66 }
67 else if (!VALID_PTR(aMem))
68 {
69 RTPrintf("tstMemAutoPtr(%d): Destructor called with a bad handle %p\n", aMem);
70 g_cErrors++;
71 }
72 RTMemEfFree(aMem);
73 g_cFrees++;
74}
75
76
77void *tstMemAutoPtrAllocatorNoZero(void *pvOld, size_t cbNew)
78{
79 void *pvNew = RTMemRealloc(pvOld, cbNew);
80 if (pvNew)
81 memset(pvNew, 0xfe, cbNew);
82 return pvNew;
83}
84
85
86int main()
87{
88 RTR3Init(false);
89 RTPrintf("tstMemAutoPtr: TESTING...\n");
90
91#define CHECK_EXPR(expr) \
92 do { bool const f = !!(expr); if (!f) { RTPrintf("tstMemAutoPtr(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)
93
94 /*
95 * Some simple stuff.
96 */
97 {
98 RTMemAutoPtr<char> NilObj;
99 CHECK_EXPR(!NilObj);
100 CHECK_EXPR(NilObj.get() == NULL);
101 CHECK_EXPR(NilObj.release() == NULL);
102 NilObj.reset();
103 }
104
105 {
106 RTMemAutoPtr<char> Alloc(10);
107 CHECK_EXPR(Alloc.get() != NULL);
108 char *pch = Alloc.release();
109 CHECK_EXPR(pch != NULL);
110 CHECK_EXPR(Alloc.get() == NULL);
111
112 RTMemAutoPtr<char> Manage(pch);
113 CHECK_EXPR(Manage.get() == pch);
114 CHECK_EXPR(&Manage[0] == pch);
115 CHECK_EXPR(&Manage[1] == &pch[1]);
116 CHECK_EXPR(&Manage[9] == &pch[9]);
117 }
118
119 /*
120 * Use the electric fence memory API to check alternative template
121 * arguments and also check some subscript / reference limit thing.
122 */
123 {
124 RTMemAutoPtr<char, RTMemEfAutoFree<char>, RTMemEfRealloc> Electric(10);
125 CHECK_EXPR(Electric.get() != NULL);
126 Electric[0] = '0';
127 CHECK_EXPR(Electric[0] == '0');
128 CHECK_EXPR(*Electric == '0');
129 //CHECK_EXPR(Electric == '0');
130 Electric[9] = '1';
131 CHECK_EXPR(Electric[9] == '1');
132 /* Electric[10] = '2'; - this will crash (of course) */
133 }
134
135 /*
136 * Check that memory is actually free when it should be and isn't when it shouldn't.
137 * Use the electric heap to get some extra checks.
138 */
139 g_cFrees = 0;
140 {
141 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt(128);
142 FreeIt[127] = '0';
143 }
144 CHECK_EXPR(g_cFrees == 1);
145
146 g_cFrees = 0;
147 {
148 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt2(128);
149 FreeIt2[127] = '1';
150 FreeIt2.reset();
151 FreeIt2.alloc(128);
152 FreeIt2[127] = '2';
153 FreeIt2.reset(FreeIt2.get()); /* this one is weird, but it's how things works... */
154 }
155 CHECK_EXPR(g_cFrees == 2);
156
157 g_cFrees = 0;
158 {
159 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> DontFreeIt(256);
160 DontFreeIt[255] = '0';
161 RTMemEfFree(DontFreeIt.release());
162 }
163 CHECK_EXPR(g_cFrees == 0);
164
165 g_cFrees = 0;
166 {
167 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt3(128);
168 FreeIt3[127] = '0';
169 CHECK_EXPR(FreeIt3.realloc(128));
170 FreeIt3[127] = '0';
171 CHECK_EXPR(FreeIt3.realloc(256));
172 FreeIt3[255] = '0';
173 CHECK_EXPR(FreeIt3.realloc(64));
174 FreeIt3[63] = '0';
175 CHECK_EXPR(FreeIt3.realloc(32));
176 FreeIt3[31] = '0';
177 }
178 CHECK_EXPR(g_cFrees == 1);
179
180 g_cFrees = 0;
181 {
182 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt4;
183 CHECK_EXPR(FreeIt4.alloc(123));
184 CHECK_EXPR(FreeIt4.realloc(543));
185 FreeIt4 = (char *)NULL;
186 CHECK_EXPR(FreeIt4.get() == NULL);
187 }
188 CHECK_EXPR(g_cFrees == 1);
189
190 /*
191 * Check the ->, [] and * (unary) operators with some useful struct.
192 */
193 {
194 RTMemAutoPtr<TSTMEMAUTOPTRSTRUCT> Struct1(1);
195 Struct1->a = 0x11223344;
196 Struct1->b = 0x55667788;
197 Struct1->c = 0x99aabbcc;
198 CHECK_EXPR(Struct1->a == 0x11223344);
199 CHECK_EXPR(Struct1->b == 0x55667788);
200 CHECK_EXPR(Struct1->c == 0x99aabbcc);
201
202 Struct1[0].a = 0x11223344;
203 Struct1[0].b = 0x55667788;
204 Struct1[0].c = 0x99aabbcc;
205 CHECK_EXPR(Struct1[0].a == 0x11223344);
206 CHECK_EXPR(Struct1[0].b == 0x55667788);
207 CHECK_EXPR(Struct1[0].c == 0x99aabbcc);
208
209 (*Struct1).a = 0x11223344;
210 (*Struct1).b = 0x55667788;
211 (*Struct1).c = 0x99aabbcc;
212 CHECK_EXPR((*Struct1).a == 0x11223344);
213 CHECK_EXPR((*Struct1).b == 0x55667788);
214 CHECK_EXPR((*Struct1).c == 0x99aabbcc);
215
216 /* since at it... */
217 Struct1.get()->a = 0x11223344;
218 Struct1.get()->b = 0x55667788;
219 Struct1.get()->c = 0x99aabbcc;
220 CHECK_EXPR(Struct1.get()->a == 0x11223344);
221 CHECK_EXPR(Struct1.get()->b == 0x55667788);
222 CHECK_EXPR(Struct1.get()->c == 0x99aabbcc);
223 }
224
225 /*
226 * Check the zeroing of memory.
227 */
228 {
229 RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed1(1, true);
230 CHECK_EXPR(*Zeroed1 == 0);
231 }
232
233 {
234 RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed2;
235 Zeroed2.alloc(5, true);
236 CHECK_EXPR(Zeroed2[0] == 0);
237 CHECK_EXPR(Zeroed2[1] == 0);
238 CHECK_EXPR(Zeroed2[2] == 0);
239 CHECK_EXPR(Zeroed2[3] == 0);
240 CHECK_EXPR(Zeroed2[4] == 0);
241 }
242
243
244 /*
245 * Summary.
246 */
247 if (!g_cErrors)
248 RTPrintf("tstMemAutoPtr: SUCCESS\n");
249 else
250 RTPrintf("tstMemAutoPtr: FAILED - %d errors\n", g_cErrors);
251 return !!g_cErrors;
252}
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