]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/2.cc
Reshuffle 21_strings testsuite.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / element_access / wchar_t / 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 = true;
33 typedef std::wstring::size_type csize_type;
34 typedef std::wstring::iterator siterator;
35 typedef std::wstring::reverse_iterator sriterator;
36 csize_type npos = std::wstring::npos;
37 csize_type csz01, csz02;
38 siterator it1;
39 sriterator rit1;
40
41 std::wstring str01(L"montara beach, half moon bay");
42 const std::wstring str02(L"ocean beach, san francisco");
43 std::wstring str03;
44
45 // 21.3 p 5
46
47 // References, pointers, and iterators referring to the elements of
48 // a basic_string may be invalidated by the following uses of that
49 // basic_string object:
50
51 // ...
52
53 // Susequent to any of the above uses except the forms of insert()
54 // and erase() which return iterators, the first call to non-const
55 // member functions operator[](), at(), begin(), rbegin(), end(), or
56 // rend()
57
58 str03 = str01;
59 it1 = str01.begin();
60 *it1 = L'x';
61 VERIFY( str01[0] == L'x' );
62 VERIFY( str03[0] == L'm' );
63
64 str03 = str01;
65 csz01 = str01.size();
66 rit1 = str01.rbegin(); // NB: Pointing at one-past the end, so ...
67 *rit1 = L'z'; // ... but it's taken care of here
68 VERIFY( str01[csz01 - 1] == L'z' );
69 VERIFY( str03[csz01 - 1] == L'y' );
70
71 str03 = str01;
72 csz01 = str01.size();
73 std::wstring::reference r1 = str01.at(csz01 - 2);
74 VERIFY( str03 == str01 );
75 r1 = L'd';
76 VERIFY( str01[csz01 - 2] == L'd' );
77 VERIFY( str03[csz01 - 2] == L'a' );
78
79 str03 = str01;
80 csz01 = str01.size();
81 std::wstring::reference r2 = str01[csz01 - 3];
82 VERIFY( str03 == str01 );
83 r2 = L'w';
84 VERIFY( str01[csz01 - 3] == L'w' );
85 VERIFY( str03[csz01 - 3] == L'b' );
86
87 str03 = str01;
88 csz02 = str01.size();
89 it1 = str01.end();
90 VERIFY( str03 == str01 );
91 --it1;
92 *it1 = L'q';
93 VERIFY( str01[csz02 - 1] == L'q' );
94 VERIFY( str03[csz02 - 1] == L'z' );
95
96 str03 = str01;
97 rit1 = str01.rend();
98 VERIFY( str03 == str01 );
99 --rit1;
100 *rit1 = L'p';
101 VERIFY( str01[0] == L'p' );
102 VERIFY( str03[0] == L'x' );
103
104 // need to also test for const begin/const end
105 #ifdef DEBUG_ASSERT
106 assert(test);
107 #endif
108 return test;
109 }
110
111 int main()
112 {
113 test01();
114 return 0;
115 }