1 /***********************************************************************************
2   test_algobase.cpp
3 
4  * Copyright (c) 1997
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 ***********************************************************************************/
16 
17 # include "Prefix.h"
18 # if defined (EH_NEW_HEADERS)
19 # ifdef __SUNPRO_CC
20 # include <stdio.h>
21 # endif
22 #include <algorithm>
23 # else
24 #include <algo.h>
25 # endif
26 #include "Tests.h"
27 #include "LeakCheck.h"
28 #include "TestClass.h"
29 
30 // EH_USE_STD
31 
32 enum { kBufferSize = 100 };
33 
34 struct test_uninitialized_copy
35 {
36     test_uninitialized_copy()
37         : stuff( new TestClass[kBufferSize] ), end_of_stuff(stuff + kBufferSize) {
38         gTestController.SetCurrentTestName("uninitialized_copy()");
39         }
40 
41     ~test_uninitialized_copy() { delete[] stuff; }
42 
43     void operator()( TestClass* buffer ) const
44     {
45         EH_STD::uninitialized_copy((TestClass*)stuff, (TestClass*)end_of_stuff, buffer );
46         EH_ASSERT( EH_STD::equal( (TestClass*)stuff, (TestClass*)end_of_stuff, buffer ) );
47         stl_destroy( buffer, buffer+kBufferSize );
48     }
49 
50 private:
51     TestClass * stuff;
52     TestClass * end_of_stuff;
53 };
54 
55 struct test_uninitialized_fill
56 {
57     test_uninitialized_fill() {
58         gTestController.SetCurrentTestName("uninitialized_fill()");
59     }
60 
61     void operator()( TestClass* buffer ) const
62     {
63         TestClass* buf_end = buffer + kBufferSize;
64         EH_STD::uninitialized_fill( buffer, buf_end, testValue );
65         for ( EH_CSTD::size_t i = 0; i < kBufferSize; i++ )
66             EH_ASSERT( buffer[i] == testValue );
67         stl_destroy( buffer, buf_end );
68     }
69 private:
70     TestClass testValue;
71 };
72 
73 struct test_uninitialized_fill_n
74 {
75     test_uninitialized_fill_n() {
76         gTestController.SetCurrentTestName("uninitialized_fill_n()");
77     }
78     void operator()( TestClass* buffer ) const
79     {
80         TestClass* end = buffer + kBufferSize;
81         EH_STD::uninitialized_fill_n( buffer, (EH_CSTD::size_t)kBufferSize, testValue );
82         for ( EH_CSTD::size_t i = 0; i < kBufferSize; i++ )
83             EH_ASSERT( buffer[i] == testValue );
84         stl_destroy( buffer, end );
85     }
86 private:
87     TestClass testValue;
88 };
89 
90 void test_algobase()
91 {
92   // force alignment
93   double arr[ sizeof(TestClass) * kBufferSize ];
94   TestClass* c = (TestClass*)arr;
95   WeakCheck( c, test_uninitialized_copy() );
96   WeakCheck( c, test_uninitialized_fill() );
97   WeakCheck( c, test_uninitialized_fill_n() );
98 }
99