| 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://llvm.org/PR14074. 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 | // UNSUPPORTED: no-filesystem |
| 21 | |
| 22 | #include <cassert> |
| 23 | #include <cstddef> |
| 24 | #include <cstdio> |
| 25 | #include <fstream> |
| 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | #include "test_macros.h" |
| 29 | #include "platform_support.h" |
| 30 | |
| 31 | |
| 32 | // Count the number of bytes in a file -- make sure to use only functionality |
| 33 | // provided by the C library to avoid relying on the C++ library, which we're |
| 34 | // trying to test. |
| 35 | static std::size_t count_bytes(char const* filename) { |
| 36 | std::FILE* f = std::fopen(filename: filename, modes: "rb" ); |
| 37 | std::size_t count = 0; |
| 38 | while (std::fgetc(stream: f) != EOF) |
| 39 | ++count; |
| 40 | std::fclose(stream: f); |
| 41 | return count; |
| 42 | } |
| 43 | |
| 44 | int main(int, char**) { |
| 45 | { |
| 46 | // with basic_stringbuf |
| 47 | std::basic_stringbuf<char> buf; |
| 48 | std::streamsize sz = buf.sputn(s: "\xFF" , n: 1); |
| 49 | assert(sz == 1); |
| 50 | assert(buf.str().size() == 1); |
| 51 | } |
| 52 | { |
| 53 | // with basic_filebuf |
| 54 | std::string temp = get_temp_file_name(); |
| 55 | { |
| 56 | std::basic_filebuf<char> buf; |
| 57 | buf.open(s: temp.c_str(), mode: std::ios_base::out); |
| 58 | std::streamsize sz = buf.sputn(s: "\xFF" , n: 1); |
| 59 | assert(sz == 1); |
| 60 | } |
| 61 | assert(count_bytes(temp.c_str()) == 1); |
| 62 | std::remove(filename: temp.c_str()); |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |