summaryrefslogtreecommitdiff
path: root/libcxx/test/std/strings/basic.string/string.nonmembers/string.io/get_line.pass.cpp
blob: 28b1f9b67f28d08f32d9915f8d111025d6610c75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <string>

// template<class charT, class traits, class Allocator>
//   basic_istream<charT,traits>&
//   getline(basic_istream<charT,traits>& is,
//           basic_string<charT,traits,Allocator>& str);

#include <cassert>
#include <sstream>
#include <string>

#include "make_string.h"
#include "min_allocator.h"
#include "stream_types.h"
#include "test_macros.h"

template <class CharT, class Alloc, class Stream, class Streambuf>
void test() {
  using string_type    = std::basic_string<CharT, std::char_traits<CharT>, Alloc>;
  using stream_type    = std::basic_istream<CharT>;
  using streambuf_type = Streambuf;

  {
    streambuf_type sb(MAKE_CSTRING(CharT, " abc\n  def\n   ghij"));
    stream_type in(&sb);
    string_type s(MAKE_CSTRING(CharT, "initial text"));
    std::getline(in, s);
    assert(in.good());
    assert(s == MAKE_CSTRING(CharT, " abc"));
    std::getline(in, s);
    assert(in.good());
    assert(s == MAKE_CSTRING(CharT, "  def"));
    std::getline(in, s);
    assert(in.eof());
    assert(s == MAKE_CSTRING(CharT, "   ghij"));
  }
#ifndef TEST_HAS_NO_EXCEPTIONS
  {
    streambuf_type sb(MAKE_CSTRING(CharT, "hello"));
    stream_type is(&sb);
    is.exceptions(std::ios_base::eofbit);

    string_type s;
    bool threw = false;
    try {
      std::getline(is, s);
    } catch (std::ios::failure const&) {
      threw = true;
    }

    assert(!is.bad());
    assert(!is.fail());
    assert(is.eof());
    assert(threw);
    assert(s == MAKE_CSTRING(CharT, "hello"));
  }
  {
    streambuf_type sb(MAKE_CSTRING(CharT, ""));
    stream_type is(&sb);
    is.exceptions(std::ios_base::failbit);

    string_type s;
    bool threw = false;
    try {
      std::getline(is, s);
    } catch (std::ios::failure const&) {
      threw = true;
    }

    assert(!is.bad());
    assert(is.fail());
    assert(is.eof());
    assert(threw);
    assert(s == MAKE_CSTRING(CharT, ""));
  }
#endif // TEST_HAS_NO_EXCEPTIONS
}

template <template <class> class Alloc>
void test_alloc() {
  test<char, Alloc<char>, std::basic_istringstream<char>, std::basic_stringbuf<char> >();
  test<char, Alloc<char>, std::basic_istringstream<char>, non_buffering_streambuf<char> >();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  test<wchar_t, Alloc<wchar_t>, std::basic_istringstream<wchar_t>, std::basic_stringbuf<wchar_t> >();
  test<wchar_t, Alloc<wchar_t>, std::basic_istringstream<wchar_t>, non_buffering_streambuf<wchar_t> >();
#endif
}

void test_tiny_allocator() {
  {
    std::string in_str =
        "this is a too long line for the string that has to be longer because the implementation is broken\n";
    std::istringstream iss(in_str);
    std::basic_string<char, std::char_traits<char>, tiny_size_allocator<40, char> > str;
    std::getline(iss, str);
    assert(iss.rdstate() & std::ios::failbit);
    assert(str == in_str.substr(0, str.max_size()).c_str());
  }
  {
    std::string in_str =
        "this is a too long line for the string that has to be longer because the implementation is broken";
    std::istringstream iss(in_str);
    std::basic_string<char, std::char_traits<char>, tiny_size_allocator<40, char> > str;
    std::getline(iss, str);
    assert(iss.rdstate() & std::ios::failbit);
    assert(str == in_str.substr(0, str.max_size()).c_str());
  }
}

int main(int, char**) {
  test_alloc<std::allocator>();
#if TEST_STD_VER >= 11
  test_alloc<min_allocator>();
#endif
  test_tiny_allocator();

  return 0;
}