brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · c98a889 Raw
141 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// UNSUPPORTED: c++03, c++11, c++14, c++1710// UNSUPPORTED: no-localization11// UNSUPPORTED: libcpp-has-no-experimental-syncstream12 13// <syncstream>14 15// template <class charT, class traits, class Allocator>16// class basic_osyncstream;17 18// explicit basic_osyncstream(streambuf_type* os);19 20#include <cassert>21#include <concepts>22#include <syncstream>23#include <sstream>24 25#include "test_macros.h"26#include "constexpr_char_traits.h"27#include "test_allocator.h"28 29template <class CharT>30void test() {31  {32    using OS = std::basic_osyncstream<CharT>;33    using W  = std::basic_stringbuf<CharT>;34 35    static_assert(!std::convertible_to<std::basic_syncbuf<CharT>*, OS>);36    static_assert(std::constructible_from<OS, std::basic_syncbuf<CharT>*>);37 38    {39      OS os{nullptr};40      assert(os.get_wrapped() == nullptr);41      assert(os.rdbuf()->get_wrapped() == nullptr);42      assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());43    }44    {45      W w;46#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)47      assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);48#endif49      {50        OS os{&w};51#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)52        assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);53#endif54        assert(os.get_wrapped() == &w);55        assert(os.rdbuf()->get_wrapped() == &w);56        assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());57      }58#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)59      assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);60#endif61    }62  }63 64  {65    using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>>;66    using W  = std::basic_stringbuf<CharT, constexpr_char_traits<CharT>>;67 68    static_assert(!std::convertible_to<std::basic_syncbuf<CharT, constexpr_char_traits<CharT>>*, OS>);69    static_assert(std::constructible_from<OS, std::basic_syncbuf<CharT, constexpr_char_traits<CharT>>*>);70 71    {72      OS os{nullptr};73      assert(os.get_wrapped() == nullptr);74      assert(os.rdbuf()->get_wrapped() == nullptr);75      assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());76    }77    {78      W w;79#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)80      assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);81#endif82      {83        OS os{&w};84#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)85        assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);86#endif87        assert(os.get_wrapped() == &w);88        assert(os.rdbuf()->get_wrapped() == &w);89        assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());90      }91#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)92      assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);93#endif94    }95  }96 97  {98    using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;99    using W  = std::basic_stringbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;100 101    static_assert(102        !std::convertible_to<std::basic_syncbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>*, OS>);103    static_assert(104        std::constructible_from<OS, std::basic_syncbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>*>);105 106    {107      OS os{nullptr};108      assert(os.get_wrapped() == nullptr);109      assert(os.rdbuf()->get_wrapped() == nullptr);110      assert(os.rdbuf()->get_allocator() == test_allocator<CharT>());111    }112    {113      W w;114#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)115      assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);116#endif117      {118        OS os{&w};119#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)120        assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);121#endif122        assert(os.get_wrapped() == &w);123        assert(os.rdbuf()->get_wrapped() == &w);124        assert(os.rdbuf()->get_allocator() == test_allocator<CharT>());125      }126#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)127      assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);128#endif129    }130  }131}132 133int main(int, char**) {134  test<char>();135#ifndef TEST_HAS_NO_WIDE_CHARACTERS136  test<wchar_t>();137#endif138 139  return 0;140}141