OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libstdc++-v3/] [testsuite/] [24_iterators/] [iterator.cc] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jlechner
// 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.
15
//
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
20
 
21
 
22
#include <string>
23
#include <vector>
24
#include <testsuite_hooks.h>
25
 
26
int
27
string_stuff()
28
{
29
   int failures(0);
30
 
31
   std::string s("abcde");
32
 
33
   std::string::iterator i1(s.begin());
34
   if (*i1 != 'a')
35
      ++failures;
36
 
37
   ++i1;
38
   if (*i1 != 'b')
39
      ++failures;
40
 
41
   if (*i1++ != 'b')
42
       ++failures;
43
   if (*i1 != 'c')
44
      ++failures;
45
 
46
   ++ ++i1;
47
   if (*i1 != 'e')
48
      ++failures;
49
 
50
   --i1;
51
   if (*i1 != 'd')
52
      ++failures;
53
 
54
   if (*i1-- != 'd')
55
      ++failures;
56
   if (*i1 != 'c')
57
      ++failures;
58
 
59
   -- --i1;
60
   if (*i1 != 'a')
61
      ++failures;
62
 
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;
69
 
70
   std::iterator_traits<std::string::iterator>::value_type v1;
71
   v1 = i1[0];
72
   if (v1 != 'a')
73
      ++failures;
74
 
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';
82
 
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;
95
 
96
   std::string::iterator i3;
97
   i3 = i1;
98
   if ((i3 == i1) != true)
99
      ++failures;
100
 
101
   i3 += 5;
102
   if ((i3 == i2) != true)
103
      ++failures;
104
 
105
   i3 -= 5;
106
   if ((i3 == i1) != true)
107
      ++failures;
108
 
109
   if (i3 + 5 != i2)
110
      ++failures;
111
 
112
   if (5 + i3 != i2)
113
      ++failures;
114
 
115
   if (i2 - 5 != i3)
116
      ++failures;
117
 
118
   if (i1[0] != 'a')
119
      ++failures;
120
 
121
   i1[4] = 'x';
122
   if (i2[-1] != 'x')
123
      ++failures;
124
   i1[4] = 'e';
125
 
126
   i1[2] = 'x';
127
   if (i2[-3] != 'x')
128
      ++failures;
129
   i1[2] = 'c';
130
 
131
   std::string::const_iterator ci1(s.begin());
132
   if (*ci1 != 'a')
133
      ++failures;
134
 
135
   ++ci1;
136
   if (*ci1 != 'b')
137
      ++failures;
138
 
139
   if (*ci1++ != 'b')
140
      ++failures;
141
   if (*ci1 != 'c')
142
      ++failures;
143
 
144
   ++ ++ci1;
145
   if (*ci1 != 'e')
146
      ++failures;
147
 
148
   --ci1;
149
   if (*ci1 != 'd')
150
      ++failures;
151
 
152
   if (*ci1-- != 'd')
153
      ++failures;
154
   if (*ci1 != 'c')
155
      ++failures;
156
 
157
   -- --ci1;
158
   if (*ci1 != 'a')
159
      ++failures;
160
 
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;
167
 
168
   std::iterator_traits<std::string::const_iterator>::value_type v2;
169
   v2 = ci1[0];
170
   if (v2 != 'a')
171
     ++failures;
172
 
173
   std::iterator_traits<std::string::const_iterator>::reference r2(ci1[0]);
174
   if (r2 != 'a')
175
      ++failures;
176
 
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;
189
 
190
   std::string::const_iterator ci3;
191
   ci3 = ci1;
192
   if ((ci3 == ci1) != true)
193
      ++failures;
194
 
195
   ci3 += 5;
196
   if ((ci3 == ci2) != true)
197
      ++failures;
198
 
199
   ci3 -= 5;
200
   if ((ci3 == ci1) != true)
201
      ++failures;
202
 
203
   if (ci3 + 5 != ci2)
204
      ++failures;
205
 
206
   if (5 + ci3 != ci2)
207
      ++failures;
208
 
209
   if (ci2 - 5 != ci3)
210
      ++failures;
211
 
212
   if (ci1[2] != 'c')
213
      ++failures;
214
 
215
   if (ci2[-1] != 'e')
216
      ++failures;
217
 
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;
243
 
244
   const std::string cs("ABCDE");
245
   std::string::const_iterator ci5(cs.begin());
246
   if (ci5[0] != 'A')
247
      ++failures;
248
 
249
   return failures;
250
}
251
 
252
int
253
vector_stuff()
254
{
255
   int failures(0);
256
 
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));
263
 
264
   std::vector<int>::iterator i1(v.begin());
265
   if (*i1 != 1)
266
      ++failures;
267
 
268
   ++i1;
269
   if (*i1 != 2)
270
      ++failures;
271
 
272
   if (*i1++ != 2)
273
      ++failures;
274
   if (*i1 != 3)
275
      ++failures;
276
 
277
   ++ ++i1;
278
   if (*i1 != 5)
279
      ++failures;
280
 
281
   --i1;
282
   if (*i1 != 4)
283
      ++failures;
284
 
285
   if (*i1-- != 4)
286
      ++failures;
287
   if (*i1 != 3)
288
      ++failures;
289
 
290
   -- --i1;
291
   if (*i1 != 1)
292
      ++failures;
293
 
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;
300
 
301
   std::iterator_traits<std::vector<int>::iterator>::value_type v1;
302
   v1 = i1[0];
303
   if (v1 != 1)
304
      ++failures;
305
 
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;
313
 
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;
326
 
327
   std::vector<int>::iterator i3;
328
   i3 = i1;
329
   if ((i3 == i1) != true)
330
      ++failures;
331
 
332
   i3 += 5;
333
   if ((i3 == i2) != true)
334
      ++failures;
335
 
336
   i3 -= 5;
337
   if ((i3 == i1) != true)
338
      ++failures;
339
 
340
   if (i3 + 5 != i2)
341
      ++failures;
342
 
343
   if (5 + i3 != i2)
344
      ++failures;
345
 
346
   if (i2 - 5 != i3)
347
      ++failures;
348
 
349
   if (i1[0] != 1)
350
      ++failures;
351
 
352
   i1[4] = 9;
353
   if (i2[-1] != 9)
354
      ++failures;
355
   i1[4] = 5;
356
 
357
   i1[2] = 9;
358
   if (i2[-3] != 9)
359
      ++failures;
360
   i1[2] = 3;
361
 
362
   std::vector<int>::const_iterator ci1(v.begin());
363
   if (*ci1 != 1)
364
      ++failures;
365
 
366
   ++ci1;
367
   if (*ci1 != 2)
368
      ++failures;
369
 
370
   if (*ci1++ != 2)
371
      ++failures;
372
   if (*ci1 != 3)
373
      ++failures;
374
 
375
   ++ ++ci1;
376
   if (*ci1 != 5)
377
      ++failures;
378
 
379
   --ci1;
380
   if (*ci1 != 4)
381
      ++failures;
382
 
383
   if (*ci1-- != 4)
384
      ++failures;
385
   if (*ci1 != 3)
386
      ++failures;
387
 
388
   -- --ci1;
389
   if (*ci1 != 1)
390
      ++failures;
391
 
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;
398
 
399
   std::iterator_traits<std::vector<int>::const_iterator>::value_type v2;
400
   v2 = ci1[0];
401
   if (v2 != 1)
402
      ++failures;
403
 
404
   std::iterator_traits<std::vector<int>::const_iterator>::reference
405
      r2(ci1[0]);
406
   if (r2 != 1)
407
      ++failures;
408
 
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;
421
 
422
   std::vector<int>::const_iterator ci3;
423
   ci3 = ci1;
424
   if ((ci3 == ci1) != true)
425
      ++failures;
426
 
427
   ci3 += 5;
428
   if ((ci3 == ci2) != true)
429
      ++failures;
430
 
431
   ci3 -= 5;
432
   if ((ci3 == ci1) != true)
433
      ++failures;
434
 
435
   if (ci3 + 5 != ci2)
436
      ++failures;
437
 
438
   if (5 + ci3 != ci2)
439
      ++failures;
440
 
441
   if (ci2 - 5 != ci3)
442
      ++failures;
443
 
444
   if (ci1[2] != 3)
445
      ++failures;
446
 
447
   if (ci2[-1] != 5)
448
      ++failures;
449
 
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;
475
 
476
   const std::vector<int> cv(v);
477
   std::vector<int>::const_iterator ci5(cv.begin());
478
   if (ci5[0] != 1)
479
      ++failures;
480
 
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;
486
 
487
   return failures;
488
}
489
 
490
int
491
reverse_stuff()
492
{
493
   int failures(0);
494
 
495
   std::string s("abcde");
496
 
497
   std::string::reverse_iterator ri(s.rbegin());
498
   if (*ri != 'e')
499
      ++failures;
500
 
501
   std::iterator_traits<std::string::reverse_iterator>::difference_type d;
502
   d = s.rend() - ri;
503
   if (d != 5)
504
      ++failures;
505
 
506
   const std::string cs("abcde");
507
   std::string::const_reverse_iterator cri(cs.rend());
508
   if (cri - 5 != cs.rbegin())
509
      ++failures;
510
 
511
   return failures;
512
}
513
 
514
// the following should be compiler errors
515
// flag runtime errors in case they slip through the compiler
516
int
517
wrong_stuff()
518
{
519
   int failures(0);
520
 
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
527
 
528
#ifdef ITER24_F2
529
   std::string s;
530
   char *i = s.begin();         // begin() doesn't return a pointer
531
   failures++;
532
#endif
533
 
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
541
 
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
547
 
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
555
 
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
562
 
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
569
 
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
576
 
577
   return failures;
578
}
579
 
580
// libstdc++/6642
581
int
582
test6642()
583
{
584
   std::string s;
585
   std::string::iterator it = s.begin();
586
   std::string::const_iterator cit = s.begin();
587
 
588
   return it - cit;
589
}
590
 
591
int
592
main()
593
{
594
   int failures(0);
595
 
596
   failures += string_stuff();
597
 
598
   failures += vector_stuff();
599
 
600
   failures += reverse_stuff();
601
 
602
   failures += wrong_stuff();
603
 
604
   failures += test6642();
605
 
606
   VERIFY(failures == 0);
607
   return 0;
608
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.