ReactOS 0.4.16-dev-91-g764881a
swap_test.cpp
Go to the documentation of this file.
1#include <vector>
2#include <algorithm>
3#include <vector>
4#include <queue>
5
6#if 0 /* temporary: investigation of problem with swap */
7#include <iostream>
8#include <typeinfo>
9#endif
10
12
13#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
14using namespace std;
15#endif
16
17//
18// TestCase class
19//
20class SwapTest : public CPPUNIT_NS::TestCase
21{
26#if defined (STLPORT) && \
27 !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) && !defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
29#endif
32
33protected:
34 void swap1();
35 void swprnge1();
38};
39
41
42//
43// tests implementation
44//
46{
47 int a = 42;
48 int b = 19;
49 swap(a, b);
50
51 CPPUNIT_ASSERT(a==19);
52 CPPUNIT_ASSERT(b==42);
53}
54
56{
57 char word1[] = "World";
58 char word2[] = "Hello";
59 swap_ranges((char*)word1, (char*)word1 + ::strlen(word1), (char*)word2);
60 CPPUNIT_ASSERT(!strcmp(word1, "Hello"));
61 CPPUNIT_ASSERT(!strcmp(word2, "World"));
62}
63
64class Obj
65{
66 public:
67 Obj() :
68 v( 0 )
69 { }
70 Obj( const Obj& ) :
71 v( 1 )
72 { }
73
74 Obj& operator =( const Obj& )
75 { v = 2; return *this; }
76
77 int v;
78};
79
80/*
81 * Following two tests check the corectness of specialization of swap():
82 * for containers with container::swap method swap( a, b ) should
83 * use a.swap( b ), but don't try to do this substitution for container
84 * without swap method (in this case swap should be made via explicit members
85 * exchange; this assume usage of temporary object)
86 *
87 */
89{
92
93 v1.push( Obj() );
94 v1.back().v = -1;
95 v1.push( Obj() );
96 v1.back().v = -2;
97
98 v2.push( Obj() );
99 v2.back().v = 10;
100 v2.push( Obj() );
101 v2.back().v = 11;
102 v2.push( Obj() );
103 v2.back().v = 12;
104
105 CPPUNIT_CHECK( v1.size() == 2 );
106 CPPUNIT_CHECK( v2.size() == 3 );
107
108 swap( v1, v2 ); // this shouldn't try make it as v1.swap( v2 ), no queue::swap method!
109
110 CPPUNIT_CHECK( v1.size() == 3 );
111 CPPUNIT_CHECK( v2.size() == 2 );
112
113 // either copy constructor or assignment operator
114 CPPUNIT_CHECK( v1.front().v == 1 || v1.front().v == 2 );
115 CPPUNIT_CHECK( v1.back().v == 1 || v1.back().v == 2 );
116 CPPUNIT_CHECK( v2.front().v == 1 || v2.front().v == 2 );
117 CPPUNIT_CHECK( v2.back().v == 1 || v2.back().v == 2 );
118}
119
121{
122#if 0 /* temporary: investigation of problem with swap */
123 if ( typeid(/* _STLP_PRIV */ _SwapImplemented<vector<Obj> >::_Ret) == typeid(_STLP_PRIV __false_type) ) {
124 cerr << "false type" << endl;
125 } else if ( typeid(/* _STLP_PRIV */ _SwapImplemented<vector<Obj> >::_Ret) == typeid(_STLP_PRIV __true_type) ) {
126 cerr << "true type" << endl;
127 } else {
128 cerr << "unknown type" << endl;
129 }
130#endif /* end of temporary */
131#if !defined (STLPORT) || \
132 defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) || defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
133 vector<Obj> v1;
134 vector<Obj> v2;
135
136 v1.push_back( Obj() );
137 v1.push_back( Obj() );
138
139 v1[0].v = -1;
140 v1[1].v = -2;
141
142 v2.push_back( Obj() );
143 v2.push_back( Obj() );
144 v2.push_back( Obj() );
145
146 v2[0].v = 10;
147 v2[1].v = 11;
148 v2[2].v = 12;
149
150 CPPUNIT_CHECK( v1.size() == 2 );
151 CPPUNIT_CHECK( v2.size() == 3 );
152
153 swap( v1, v2 ); // this should has effect v1.swap( v2 )
154
155 CPPUNIT_CHECK( v1.size() == 3 );
156 CPPUNIT_CHECK( v2.size() == 2 );
157
158 CPPUNIT_CHECK( v1[0].v == 10 );
159 CPPUNIT_CHECK( v1[1].v == 11 );
160 CPPUNIT_CHECK( v1[2].v == 12 );
161
162 CPPUNIT_CHECK( v2[0].v == -1 );
163 CPPUNIT_CHECK( v2[1].v == -2 );
164#endif
165}
_STLP_INLINE_LOOP _ForwardIter2 swap_ranges(_ForwardIter1 __first1, _ForwardIter1 __last1, _ForwardIter2 __first2)
Definition: _algo.h:160
#define _STLP_PRIV
Definition: _dm.h:70
basic_ostream< _CharT, _Traits > &_STLP_CALL endl(basic_ostream< _CharT, _Traits > &__os)
Definition: _ostream.h:357
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
Obj & operator=(const Obj &)
Definition: swap_test.cpp:74
Obj(const Obj &)
Definition: swap_test.cpp:70
int v
Definition: swap_test.cpp:77
Obj()
Definition: swap_test.cpp:67
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(swprnge1)
void swap_container_spec()
Definition: swap_test.cpp:120
void swap_container_non_spec()
Definition: swap_test.cpp:88
CPPUNIT_TEST(swap_container_spec)
CPPUNIT_TEST(swap_container_non_spec)
void swap1()
Definition: swap_test.cpp:45
void swprnge1()
Definition: swap_test.cpp:55
CPPUNIT_TEST(swap1)
CPPUNIT_TEST_SUITE(SwapTest)
Definition: _queue.h:67
#define CPPUNIT_CHECK(X)
Definition: cppunit_mini.h:195
#define CPPUNIT_IGNORE
Definition: cppunit_mini.h:185
#define CPPUNIT_TEST_SUITE_REGISTRATION(X)
Definition: cppunit_mini.h:193
#define CPPUNIT_ASSERT(X)
Definition: cppunit_mini.h:200
const GLdouble * v
Definition: gl.h:2040
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLfloat GLfloat v1
Definition: glext.h:6062
GLfloat GLfloat GLfloat v2
Definition: glext.h:6063
#define cerr
Definition: iostream.cpp:39
Definition: features.h:417
#define swap(a, b)
Definition: qsort.c:63