Files
libsst/ZTestSuite/Test-ZAlloc.cpp
2026-04-03 00:22:39 -05:00

101 lines
2.2 KiB
C++

/*
Test-ZAlloc.cpp
Author: James Russell <jcrussell@762studios.com>
Purpose: Unit Test the TestZAlloc class.
Changelog:
12/18/2011 - Removed dependency on ZString (crertel)
2011/09/18 - creation (jcrussell)
*/
#include "ZUnitTest.hpp"
#include <ZUtil/ZAlloc.hpp>
static const char* testNew();
static const char* testAlloc();
//List of unit tests
ZUnitTest ZAllocUnitTests[] =
{
{ "Test New", testNew },
{ "Test Alloc", testAlloc }
};
//Now declare the ZUnitTestBlock associated with this.
DECLARE_ZTESTBLOCK(ZAlloc);
/*************************************************************************/
struct AllocTestObject
{
//Constructor
AllocTestObject() { AllocTestObject::Instances++; }
//Parameterized constructor
AllocTestObject(int _val) { AllocTestObject::Instances++; Value = _val; }
//Destructor
~AllocTestObject() { AllocTestObject::Instances--; }
//Value field
int Value;
//Static Instance Count
static int Instances;
//Virtual Function
virtual bool Foo() const { return true; }
};
struct AllocTestObjectSubclass : public AllocTestObject
{
//Constructor
AllocTestObjectSubclass() : AllocTestObject() {}
//Subclass Override
bool Foo() const { return false; }
//Subclass Method
bool Bar() const { return true; }
};
//Defaults to zero
int AllocTestObject::Instances = 0;
static const char* testNew()
{
AllocTestObject *testObj1 = znew AllocTestObject();
TASSERT(AllocTestObject::Instances == 1, "Failed to call constructor with ZNew!");
AllocTestObject *testObj2 = znew AllocTestObject(5);
TASSERT(AllocTestObject::Instances == 2, "Failed to call constructor with ZNew!");
TASSERT(testObj2->Value == 5, "Failed to call correct constructor with ZNew!");
zdelete testObj1;
TASSERT(AllocTestObject::Instances == 1, "Failed to deallocate with ZDel!");
zdelete testObj2;
TASSERT(AllocTestObject::Instances == 0, "Failed to deallocate with ZDel!");
return ZTEST_SUCCESS;
}
static const char* testAlloc()
{
AllocTestObject *testArray = zalloc(AllocTestObject, 12);
TASSERT(AllocTestObject::Instances == 12, "Failed to call all constructors with ZAlloc!");
zfree(testArray);
TASSERT(AllocTestObject::Instances == 0, "Failed to call all destructors with ZFree!");
return ZTEST_SUCCESS;
}