~blep/cppunit2/cpptl-integration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "testing.h"
#include <cpptl/config.h>
#include <cpptl/shellsort.h>
#include <iostream>
#include <algorithm>


# define CPPUT_CHECK_SEQUENCE_CUSTOM_EQUAL  \
   CPPUT_BEGIN_CHECKING_MACRO() \
   ::CppUT::checkCustomEqualitySequenceEqual



struct TestInt
{
   static unsigned int serial;

   struct StrictEqual
   {
      bool operator()( const TestInt &x, const TestInt &y ) const
      {
         return x.value() == y.value()  &&  x.index() == y.index();
      }
   };

   TestInt()
      : value_( 0 )
      , serial_( ++serial )
   {
   }

   TestInt( int value )
      : value_( value )
      , serial_( ++serial )
   {
   }

   bool operator <( const TestInt &other ) const
   {
      return value_ < other.value_;
   }

   bool operator ==( const TestInt &other ) const
   {
      return value_ == other.value_;
   }

   bool operator >=( const TestInt &other ) const
   {
      return value_ >= other.value_;
   }

   void swap( TestInt &other )
   {
      CppTL::trivialSwap( value_, other.value_ );
      CppTL::trivialSwap( serial_, other.serial_ );
   }

   int value() const
   {
      return value_;
   }

   unsigned int index() const
   {
      return serial_;
   }

private:
   int value_;
   unsigned int serial_;
};

unsigned int TestInt::serial = 0;


inline std::ostream &operator <<( std::ostream &os, const TestInt &v )
{
   os << "(" << v.value() << "@" << v.index() << ")";
   return os;
}


CPPUT_SUITE( "ShellSort" ) {


CPPUT_TEST_FUNCTION( testShellSortRandom )
{
   for ( int loop = 0; loop < 10; ++loop )
   {
      TestInt::serial = 0;
      int length = (rand() % 5000) + 1;
      std::deque<TestInt> data( length );
      for ( int index = 0; index < length; ++index )
      {
         data[index] = rand() % length;
      }
      std::deque<TestInt> actual( data );
      CppTL::shellSort( actual.begin(), actual.end() );
      std::deque<TestInt> expected( data);
      std::sort( expected.begin(), expected.end() );
      CPPUT_CHECK_SEQUENCE_EQUAL( CppTL::Enum::container(expected),
                                  CppTL::Enum::container(actual) );
   }
}

//
//CPPUT_TEST_FUNCTION_IN( testShellSortRandom, "ShellSort" )
//{
//   for ( int loop = 0; loop < 100; ++loop )
//   {
//      TestInt::serial = 0;
//      int length = (rand() % 5000) + 1;
//      std::vector<TestInt> data( length );
//      for ( int index = 0; index < length; ++index )
//      {
//         data[index] = rand() % length;
//      }
//      std::vector<TestInt> actual( data );
//      CppTL::shellSort( &actual[0], &actual[0] + actual.size() );
////      CppTL::shellSort( actual.begin(), actual.end() );
//      std::vector<TestInt> expected( data);
//      std::stable_sort( expected.begin(), expected.end() );
//      CPPUT_CHECK_SEQUENCE_CUSTOM_EQUAL( CppTL::Enum::container(expected),
//                                         CppTL::Enum::container(actual),
//                                         TestInt::StrictEqual() );
//   }
//}

} // CPPUT_SUITE( "ShellSort" ) {