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