156 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, charT delim);15 16#include <string>17#include <sstream>18#include <cassert>19 20#include "min_allocator.h"21#include "test_macros.h"22 23template <template <class> class Alloc>24void test_string() {25 using S = std::basic_string<char, std::char_traits<char>, Alloc<char> >;26#ifndef TEST_HAS_NO_WIDE_CHARACTERS27 using WS = std::basic_string<wchar_t, std::char_traits<wchar_t>, Alloc<wchar_t> >;28#endif29 30 {31 std::istringstream in(" abc* def** ghij");32 S s("initial text");33 std::getline(in, s, '*');34 assert(in.good());35 assert(s == " abc");36 std::getline(in, s, '*');37 assert(in.good());38 assert(s == " def");39 std::getline(in, s, '*');40 assert(in.good());41 assert(s == "");42 std::getline(in, s, '*');43 assert(in.eof());44 assert(s == " ghij");45 }46#ifndef TEST_HAS_NO_WIDE_CHARACTERS47 {48 std::wistringstream in(L" abc* def** ghij");49 WS s(L"initial text");50 std::getline(in, s, L'*');51 assert(in.good());52 assert(s == L" abc");53 std::getline(in, s, L'*');54 assert(in.good());55 assert(s == L" def");56 std::getline(in, s, L'*');57 assert(in.good());58 assert(s == L"");59 std::getline(in, s, L'*');60 assert(in.eof());61 assert(s == L" ghij");62 }63#endif64 65#ifndef TEST_HAS_NO_EXCEPTIONS66 {67 std::basic_stringbuf<char> sb("hello");68 std::basic_istream<char> is(&sb);69 is.exceptions(std::ios::eofbit);70 71 S s;72 bool threw = false;73 try {74 std::getline(is, s, '\n');75 } catch (std::ios::failure const&) {76 threw = true;77 }78 79 assert(!is.bad());80 assert(!is.fail());81 assert(is.eof());82 assert(threw);83 assert(s == "hello");84 }85# ifndef TEST_HAS_NO_WIDE_CHARACTERS86 {87 std::basic_stringbuf<wchar_t> sb(L"hello");88 std::basic_istream<wchar_t> is(&sb);89 is.exceptions(std::ios::eofbit);90 91 WS s;92 bool threw = false;93 try {94 std::getline(is, s, L'\n');95 } catch (std::ios::failure const&) {96 threw = true;97 }98 99 assert(!is.bad());100 assert(!is.fail());101 assert(is.eof());102 assert(threw);103 assert(s == L"hello");104 }105# endif // TEST_HAS_NO_WIDE_CHARACTERS106 {107 std::basic_stringbuf<char> sb;108 std::basic_istream<char> is(&sb);109 is.exceptions(std::ios::failbit);110 111 S s;112 bool threw = false;113 try {114 std::getline(is, s, '\n');115 } catch (std::ios::failure const&) {116 threw = true;117 }118 119 assert(!is.bad());120 assert(is.fail());121 assert(is.eof());122 assert(threw);123 assert(s == "");124 }125# ifndef TEST_HAS_NO_WIDE_CHARACTERS126 {127 std::basic_stringbuf<wchar_t> sb;128 std::basic_istream<wchar_t> is(&sb);129 is.exceptions(std::ios::failbit);130 131 WS s;132 bool threw = false;133 try {134 std::getline(is, s, L'\n');135 } catch (std::ios::failure const&) {136 threw = true;137 }138 139 assert(!is.bad());140 assert(is.fail());141 assert(is.eof());142 assert(threw);143 assert(s == L"");144 }145# endif // TEST_HAS_NO_WIDE_CHARACTERS146#endif // TEST_HAS_NO_EXCEPTIONS147}148 149int main(int, char**) {150 test_string<std::allocator>();151#if TEST_STD_VER >= 11152 test_string<min_allocator>();153#endif154 return 0;155}156