📄 istream_unformatted.cc
字号:
VERIFY( is_05.gcount() == 0 ); VERIFY( static_cast<bool>(is_05.rdstate() & stateeof) ); VERIFY( static_cast<bool>(is_05.rdstate() & statefail) ); #ifdef DEBUG_ASSERT assert(test);#endif return 0;}// http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00177.htmlinttest04(){ bool test = true; const std::string str_00("Red_Garland_Qunitet-Soul_Junction"); std::string strtmp; char c_array[str_00.size() + 4]; std::stringbuf isbuf_00(str_00, std::ios_base::in); std::istream is_00(&isbuf_00); std::ios_base::iostate state1, state2, statefail, stateeof; statefail = std::ios_base::failbit; stateeof = std::ios_base::eofbit; state1 = stateeof | statefail; VERIFY( is_00.gcount() == 0 ); is_00.read(c_array, str_00.size() + 1); VERIFY( is_00.gcount() == str_00.size() ); VERIFY( is_00.rdstate() == state1 ); is_00.read(c_array, str_00.size()); VERIFY( is_00.rdstate() == state1 );#ifdef DEBUG_ASSERT assert(test);#endif return 0;}// http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.htmlinttest05(){ const char* charray = "\n""a\n""aa\n""aaa\n""aaaa\n""aaaaa\n""aaaaaa\n""aaaaaaa\n""aaaaaaaa\n""aaaaaaaaa\n""aaaaaaaaaa\n""aaaaaaaaaaa\n""aaaaaaaaaaaa\n""aaaaaaaaaaaaa\n""aaaaaaaaaaaaaa\n"; bool test = true; const std::streamsize it = 5; std::streamsize br = 0; char tmp[it]; std::stringbuf sb(charray, std::ios_base::in); std::istream ifs(&sb); std::streamsize blen = std::strlen(charray); VERIFY(!(!ifs)); while(ifs.getline(tmp, it) || ifs.gcount()) { br += ifs.gcount(); if(ifs.eof()) { // Just sanity checks to make sure we've extracted the same // number of chars that were in the streambuf VERIFY(br == blen); // Also, we should only set the failbit if we could // _extract_ no chars from the stream, i.e. the first read // returned EOF. VERIFY(ifs.fail() && ifs.gcount() == 0); } else if(ifs.fail()) { // delimiter not read // // either // -> extracted no characters // or // -> n - 1 characters are stored ifs.clear(ifs.rdstate() & ~std::ios::failbit); VERIFY((ifs.gcount() == 0) || (std::strlen(tmp) == it - 1)); VERIFY(!(!ifs)); continue; } else { // delimiter was read. // // -> strlen(__s) < n - 1 // -> delimiter was seen -> gcount() > strlen(__s) VERIFY(ifs.gcount() == std::strlen(tmp) + 1); continue; } } return 0;}// http://gcc.gnu.org/ml/libstdc++/2000-07/msg00126.htmlinttest06(){ using namespace std; bool test = true; const streamsize it = 5; char tmp[it]; const char* str_lit = "abcd\n"; stringbuf strbuf(str_lit, std::ios_base::in); istream istr(&strbuf); istr.getline(tmp,it); VERIFY( istr.gcount() == it ); // extracted whole string VERIFY( strlen(tmp) == 4 ); // stored all but '\n' VERIFY( !istr.eof() ); // extracted up to but not eof VERIFY( !istr.fail() ); // failbit not set char c = 'z'; istr.get(c); VERIFY( c == 'z' ); VERIFY( istr.eof() );#ifdef DEBUG_ASSERT assert(test);#endif return 0;}// bug reported by bgarcia@laurelnetworks.com// http://gcc.gnu.org/ml/libstdc++-prs/2000-q3/msg00041.htmlvoidtest07(){ bool test = true; const char* tfn = "istream_unformatted-1.txt"; std::ifstream infile; infile.open(tfn); VERIFY( !(!infile) ); while (infile) { std::string line; std::ostringstream line_ss; while (infile.peek() == '\n') infile.get(); infile.get(*(line_ss.rdbuf())); line = line_ss.str(); VERIFY( line == "1234567890" || line == "" ); }}// 2002-04-19 PR libstdc++ 6360voidtest08(){ using namespace std; bool test = true; stringstream ss("abcd" "\xFF" "1234ina donna coolbrith"); char c; ss >> c; VERIFY( c == 'a' ); ss.ignore(8); ss >> c; VERIFY( c == 'i' );} // Theodore Papadopoulo void test09(){ using namespace std; bool test = true; istringstream iss("Juana Briones"); char tab[13]; iss.read(tab, 13); if (!iss) test = false; VERIFY( test );}// libstdc++/70220voidtest10(){ using namespace std; bool test = true; typedef string string_type; typedef stringbuf stringbuf_type; typedef istream istream_type; int res = 0; streamsize n; string_type input("abcdefg\n"); stringbuf_type sbuf(input); istream_type istr(&sbuf); istr.ignore(0); if (istr.gcount() != 0) test = false; VERIFY( test ); istr.ignore(0, 'b'); if (istr.gcount() != 0) test = false; VERIFY( test ); istr.ignore(); // Advance to next position. istr.ignore(0, 'b'); if ((n=istr.gcount()) != 0) test = false; VERIFY( test ); if (istr.peek() != 'b') test = false; VERIFY( test );}// libstdc++/8258class mybuf : public std::basic_streambuf<char> { };void test11(){ bool test = true; using namespace std; char arr[10]; mybuf sbuf; basic_istream<char, char_traits<char> > istr(&sbuf); VERIFY(istr.rdstate() == ios_base::goodbit); VERIFY(istr.readsome(arr, 10) == 0); VERIFY(istr.rdstate() == ios_base::goodbit);}// libstdc++/6746 void test12(){ using namespace std; bool test = true; streamsize sum = 0; istringstream iss("shamma shamma"); // test01 size_t i = iss.rdbuf()->in_avail(); VERIFY( i != 0 ); // test02 streamsize extracted; do { char buf[1024]; extracted = iss.readsome(buf, sizeof buf); sum += extracted; } while (iss.good() && extracted); VERIFY( sum != 0 ); } // libstdc++/6746 void test13(){ using namespace std; bool test = true; streamsize sum = 0; ifstream ifs("istream_unformatted-1.tst"); // test01 size_t i = ifs.rdbuf()->in_avail(); VERIFY( i != 0 ); // test02 streamsize extracted; do { char buf[1024]; extracted = ifs.readsome(buf, sizeof buf); sum += extracted; } while (ifs.good() && extracted); VERIFY( sum != 0 ); } int main(){ test01(); test02(); test03(); test04(); test05(); test06(); test07(); test08(); test09(); test10(); test11(); test12(); test13(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -