sstream.tcc (pbackfail): Implement correctly 27.7.1.3/2, bullet 2...
authorPaolo Carlini <pcarlini@suse.de>
Fri, 1 Oct 2004 21:01:29 +0000 (21:01 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Fri, 1 Oct 2004 21:01:29 +0000 (21:01 +0000)
2004-10-01  Paolo Carlini  <pcarlini@suse.de>

* include/bits/sstream.tcc (pbackfail): Implement correctly
27.7.1.3/2, bullet 2: if mode & ios_base::out is false do not
write in the buffer.
* testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc: New.
* testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc: Likewise.
* testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc: Likewise.
* testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc: Likewise.
* testsuite/27_io/basic_istream/putback/char/1.cc: Tweak consistently.

From-SVN: r88407

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/sstream.tcc
libstdc++-v3/testsuite/27_io/basic_istream/putback/char/1.cc
libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc [new file with mode: 0644]

index 88bd3a757680a64a09087389deaa195cbce99b1d..ff73a85c1555060fda1bcd5e3e392132a1faa1ab 100644 (file)
@@ -1,3 +1,14 @@
+2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+
+       * include/bits/sstream.tcc (pbackfail): Implement correctly
+       27.7.1.3/2, bullet 2: if mode & ios_base::out is false do not
+       write in the buffer.
+       * testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc: New.
+       * testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc: Likewise.
+       * testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc: Likewise.
+       * testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc: Likewise.
+       * testsuite/27_io/basic_istream/putback/char/1.cc: Tweak consistently.
+
 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
 
        * include/bits/sstream.tcc (seekpos): Minor rearrangement of two
index 19a24bf0f189b938d4fb5cdc8e249a66e6568917..76cda2974eed72c77516ecd4e977c6927e2ba5b9 100644 (file)
@@ -47,24 +47,29 @@ namespace std
     pbackfail(int_type __c)
     {
       int_type __ret = traits_type::eof();
-      const bool __testeof = traits_type::eq_int_type(__c, __ret);
-
       if (this->eback() < this->gptr())
        {
-         const bool __testeq = traits_type::eq(traits_type::to_char_type(__c),
-                                               this->gptr()[-1]);
-         this->gbump(-1);
-
          // Try to put back __c into input sequence in one of three ways.
          // Order these tests done in is unspecified by the standard.
-         if (!__testeof && __testeq)
-           __ret = __c;
-         else if (__testeof)
-           __ret = traits_type::not_eof(__c);
+         const bool __testeof = traits_type::eq_int_type(__c, __ret);
+         if (!__testeof)
+           {
+             const bool __testeq = traits_type::eq(traits_type::
+                                                   to_char_type(__c),
+                                                   this->gptr()[-1]);    
+             const bool __testout = this->_M_mode & ios_base::out;
+             if (__testeq || __testout)
+               {
+                 this->gbump(-1);
+                 if (!__testeq)
+                   *this->gptr() = traits_type::to_char_type(__c);
+                 __ret = __c;
+               }
+           }
          else
            {
-             *this->gptr() = traits_type::to_char_type(__c);
-             __ret = __c;
+             this->gbump(-1);
+             __ret = traits_type::not_eof(__c);
            }
        }
       return __ret;
index 870d57aa3557e1c0c3c9dfb19daad22b03012fd3..b3c64f60d4df128d1880b7b7d4901f03d4ee921f 100644 (file)
@@ -49,11 +49,11 @@ test01()
   is_04.ignore(30);
   is_04.clear();
   state1 = is_04.rdstate();
-  is_04.putback('|');
+  is_04.putback('t');
   VERIFY( is_04.gcount() == 0 );  // DR 60
   state2 = is_04.rdstate();
   VERIFY( state1 == state2 );
-  VERIFY( is_04.peek() == '|' );
+  VERIFY( is_04.peek() == 't' );
 
   // istream& unget()
   is_04.clear();
diff --git a/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc b/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc
new file mode 100644 (file)
index 0000000..5e83299
--- /dev/null
@@ -0,0 +1,76 @@
+// 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+class my_stringbuf : public std::stringbuf
+{
+public:
+  my_stringbuf(const std::string& str, std::ios_base::openmode mode)
+  : std::stringbuf(str, mode) { }
+
+  int_type 
+  pub_pbackfail(int_type c) 
+  { return this->pbackfail(c); }
+};
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  typedef my_stringbuf::int_type    int_type;
+  typedef my_stringbuf::traits_type traits_type;
+
+  my_stringbuf sbuf("any", ios_base::in | ios_base::out);
+
+  int_type c = sbuf.sgetc();
+  VERIFY( c == 'a' );
+
+  c = sbuf.pub_pbackfail('z');
+  VERIFY( c == traits_type::eof() );
+  c = sbuf.sbumpc();
+  VERIFY( c == 'a' );
+  c = sbuf.pub_pbackfail('a');
+  VERIFY( c == 'a' );
+  c = sbuf.sbumpc();
+  VERIFY( c == 'a' );
+  
+  c = sbuf.pub_pbackfail('x');
+  VERIFY( c == 'x' );
+  c = sbuf.sbumpc();
+  VERIFY( c == 'x' );
+
+  const int_type eof = traits_type::eof();
+  c = sbuf.pub_pbackfail(eof);
+  VERIFY( c == traits_type::not_eof(eof) );
+  c = sbuf.sgetc();
+  VERIFY( c == 'x' );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc b/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc
new file mode 100644 (file)
index 0000000..832cee6
--- /dev/null
@@ -0,0 +1,64 @@
+// 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+class my_stringbuf : public std::stringbuf
+{
+public:
+  my_stringbuf(const std::string& str, std::ios_base::openmode mode)
+  : std::stringbuf(str, mode) { }
+
+  int_type 
+  pub_pbackfail(int_type __c) 
+  { return this->pbackfail(__c); }
+};
+
+// We weren't enforcing 27.7.1.3/2, bullet 2: "... and if
+// mode & ios_base::out is nonzero, ..."
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  typedef my_stringbuf::int_type    int_type;
+  typedef my_stringbuf::traits_type traits_type;
+
+  my_stringbuf sbuf("any", ios_base::in);
+  
+  int_type c = sbuf.sbumpc();
+  VERIFY( c == 'a' );
+
+  c = sbuf.pub_pbackfail('x');
+  VERIFY( c == traits_type::eof() );
+  VERIFY( sbuf.str() == "any" );
+  c = sbuf.sgetc();
+  VERIFY( c == 'n' );
+}
+
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc b/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc
new file mode 100644 (file)
index 0000000..84ea872
--- /dev/null
@@ -0,0 +1,76 @@
+// 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+class my_stringbuf : public std::wstringbuf
+{
+public:
+  my_stringbuf(const std::wstring& str, std::ios_base::openmode mode)
+  : std::wstringbuf(str, mode) { }
+
+  int_type 
+  pub_pbackfail(int_type c) 
+  { return this->pbackfail(c); }
+};
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  typedef my_stringbuf::int_type    int_type;
+  typedef my_stringbuf::traits_type traits_type;
+
+  my_stringbuf sbuf(L"any", ios_base::in | ios_base::out);
+  
+  int_type c = sbuf.sgetc();
+  VERIFY( c == L'a' );
+
+  c = sbuf.pub_pbackfail(L'z');
+  VERIFY( c == traits_type::eof() );
+  c = sbuf.sbumpc();
+  VERIFY( c == L'a' );
+  c = sbuf.pub_pbackfail(L'a');
+  VERIFY( c == L'a' );
+  c = sbuf.sbumpc();
+  VERIFY( c == L'a' );
+  
+  c = sbuf.pub_pbackfail(L'x');
+  VERIFY( c == L'x' );
+  c = sbuf.sbumpc();
+  VERIFY( c == L'x' );
+
+  const int_type eof = traits_type::eof();
+  c = sbuf.pub_pbackfail(eof);
+  VERIFY( c == traits_type::not_eof(eof) );
+  c = sbuf.sgetc();
+  VERIFY( c == L'x' );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc b/libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc
new file mode 100644 (file)
index 0000000..eb5172f
--- /dev/null
@@ -0,0 +1,64 @@
+// 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+class my_stringbuf : public std::wstringbuf
+{
+public:
+  my_stringbuf(const std::wstring& str, std::ios_base::openmode mode)
+  : std::wstringbuf(str, mode) { }
+
+  int_type 
+  pub_pbackfail(int_type __c) 
+  { return this->pbackfail(__c); }
+};
+
+// We weren't enforcing 27.7.1.3/2, bullet 2: "... and if
+// mode & ios_base::out is nonzero, ..."
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  typedef my_stringbuf::int_type    int_type;
+  typedef my_stringbuf::traits_type traits_type;
+
+  my_stringbuf sbuf(L"any", ios_base::in);
+  
+  int_type c = sbuf.sbumpc();
+  VERIFY( c == L'a' );
+
+  c = sbuf.pub_pbackfail(L'x');
+  VERIFY( c == traits_type::eof() );
+  VERIFY( sbuf.str() == L"any" );
+  c = sbuf.sgetc();
+  VERIFY( c == L'n' );
+}
+
+
+int main()
+{
+  test01();
+  return 0;
+}