127 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <string>10 11// template<class charT, class traits, class Allocator>12// basic_istream<charT,traits>&13// getline(basic_istream<charT,traits>& is,14// basic_string<charT,traits,Allocator>& str);15 16#include <cassert>17#include <sstream>18#include <string>19 20#include "make_string.h"21#include "min_allocator.h"22#include "stream_types.h"23#include "test_macros.h"24 25template <class CharT, class Alloc, class Stream, class Streambuf>26void test() {27 using string_type = std::basic_string<CharT, std::char_traits<CharT>, Alloc>;28 using stream_type = std::basic_istream<CharT>;29 using streambuf_type = Streambuf;30 31 {32 streambuf_type sb(MAKE_CSTRING(CharT, " abc\n def\n ghij"));33 stream_type in(&sb);34 string_type s(MAKE_CSTRING(CharT, "initial text"));35 std::getline(in, s);36 assert(in.good());37 assert(s == MAKE_CSTRING(CharT, " abc"));38 std::getline(in, s);39 assert(in.good());40 assert(s == MAKE_CSTRING(CharT, " def"));41 std::getline(in, s);42 assert(in.eof());43 assert(s == MAKE_CSTRING(CharT, " ghij"));44 }45#ifndef TEST_HAS_NO_EXCEPTIONS46 {47 streambuf_type sb(MAKE_CSTRING(CharT, "hello"));48 stream_type is(&sb);49 is.exceptions(std::ios_base::eofbit);50 51 string_type s;52 bool threw = false;53 try {54 std::getline(is, s);55 } catch (std::ios::failure const&) {56 threw = true;57 }58 59 assert(!is.bad());60 assert(!is.fail());61 assert(is.eof());62 assert(threw);63 assert(s == MAKE_CSTRING(CharT, "hello"));64 }65 {66 streambuf_type sb(MAKE_CSTRING(CharT, ""));67 stream_type is(&sb);68 is.exceptions(std::ios_base::failbit);69 70 string_type s;71 bool threw = false;72 try {73 std::getline(is, s);74 } catch (std::ios::failure const&) {75 threw = true;76 }77 78 assert(!is.bad());79 assert(is.fail());80 assert(is.eof());81 assert(threw);82 assert(s == MAKE_CSTRING(CharT, ""));83 }84#endif // TEST_HAS_NO_EXCEPTIONS85}86 87template <template <class> class Alloc>88void test_alloc() {89 test<char, Alloc<char>, std::basic_istringstream<char>, std::basic_stringbuf<char> >();90 test<char, Alloc<char>, std::basic_istringstream<char>, non_buffering_streambuf<char> >();91#ifndef TEST_HAS_NO_WIDE_CHARACTERS92 test<wchar_t, Alloc<wchar_t>, std::basic_istringstream<wchar_t>, std::basic_stringbuf<wchar_t> >();93 test<wchar_t, Alloc<wchar_t>, std::basic_istringstream<wchar_t>, non_buffering_streambuf<wchar_t> >();94#endif95}96 97void test_tiny_allocator() {98 {99 std::string in_str =100 "this is a too long line for the string that has to be longer because the implementation is broken\n";101 std::istringstream iss(in_str);102 std::basic_string<char, std::char_traits<char>, tiny_size_allocator<40, char> > str;103 std::getline(iss, str);104 assert(iss.rdstate() & std::ios::failbit);105 assert(str == in_str.substr(0, str.max_size()).c_str());106 }107 {108 std::string in_str =109 "this is a too long line for the string that has to be longer because the implementation is broken";110 std::istringstream iss(in_str);111 std::basic_string<char, std::char_traits<char>, tiny_size_allocator<40, char> > str;112 std::getline(iss, str);113 assert(iss.rdstate() & std::ios::failbit);114 assert(str == in_str.substr(0, str.max_size()).c_str());115 }116}117 118int main(int, char**) {119 test_alloc<std::allocator>();120#if TEST_STD_VER >= 11121 test_alloc<min_allocator>();122#endif123 test_tiny_allocator();124 125 return 0;126}127