sl@0: /*********************************************************************************** sl@0: TestClass.h sl@0: sl@0: * Copyright (c) 1997-1998 sl@0: * Mark of the Unicorn, Inc. sl@0: * sl@0: * Permission to use, copy, modify, distribute and sell this software sl@0: * and its documentation for any purpose is hereby granted without fee, sl@0: * provided that the above copyright notice appear in all copies and sl@0: * that both that copyright notice and this permission notice appear sl@0: * in supporting documentation. Mark of the Unicorn makes no sl@0: * representations about the suitability of this software for any sl@0: * purpose. It is provided "as is" without express or implied warranty. sl@0: sl@0: SUMMARY: TestClass simulates a class that uses resources. It is designed to sl@0: cause exceptions when it is constructed or copied. sl@0: sl@0: ***********************************************************************************/ sl@0: #ifndef INCLUDED_MOTU_TestClass sl@0: #define INCLUDED_MOTU_TestClass 1 sl@0: sl@0: # include "Prefix.h" sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: #include sl@0: #include "random_number.h" sl@0: #include "nc_alloc.h" sl@0: sl@0: class TestClass sl@0: { sl@0: public: sl@0: inline TestClass(); sl@0: inline TestClass( int value ); sl@0: inline TestClass( const TestClass& rhs ); sl@0: inline ~TestClass(); sl@0: sl@0: inline TestClass& operator=( const TestClass& rhs ); sl@0: inline int value() const; sl@0: sl@0: inline TestClass operator!() const; sl@0: sl@0: bool operator==( const TestClass& rhs ) const sl@0: { sl@0: return value() == rhs.value(); sl@0: } sl@0: sl@0: bool operator<( const TestClass& rhs ) const { sl@0: return value() < rhs.value(); sl@0: } sl@0: sl@0: protected: sl@0: static inline unsigned int get_random(unsigned range = UINT_MAX); sl@0: private: sl@0: inline void Init( int value ); sl@0: sl@0: #if TESTCLASS_DEEP_DATA sl@0: int *p; sl@0: #else sl@0: int v; sl@0: #endif sl@0: }; sl@0: sl@0: #if defined( __MWERKS__ ) && __MWERKS__ <= 0x3000 && !__SGI_STL sl@0: # if defined( __MSL__ ) && __MSL__ < 0x2406 sl@0: # include sl@0: __MSL_FIX_ITERATORS__(TestClass); sl@0: __MSL_FIX_ITERATORS__(const TestClass); sl@0: typedef EH_STD::pair pair_testclass_testclass; sl@0: __MSL_FIX_ITERATORS__( pair_testclass_testclass ); sl@0: __MSL_FIX_ITERATORS__( const pair_testclass_testclass ); sl@0: # endif sl@0: #endif sl@0: sl@0: inline void TestClass::Init( int value ) sl@0: { sl@0: #if TESTCLASS_DEEP_DATA sl@0: p = new int( value ); sl@0: #else sl@0: simulate_constructor(); sl@0: v = value; sl@0: #endif sl@0: } sl@0: sl@0: inline TestClass::TestClass() sl@0: { sl@0: Init( int(get_random()) ); sl@0: } sl@0: sl@0: inline TestClass::TestClass( int value ) sl@0: { sl@0: Init( value ); sl@0: } sl@0: sl@0: inline TestClass::TestClass( const TestClass& rhs ) sl@0: { sl@0: Init( rhs.value() ); sl@0: } sl@0: sl@0: inline TestClass::~TestClass() sl@0: { sl@0: #if TESTCLASS_DEEP_DATA sl@0: delete p; sl@0: #else sl@0: simulate_destructor(); sl@0: #endif sl@0: } sl@0: sl@0: inline TestClass& TestClass::operator=( const TestClass& rhs ) sl@0: { sl@0: #if TESTCLASS_DEEP_DATA sl@0: int *newP = new int( rhs.value() ); sl@0: delete p; sl@0: p = newP; sl@0: #else sl@0: simulate_possible_failure(); sl@0: v = rhs.value(); sl@0: #endif sl@0: return *this; sl@0: } sl@0: sl@0: inline int TestClass::value() const sl@0: { sl@0: #if TESTCLASS_DEEP_DATA sl@0: return *p; sl@0: #else sl@0: return v; sl@0: #endif sl@0: } sl@0: sl@0: inline TestClass TestClass::operator!() const sl@0: { sl@0: return TestClass( value()+1 ); sl@0: } sl@0: sl@0: inline bool operator>( const TestClass& lhs, const TestClass& rhs ) { sl@0: return rhs < lhs; sl@0: } sl@0: sl@0: inline bool operator>=( const TestClass& lhs, const TestClass& rhs ) { sl@0: return !(lhs < rhs); sl@0: } sl@0: sl@0: inline bool operator<=( const TestClass& lhs, const TestClass& rhs ) { sl@0: return !(rhs < lhs); sl@0: } sl@0: sl@0: inline bool operator != ( const TestClass& lhs, const TestClass& rhs ) { sl@0: return lhs.value() != rhs.value(); sl@0: } sl@0: sl@0: inline unsigned int TestClass::get_random( unsigned range ) sl@0: { sl@0: return random_number( range ); sl@0: } sl@0: sl@0: extern std::ostream& operator << ( std::ostream& s, const TestClass&); sl@0: sl@0: #endif // INCLUDED_MOTU_TestClass