tstMemAutoPtr.cpp revision dced478b440a327fb550155c0f73c1ac968ad93b
/* $Id$ */
/** @file
* IPRT - Testcase the RTMemAutoPtr template.
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct TSTMEMAUTOPTRSTRUCT
{
uint32_t a;
uint32_t b;
uint32_t c;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifndef TST_MEM_AUTO_PTR_ONLY_DISAS
static unsigned g_cErrors = 0;
static unsigned g_cFrees;
#endif
/*
* Feel free to inspect with gdb / objdump / whatever / g++ -fverbose-asm in
* a release build and compare with tstMemAutoPtrDisas1PureC.
*/
{
if (!Handle)
{
{
return VINF_SUCCESS;
}
}
return VERR_TRY_AGAIN;
}
/*
* For comparing to tstMemAutoPtrDisas1.
*/
{
if (pHandle)
{
{
return VINF_SUCCESS;
}
}
return VERR_TRY_AGAIN;
}
#ifndef TST_MEM_AUTO_PTR_ONLY_DISAS
template <class T>
void tstMemAutoPtrDestructorCounter(T *aMem)
{
{
RTPrintf("tstMemAutoPtr(%d): Destructor called with NILL handle!\n");
g_cErrors++;
}
{
g_cErrors++;
}
g_cFrees++;
}
{
if (pvNew)
return pvNew;
}
int main()
{
RTR3Init();
RTPrintf("tstMemAutoPtr: TESTING...\n");
#define CHECK_EXPR(expr) \
do { bool const f = !!(expr); if (!f) { RTPrintf("tstMemAutoPtr(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)
/*
* Some simple stuff.
*/
{
CHECK_EXPR(!NilObj);
}
{
}
/*
* Use the electric fence memory API to check alternative template
* arguments and also check some subscript / reference limit thing.
*/
{
Electric[0] = '0';
//CHECK_EXPR(Electric == '0');
/* Electric[10] = '2'; - this will crash (of course) */
}
/*
* Check that memory is actually free when it should be and isn't when it shouldn't.
* Use the electric heap to get some extra checks.
*/
g_cFrees = 0;
{
}
g_cFrees = 0;
{
}
g_cFrees = 0;
{
}
CHECK_EXPR(g_cFrees == 0);
g_cFrees = 0;
{
}
g_cFrees = 0;
{
}
/*
* Check the ->, [] and * (unary) operators with some useful struct.
*/
{
Struct1->a = 0x11223344;
Struct1->b = 0x55667788;
Struct1->c = 0x99aabbcc;
Struct1[0].a = 0x11223344;
Struct1[0].b = 0x55667788;
Struct1[0].c = 0x99aabbcc;
(*Struct1).a = 0x11223344;
(*Struct1).b = 0x55667788;
(*Struct1).c = 0x99aabbcc;
/* since at it... */
}
/*
* Check the zeroing of memory.
*/
{
RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed1(1, true);
CHECK_EXPR(*Zeroed1 == 0);
}
{
CHECK_EXPR(Zeroed2[0] == 0);
}
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstMemAutoPtr: SUCCESS\n");
else
return !!g_cErrors;
}
#endif /* TST_MEM_AUTO_PTR_ONLY_DISAS */