home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stl453up.zip / stl453fx / test / eh / TestClass.h < prev    next >
C/C++ Source or Header  |  2002-04-29  |  4KB  |  173 lines

  1. /***********************************************************************************
  2.     TestClass.h
  3.     
  4.  * Copyright (c) 1997-1998
  5.  * Mark of the Unicorn, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute and sell this software
  8.  * and its documentation for any purpose is hereby granted without fee,
  9.  * provided that the above copyright notice appear in all copies and
  10.  * that both that copyright notice and this permission notice appear
  11.  * in supporting documentation.  Mark of the Unicorn makes no
  12.  * representations about the suitability of this software for any
  13.  * purpose.  It is provided "as is" without express or implied warranty.
  14.         
  15.         SUMMARY: TestClass simulates a class that uses resources. It is designed to
  16.             cause exceptions when it is constructed or copied.
  17.         
  18. ***********************************************************************************/
  19. #ifndef INCLUDED_MOTU_TestClass
  20. #define INCLUDED_MOTU_TestClass 1
  21.  
  22. # include "Prefix.h"
  23.  
  24. # if defined (EH_NEW_HEADERS)
  25. #  include <functional>
  26. #  include <utility>
  27. #  include <climits>
  28. # else
  29. #  include <function.h>
  30. #  include <pair.h>
  31. #  include <limits.h>
  32. # endif
  33.  
  34.  
  35. #include <iosfwd>
  36. #include "random_number.h"
  37. #include "nc_alloc.h"
  38.  
  39. class TestClass
  40. {
  41. public:
  42.     inline TestClass();
  43.     inline TestClass( int value );
  44.     inline TestClass( const TestClass& rhs );
  45.     inline ~TestClass();
  46.     
  47.     inline TestClass& operator=( const TestClass& rhs );
  48.     inline int value() const;
  49.     
  50.     inline TestClass operator!() const;
  51.  
  52.     bool operator==( const TestClass& rhs ) const
  53.     {
  54.       return value() == rhs.value();
  55.     }
  56.  
  57.     bool operator<( const TestClass& rhs ) const {
  58.       return value() < rhs.value();
  59.     }
  60.  
  61. protected:
  62.     static inline unsigned int get_random(unsigned range = UINT_MAX);
  63. private:
  64.     inline void Init( int value );
  65.  
  66. #if TESTCLASS_DEEP_DATA
  67.     int *p;
  68. #else
  69.     int v;
  70. #endif
  71. };
  72.  
  73. #if defined( __MWERKS__ ) && __MWERKS__ <= 0x3000 && !__SGI_STL
  74. # if defined( __MSL__ ) && __MSL__ < 0x2406
  75. #  include <iterator.h>
  76. __MSL_FIX_ITERATORS__(TestClass);
  77. __MSL_FIX_ITERATORS__(const TestClass);
  78. typedef EH_STD::pair<const TestClass, TestClass> pair_testclass_testclass;
  79. __MSL_FIX_ITERATORS__( pair_testclass_testclass );
  80. __MSL_FIX_ITERATORS__( const pair_testclass_testclass );
  81. # endif
  82. #endif
  83.  
  84. inline void TestClass::Init( int value )
  85. {
  86. #if TESTCLASS_DEEP_DATA
  87.     p = new int( value );
  88. #else
  89.     simulate_constructor();
  90.     v = value;
  91. #endif
  92. }
  93.  
  94. inline TestClass::TestClass()
  95. {
  96.     Init( int(get_random()) );
  97. }
  98.  
  99. inline TestClass::TestClass( int value )
  100. {
  101.     Init( value );
  102. }
  103.  
  104. inline TestClass::TestClass( const TestClass& rhs )
  105. {
  106.     Init( rhs.value() );
  107. }
  108.  
  109. inline TestClass::~TestClass()
  110. {
  111. #if TESTCLASS_DEEP_DATA
  112.     delete p;
  113. #else
  114.     simulate_destructor();
  115. #endif
  116. }
  117.  
  118. inline TestClass& TestClass::operator=( const TestClass& rhs )
  119. {
  120. #if TESTCLASS_DEEP_DATA
  121.     int *newP = new int( rhs.value() );
  122.     delete p;
  123.     p = newP;
  124. #else
  125.     simulate_possible_failure();
  126.     v = rhs.value();
  127. #endif
  128.     return *this;
  129. }
  130.  
  131. inline int TestClass::value() const
  132. {
  133. #if TESTCLASS_DEEP_DATA
  134.     return *p;
  135. #else
  136.     return v;
  137. #endif
  138. }
  139.  
  140. inline TestClass TestClass::operator!() const
  141. {
  142.     return TestClass( value()+1 );
  143. }
  144.  
  145. inline bool operator>( const TestClass& lhs, const TestClass& rhs ) {
  146.     return rhs < lhs;
  147. }
  148.  
  149. inline bool operator>=( const TestClass& lhs, const TestClass& rhs ) {
  150.     return !(lhs < rhs);
  151. }
  152.  
  153. inline bool operator<=( const TestClass& lhs, const TestClass& rhs ) {
  154.     return !(rhs < lhs);
  155. }
  156.  
  157. inline bool operator != ( const TestClass& lhs, const TestClass& rhs ) {
  158.     return lhs.value() != rhs.value();
  159. }
  160.  
  161. inline unsigned int TestClass::get_random( unsigned range )
  162. {
  163.     return random_number( range );
  164. }
  165.  
  166. # ifdef EH_NEW_IOSTREAMS
  167. extern EH_STD::ostream& operator << ( EH_STD::ostream& s, const TestClass&);
  168. # else
  169. extern ostream& operator << ( ostream& s, const TestClass&);
  170. # endif
  171.  
  172. #endif // INCLUDED_MOTU_TestClass
  173.