Debian Codegraph @

libstdc++-v3/testsuite/27_io/basic_stringbuf/seekoff/char/1.cc

raw
1 // 981208 bkoz test functionality of basic_stringbuf for char_type == char
2
3 // Copyright (C) 1997-2025 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 #include <sstream>
21 #include <testsuite_hooks.h>
22
23 std::string str_01("mykonos. . . or what?");
24 std::stringbuf strb_01(str_01);
25
26 // test overloaded virtual functions
27 void test04()
28 {
29 std::string str_tmp;
30 std::streamsize strmsz_1, strmsz_2;
31 typedef std::stringbuf::int_type int_type;
32 typedef std::stringbuf::traits_type traits_type;
33 typedef std::stringbuf::pos_type pos_type;
34 typedef std::stringbuf::off_type off_type;
35
36 int_type c1 = strb_01.sbumpc();
37 int_type c2;
38
39 // BUFFER MANAGEMENT & POSITIONING
40
41 // seekoff
42 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
43 // alters the stream position to off
44 pos_type pt_1(off_type(-1));
45 pos_type pt_2(off_type(0));
46 off_type off_1 = 0;
47 off_type off_2 = 0;
48 strb_01.str(str_01); //in|out ("mykonos. . . or what?");
49
50 //IN|OUT
51 //beg
52 pt_1 = strb_01.pubseekoff(2, std::ios_base::beg);
53 off_1 = off_type(pt_1);
54 VERIFY( off_1 >= 0 );
55 c1 = strb_01.snextc(); //current in pointer +1
56 VERIFY( c1 == 'o' );
57 c2 = strb_01.sputc('x'); //test current out pointer
58 str_tmp = std::string("myxonos. . . or what?");
59 VERIFY( strb_01.str() == str_tmp );
60 //cur
61 pt_1 = strb_01.pubseekoff(2, std::ios_base::cur);
62 off_1 = off_type(pt_1);
63 VERIFY( off_1 == -1 ); // can't seekoff for in and out + cur in sstreams
64 pt_1 = strb_01.pubseekoff(2, std::ios_base::cur, std::ios_base::in);
65 off_1 = off_type(pt_1);
66 pt_2 = strb_01.pubseekoff(2, std::ios_base::cur, std::ios_base::in);
67 off_2 = off_type(pt_2);
68 VERIFY( off_2 == off_1 + 2 );
69 c1 = strb_01.snextc(); //current in pointer + 1
70 VERIFY( c1 == ' ' );
71 c2 = strb_01.sputc('x'); //test current out pointer
72 str_tmp = std::string("myxxnos. . . or what?");
73 VERIFY( strb_01.str() == str_tmp );
74 //end
75 pt_2 = strb_01.pubseekoff(2, std::ios_base::end);
76 off_1 = off_type(pt_2);
77 VERIFY( off_1 == -1 ); // not a valid position
78 VERIFY( strb_01.str() == str_tmp );
79 // end part two (from the filebuf tests)
80 strb_01.pubseekoff(0, std::ios_base::end);
81 strmsz_1 = strb_01.in_avail(); // 0 cuz at the end
82 c1 = strb_01.sgetc();
83 c2 = strb_01.sungetc();
84 strmsz_2 = strb_01.in_avail(); // 1
85 strb_01.sgetc();
86 VERIFY( c1 != c2 );
87 VERIFY( strmsz_2 != strmsz_1 );
88 VERIFY( strmsz_2 == 1 );
89 // end part three
90 str_tmp = " ravi shankar meets carlos santana in LoHa ";
91 str_tmp += str_tmp;
92 strmsz_1 = strb_01.str().size();
93 strmsz_2 = strb_01.sputn(str_tmp.c_str(), str_tmp.size());
94 strb_01.pubseekoff(0, std::ios_base::end);
95 strb_01.sputc('<');
96 str_tmp = strb_01.str();
97 VERIFY(static_cast<std::streamsize>(str_tmp.size()) == strmsz_1 + strmsz_2 + 1);
98 // IN
99 // OUT
100 }
101
102 int main()
103 {
104 test04();
105 return 0;
106 }
107
108
109
110 // more candy!!!