]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_filebuf/sputbackc/char/1.cc
Reshuffle 27_io testsuite.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_filebuf / sputbackc / char / 1.cc
1 // 2001-05-21 Benjamin Kosnik <bkoz@redhat.com>
2
3 // Copyright (C) 2001, 2002, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 27.8.1.4 Overridden virtual functions
22
23 #include <fstream>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <locale>
30 #include <testsuite_hooks.h>
31
32 // @require@ %-*.tst %-*.txt
33 // @diff@ %-*.tst %*.txt
34
35 // NB: This test assumes that _M_buf_size == 40, and not the usual
36 // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be
37 // simulated a bit more readily.
38 // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended.
39 const int buffer_size = 8192;
40 //const int buffer_size = 40;
41
42 const char carray_01[] = "santa cruz or sandiego?";
43 const char carray_02[] = "memphis, new orleans, and savanah";
44 const char name_01[] = "filebuf_virtuals-1.txt"; // file with data in it
45 const char name_02[] = "filebuf_virtuals-2.txt"; // empty file, need to create
46 const char name_03[] = "filebuf_virtuals-3.txt"; // empty file, need to create
47 const char name_04[] = "filebuf_virtuals-4.txt"; // empty file, need to create
48 const char name_05[] = "filebuf_virtuals-5.txt"; // empty file, need to create
49 const char name_06[] = "filebuf_virtuals-6.txt"; // empty file, need to create
50 const char name_07[] = "filebuf_virtuals-7.txt"; // empty file, need to create
51 const char name_08[] = "filebuf_virtuals-8.txt"; // empty file, need to create
52
53 class derived_filebuf: public std::filebuf
54 {
55 public:
56 void
57 set_size(int_type __size) { _M_buf_size_opt = __size; }
58 };
59
60 derived_filebuf fb_01; // in
61 derived_filebuf fb_02; // out
62 derived_filebuf fb_03; // in | out
63
64 // Initialize filebufs to be the same size regardless of platform.
65 void test03()
66 {
67 fb_01.set_size(buffer_size);
68 fb_02.set_size(buffer_size);
69 fb_03.set_size(buffer_size);
70 }
71
72 // Test overloaded virtual functions.
73 void test05()
74 {
75 typedef std::filebuf::int_type int_type;
76 typedef std::filebuf::traits_type traits_type;
77 typedef std::filebuf::pos_type pos_type;
78 typedef std::filebuf::off_type off_type;
79 typedef size_t size_type;
80
81 bool test = true;
82 std::filebuf f_tmp;
83 std::streamsize strmsz_1, strmsz_2;
84 std::streamoff strmof_1, strmof_2;
85 int i = 0, j = 0, k = 0;
86
87 fb_01.open(name_01, std::ios_base::in);
88 fb_02.open(name_02, std::ios_base::out | std::ios_base::trunc);
89 fb_03.open(name_03, std::ios_base::out | std::ios_base::in | std::ios_base::trunc);
90
91 int_type c1 = fb_01.sbumpc();
92 int_type c2 = fb_02.sbumpc();
93 int_type c3 = fb_01.sbumpc();
94 int_type c4 = fb_02.sbumpc();
95 int_type c5 = fb_03.sbumpc();
96 int_type c6 = fb_01.sgetc();
97 int_type c7 = fb_02.sgetc();
98 int_type c8 = fb_01.sgetc();
99 int_type c9 = fb_02.sgetc();
100
101 // PUT
102 strmsz_1 = fb_03.sputn("racadabras", 10);//"abracadabras or what?"
103 strmsz_2 = fb_03.sputn(", i wanna reach out and", 10);
104 strmsz_1 = fb_02.sputn("racadabras", 10);
105 strmsz_1 = fb_01.sputn("racadabra", 10);
106
107 // PUTBACK
108 // int_type pbfail(int_type c)
109 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
110 // "pending sequence" is:
111 // 1) everything as defined in underflow
112 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
113 // sequence is backed up one char before the pending sequence is
114 // determined.
115 // 3) + if (not 2) then c is prepended. Left unspecified is
116 // whether the input sequence is backedup or modified in any way
117 // returns traits::eof() for failure, unspecified other value for success
118
119 // int_type sputbackc(char_type c)
120 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
121 // otherwise decrements in_cur and returns *gptr()
122 c1 = fb_03.sgetc(); // -1
123 c2 = fb_03.sputbackc('z');
124 strmsz_2 = fb_03.in_avail();
125 c3 = fb_03.sgetc();
126 VERIFY( c3 == c2 );
127 VERIFY( c1 != c3 );
128 VERIFY( 1 == strmsz_2 );
129 //test for _in_cur == _in_beg
130 // fb_03._M_out_beg = "bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZracada" etc
131 fb_03.pubseekoff(10, std::ios_base::beg,
132 std::ios_base::in | std::ios_base::out);
133 fb_03.sputc('m');
134 strmsz_1 = fb_03.in_avail();
135 c1 = fb_03.sgetc();
136 fb_03.snextc();
137 c2 = fb_03.sputbackc('z');
138 strmsz_2 = fb_03.in_avail();
139 c3 = fb_03.sgetc();
140 VERIFY( c1 != c2 );
141 VERIFY( c3 == c2 );
142 VERIFY( c1 != c3 );
143 VERIFY( c2 == 'z' );
144 // VERIFY( strmsz_1 == strmsz_2 );
145 // test for replacing char with identical one
146 fb_03.snextc();
147 fb_03.sputc('u');
148 fb_03.sputc('v');
149 fb_03.sputc('a');
150 strmsz_1 = fb_03.in_avail();
151 c2 = fb_03.sputbackc('a');
152 strmsz_2 = fb_03.in_avail();
153 c3 = fb_03.sgetc();
154 VERIFY( c3 == c2 );
155 VERIFY( strmsz_1 + 1 == strmsz_2 );
156 //test for ios_base::out
157 c1 = fb_02.sgetc(); // undefined
158 c2 = fb_02.sputbackc('a');
159 VERIFY( c1 == c2 );
160 VERIFY( c1 == -1 );
161 }
162
163 main()
164 {
165 test03();
166 test05();
167 return 0;
168 }