OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libstdc++-v3/] [testsuite/] [21_strings/] [basic_string/] [insert/] [char/] [1.cc] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jlechner
// 1999-06-03 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19
// USA.
20
 
21
// 21.3.5.4 basic_string::insert
22
 
23
#include <string>
24
#include <stdexcept>
25
#include <testsuite_hooks.h>
26
 
27
int test01(void)
28
{
29
  bool test __attribute__((unused)) = true;
30
  typedef std::string::size_type csize_type;
31
  typedef std::string::iterator citerator;
32
  csize_type csz01, csz02;
33
 
34
  const std::string str01("rodeo beach, marin");
35
  const std::string str02("baker beach, san francisco");
36
  std::string str03;
37
 
38
  // string& insert(size_type p1, const string& str, size_type p2, size_type n)
39
  // requires:
40
  //   1) p1 <= size()
41
  //   2) p2 <= str.size()
42
  //   3) rlen = min(n, str.size() - p2)
43
  // throws:
44
  //   1) out_of_range if p1 > size() || p2 > str.size()
45
  //   2) length_error if size() >= npos - rlen
46
  // effects:
47
  // replaces *this with new string of length size() + rlen such that
48
  // nstr[0]  to nstr[p1] == thisstr[0] to thisstr[p1]
49
  // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen]
50
  // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...]  
51
  str03 = str01;
52
  csz01 = str03.size();
53
  csz02 = str02.size();
54
  try {
55
    str03.insert(csz01 + 1, str02, 0, 5);
56
    VERIFY( false );
57
  }
58
  catch(std::out_of_range& fail) {
59
    VERIFY( true );
60
  }
61
  catch(...) {
62
    VERIFY( false );
63
  }
64
 
65
  str03 = str01;
66
  csz01 = str03.size();
67
  csz02 = str02.size();
68
  try {
69
    str03.insert(0, str02, csz02 + 1, 5);
70
    VERIFY( false );
71
  }
72
  catch(std::out_of_range& fail) {
73
    VERIFY( true );
74
  }
75
  catch(...) {
76
    VERIFY( false );
77
  }
78
 
79
  csz01 = str01.max_size();
80
  try {
81
    std::string str04(csz01, 'b');
82
    str03 = str04;
83
    csz02 = str02.size();
84
    try {
85
      str03.insert(0, str02, 0, 5);
86
      VERIFY( false );
87
    }
88
    catch(std::length_error& fail) {
89
      VERIFY( true );
90
    }
91
    catch(...) {
92
      VERIFY( false );
93
    }
94
  }
95
  catch(std::bad_alloc& failure){
96
    VERIFY( true );
97
  }
98
  catch(std::exception& failure){
99
    VERIFY( false );
100
  }
101
 
102
  str03 = str01;
103
  csz01 = str03.size();
104
  csz02 = str02.size();
105
  str03.insert(13, str02, 0, 12);
106
  VERIFY( str03 == "rodeo beach, baker beach,marin" );
107
 
108
  str03 = str01;
109
  csz01 = str03.size();
110
  csz02 = str02.size();
111
  str03.insert(0, str02, 0, 12);
112
  VERIFY( str03 == "baker beach,rodeo beach, marin" );
113
 
114
  str03 = str01;
115
  csz01 = str03.size();
116
  csz02 = str02.size();
117
  str03.insert(csz01, str02, 0, csz02);
118
  VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
119
 
120
  // string& insert(size_type __p, const string& string);
121
  // insert(p1, str, 0, npos)
122
  str03 = str01;
123
  csz01 = str03.size();
124
  csz02 = str02.size();
125
  str03.insert(csz01, str02);
126
  VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
127
 
128
  str03 = str01;
129
  csz01 = str03.size();
130
  csz02 = str02.size();
131
  str03.insert(0, str02);
132
  VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" );
133
 
134
  // string& insert(size_type __p, const char* s, size_type n);
135
  // insert(p1, string(s,n))
136
  str03 = str02;
137
  csz01 = str03.size();
138
  str03.insert(0, "-break at the bridge", 20);
139
  VERIFY( str03 == "-break at the bridgebaker beach, san francisco" );
140
 
141
  // string& insert(size_type __p, const char* s);
142
  // insert(p1, string(s))
143
  str03 = str02;
144
  str03.insert(0, "-break at the bridge");
145
  VERIFY( str03 == "-break at the bridgebaker beach, san francisco" );
146
 
147
  // string& insert(size_type __p, size_type n, char c)
148
  // insert(p1, string(n,c))
149
  str03 = str02;
150
  csz01 = str03.size();
151
  str03.insert(csz01, 5, 'z');
152
  VERIFY( str03 == "baker beach, san franciscozzzzz" );
153
 
154
  // iterator insert(iterator p, char c)
155
  // inserts a copy of c before the character referred to by p
156
  str03 = str02;
157
  citerator cit01 = str03.begin();
158
  str03.insert(cit01, 'u');
159
  VERIFY( str03 == "ubaker beach, san francisco" );
160
 
161
  // iterator insert(iterator p, size_type n,  char c)
162
  // inserts n copies of c before the character referred to by p
163
  str03 = str02;
164
  cit01 = str03.begin();
165
  str03.insert(cit01, 5, 'u');
166
  VERIFY( str03 == "uuuuubaker beach, san francisco" );
167
 
168
  // template<inputit>
169
  //   void 
170
  //   insert(iterator p, inputit first, inputit, last)
171
  // ISO-14882: defect #7 part 1 clarifies this member function to be:
172
  // insert(p - begin(), string(first,last))
173
  str03 = str02;
174
  csz01 = str03.size();
175
  str03.insert(str03.begin(), str01.begin(), str01.end());
176
  VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
177
 
178
  str03 = str02;
179
  csz01 = str03.size();
180
  str03.insert(str03.end(), str01.begin(), str01.end());
181
  VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" );
182
  return test;
183
}
184
 
185
int main()
186
{
187
  __gnu_test::set_memory_limits();
188
  test01();
189
  return 0;
190
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.