Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Designing Robust Objects with Boost

, 16 Aug 2004
Tutorial on designing classes using Boost libraries
//*******************************************************************
//
// $Workfile: AppVersionTests.cpp $
//
// $Modtime: 8/15/04 10:23a $
//
// Unit tests for the AppVersion class.
//

#include "stdafx.h"
#pragma warning(push, 2)
#include <sstream>
#include <boost/test/unit_test.hpp>
#pragma warning(pop)

#include "AppVersion.h"

#include "AppVersionTests.h"

using namespace std;
using namespace boost::unit_test_framework;

#if defined(_DEBUG)
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif // defined(_DEBUG)

namespace	// local functions
{

//*******************************************************************
//
void AppVersionConstructorTests(void)
{
	// check default constructor
	AppVersion a;
	BOOST_CHECK_EQUAL(a.GetMajor(), 0U);
	BOOST_CHECK_EQUAL(a.GetMinor(), 0U);
	BOOST_CHECK_EQUAL(a.GetBuild(), 0U);
	BOOST_CHECK_EQUAL(a.GetRevision(), 0U);

	// check constructor
	AppVersion b(1, 2, 3, 4);
	BOOST_CHECK_EQUAL(b.GetMajor(), 1U);
	BOOST_CHECK_EQUAL(b.GetMinor(), 2U);
	BOOST_CHECK_EQUAL(b.GetBuild(), 3U);
	BOOST_CHECK_EQUAL(b.GetRevision(), 4U);

	// check copy constructor
	AppVersion c(b);
	BOOST_CHECK_EQUAL(c.GetMajor(), 1U);
	BOOST_CHECK_EQUAL(c.GetMinor(), 2U);
	BOOST_CHECK_EQUAL(c.GetBuild(), 3U);
	BOOST_CHECK_EQUAL(c.GetRevision(), 4U);
}


//*******************************************************************
//
void AppVersionAssignmentTests(void)
{
	// check assignment
	AppVersion a(1, 2, 3, 4);
	AppVersion b;
	b = a;
	BOOST_CHECK_EQUAL(b.GetMajor(), 1U);
	BOOST_CHECK_EQUAL(b.GetMinor(), 2U);
	BOOST_CHECK_EQUAL(b.GetBuild(), 3U);
	BOOST_CHECK_EQUAL(b.GetRevision(), 4U);

	// check self-assignment
	b = b;
	BOOST_CHECK_EQUAL(b.GetMajor(), 1U);
	BOOST_CHECK_EQUAL(b.GetMinor(), 2U);
	BOOST_CHECK_EQUAL(b.GetBuild(), 3U);
	BOOST_CHECK_EQUAL(b.GetRevision(), 4U);
}


//*******************************************************************
//
void AppVersionTextTests(void)
{
	AppVersion a(1, 2, 3, 4);

	BOOST_CHECK_EQUAL(GetText(a), "1.2.3.4");

	ostringstream os;
	os << a;
	BOOST_CHECK_EQUAL(os.str(), "1.2.3.4");
}


//*******************************************************************
//
void AppVersionComparisonTests(void)
{
	// check operators - the same
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0), AppVersion(0, 0, 0, 0));
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 1), AppVersion(0, 0, 0, 1));
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 1, 0), AppVersion(0, 0, 1, 0));
	BOOST_CHECK_EQUAL(AppVersion(0, 1, 0, 0), AppVersion(0, 1, 0, 0));
	BOOST_CHECK_EQUAL(AppVersion(1, 0, 0, 0), AppVersion(1, 0, 0, 0));

	// check operators - the same
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0) <  AppVersion(0, 0, 0, 0), false);
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0) <= AppVersion(0, 0, 0, 0), true);
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0) == AppVersion(0, 0, 0, 0), true);
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0) != AppVersion(0, 0, 0, 0), false);
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0) >= AppVersion(0, 0, 0, 0), true);
	BOOST_CHECK_EQUAL(AppVersion(0, 0, 0, 0) >  AppVersion(0, 0, 0, 0), false);

	// check operators - the same
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <  AppVersion(1, 2, 3, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <= AppVersion(1, 2, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) == AppVersion(1, 2, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) != AppVersion(1, 2, 3, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >= AppVersion(1, 2, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >  AppVersion(1, 2, 3, 4), false);

	// check operators - different in revision
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <  AppVersion(1, 2, 3, 5), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <= AppVersion(1, 2, 3, 5), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) == AppVersion(1, 2, 3, 5), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) != AppVersion(1, 2, 3, 5), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >= AppVersion(1, 2, 3, 5), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >  AppVersion(1, 2, 3, 5), false);

	// check operators - different in build
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <  AppVersion(1, 2, 4, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <= AppVersion(1, 2, 4, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) == AppVersion(1, 2, 4, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) != AppVersion(1, 2, 4, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >= AppVersion(1, 2, 4, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >  AppVersion(1, 2, 4, 4), false);

	// check operators - different in minor
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <  AppVersion(1, 3, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <= AppVersion(1, 3, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) == AppVersion(1, 3, 3, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) != AppVersion(1, 3, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >= AppVersion(1, 3, 3, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >  AppVersion(1, 3, 3, 4), false);

	// check operators - different in major
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <  AppVersion(2, 2, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) <= AppVersion(2, 2, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) == AppVersion(2, 2, 3, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) != AppVersion(2, 2, 3, 4), true);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >= AppVersion(2, 2, 3, 4), false);
	BOOST_CHECK_EQUAL(AppVersion(1, 2, 3, 4) >  AppVersion(2, 2, 3, 4), false);
}


//*******************************************************************
//
void AppVersionSerializationTests(void)
{
	// serialization test
	TCHAR TempPath[_MAX_PATH];
	if (GetTempPath(sizeof(TempPath) / sizeof(TCHAR), TempPath) == 0)
		BOOST_ERROR("Could not obtain temporary directory.");

	TCHAR TempFilename[_MAX_PATH];
	if (GetTempFileName(TempPath, "Tst", 0, TempFilename) == 0)
		BOOST_ERROR("Could not create temporary filename.");

	CFile ArchiveFile;
	if (ArchiveFile.Open(TempFilename, CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive | CFile::typeBinary) == 0)
		BOOST_ERROR("Could not create temporary file.");
	CArchive StoreArchive(&ArchiveFile, CArchive::store);
	AppVersion SerOut(1, 2, 3, 4);
	SerOut.Serialize(StoreArchive);
	StoreArchive.Close();
	ArchiveFile.Close();

	AppVersion SerIn;
	if (ArchiveFile.Open(TempFilename, CFile::modeRead | CFile::shareExclusive | CFile::typeBinary) == 0)
		BOOST_ERROR("Could not open temporary file.");
	CArchive LoadArchive(&ArchiveFile, CArchive::load);
	SerIn.Serialize(LoadArchive);
	LoadArchive.Close();
	ArchiveFile.Close();

	BOOST_CHECK_EQUAL(SerOut, SerIn);

	CFile::Remove(TempFilename);
}


//*******************************************************************
//
void AppVersionDumpTests(void)
{
#if defined(_DEBUG)
	AppVersion a(1, 2, 3, 4);
	
	a.Dump(afxDump);
#endif
}

}	// unnamed namespace


//*******************************************************************
//
void AppVersionTests(test_suite* CoreTestSuite)
{
	CoreTestSuite->add(BOOST_TEST_CASE(&AppVersionConstructorTests));
	CoreTestSuite->add(BOOST_TEST_CASE(&AppVersionAssignmentTests));
	CoreTestSuite->add(BOOST_TEST_CASE(&AppVersionTextTests));
	CoreTestSuite->add(BOOST_TEST_CASE(&AppVersionComparisonTests));
	CoreTestSuite->add(BOOST_TEST_CASE(&AppVersionSerializationTests));
	CoreTestSuite->add(BOOST_TEST_CASE(&AppVersionDumpTests));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jim D'Agostino
Web Developer
United States United States
Jim has been developing software for over 25 years. He is a consultant specializing in writing software for commercial products. He has developed software for embedded systems, device drivers, and windows applications.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 17 Aug 2004
Article Copyright 2004 by Jim D'Agostino
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid