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