76 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// <sstream>10 11// How the constructors of basic_stringbuf initialize the buffer pointers is12// not specified. For some constructors it's implementation defined whether the13// pointers are set to nullptr. Libc++'s implementation directly uses the SSO14// buffer of a std::string as the initial size. This test validates that15// behaviour.16//17// This behaviour is allowed by LWG2995.18 19#include <sstream>20#include <cassert>21 22#include "test_macros.h"23 24template <class CharT>25struct test_buf : public std::basic_stringbuf<CharT> {26 typedef std::basic_streambuf<CharT> base;27 typedef typename base::char_type char_type;28 typedef typename base::int_type int_type;29 typedef typename base::traits_type traits_type;30 31 char_type* pbase() const { return base::pbase(); }32 char_type* pptr() const { return base::pptr(); }33 char_type* epptr() const { return base::epptr(); }34 void gbump(int n) { base::gbump(n); }35 36 virtual int_type overflow(int_type c = traits_type::eof()) { return base::overflow(c); }37 38 test_buf() = default;39 explicit test_buf(std::ios_base::openmode which) : std::basic_stringbuf<CharT>(which) {}40 41 explicit test_buf(const std::basic_string<CharT>& s) : std::basic_stringbuf<CharT>(s) {}42};43 44template <class CharT>45static void test() {46 std::size_t size = std::basic_string<CharT>().capacity(); // SSO buffer size.47 {48 test_buf<CharT> b;49 assert(b.pbase() != nullptr);50 assert(b.pptr() == b.pbase());51 assert(b.epptr() == b.pbase() + size);52 }53 {54 test_buf<CharT> b(std::ios_base::out);55 assert(b.pbase() != nullptr);56 assert(b.pptr() == b.pbase());57 assert(b.epptr() == b.pbase() + size);58 }59 {60 std::basic_string<CharT> s;61 s.reserve(1024);62 test_buf<CharT> b(s);63 assert(b.pbase() != nullptr);64 assert(b.pptr() == b.pbase());65 assert(b.epptr() == b.pbase() + size); // copy so uses size66 }67}68 69int main(int, char**) {70 test<char>();71#ifndef TEST_HAS_NO_WIDE_CHARACTERS72 test<wchar_t>();73#endif74 return 0;75}76