Home | History | Annotate | Download | only in eh
      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 #  include <functional>
     25 #  include <utility>
     26 #  include <climits>
     27 
     28 #include <iosfwd>
     29 #include "random_number.h"
     30 #include "nc_alloc.h"
     31 
     32 class TestClass
     33 {
     34 public:
     35     inline TestClass();
     36     inline TestClass( int value );
     37     inline TestClass( const TestClass& rhs );
     38     inline ~TestClass();
     39 
     40     inline TestClass& operator=( const TestClass& rhs );
     41     inline int value() const;
     42 
     43     inline TestClass operator!() const;
     44 
     45     bool operator==( const TestClass& rhs ) const
     46     {
     47       return value() == rhs.value();
     48     }
     49 
     50     bool operator<( const TestClass& rhs ) const {
     51       return value() < rhs.value();
     52     }
     53 
     54 protected:
     55     static inline unsigned int get_random(unsigned range = UINT_MAX);
     56 private:
     57   inline void Init( int value );
     58 
     59 #if TESTCLASS_DEEP_DATA
     60     int *p;
     61 #else
     62   int v;
     63 #endif
     64 };
     65 
     66 #if defined( __MWERKS__ ) && __MWERKS__ <= 0x3000 && !__SGI_STL
     67 # if defined( __MSL__ ) && __MSL__ < 0x2406
     68 #  include <iterator.h>
     69 __MSL_FIX_ITERATORS__(TestClass);
     70 __MSL_FIX_ITERATORS__(const TestClass);
     71 typedef EH_STD::pair<const TestClass, TestClass> pair_testclass_testclass;
     72 __MSL_FIX_ITERATORS__( pair_testclass_testclass );
     73 __MSL_FIX_ITERATORS__( const pair_testclass_testclass );
     74 # endif
     75 #endif
     76 
     77 inline void TestClass::Init( int value )
     78 {
     79 #if TESTCLASS_DEEP_DATA
     80   p = new int( value );
     81 #else
     82   simulate_constructor();
     83   v = value;
     84 #endif
     85 }
     86 
     87 inline TestClass::TestClass()
     88 {
     89   Init( int(get_random()) );
     90 }
     91 
     92 inline TestClass::TestClass( int value )
     93 {
     94   Init( value );
     95 }
     96 
     97 inline TestClass::TestClass( const TestClass& rhs )
     98 {
     99   Init( rhs.value() );
    100 }
    101 
    102 inline TestClass::~TestClass()
    103 {
    104 #if TESTCLASS_DEEP_DATA
    105   delete p;
    106 #else
    107   simulate_destructor();
    108 #endif
    109 }
    110 
    111 inline TestClass& TestClass::operator=( const TestClass& rhs )
    112 {
    113 #if TESTCLASS_DEEP_DATA
    114   int *newP = new int( rhs.value() );
    115   delete p;
    116   p = newP;
    117 #else
    118   simulate_possible_failure();
    119     v = rhs.value();
    120 #endif
    121     return *this;
    122 }
    123 
    124 inline int TestClass::value() const
    125 {
    126 #if TESTCLASS_DEEP_DATA
    127   return *p;
    128 #else
    129   return v;
    130 #endif
    131 }
    132 
    133 inline TestClass TestClass::operator!() const
    134 {
    135     return TestClass( value()+1 );
    136 }
    137 
    138 inline bool operator>( const TestClass& lhs, const TestClass& rhs ) {
    139     return rhs < lhs;
    140 }
    141 
    142 inline bool operator>=( const TestClass& lhs, const TestClass& rhs ) {
    143     return !(lhs < rhs);
    144 }
    145 
    146 inline bool operator<=( const TestClass& lhs, const TestClass& rhs ) {
    147     return !(rhs < lhs);
    148 }
    149 
    150 inline bool operator != ( const TestClass& lhs, const TestClass& rhs ) {
    151     return lhs.value() != rhs.value();
    152 }
    153 
    154 inline unsigned int TestClass::get_random( unsigned range )
    155 {
    156     return random_number( range );
    157 }
    158 
    159 extern std::ostream& operator << ( std::ostream& s, const TestClass&);
    160 
    161 #endif // INCLUDED_MOTU_TestClass
    162