OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / element_access / char / 2.cc
1 // 1999-06-08 bkoz
2
3 // Copyright (C) 1999, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library 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 along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // 21.3 template class basic_string
22
23 #include <string>
24 #include <stdexcept>
25 #include <testsuite_hooks.h>
26
27 // Do a quick sanity check on known problems with element access and
28 // ref-counted strings. These should all pass, regardless of the
29 // underlying string implementation, of course.
30 bool test01(void)
31 {
32   bool test __attribute__((unused)) = true;
33   typedef std::string::size_type csize_type;
34   typedef std::string::iterator siterator;
35   typedef std::string::reverse_iterator sriterator;
36   csize_type csz01, csz02;
37   siterator it1;
38   sriterator rit1;  
39
40   std::string str01("montara beach, half moon bay");
41   const std::string str02("ocean beach, san francisco");
42   std::string str03;
43
44   // 21.3 p 5
45
46   // References, pointers, and iterators referring to the elements of
47   // a basic_string may be invalidated by the following uses of that
48   // basic_string object:
49
50   // ...
51
52   // Susequent to any of the above uses except the forms of insert()
53   // and erase() which return iterators, the first call to non-const
54   // member functions operator[](), at(), begin(), rbegin(), end(), or
55   // rend()
56
57   str03 = str01;
58   it1 = str01.begin();
59   *it1 = 'x';
60   VERIFY( str01[0] == 'x' );
61   VERIFY( str03[0] == 'm' );
62
63   str03 = str01; 
64   csz01 = str01.size();
65   rit1 = str01.rbegin(); // NB: Pointing at one-past the end, so ...
66   *rit1 = 'z';           // ... but it's taken care of here 
67   VERIFY( str01[csz01 - 1] == 'z' );
68   VERIFY( str03[csz01 - 1] == 'y' );
69
70   str03 = str01;
71   csz01 = str01.size();
72   std::string::reference r1 = str01.at(csz01 - 2);
73   VERIFY( str03 == str01 );
74   r1 = 'd';
75   VERIFY( str01[csz01 - 2] == 'd' );
76   VERIFY( str03[csz01 - 2] == 'a' );
77
78   str03 = str01; 
79   csz01 = str01.size();
80   std::string::reference r2 = str01[csz01 - 3];
81   VERIFY( str03 == str01 );
82   r2 = 'w'; 
83   VERIFY( str01[csz01 - 3] == 'w' );
84   VERIFY( str03[csz01 - 3] == 'b' );
85
86   str03 = str01;
87   csz02 = str01.size();
88   it1 = str01.end();
89   VERIFY( str03 == str01 );
90   --it1;
91   *it1 = 'q'; 
92   VERIFY( str01[csz02 - 1] == 'q' );
93   VERIFY( str03[csz02 - 1] == 'z' );
94
95   str03 = str01;
96   rit1 = str01.rend();
97   VERIFY( str03 == str01 );
98   --rit1;       
99   *rit1 = 'p'; 
100   VERIFY( str01[0] == 'p' );
101   VERIFY( str03[0] == 'x' );
102
103   // need to also test for const begin/const end
104   return test;
105 }
106
107 int main()
108
109   test01();
110   return 0;
111 }