brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 0888087 Raw
102 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// template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >12// class basic_stringstream13 14// explicit basic_stringstream(const basic_string<charT,traits,Allocator>& str,15//                             ios_base::openmode which = ios_base::out|ios_base::in);16 17#include <sstream>18#include <cassert>19 20#include "test_macros.h"21#include "operator_hijacker.h"22 23template<typename T>24struct NoDefaultAllocator : std::allocator<T>25{26  template<typename U> struct rebind { using other = NoDefaultAllocator<U>; };27  NoDefaultAllocator(int id_) : id(id_) { }28  template<typename U> NoDefaultAllocator(const NoDefaultAllocator<U>& a) : id(a.id) { }29  int id;30};31 32 33int main(int, char**)34{35    {36        std::stringstream ss(" 123 456 ");37        assert(ss.rdbuf() != nullptr);38        assert(ss.good());39        assert(ss.str() == " 123 456 ");40        int i = 0;41        ss >> i;42        assert(i == 123);43        ss >> i;44        assert(i == 456);45        ss << i << ' ' << 123;46        assert(ss.str() == "456 1236 ");47    }48    {49      std::basic_stringstream<char, std::char_traits<char>, operator_hijacker_allocator<char> > ss(" 123 456 ");50      assert(ss.rdbuf() != nullptr);51      assert(ss.good());52      assert(ss.str() == " 123 456 ");53      int i = 0;54      ss >> i;55      assert(i == 123);56      ss >> i;57      assert(i == 456);58      ss << i << ' ' << 123;59      assert(ss.str() == "456 1236 ");60    }61#ifndef TEST_HAS_NO_WIDE_CHARACTERS62    {63        std::wstringstream ss(L" 123 456 ");64        assert(ss.rdbuf() != nullptr);65        assert(ss.good());66        assert(ss.str() == L" 123 456 ");67        int i = 0;68        ss >> i;69        assert(i == 123);70        ss >> i;71        assert(i == 456);72        ss << i << ' ' << 123;73        assert(ss.str() == L"456 1236 ");74    }75    {76      std::basic_stringstream<wchar_t, std::char_traits<wchar_t>, operator_hijacker_allocator<wchar_t> > ss(77          L" 123 456 ");78      assert(ss.rdbuf() != nullptr);79      assert(ss.good());80      assert(ss.str() == L" 123 456 ");81      int i = 0;82      ss >> i;83      assert(i == 123);84      ss >> i;85      assert(i == 456);86      ss << i << ' ' << 123;87      assert(ss.str() == L"456 1236 ");88    }89#endif90    { // This is https://llvm.org/PR3372791        typedef std::basic_string   <char, std::char_traits<char>, NoDefaultAllocator<char> > S;92        typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB;93 94        S s(NoDefaultAllocator<char>(1));95        SB sb(s);96        // This test is not required by the standard, but *where else* could it get the allocator?97        assert(sb.str().get_allocator() == s.get_allocator());98    }99 100  return 0;101}102