]>
Commit | Line | Data |
---|---|---|
b9e8095b | 1 | // 1999-06-08 bkoz |
2 | ||
fbd26352 | 3 | // Copyright (C) 1999-2019 Free Software Foundation, Inc. |
b9e8095b | 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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
b9e8095b | 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 | |
6bc9506f | 17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
b9e8095b | 19 | |
20 | // 21.3.4 basic_string element access | |
21 | ||
22 | #include <string> | |
23 | #include <stdexcept> | |
0194306c | 24 | #include <testsuite_hooks.h> |
b9e8095b | 25 | |
31483268 | 26 | void test01(void) |
b9e8095b | 27 | { |
4aca39bb | 28 | typedef std::wstring::size_type csize_type; |
29 | typedef std::wstring::const_reference cref; | |
30 | typedef std::wstring::reference ref; | |
b9e8095b | 31 | csize_type csz01, csz02; |
32 | ||
4aca39bb | 33 | const std::wstring str01(L"tamarindo, costa rica"); |
34 | std::wstring str02(L"41st street beach, capitola, california"); | |
35 | std::wstring str03; | |
b9e8095b | 36 | |
37 | // const_reference operator[] (size_type pos) const; | |
38 | csz01 = str01.size(); | |
39 | cref cref1 = str01[csz01 - 1]; | |
4aca39bb | 40 | VERIFY( cref1 == L'a' ); |
b9e8095b | 41 | cref cref2 = str01[csz01]; |
4aca39bb | 42 | VERIFY( cref2 == wchar_t() ); |
b9e8095b | 43 | |
44 | // reference operator[] (size_type pos); | |
45 | csz02 = str02.size(); | |
46 | ref ref1 = str02[csz02 - 1]; | |
4aca39bb | 47 | VERIFY( ref1 == L'a' ); |
b9e8095b | 48 | ref ref2 = str02[1]; |
4aca39bb | 49 | VERIFY( ref2 == L'1' ); |
b9e8095b | 50 | |
51 | // const_reference at(size_type pos) const; | |
52 | csz01 = str01.size(); | |
53 | cref cref3 = str01.at(csz01 - 1); | |
4aca39bb | 54 | VERIFY( cref3 == L'a' ); |
b9e8095b | 55 | try { |
f8ef786c | 56 | str01.at(csz01); |
43a8c05a | 57 | VERIFY( false ); // Should not get here, as exception thrown. |
b9e8095b | 58 | } |
59 | catch(std::out_of_range& fail) { | |
43a8c05a | 60 | VERIFY( true ); |
b9e8095b | 61 | } |
62 | catch(...) { | |
43a8c05a | 63 | VERIFY( false ); |
b9e8095b | 64 | } |
65 | ||
66 | // reference at(size_type pos); | |
67 | csz01 = str02.size(); | |
68 | ref ref3 = str02.at(csz02 - 1); | |
4aca39bb | 69 | VERIFY( ref3 == L'a' ); |
b9e8095b | 70 | try { |
f8ef786c | 71 | str02.at(csz02); |
43a8c05a | 72 | VERIFY( false ); // Should not get here, as exception thrown. |
b9e8095b | 73 | } |
74 | catch(std::out_of_range& fail) { | |
43a8c05a | 75 | VERIFY( true ); |
b9e8095b | 76 | } |
77 | catch(...) { | |
43a8c05a | 78 | VERIFY( false ); |
b9e8095b | 79 | } |
b9e8095b | 80 | } |
81 | ||
82 | int main() | |
83 | { | |
b9e8095b | 84 | test01(); |
112873cc | 85 | return 0; |
b9e8095b | 86 | } |