]>
Commit | Line | Data |
---|---|---|
1 | // 1999-06-08 bkoz | |
2 | ||
3 | // Copyright (C) 1999-2020 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 3, 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 COPYING3. If not see | |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
20 | // 21.3 template class basic_string | |
21 | ||
22 | #include <string> | |
23 | #include <stdexcept> | |
24 | #include <testsuite_hooks.h> | |
25 | ||
26 | // Do a quick sanity check on known problems with element access and | |
27 | // ref-counted strings. These should all pass, regardless of the | |
28 | // underlying string implementation, of course. | |
29 | void test01(void) | |
30 | { | |
31 | typedef std::wstring::size_type csize_type; | |
32 | typedef std::wstring::iterator siterator; | |
33 | typedef std::wstring::reverse_iterator sriterator; | |
34 | csize_type csz01, csz02; | |
35 | siterator it1; | |
36 | sriterator rit1; | |
37 | ||
38 | std::wstring str01(L"montara beach, half moon bay"); | |
39 | const std::wstring str02(L"ocean beach, san francisco"); | |
40 | std::wstring str03; | |
41 | ||
42 | // 21.3 p 5 | |
43 | ||
44 | // References, pointers, and iterators referring to the elements of | |
45 | // a basic_string may be invalidated by the following uses of that | |
46 | // basic_string object: | |
47 | ||
48 | // ... | |
49 | ||
50 | // Susequent to any of the above uses except the forms of insert() | |
51 | // and erase() which return iterators, the first call to non-const | |
52 | // member functions operator[](), at(), begin(), rbegin(), end(), or | |
53 | // rend() | |
54 | ||
55 | str03 = str01; | |
56 | it1 = str01.begin(); | |
57 | *it1 = L'x'; | |
58 | VERIFY( str01[0] == L'x' ); | |
59 | VERIFY( str03[0] == L'm' ); | |
60 | ||
61 | str03 = str01; | |
62 | csz01 = str01.size(); | |
63 | rit1 = str01.rbegin(); // NB: Pointing at one-past the end, so ... | |
64 | *rit1 = L'z'; // ... but it's taken care of here | |
65 | VERIFY( str01[csz01 - 1] == L'z' ); | |
66 | VERIFY( str03[csz01 - 1] == L'y' ); | |
67 | ||
68 | str03 = str01; | |
69 | csz01 = str01.size(); | |
70 | std::wstring::reference r1 = str01.at(csz01 - 2); | |
71 | VERIFY( str03 == str01 ); | |
72 | r1 = L'd'; | |
73 | VERIFY( str01[csz01 - 2] == L'd' ); | |
74 | VERIFY( str03[csz01 - 2] == L'a' ); | |
75 | ||
76 | str03 = str01; | |
77 | csz01 = str01.size(); | |
78 | std::wstring::reference r2 = str01[csz01 - 3]; | |
79 | VERIFY( str03 == str01 ); | |
80 | r2 = L'w'; | |
81 | VERIFY( str01[csz01 - 3] == L'w' ); | |
82 | VERIFY( str03[csz01 - 3] == L'b' ); | |
83 | ||
84 | str03 = str01; | |
85 | csz02 = str01.size(); | |
86 | it1 = str01.end(); | |
87 | VERIFY( str03 == str01 ); | |
88 | --it1; | |
89 | *it1 = L'q'; | |
90 | VERIFY( str01[csz02 - 1] == L'q' ); | |
91 | VERIFY( str03[csz02 - 1] == L'z' ); | |
92 | ||
93 | str03 = str01; | |
94 | rit1 = str01.rend(); | |
95 | VERIFY( str03 == str01 ); | |
96 | --rit1; | |
97 | *rit1 = L'p'; | |
98 | VERIFY( str01[0] == L'p' ); | |
99 | VERIFY( str03[0] == L'x' ); | |
100 | ||
101 | // need to also test for const begin/const end | |
102 | } | |
103 | ||
104 | int main() | |
105 | { | |
106 | test01(); | |
107 | return 0; | |
108 | } |