Debian Codegraph @

libstdc++-v3/testsuite/27_io/basic_istream/ignore/char/93672.cc

raw
1 // { dg-do run }
2
3 #include <sstream>
4 #include <limits>
5 #include <testsuite_hooks.h>
6
7 void
8 test_pr93672() // std::basic_istream::ignore hangs if delim MSB is set
9 {
10 std::istringstream in(".\xfc..\xfd...\xfe.");
11
12 // This should find '\xfd' even on platforms where char is signed,
13 // because the delimiter is correctly converted to the stream's int_type.
14 in.ignore(100, std::char_traits<char>::to_int_type('\xfc'));
15 VERIFY( in.gcount() == 2 );
16 VERIFY( ! in.eof() );
17
18 // This should work equivalently to traits_type::to_int_type
19 in.ignore(100, (unsigned char)'\xfd');
20 VERIFY( in.gcount() == 3 );
21 VERIFY( ! in.eof() );
22
23 // This only works if char is unsigned.
24 in.ignore(100, '\xfe');
25 if (std::numeric_limits<char>::is_signed)
26 {
27 // When char is signed, '\xfe' != traits_type::to_int_type('\xfe')
28 // so the delimiter does not match the character in the input sequence,
29 // and ignore consumes all input until EOF.
30 VERIFY( in.gcount() == 5 );
31 VERIFY( in.eof() );
32 }
33 else
34 {
35 // When char is unsigned, '\xfe' == to_int_type('\xfe') so the delimiter
36 // matches the character in the input sequence, and doesn't reach EOF.
37 VERIFY( in.gcount() == 4 );
38 VERIFY( ! in.eof() );
39 }
40
41 in.clear();
42 in.str(".a.");
43 in.ignore(100, 'a' + 256); // Should not match 'a'
44 VERIFY( in.gcount() == 3 );
45 VERIFY( in.eof() );
46 }
47
48 // Custom traits type that inherits all behaviour from std::char_traits<char>.
49 struct traits : std::char_traits<char> { };
50
51 void
52 test_primary_template()
53 {
54 // Check that the primary template for std::basic_istream::ignore
55 // works the same as the std::istream::ignore specialization.
56 // The infinite loop bug was never present in the primary template,
57 // because it doesn't use traits_type::find to search the input sequence.
58
59 std::basic_istringstream<char, traits> in(".\xfc..\xfd...\xfe.");
60
61 // This should find '\xfd' even on platforms where char is signed,
62 // because the delimiter is correctly converted to the stream's int_type.
63 in.ignore(100, std::char_traits<char>::to_int_type('\xfc'));
64 VERIFY( in.gcount() == 2 );
65 VERIFY( ! in.eof() );
66
67 // This should work equivalently to traits_type::to_int_type
68 in.ignore(100, (unsigned char)'\xfd');
69 VERIFY( in.gcount() == 3 );
70 VERIFY( ! in.eof() );
71
72 // This only works if char is unsigned.
73 in.ignore(100, '\xfe');
74 if (std::numeric_limits<char>::is_signed)
75 {
76 // When char is signed, '\xfe' != traits_type::to_int_type('\xfe')
77 // so the delimiter does not match the character in the input sequence,
78 // and ignore consumes all input until EOF.
79 VERIFY( in.gcount() == 5 );
80 VERIFY( in.eof() );
81 }
82 else
83 {
84 // When char is unsigned, '\xfe' == to_int_type('\xfe') so the delimiter
85 // matches the character in the input sequence, and doesn't reach EOF.
86 VERIFY( in.gcount() == 4 );
87 VERIFY( ! in.eof() );
88 }
89
90 in.clear();
91 in.str(".a.");
92 in.ignore(100, 'a' + 256); // Should not match 'a'
93 VERIFY( in.gcount() == 3 );
94 VERIFY( in.eof() );
95 }
96
97 int main()
98 {
99 test_pr93672();
100 test_primary_template();
101 }