~ubuntu-branches/ubuntu/wily/bombono-dvd/wily

« back to all changes in this revision

Viewing changes to src/mlib/tests/any_iterator/any_iterator_wrapper_test.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-11-04 11:46:25 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20101104114625-8xfdhvhpsm51i0nu
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  (C) Copyright Thomas Becker 2005. Permission to copy, use, modify, sell and
 
2
//  distribute this software is granted provided this copyright notice appears
 
3
//  in all copies. This software is provided "as is" without express or implied
 
4
//  warranty, and with no claim as to its suitability for any purpose.
 
5
 
 
6
// Revision History
 
7
// ================
 
8
//
 
9
// 27 Dec 2006 (Thomas Becker) Created
 
10
 
 
11
// Includes
 
12
// ========
 
13
 
 
14
#ifdef _MSC_VER
 
15
 
 
16
  // Enable rudimentary memory tracking under Win32.
 
17
  #define _CRTDBG_MAP_ALLOC
 
18
  #include <stdlib.h>
 
19
  #include <crtdbg.h>
 
20
 
 
21
  // Disable stupid warning about what Microsoft thinks is deprecated
 
22
  #pragma warning( disable : 4996 )
 
23
 
 
24
#endif
 
25
 
 
26
#include <mlib/any_iterator/detail/any_iterator_wrapper.hpp>
 
27
#include<vector>
 
28
 
 
29
#define ASSERT_RETURN_FALSE( b ) if( ! (b) ){ return false; }
 
30
 
 
31
using namespace IteratorTypeErasure;
 
32
 
 
33
bool test_any_iterator_wrapper()
 
34
{
 
35
 
 
36
  std::vector<int> vect_of_ints;
 
37
  vect_of_ints.push_back(42);
 
38
  vect_of_ints.push_back(43);
 
39
  vect_of_ints.push_back(47);
 
40
 
 
41
  //////////////////////////////////////////////////////////////////////
 
42
  //
 
43
  // Incrementable
 
44
  //
 
45
  /////////////////////////////////////////////////////////////////////
 
46
  
 
47
  detail::any_iterator_wrapper<
 
48
    std::vector<int>::const_iterator,
 
49
    const int,
 
50
    boost::incrementable_traversal_tag,
 
51
    int const &,
 
52
    ptrdiff_t
 
53
  > default_constructed_const_incrementable_wrapper;
 
54
 
 
55
  detail::any_iterator_wrapper<
 
56
    std::vector<int>::const_iterator,
 
57
    const int,
 
58
    boost::incrementable_traversal_tag,
 
59
    int const &,
 
60
    ptrdiff_t
 
61
  > const_incrementable_wrapper(vect_of_ints.begin());
 
62
 
 
63
  ASSERT_RETURN_FALSE( 42 == const_incrementable_wrapper.dereference() );
 
64
  const_incrementable_wrapper.increment();
 
65
  ASSERT_RETURN_FALSE( 43 == const_incrementable_wrapper.dereference() );
 
66
  
 
67
  detail::any_iterator_abstract_base<
 
68
    const int,
 
69
    boost::incrementable_traversal_tag,
 
70
    int const &,
 
71
    ptrdiff_t
 
72
  >* const_incrementable_wrapper_clone = const_incrementable_wrapper.clone();
 
73
 
 
74
  ASSERT_RETURN_FALSE( 43 == const_incrementable_wrapper_clone->dereference() );
 
75
 
 
76
  delete const_incrementable_wrapper_clone;
 
77
 
 
78
  detail::any_iterator_wrapper<
 
79
    std::vector<int>::iterator,
 
80
    int,
 
81
    boost::incrementable_traversal_tag,
 
82
    int &,
 
83
    ptrdiff_t
 
84
  > incrementable_wrapper(vect_of_ints.begin());
 
85
 
 
86
  incrementable_wrapper.dereference() = 4711;
 
87
  ASSERT_RETURN_FALSE( 4711 == incrementable_wrapper.dereference() );
 
88
  incrementable_wrapper.dereference() = 42;
 
89
  
 
90
  const_incrementable_wrapper_clone = incrementable_wrapper.make_const_clone_with_const_value_type();
 
91
  ASSERT_RETURN_FALSE( 42 == const_incrementable_wrapper_clone->dereference() );
 
92
 
 
93
  detail::any_iterator_abstract_base<
 
94
    int,
 
95
    boost::incrementable_traversal_tag,
 
96
    int const &,
 
97
    ptrdiff_t
 
98
  >* const_incrementable_wrapper_clone_with_non_const_value_type = incrementable_wrapper.make_const_clone_with_non_const_value_type();
 
99
  ASSERT_RETURN_FALSE( 42 == const_incrementable_wrapper_clone_with_non_const_value_type->dereference() );
 
100
  
 
101
  delete const_incrementable_wrapper_clone;
 
102
  delete const_incrementable_wrapper_clone_with_non_const_value_type;
 
103
 
 
104
  //////////////////////////////////////////////////////////////////////
 
105
  //
 
106
  // Single Pass
 
107
  //
 
108
  /////////////////////////////////////////////////////////////////////
 
109
  
 
110
  detail::any_iterator_wrapper<
 
111
    std::vector<int>::const_iterator,
 
112
    const int,
 
113
    boost::single_pass_traversal_tag,
 
114
    int const &,
 
115
    ptrdiff_t
 
116
  > default_constructed_single_pass_wrapper;
 
117
 
 
118
  detail::any_iterator_wrapper<
 
119
    std::vector<int>::const_iterator,
 
120
    const int,
 
121
    boost::single_pass_traversal_tag,
 
122
    int const &,
 
123
    ptrdiff_t
 
124
  > const_single_pass_wrapper(vect_of_ints.begin());
 
125
 
 
126
  ASSERT_RETURN_FALSE( 42 == const_single_pass_wrapper.dereference() );
 
127
  const_single_pass_wrapper.increment();
 
128
  ASSERT_RETURN_FALSE( 43 == const_single_pass_wrapper.dereference() );
 
129
  
 
130
  detail::any_iterator_abstract_base<
 
131
    const int,
 
132
    boost::single_pass_traversal_tag,
 
133
    int const &,
 
134
    ptrdiff_t
 
135
  >* const_single_pass_wrapper_clone = const_single_pass_wrapper.clone();
 
136
 
 
137
  ASSERT_RETURN_FALSE( 43 == const_single_pass_wrapper_clone->dereference() );
 
138
 
 
139
  delete const_single_pass_wrapper_clone;
 
140
 
 
141
  detail::any_iterator_wrapper<
 
142
    std::vector<int>::iterator,
 
143
    int,
 
144
    boost::single_pass_traversal_tag,
 
145
    int &,
 
146
    ptrdiff_t
 
147
  > single_pass_wrapper(vect_of_ints.begin());
 
148
 
 
149
  single_pass_wrapper.dereference() = 4711;
 
150
  ASSERT_RETURN_FALSE( 4711 == single_pass_wrapper.dereference() );
 
151
  single_pass_wrapper.dereference() = 42;
 
152
  
 
153
  const_single_pass_wrapper_clone = single_pass_wrapper.make_const_clone_with_const_value_type();
 
154
  ASSERT_RETURN_FALSE( 42 == const_single_pass_wrapper_clone->dereference() );
 
155
 
 
156
  detail::any_iterator_abstract_base<
 
157
    int,
 
158
    boost::single_pass_traversal_tag,
 
159
    int const &,
 
160
    ptrdiff_t
 
161
  >* const_single_pass_wrapper_clone_with_non_const_value_type = const_single_pass_wrapper.make_const_clone_with_non_const_value_type();
 
162
  ASSERT_RETURN_FALSE( 43 == const_single_pass_wrapper_clone_with_non_const_value_type->dereference() );
 
163
  
 
164
  delete const_single_pass_wrapper_clone;
 
165
  delete const_single_pass_wrapper_clone_with_non_const_value_type;
 
166
 
 
167
  //////////////////////////////////////////////////////////////////////
 
168
  //
 
169
  // Forward
 
170
  //
 
171
  /////////////////////////////////////////////////////////////////////
 
172
  
 
173
  detail::any_iterator_wrapper<
 
174
    std::vector<int>::const_iterator,
 
175
    const int,
 
176
    boost::forward_traversal_tag,
 
177
    int const &,
 
178
    ptrdiff_t
 
179
  > default_constructed_forward_wrapper;
 
180
 
 
181
  detail::any_iterator_wrapper<
 
182
    std::vector<int>::const_iterator,
 
183
    const int,
 
184
    boost::forward_traversal_tag,
 
185
    int const &,
 
186
    ptrdiff_t
 
187
  > const_forward_wrapper(vect_of_ints.begin());
 
188
 
 
189
  ASSERT_RETURN_FALSE( 42 == const_forward_wrapper.dereference() );
 
190
  const_forward_wrapper.increment();
 
191
  ASSERT_RETURN_FALSE( 43 == const_forward_wrapper.dereference() );
 
192
  
 
193
  detail::any_iterator_abstract_base<
 
194
    const int,
 
195
    boost::forward_traversal_tag,
 
196
    int const &,
 
197
    ptrdiff_t
 
198
  >* const_forward_wrapper_clone = const_forward_wrapper.clone();
 
199
 
 
200
  ASSERT_RETURN_FALSE( 43 == const_forward_wrapper_clone->dereference() );
 
201
 
 
202
  ASSERT_RETURN_FALSE( const_forward_wrapper.equal(*const_forward_wrapper_clone) );
 
203
  const_forward_wrapper_clone->increment();
 
204
  ASSERT_RETURN_FALSE( ! const_forward_wrapper.equal(*const_forward_wrapper_clone) );
 
205
 
 
206
  delete const_forward_wrapper_clone;
 
207
 
 
208
  detail::any_iterator_wrapper<
 
209
    std::vector<int>::iterator,
 
210
    int,
 
211
    boost::forward_traversal_tag,
 
212
    int &,
 
213
    ptrdiff_t
 
214
  > forward_wrapper(vect_of_ints.begin());
 
215
 
 
216
  forward_wrapper.dereference() = 4711;
 
217
  ASSERT_RETURN_FALSE( 4711 == forward_wrapper.dereference() );
 
218
  forward_wrapper.dereference() = 42;
 
219
  
 
220
  const_forward_wrapper_clone = forward_wrapper.make_const_clone_with_const_value_type();
 
221
  ASSERT_RETURN_FALSE( 42 == const_forward_wrapper_clone->dereference() );
 
222
  detail::any_iterator_abstract_base<
 
223
    int,
 
224
    boost::forward_traversal_tag,
 
225
    int const &,
 
226
    ptrdiff_t
 
227
  >* const_forward_wrapper_clone_with_non_const_value_type = const_forward_wrapper.make_const_clone_with_non_const_value_type();
 
228
  ASSERT_RETURN_FALSE( 43 == const_forward_wrapper_clone_with_non_const_value_type->dereference() );
 
229
  
 
230
  delete const_forward_wrapper_clone;
 
231
  delete const_forward_wrapper_clone_with_non_const_value_type;
 
232
 
 
233
  //////////////////////////////////////////////////////////////////////
 
234
  //
 
235
  // Bidirectional
 
236
  //
 
237
  /////////////////////////////////////////////////////////////////////
 
238
  
 
239
  detail::any_iterator_wrapper<
 
240
    std::vector<int>::const_iterator,
 
241
    const int,
 
242
    boost::bidirectional_traversal_tag,
 
243
    int const &,
 
244
    ptrdiff_t
 
245
  > default_constructed_bidirectional_wrapper;
 
246
 
 
247
  detail::any_iterator_wrapper<
 
248
    std::vector<int>::const_iterator,
 
249
    const int,
 
250
    boost::bidirectional_traversal_tag,
 
251
    int const &,
 
252
    ptrdiff_t
 
253
  > const_bidirectional_wrapper(vect_of_ints.begin());
 
254
 
 
255
  ASSERT_RETURN_FALSE( 42 == const_bidirectional_wrapper.dereference() );
 
256
  const_bidirectional_wrapper.increment();
 
257
  ASSERT_RETURN_FALSE( 43 == const_bidirectional_wrapper.dereference() );
 
258
  const_bidirectional_wrapper.decrement();
 
259
  ASSERT_RETURN_FALSE( 42 == const_bidirectional_wrapper.dereference() );
 
260
  
 
261
  detail::any_iterator_abstract_base<
 
262
    const int,
 
263
    boost::bidirectional_traversal_tag,
 
264
    int const &,
 
265
    ptrdiff_t
 
266
  >* const_bidirectional_wrapper_clone = const_bidirectional_wrapper.clone();
 
267
 
 
268
  ASSERT_RETURN_FALSE( 42 == const_bidirectional_wrapper_clone->dereference() );
 
269
 
 
270
  ASSERT_RETURN_FALSE( const_bidirectional_wrapper.equal(*const_bidirectional_wrapper_clone) );
 
271
  const_bidirectional_wrapper_clone->increment();
 
272
  ASSERT_RETURN_FALSE( ! const_bidirectional_wrapper.equal(*const_bidirectional_wrapper_clone) );
 
273
 
 
274
  delete const_bidirectional_wrapper_clone;
 
275
 
 
276
  detail::any_iterator_wrapper<
 
277
    std::vector<int>::iterator,
 
278
    int,
 
279
    boost::bidirectional_traversal_tag,
 
280
    int &,
 
281
    ptrdiff_t
 
282
  > bidirectional_wrapper(vect_of_ints.begin());
 
283
 
 
284
  bidirectional_wrapper.dereference() = 4711;
 
285
  ASSERT_RETURN_FALSE( 4711 == bidirectional_wrapper.dereference() );
 
286
  bidirectional_wrapper.dereference() = 42;
 
287
  
 
288
  const_bidirectional_wrapper_clone = bidirectional_wrapper.make_const_clone_with_const_value_type();
 
289
  ASSERT_RETURN_FALSE( 42 == const_bidirectional_wrapper_clone->dereference() );
 
290
 
 
291
  detail::any_iterator_abstract_base<
 
292
    int,
 
293
    boost::bidirectional_traversal_tag,
 
294
    int const &,
 
295
    ptrdiff_t
 
296
  >* const_bidirectional_wrapper_clone_with_non_const_value_type = const_bidirectional_wrapper.make_const_clone_with_non_const_value_type();
 
297
  ASSERT_RETURN_FALSE( 42 == const_bidirectional_wrapper_clone_with_non_const_value_type->dereference() );
 
298
  
 
299
  delete const_bidirectional_wrapper_clone;
 
300
  delete const_bidirectional_wrapper_clone_with_non_const_value_type;
 
301
 
 
302
  //////////////////////////////////////////////////////////////////////
 
303
  //
 
304
  // Random Access
 
305
  //
 
306
  /////////////////////////////////////////////////////////////////////
 
307
  
 
308
  detail::any_iterator_wrapper<
 
309
    std::vector<int>::const_iterator,
 
310
    const int,
 
311
    boost::random_access_traversal_tag,
 
312
    int const &,
 
313
    ptrdiff_t
 
314
  > default_constructed_random_access_wrapper;
 
315
 
 
316
  detail::any_iterator_wrapper<
 
317
    std::vector<int>::const_iterator,
 
318
    const int,
 
319
    boost::random_access_traversal_tag,
 
320
    int const &,
 
321
    ptrdiff_t
 
322
  > const_random_access_wrapper(vect_of_ints.begin());
 
323
 
 
324
  ASSERT_RETURN_FALSE( 42 == const_random_access_wrapper.dereference() );
 
325
  const_random_access_wrapper.increment();
 
326
  ASSERT_RETURN_FALSE( 43 == const_random_access_wrapper.dereference() );
 
327
  const_random_access_wrapper.decrement();
 
328
  ASSERT_RETURN_FALSE( 42 == const_random_access_wrapper.dereference() );
 
329
  
 
330
  const_random_access_wrapper.advance(2);
 
331
  ASSERT_RETURN_FALSE( 47 == const_random_access_wrapper.dereference() );
 
332
  const_random_access_wrapper.advance(-2);
 
333
  ASSERT_RETURN_FALSE( 42 == const_random_access_wrapper.dereference() );
 
334
  
 
335
  detail::any_iterator_wrapper<
 
336
    std::vector<int>::const_iterator,
 
337
    const int,
 
338
    boost::random_access_traversal_tag,
 
339
    int const &,
 
340
    ptrdiff_t
 
341
  > other_const_random_access_wrapper = const_random_access_wrapper;
 
342
  other_const_random_access_wrapper.advance(3);
 
343
  ASSERT_RETURN_FALSE( 3 == const_random_access_wrapper.distance_to(other_const_random_access_wrapper) );
 
344
 
 
345
  detail::any_iterator_abstract_base<
 
346
    const int,
 
347
    boost::random_access_traversal_tag,
 
348
    int const &,
 
349
    ptrdiff_t
 
350
  >* const_random_access_wrapper_clone = const_random_access_wrapper.clone();
 
351
 
 
352
  ASSERT_RETURN_FALSE( 42 == const_random_access_wrapper_clone->dereference() );
 
353
 
 
354
  ASSERT_RETURN_FALSE( const_random_access_wrapper.equal(*const_random_access_wrapper_clone) );
 
355
  const_random_access_wrapper_clone->increment();
 
356
  ASSERT_RETURN_FALSE( ! const_random_access_wrapper.equal(*const_random_access_wrapper_clone) );
 
357
 
 
358
  delete const_random_access_wrapper_clone;
 
359
 
 
360
  detail::any_iterator_wrapper<
 
361
    std::vector<int>::iterator,
 
362
    int,
 
363
    boost::random_access_traversal_tag,
 
364
    int &,
 
365
    ptrdiff_t
 
366
  > random_access_wrapper(vect_of_ints.begin());
 
367
 
 
368
  random_access_wrapper.dereference() = 4711;
 
369
  ASSERT_RETURN_FALSE( 4711 == random_access_wrapper.dereference() );
 
370
  random_access_wrapper.dereference() = 42;
 
371
  
 
372
  const_random_access_wrapper_clone = random_access_wrapper.make_const_clone_with_const_value_type();
 
373
  ASSERT_RETURN_FALSE( 42 == const_random_access_wrapper_clone->dereference() );
 
374
  detail::any_iterator_abstract_base<
 
375
    int,
 
376
    boost::random_access_traversal_tag,
 
377
    int const &,
 
378
    ptrdiff_t
 
379
  >* const_random_access_wrapper_clone_with_non_const_value_type = const_random_access_wrapper.make_const_clone_with_non_const_value_type();
 
380
  ASSERT_RETURN_FALSE( 42 == const_random_access_wrapper_clone_with_non_const_value_type->dereference() );
 
381
  
 
382
  delete const_random_access_wrapper_clone;
 
383
  delete const_random_access_wrapper_clone_with_non_const_value_type;
 
384
 
 
385
  return true;
 
386
}