TestClass.h

00001 /***********************************************************************************
00002         TestClass.h
00003         
00004  * Copyright (c) 1997-1998
00005  * Mark of the Unicorn, Inc.
00006  *
00007  * Permission to use, copy, modify, distribute and sell this software
00008  * and its documentation for any purpose is hereby granted without fee,
00009  * provided that the above copyright notice appear in all copies and
00010  * that both that copyright notice and this permission notice appear
00011  * in supporting documentation.  Mark of the Unicorn makes no
00012  * representations about the suitability of this software for any
00013  * purpose.  It is provided "as is" without express or implied warranty.
00014                 
00015                 SUMMARY: TestClass simulates a class that uses resources. It is designed to
00016                         cause exceptions when it is constructed or copied.
00017                 
00018 ***********************************************************************************/
00019 #ifndef INCLUDED_MOTU_TestClass
00020 #define INCLUDED_MOTU_TestClass 1
00021 
00022 # include "Prefix.h"
00023 
00024 # if defined (EH_NEW_HEADERS)
00025 #  include <functional>
00026 #  include <utility>
00027 #  include <climits>
00028 # else
00029 #  include <function.h>
00030 #  include <pair.h>
00031 #  include <limits.h>
00032 # endif
00033 
00034 
00035 #include <iosfwd>
00036 #include "random_number.h"
00037 #include "nc_alloc.h"
00038 
00039 class TestClass
00040 {
00041 public:
00042     inline TestClass();
00043     inline TestClass( int value );
00044     inline TestClass( const TestClass& rhs );
00045     inline ~TestClass();
00046     
00047     inline TestClass& operator=( const TestClass& rhs );
00048     inline int value() const;
00049         
00050     inline TestClass operator!() const;
00051 
00052     bool operator==( const TestClass& rhs ) const
00053     {
00054       return value() == rhs.value();
00055     }
00056 
00057     bool operator<( const TestClass& rhs ) const {
00058       return value() < rhs.value();
00059     }
00060 
00061 protected:
00062     static inline unsigned int get_random(unsigned range = UINT_MAX);
00063 private:
00064         inline void Init( int value );
00065 
00066 #if TESTCLASS_DEEP_DATA
00067     int *p;
00068 #else
00069         int v;
00070 #endif
00071 };
00072 
00073 #if defined( __MWERKS__ ) && __MWERKS__ <= 0x3000 && !__SGI_STL
00074 # if defined( __MSL__ ) && __MSL__ < 0x2406
00075 #  include <iterator.h>
00076 __MSL_FIX_ITERATORS__(TestClass);
00077 __MSL_FIX_ITERATORS__(const TestClass);
00078 typedef EH_STD::pair<const TestClass, TestClass> pair_testclass_testclass;
00079 __MSL_FIX_ITERATORS__( pair_testclass_testclass );
00080 __MSL_FIX_ITERATORS__( const pair_testclass_testclass );
00081 # endif
00082 #endif
00083 
00084 inline void TestClass::Init( int value )
00085 {
00086 #if TESTCLASS_DEEP_DATA
00087         p = new int( value );
00088 #else
00089         simulate_constructor();
00090         v = value;
00091 #endif
00092 }
00093 
00094 inline TestClass::TestClass()
00095 {
00096         Init( int(get_random()) );
00097 }
00098 
00099 inline TestClass::TestClass( int value )
00100 {
00101         Init( value );
00102 }
00103 
00104 inline TestClass::TestClass( const TestClass& rhs )
00105 {
00106         Init( rhs.value() );
00107 }
00108 
00109 inline TestClass::~TestClass()
00110 {
00111 #if TESTCLASS_DEEP_DATA
00112         delete p;
00113 #else
00114         simulate_destructor();
00115 #endif
00116 }
00117 
00118 inline TestClass& TestClass::operator=( const TestClass& rhs )
00119 {
00120 #if TESTCLASS_DEEP_DATA
00121         int *newP = new int( rhs.value() );
00122         delete p;
00123         p = newP;
00124 #else
00125         simulate_possible_failure();
00126     v = rhs.value();
00127 #endif
00128     return *this;
00129 }
00130 
00131 inline int TestClass::value() const
00132 {
00133 #if TESTCLASS_DEEP_DATA
00134         return *p;
00135 #else
00136         return v;
00137 #endif
00138 }
00139 
00140 inline TestClass TestClass::operator!() const
00141 {
00142     return TestClass( value()+1 );
00143 }
00144 
00145 inline bool operator>( const TestClass& lhs, const TestClass& rhs ) {
00146     return rhs < lhs;
00147 }
00148 
00149 inline bool operator>=( const TestClass& lhs, const TestClass& rhs ) {
00150     return !(lhs < rhs);
00151 }
00152 
00153 inline bool operator<=( const TestClass& lhs, const TestClass& rhs ) {
00154     return !(rhs < lhs);
00155 }
00156 
00157 inline bool operator != ( const TestClass& lhs, const TestClass& rhs ) {
00158     return lhs.value() != rhs.value();
00159 }
00160 
00161 inline unsigned int TestClass::get_random( unsigned range )
00162 {
00163     return random_number( range );
00164 }
00165 
00166 # ifdef EH_NEW_IOSTREAMS
00167 extern EH_STD::ostream& operator << ( EH_STD::ostream& s, const TestClass&);
00168 # else
00169 extern ostream& operator << ( ostream& s, const TestClass&);
00170 # endif
00171 
00172 #endif // INCLUDED_MOTU_TestClass

Generated on Mon Jun 5 10:20:48 2006 for Intelligence.kdevelop by  doxygen 1.4.6