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-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <streambuf>
10
11 // template <class charT, class traits = char_traits<charT> >
12 // class basic_streambuf;
13
14 // streamsize xsputn(const char_type* s, streamsize n);
15
16 // Test https://bugs.llvm.org/show_bug.cgi?id=14074. The bug is really inside
17 // basic_streambuf, but I can't seem to reproduce without going through one
18 // of its derived classes.
19
20 #include <cassert>
21 #include <cstddef>
22 #include <cstdio>
23 #include <fstream>
24 #include <sstream>
25 #include <string>
26 #include "test_macros.h"
27 #include "platform_support.h"
28
29
30 // Count the number of bytes in a file -- make sure to use only functionality
31 // provided by the C library to avoid relying on the C++ library, which we're
32 // trying to test.
count_bytes(char const * filename)33 static std::size_t count_bytes(char const* filename) {
34 std::FILE* f = std::fopen(filename, "rb");
35 std::size_t count = 0;
36 while (std::fgetc(f) != EOF)
37 ++count;
38 std::fclose(f);
39 return count;
40 }
41
main(int,char **)42 int main(int, char**) {
43 {
44 // with basic_stringbuf
45 std::basic_stringbuf<char> buf;
46 std::streamsize sz = buf.sputn("\xFF", 1);
47 assert(sz == 1);
48 assert(buf.str().size() == 1);
49 }
50 {
51 // with basic_filebuf
52 std::string temp = get_temp_file_name();
53 {
54 std::basic_filebuf<char> buf;
55 buf.open(temp.c_str(), std::ios_base::out);
56 std::streamsize sz = buf.sputn("\xFF", 1);
57 assert(sz == 1);
58 }
59 assert(count_bytes(temp.c_str()) == 1);
60 std::remove(temp.c_str());
61 }
62
63 return 0;
64 }
65