brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · de17cd4 Raw
74 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// FILE_DEPENDENCIES: xsgetn.test.dat10 11// <fstream>12 13// template <class charT, class traits = char_traits<charT> >14// class basic_ifstream15 16// streamsize xsgetn(char_type*, streamsize) override;17 18// This isn't a required override by the standard, but most implementations19// override it, since it allows for significantly improved performance in some20// cases. All of this code is required to work, so this isn't a libc++ extension21 22#include <cassert>23#include <fstream>24#include <vector>25 26#include "test_macros.h"27 28int 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 up35  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 fine41    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 fine45    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 can49    // hold works fine50    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 can55    // hold works fine Fill the buffer up56    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 check61    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 fine66    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}74