1*03a78d15Sespie // 1999-06-08 bkoz
2*03a78d15Sespie
3*03a78d15Sespie // Copyright (C) 1999 Free Software Foundation, Inc.
4*03a78d15Sespie //
5*03a78d15Sespie // This file is part of the GNU ISO C++ Library. This library is free
6*03a78d15Sespie // software; you can redistribute it and/or modify it under the
7*03a78d15Sespie // terms of the GNU General Public License as published by the
8*03a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
9*03a78d15Sespie // any later version.
10*03a78d15Sespie
11*03a78d15Sespie // This library is distributed in the hope that it will be useful,
12*03a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*03a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*03a78d15Sespie // GNU General Public License for more details.
15*03a78d15Sespie
16*03a78d15Sespie // You should have received a copy of the GNU General Public License along
17*03a78d15Sespie // with this library; see the file COPYING. If not, write to the Free
18*03a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19*03a78d15Sespie // USA.
20*03a78d15Sespie
21*03a78d15Sespie // 21.3.4 basic_string element access
22*03a78d15Sespie
23*03a78d15Sespie #include <string>
24*03a78d15Sespie #include <stdexcept>
25*03a78d15Sespie #include <testsuite_hooks.h>
26*03a78d15Sespie
test01(void)27*03a78d15Sespie bool test01(void)
28*03a78d15Sespie {
29*03a78d15Sespie bool test = true;
30*03a78d15Sespie typedef std::string::size_type csize_type;
31*03a78d15Sespie typedef std::string::const_reference cref;
32*03a78d15Sespie typedef std::string::reference ref;
33*03a78d15Sespie csize_type npos = std::string::npos;
34*03a78d15Sespie csize_type csz01, csz02;
35*03a78d15Sespie
36*03a78d15Sespie const std::string str01("tamarindo, costa rica");
37*03a78d15Sespie std::string str02("41st street beach, capitola, california");
38*03a78d15Sespie std::string str03;
39*03a78d15Sespie
40*03a78d15Sespie // const_reference operator[] (size_type pos) const;
41*03a78d15Sespie csz01 = str01.size();
42*03a78d15Sespie cref cref1 = str01[csz01 - 1];
43*03a78d15Sespie VERIFY( cref1 == 'a' );
44*03a78d15Sespie cref cref2 = str01[csz01];
45*03a78d15Sespie VERIFY( cref2 == char() );
46*03a78d15Sespie
47*03a78d15Sespie // reference operator[] (size_type pos);
48*03a78d15Sespie csz02 = str02.size();
49*03a78d15Sespie ref ref1 = str02[csz02 - 1];
50*03a78d15Sespie VERIFY( ref1 == 'a' );
51*03a78d15Sespie ref ref2 = str02[1];
52*03a78d15Sespie VERIFY( ref2 == '1' );
53*03a78d15Sespie
54*03a78d15Sespie // const_reference at(size_type pos) const;
55*03a78d15Sespie csz01 = str01.size();
56*03a78d15Sespie cref cref3 = str01.at(csz01 - 1);
57*03a78d15Sespie VERIFY( cref3 == 'a' );
58*03a78d15Sespie try {
59*03a78d15Sespie cref cref4 = str01.at(csz01);
60*03a78d15Sespie VERIFY( false ); // Should not get here, as exception thrown.
61*03a78d15Sespie }
62*03a78d15Sespie catch(std::out_of_range& fail) {
63*03a78d15Sespie VERIFY( true );
64*03a78d15Sespie }
65*03a78d15Sespie catch(...) {
66*03a78d15Sespie VERIFY( false );
67*03a78d15Sespie }
68*03a78d15Sespie
69*03a78d15Sespie // reference at(size_type pos);
70*03a78d15Sespie csz01 = str02.size();
71*03a78d15Sespie ref ref3 = str02.at(csz02 - 1);
72*03a78d15Sespie VERIFY( ref3 == 'a' );
73*03a78d15Sespie try {
74*03a78d15Sespie ref ref4 = str02.at(csz02);
75*03a78d15Sespie VERIFY( false ); // Should not get here, as exception thrown.
76*03a78d15Sespie }
77*03a78d15Sespie catch(std::out_of_range& fail) {
78*03a78d15Sespie VERIFY( true );
79*03a78d15Sespie }
80*03a78d15Sespie catch(...) {
81*03a78d15Sespie VERIFY( false );
82*03a78d15Sespie }
83*03a78d15Sespie
84*03a78d15Sespie #ifdef DEBUG_ASSERT
85*03a78d15Sespie assert(test);
86*03a78d15Sespie #endif
87*03a78d15Sespie return test;
88*03a78d15Sespie }
89*03a78d15Sespie
main()90*03a78d15Sespie int main()
91*03a78d15Sespie {
92*03a78d15Sespie test01();
93*03a78d15Sespie return 0;
94*03a78d15Sespie }
95