brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.1 KiB · 79d20ce Raw
213 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// <istream>10 11// int sync();12 13// The fix for bug 51497 and bug 51499 require and updated dylib due to14// explicit instantiations. That means Apple backdeployment targets remain15// broken.16// XFAIL: using-built-library-before-llvm-1917 18#include <istream>19#include <cassert>20#include <streambuf>21 22#include "test_macros.h"23 24int sync_called = 0;25 26template <class CharT>27struct testbuf28    : public std::basic_streambuf<CharT>29{30    typedef std::basic_string<CharT> string_type;31    typedef std::basic_streambuf<CharT> base;32private:33    string_type str_;34public:35 36    testbuf() {}37    testbuf(const string_type& str)38        : str_(str)39    {40        base::setg(const_cast<CharT*>(str_.data()),41                   const_cast<CharT*>(str_.data()),42                   const_cast<CharT*>(str_.data()) + str_.size());43    }44 45    CharT* eback() const {return base::eback();}46    CharT* gptr() const {return base::gptr();}47    CharT* egptr() const {return base::egptr();}48 49protected:50    int sync()51    {52        ++sync_called;53        return 5;54    }55};56 57template <class CharT>58struct testbuf_pubsync_error59    : public std::basic_streambuf<CharT>60{61public:62 63    testbuf_pubsync_error() {}64protected:65    virtual int sync() { return -1; }66};67 68 69#ifndef TEST_HAS_NO_EXCEPTIONS70struct testbuf_exception { };71 72template <class CharT>73struct throwing_testbuf74    : public std::basic_streambuf<CharT>75{76    typedef std::basic_string<CharT> string_type;77    typedef std::basic_streambuf<CharT> base;78private:79    string_type str_;80public:81 82    throwing_testbuf() {}83    throwing_testbuf(const string_type& str)84        : str_(str)85    {86        base::setg(const_cast<CharT*>(str_.data()),87                   const_cast<CharT*>(str_.data()),88                   const_cast<CharT*>(str_.data()) + str_.size());89    }90 91    CharT* eback() const {return base::eback();}92    CharT* gptr() const {return base::gptr();}93    CharT* egptr() const {return base::egptr();}94 95protected:96    virtual int sync()97    {98        throw testbuf_exception();99        return 5;100    }101};102#endif // TEST_HAS_NO_EXCEPTIONS103 104int main(int, char**)105{106    {107        std::istream is(nullptr);108        assert(is.sync() == -1);109    }110    {111        testbuf<char> sb(" 123456789");112        std::istream is(&sb);113        assert(is.sync() == 0);114        assert(sync_called == 1);115    }116    {117        testbuf_pubsync_error<char> sb;118        std::istream is(&sb);119        is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);120        assert(is.sync() == -1);121        assert( is.bad());122        assert(!is.eof());123        assert( is.fail());124    }125#ifndef TEST_HAS_NO_WIDE_CHARACTERS126    {127        std::wistream is(nullptr);128        assert(is.sync() == -1);129    }130    {131        testbuf<wchar_t> sb(L" 123456789");132        std::wistream is(&sb);133        assert(is.sync() == 0);134        assert(sync_called == 2);135    }136    {137        testbuf_pubsync_error<wchar_t> sb;138        std::wistream is(&sb);139        is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);140        assert(is.sync() == -1);141        assert( is.bad());142        assert(!is.eof());143        assert( is.fail());144    }145#endif146#ifndef TEST_HAS_NO_EXCEPTIONS147    {148        testbuf_pubsync_error<char> sb;149        std::istream is(&sb);150        is.exceptions(std::ios_base::badbit);151        bool threw = false;152        try {153            is.sync();154        } catch (std::ios_base::failure const&) {155            threw = true;156        }157        assert( is.bad());158        assert(!is.eof());159        assert( is.fail());160        assert(threw);161    }162    {163        throwing_testbuf<char> sb(" 123456789");164        std::basic_istream<char> is(&sb);165        is.exceptions(std::ios_base::badbit);166        bool threw = false;167        try {168            is.sync();169        } catch (testbuf_exception const&) {170            threw = true;171        }172        assert( is.bad());173        assert(!is.eof());174        assert( is.fail());175        assert(threw);176    }177#ifndef TEST_HAS_NO_WIDE_CHARACTERS178    {179        testbuf_pubsync_error<wchar_t> sb;180        std::wistream is(&sb);181        is.exceptions(std::ios_base::badbit);182        bool threw = false;183        try {184            is.sync();185        } catch (std::ios_base::failure const&) {186            threw = true;187        }188        assert( is.bad());189        assert(!is.eof());190        assert( is.fail());191        assert(threw);192    }193    {194        throwing_testbuf<wchar_t> sb(L" 123456789");195        std::basic_istream<wchar_t> is(&sb);196        is.exceptions(std::ios_base::badbit);197        bool threw = false;198        try {199            is.sync();200        } catch (testbuf_exception const&) {201            threw = true;202        }203        assert( is.bad());204        assert(!is.eof());205        assert( is.fail());206        assert(threw);207    }208#endif // TEST_HAS_NO_WIDE_CHARACTERS209#endif // TEST_HAS_NO_EXCEPTIONS210 211    return 0;212}213