]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/substr.cc
std_fstream.h: Remove duplicate typdefs for ofstream and wofstream...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / substr.cc
CommitLineData
b2dad0e3
BK
1// 1999-06-10 bkoz
2
3// Copyright (C) 1999 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.6.7 basic_string::substr
22
23#include <string>
24#include <stdexcept>
25#ifdef DEBUG_ASSERT
26#include <assert.h>
27#endif
28
29bool test01(void)
30{
31 bool test = true;
32 typedef std::string::size_type csize_type;
33 typedef std::string::const_reference cref;
34 typedef std::string::reference ref;
35 csize_type npos = std::string::npos;
36 csize_type csz01, csz02;
37
38 const char str_lit01[] = "rockaway, pacifica";
39 const std::string str01(str_lit01);
40 std::string str02;
41
42 // basic_string<charT, _Traits, _Alloc>
43 // substr(size_type pos = 0, size_type n = npos) const;
44 csz01 = str01.size();
45 str02 = str01.substr(0, 1);
46 test &= str02 == "r";
47 str02 = str01.substr(10);
48 test &= str02 == "pacifica";
49
50 try {
51 str02 = str01.substr(csz01 + 1);
52 test &= false;
53 }
54 catch(std::out_of_range& fail) {
55 test &= true;
56 }
57 catch(...) {
58 test &= false;
59 }
60
61 try {
62 str02 = str01.substr(csz01);
63 test &= str02.size() == 0;
64 }
65 catch(std::out_of_range& fail) {
66 test &= false;
67 }
68 catch(...) {
69 test &= false;
70 }
71
72#ifdef DEBUG_ASSERT
73 assert(test);
74#endif
75 return test;
76}
77
78int main()
79{
80 test01();
81}
82
83
84
85
86