OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 27_io / basic_istream / getline / wchar_t / 2.cc
1 // Copyright (C) 2004 Free Software Foundation
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING.  If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
18
19 // 27.6.1.3 unformatted input functions
20
21 #include <cwchar> // for wcslen
22 #include <istream>
23 #include <sstream>
24 #include <testsuite_hooks.h>
25
26 // [patch] bits/istream.tcc - getline(char_type*,streamsize,char_type)
27 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.html
28 void
29 test05()
30 {
31   const wchar_t* charray = L"\n"
32 L"a\n"
33 L"aa\n"
34 L"aaa\n"
35 L"aaaa\n"
36 L"aaaaa\n"
37 L"aaaaaa\n"
38 L"aaaaaaa\n"
39 L"aaaaaaaa\n"
40 L"aaaaaaaaa\n"
41 L"aaaaaaaaaa\n"
42 L"aaaaaaaaaaa\n"
43 L"aaaaaaaaaaaa\n"
44 L"aaaaaaaaaaaaa\n"
45 L"aaaaaaaaaaaaaa\n";
46
47   bool test __attribute__((unused)) = true;
48   const std::streamsize it = 5;
49   std::streamsize br = 0;
50   wchar_t tmp[it];
51   std::wstringbuf sb(charray, std::ios_base::in);
52   std::wistream ifs(&sb);
53   std::streamsize blen = std::wcslen(charray);
54   VERIFY(!(!ifs));
55   while(ifs.getline(tmp, it) || ifs.gcount())
56     {
57       br += ifs.gcount();
58       if(ifs.eof())
59         {
60           // Just sanity checks to make sure we've extracted the same
61           // number of chars that were in the streambuf
62           VERIFY( br == blen );
63           // Also, we should only set the failbit if we could
64           // _extract_ no chars from the stream, i.e. the first read
65           // returned EOF. 
66           VERIFY( ifs.fail() && ifs.gcount() == 0 );
67         }
68       else if(ifs.fail())
69         {
70           // delimiter not read
71           //
72           // either
73           // -> extracted no characters
74           // or
75           // -> n - 1 characters are stored
76           ifs.clear(ifs.rdstate() & ~std::ios::failbit);
77           VERIFY( (ifs.gcount() == 0) || (std::wcslen(tmp) == it - 1) );
78           VERIFY( !(!ifs) );
79           continue;
80         }
81       else 
82         {
83           // delimiter was read.
84           //
85           // -> wcslen(__s) < n - 1 
86           // -> delimiter was seen -> gcount() > wcslen(__s)
87           VERIFY( ifs.gcount() == static_cast<std::streamsize>(std::wcslen(tmp)
88                                                                + 1) );
89           continue;
90         }
91     }
92 }
93
94 int 
95 main()
96 {
97   test05();
98   return 0;
99 }