|
| 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 | +// FILE_DEPENDENCIES: xsgetn.test.dat |
| 10 | + |
| 11 | +// <fstream> |
| 12 | + |
| 13 | +// template <class charT, class traits = char_traits<charT> > |
| 14 | +// class basic_ifstream |
| 15 | + |
| 16 | +// streamsize xsgetn(char_type*, streamsize) override; |
| 17 | + |
| 18 | +// This isn't a required override by the standard, but most implementations |
| 19 | +// override it, since it allows for significantly improved performance in some |
| 20 | +// cases. All of this code is required to work, so this isn't a libc++ extension |
| 21 | + |
| 22 | +#include <cassert> |
| 23 | +#include <fstream> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "test_macros.h" |
| 27 | + |
| 28 | +int main(int, char**) { |
| 29 | + std::vector<char> stream_buffer(10); |
| 30 | + std::ifstream fs("xsgetn.test.dat"); |
| 31 | + std::filebuf* fb = fs.rdbuf(); |
| 32 | + fb->pubsetbuf(stream_buffer.data(), 10); |
| 33 | + |
| 34 | + // Ensure that the buffer is set up |
| 35 | + assert(fb->sgetc() == 't'); |
| 36 | + |
| 37 | + std::vector<char> test_buffer(5); |
| 38 | + test_buffer[0] = '\0'; |
| 39 | + |
| 40 | + { // Check that a read smaller than the buffer works fine |
| 41 | + assert(fb->sgetn(test_buffer.data(), 5) == 5); |
| 42 | + assert(std::string(test_buffer.data(), 5) == "this "); |
| 43 | + } |
| 44 | + { // Check that reading up to the buffer end works fine |
| 45 | + assert(fb->sgetn(test_buffer.data(), 5) == 5); |
| 46 | + assert(std::string(test_buffer.data(), 5) == "is so"); |
| 47 | + } |
| 48 | + { // Check that reading from an empty buffer, but more than the buffer can |
| 49 | + // hold works fine |
| 50 | + test_buffer.resize(12); |
| 51 | + assert(fb->sgetn(test_buffer.data(), 12) == 12); |
| 52 | + assert(std::string(test_buffer.data(), 12) == "me random da"); |
| 53 | + } |
| 54 | + { // Check that reading from a non-empty buffer, and more than the buffer can |
| 55 | + // hold works fine Fill the buffer up |
| 56 | + test_buffer.resize(2); |
| 57 | + assert(fb->sgetn(test_buffer.data(), 2) == 2); |
| 58 | + assert(std::string(test_buffer.data(), 2) == "ta"); |
| 59 | + |
| 60 | + // Do the actual check |
| 61 | + test_buffer.resize(12); |
| 62 | + assert(fb->sgetn(test_buffer.data(), 12) == 12); |
| 63 | + assert(std::string(test_buffer.data(), 12) == " to be able "); |
| 64 | + } |
| 65 | + { // Check that trying to read more than the file size works fine |
| 66 | + test_buffer.resize(30); |
| 67 | + assert(fb->sgetn(test_buffer.data(), 30) == 24); |
| 68 | + test_buffer.resize(24); |
| 69 | + assert(std::string(test_buffer.data(), 24) == "to test buffer behaviour"); |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
0 commit comments