2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libstdc++-v3 / testsuite / 24_iterators / iterator.cc
blobfb61d8004db66de099d45b8e30b4afdd09b34fe3
1 // 24.1.5 Random access iterators
2 // 24.3.1 Iterator traits
3 // (basic_string and vector implementations)
4 //
5 // Copyright (C) 1999, 2003, 2004, 2005 Free Software Foundation, Inc.
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 // USA
22 #include <string>
23 #include <vector>
24 #include <testsuite_hooks.h>
26 int
27 string_stuff()
29 int failures(0);
31 std::string s("abcde");
33 std::string::iterator i1(s.begin());
34 if (*i1 != 'a')
35 ++failures;
37 ++i1;
38 if (*i1 != 'b')
39 ++failures;
41 if (*i1++ != 'b')
42 ++failures;
43 if (*i1 != 'c')
44 ++failures;
46 ++ ++i1;
47 if (*i1 != 'e')
48 ++failures;
50 --i1;
51 if (*i1 != 'd')
52 ++failures;
54 if (*i1-- != 'd')
55 ++failures;
56 if (*i1 != 'c')
57 ++failures;
59 -- --i1;
60 if (*i1 != 'a')
61 ++failures;
63 std::string::iterator i2;
64 i2 = s.end();
65 std::iterator_traits<std::string::iterator>::difference_type d1;
66 d1 = i2 - i1;
67 if (d1 != 5)
68 ++failures;
70 std::iterator_traits<std::string::iterator>::value_type v1;
71 v1 = i1[0];
72 if (v1 != 'a')
73 ++failures;
75 std::iterator_traits<std::string::iterator>::reference r1(i1[0]);
76 if (r1 != 'a')
77 ++failures;
78 r1 = 'x';
79 if (r1 != 'x')
80 ++failures;
81 r1 = 'a';
83 if ((i1 != i2) != true)
84 ++failures;
85 if ((i1 == i2) != false)
86 ++failures;
87 if ((i1 < i2) != true)
88 ++failures;
89 if ((i1 > i2) != false)
90 ++failures;
91 if ((i1 <= i2) != true)
92 ++failures;
93 if ((i1 >= i2) != false)
94 ++failures;
96 std::string::iterator i3;
97 i3 = i1;
98 if ((i3 == i1) != true)
99 ++failures;
101 i3 += 5;
102 if ((i3 == i2) != true)
103 ++failures;
105 i3 -= 5;
106 if ((i3 == i1) != true)
107 ++failures;
109 if (i3 + 5 != i2)
110 ++failures;
112 if (5 + i3 != i2)
113 ++failures;
115 if (i2 - 5 != i3)
116 ++failures;
118 if (i1[0] != 'a')
119 ++failures;
121 i1[4] = 'x';
122 if (i2[-1] != 'x')
123 ++failures;
124 i1[4] = 'e';
126 i1[2] = 'x';
127 if (i2[-3] != 'x')
128 ++failures;
129 i1[2] = 'c';
131 std::string::const_iterator ci1(s.begin());
132 if (*ci1 != 'a')
133 ++failures;
135 ++ci1;
136 if (*ci1 != 'b')
137 ++failures;
139 if (*ci1++ != 'b')
140 ++failures;
141 if (*ci1 != 'c')
142 ++failures;
144 ++ ++ci1;
145 if (*ci1 != 'e')
146 ++failures;
148 --ci1;
149 if (*ci1 != 'd')
150 ++failures;
152 if (*ci1-- != 'd')
153 ++failures;
154 if (*ci1 != 'c')
155 ++failures;
157 -- --ci1;
158 if (*ci1 != 'a')
159 ++failures;
161 std::string::const_iterator ci2;
162 ci2 = s.end();
163 std::iterator_traits<std::string::const_iterator>::difference_type d2;
164 d2 = ci2 - ci1;
165 if (d2 != 5)
166 ++failures;
168 std::iterator_traits<std::string::const_iterator>::value_type v2;
169 v2 = ci1[0];
170 if (v2 != 'a')
171 ++failures;
173 std::iterator_traits<std::string::const_iterator>::reference r2(ci1[0]);
174 if (r2 != 'a')
175 ++failures;
177 if ((ci1 != ci2) != true)
178 ++failures;
179 if ((ci1 == ci2) != false)
180 ++failures;
181 if ((ci1 < ci2) != true)
182 ++failures;
183 if ((ci1 > ci2) != false)
184 ++failures;
185 if ((ci1 <= ci2) != true)
186 ++failures;
187 if ((ci1 >= ci2) != false)
188 ++failures;
190 std::string::const_iterator ci3;
191 ci3 = ci1;
192 if ((ci3 == ci1) != true)
193 ++failures;
195 ci3 += 5;
196 if ((ci3 == ci2) != true)
197 ++failures;
199 ci3 -= 5;
200 if ((ci3 == ci1) != true)
201 ++failures;
203 if (ci3 + 5 != ci2)
204 ++failures;
206 if (5 + ci3 != ci2)
207 ++failures;
209 if (ci2 - 5 != ci3)
210 ++failures;
212 if (ci1[2] != 'c')
213 ++failures;
215 if (ci2[-1] != 'e')
216 ++failures;
218 // iterator and const_iterator
219 std::string::const_iterator ci4(i1);
220 if ((ci4 == i1) != true)
221 ++failures;
222 if ((ci4 != i1) != false)
223 ++failures;
224 if ((ci4 < i1) != false)
225 ++failures;
226 if ((ci4 > i1) != false)
227 ++failures;
228 if ((ci4 <= i1) != true)
229 ++failures;
230 if ((ci4 >= i1) != true)
231 ++failures;
232 ci4 = i2;
233 if ((i2 == ci4) != true)
234 ++failures;
235 if ((i2 < ci4) != false)
236 ++failures;
237 if ((i2 > ci4) != false)
238 ++failures;
239 if ((i2 <= ci4) != true)
240 ++failures;
241 if ((i2 >= ci4) != true)
242 ++failures;
244 const std::string cs("ABCDE");
245 std::string::const_iterator ci5(cs.begin());
246 if (ci5[0] != 'A')
247 ++failures;
249 return failures;
252 int
253 vector_stuff()
255 int failures(0);
257 std::vector<int> v;
258 v.push_back(int(1));
259 v.push_back(int(2));
260 v.push_back(int(3));
261 v.push_back(int(4));
262 v.push_back(int(5));
264 std::vector<int>::iterator i1(v.begin());
265 if (*i1 != 1)
266 ++failures;
268 ++i1;
269 if (*i1 != 2)
270 ++failures;
272 if (*i1++ != 2)
273 ++failures;
274 if (*i1 != 3)
275 ++failures;
277 ++ ++i1;
278 if (*i1 != 5)
279 ++failures;
281 --i1;
282 if (*i1 != 4)
283 ++failures;
285 if (*i1-- != 4)
286 ++failures;
287 if (*i1 != 3)
288 ++failures;
290 -- --i1;
291 if (*i1 != 1)
292 ++failures;
294 std::vector<int>::iterator i2;
295 i2 = v.end();
296 std::iterator_traits<std::vector<int>::iterator>::difference_type d1;
297 d1 = i2 - i1;
298 if (d1 != 5)
299 ++failures;
301 std::iterator_traits<std::vector<int>::iterator>::value_type v1;
302 v1 = i1[0];
303 if (v1 != 1)
304 ++failures;
306 std::iterator_traits<std::vector<int>::iterator>::reference r1(i1[0]);
307 if (r1 != 1)
308 ++failures;
309 r1 = 9;
310 if (r1 != 9)
311 ++failures;
312 r1 = 1;
314 if ((i1 != i2) != true)
315 ++failures;
316 if ((i1 == i2) != false)
317 ++failures;
318 if ((i1 < i2) != true)
319 ++failures;
320 if ((i1 > i2) != false)
321 ++failures;
322 if ((i1 <= i2) != true)
323 ++failures;
324 if ((i1 >= i2) != false)
325 ++failures;
327 std::vector<int>::iterator i3;
328 i3 = i1;
329 if ((i3 == i1) != true)
330 ++failures;
332 i3 += 5;
333 if ((i3 == i2) != true)
334 ++failures;
336 i3 -= 5;
337 if ((i3 == i1) != true)
338 ++failures;
340 if (i3 + 5 != i2)
341 ++failures;
343 if (5 + i3 != i2)
344 ++failures;
346 if (i2 - 5 != i3)
347 ++failures;
349 if (i1[0] != 1)
350 ++failures;
352 i1[4] = 9;
353 if (i2[-1] != 9)
354 ++failures;
355 i1[4] = 5;
357 i1[2] = 9;
358 if (i2[-3] != 9)
359 ++failures;
360 i1[2] = 3;
362 std::vector<int>::const_iterator ci1(v.begin());
363 if (*ci1 != 1)
364 ++failures;
366 ++ci1;
367 if (*ci1 != 2)
368 ++failures;
370 if (*ci1++ != 2)
371 ++failures;
372 if (*ci1 != 3)
373 ++failures;
375 ++ ++ci1;
376 if (*ci1 != 5)
377 ++failures;
379 --ci1;
380 if (*ci1 != 4)
381 ++failures;
383 if (*ci1-- != 4)
384 ++failures;
385 if (*ci1 != 3)
386 ++failures;
388 -- --ci1;
389 if (*ci1 != 1)
390 ++failures;
392 std::vector<int>::const_iterator ci2;
393 ci2 = v.end();
394 std::iterator_traits<std::vector<int>::const_iterator>::difference_type d2;
395 d2 = ci2 - ci1;
396 if (d2 != 5)
397 ++failures;
399 std::iterator_traits<std::vector<int>::const_iterator>::value_type v2;
400 v2 = ci1[0];
401 if (v2 != 1)
402 ++failures;
404 std::iterator_traits<std::vector<int>::const_iterator>::reference
405 r2(ci1[0]);
406 if (r2 != 1)
407 ++failures;
409 if ((ci1 != ci2) != true)
410 ++failures;
411 if ((ci1 == ci2) != false)
412 ++failures;
413 if ((ci1 < ci2) != true)
414 ++failures;
415 if ((ci1 > ci2) != false)
416 ++failures;
417 if ((ci1 <= ci2) != true)
418 ++failures;
419 if ((ci1 >= ci2) != false)
420 ++failures;
422 std::vector<int>::const_iterator ci3;
423 ci3 = ci1;
424 if ((ci3 == ci1) != true)
425 ++failures;
427 ci3 += 5;
428 if ((ci3 == ci2) != true)
429 ++failures;
431 ci3 -= 5;
432 if ((ci3 == ci1) != true)
433 ++failures;
435 if (ci3 + 5 != ci2)
436 ++failures;
438 if (5 + ci3 != ci2)
439 ++failures;
441 if (ci2 - 5 != ci3)
442 ++failures;
444 if (ci1[2] != 3)
445 ++failures;
447 if (ci2[-1] != 5)
448 ++failures;
450 // iterator to const_iterator
451 std::vector<int>::const_iterator ci4(i1);
452 if ((ci4 == i1) != true)
453 ++failures;
454 if ((ci4 != i1) != false)
455 ++failures;
456 if ((ci4 < i1) != false)
457 ++failures;
458 if ((ci4 > i1) != false)
459 ++failures;
460 if ((ci4 <= i1) != true)
461 ++failures;
462 if ((ci4 >= i1) != true)
463 ++failures;
464 ci4 = i2;
465 if ((i2 == ci4) != true)
466 ++failures;
467 if ((i2 < ci4) != false)
468 ++failures;
469 if ((i2 > ci4) != false)
470 ++failures;
471 if ((i2 <= ci4) != true)
472 ++failures;
473 if ((i2 >= ci4) != true)
474 ++failures;
476 const std::vector<int> cv(v);
477 std::vector<int>::const_iterator ci5(cv.begin());
478 if (ci5[0] != 1)
479 ++failures;
481 std::vector<std::string> vs;
482 vs.push_back(std::string("abc"));
483 std::vector<std::string>::iterator ivs(vs.begin());
484 if (ivs->c_str()[1] != 'b')
485 ++failures;
487 return failures;
490 int
491 reverse_stuff()
493 int failures(0);
495 std::string s("abcde");
497 std::string::reverse_iterator ri(s.rbegin());
498 if (*ri != 'e')
499 ++failures;
501 std::iterator_traits<std::string::reverse_iterator>::difference_type d;
502 d = s.rend() - ri;
503 if (d != 5)
504 ++failures;
506 const std::string cs("abcde");
507 std::string::const_reverse_iterator cri(cs.rend());
508 if (cri - 5 != cs.rbegin())
509 ++failures;
511 return failures;
514 // the following should be compiler errors
515 // flag runtime errors in case they slip through the compiler
516 int
517 wrong_stuff()
519 int failures(0);
521 #ifdef ITER24_F1
522 extern void f(std::vector<std::string*>::iterator);
523 std::vector<std::string*> vs[2];
524 f(vs); // address of array is not an iterator
525 failures++;
526 #endif
528 #ifdef ITER24_F2
529 std::string s;
530 char *i = s.begin(); // begin() doesn't return a pointer
531 failures++;
532 #endif
534 #ifdef ITER24_F3
535 std::string::const_iterator ci;
536 std::string::iterator i;
537 if (i - ci) // remove const_ is a warning
538 i++;
539 // failures++; only a warning
540 #endif
542 #ifdef ITER24_F4
543 std::vector<char>::iterator iv;
544 std::string::iterator is(iv);// vector<char> is not string
545 failures++;
546 #endif
548 #ifdef ITER24_F5
549 std::vector<char>::iterator iv;
550 std::string::iterator is;
551 if (iv == is) // vector<char> is not string
552 ++iv;
553 failures++;
554 #endif
556 #ifdef ITER24_F6
557 std::vector<char>::const_iterator ci;
558 std::vector<char>::iterator i = ci; // remove const_ is a warning
559 ++i;
560 // failures++; only a warning
561 #endif
563 #ifdef ITER24_F7
564 std::vector<int> v(1);
565 std::vector<int>::const_iterator ci(v.begin());
566 *ci = 1; // cannot assign through const_iterator
567 failures++;
568 #endif
570 #ifdef ITER24_F8
571 std::vector<const int> v(1);
572 std::vector<const int>::reference r(v.begin()[0]);
573 r = 1; // cannot assign through reference to const
574 failures++;
575 #endif
577 return failures;
580 // libstdc++/6642
582 test6642()
584 std::string s;
585 std::string::iterator it = s.begin();
586 std::string::const_iterator cit = s.begin();
588 return it - cit;
592 main()
594 int failures(0);
596 failures += string_stuff();
598 failures += vector_stuff();
600 failures += reverse_stuff();
602 failures += wrong_stuff();
604 failures += test6642();
606 VERIFY(failures == 0);
607 return 0;