|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | + |
| 11 | +// <spanstream> |
| 12 | + |
| 13 | +// template<class charT, class traits = char_traits<charT>> |
| 14 | +// class basic_ispanstream |
| 15 | +// : public basic_istream<charT, traits> { |
| 16 | + |
| 17 | +// // [spanbuf.cons], constructors |
| 18 | +// |
| 19 | +// basic_ispanstream& operator=(basic_ispanstream&& rhs); |
| 20 | + |
| 21 | +#include <cassert> |
| 22 | +#include <concepts> |
| 23 | +#include <span> |
| 24 | +#include <spanstream> |
| 25 | + |
| 26 | +#include "constexpr_char_traits.h" |
| 27 | +#include "nasty_string.h" |
| 28 | +#include "test_macros.h" |
| 29 | + |
| 30 | +template <typename CharT, typename TraitsT = std::char_traits<CharT>> |
| 31 | +void test() { |
| 32 | + using SpStream = std::basic_ispanstream<CharT, TraitsT>; |
| 33 | + |
| 34 | + CharT arr[4]; |
| 35 | + |
| 36 | + std::span<CharT> sp{arr}; |
| 37 | + assert(sp.data() == arr); |
| 38 | + assert(!sp.empty()); |
| 39 | + assert(sp.size() == 4); |
| 40 | + |
| 41 | + // Mode: default (`in` | `out`) |
| 42 | + { |
| 43 | + SpStream rhsSpSt{sp}; |
| 44 | + assert(rhsSpSt.span().data() == arr); |
| 45 | + assert(!rhsSpSt.span().empty()); |
| 46 | + assert(rhsSpSt.span().size() == 4); |
| 47 | + |
| 48 | + SpStream spSt = std::move(rhsSpSt); |
| 49 | + assert(spSt.span().data() == arr); |
| 50 | + assert(!spSt.span().empty()); |
| 51 | + assert(spSt.span().size() == 4); |
| 52 | + |
| 53 | + // After move |
| 54 | + assert(rhsSpSt.span().data() == arr); |
| 55 | + assert(!rhsSpSt.span().empty()); |
| 56 | + assert(rhsSpSt.span().size() == 4); |
| 57 | + } |
| 58 | + // Mode: `in` |
| 59 | + { |
| 60 | + SpStream rhsSpSt{sp, std::ios_base::in}; |
| 61 | + assert(rhsSpSt.span().data() == arr); |
| 62 | + assert(!rhsSpSt.span().empty()); |
| 63 | + assert(rhsSpSt.span().size() == 4); |
| 64 | + |
| 65 | + SpStream spSt = std::move(rhsSpSt); |
| 66 | + assert(spSt.span().data() == arr); |
| 67 | + assert(!spSt.span().empty()); |
| 68 | + assert(spSt.span().size() == 4); |
| 69 | + |
| 70 | + // After move |
| 71 | + assert(rhsSpSt.span().data() == arr); |
| 72 | + assert(!rhsSpSt.span().empty()); |
| 73 | + assert(rhsSpSt.span().size() == 4); |
| 74 | + } |
| 75 | + // Mode `out` |
| 76 | + { |
| 77 | + SpStream rhsSpSt{sp, std::ios_base::out}; |
| 78 | + assert(rhsSpSt.span().data() == arr); |
| 79 | + assert(rhsSpSt.span().empty()); |
| 80 | + assert(rhsSpSt.span().size() == 0); |
| 81 | + |
| 82 | + SpStream spSt = std::move(rhsSpSt); |
| 83 | + assert(spSt.span().data() == arr); |
| 84 | + assert(spSt.span().empty()); |
| 85 | + assert(spSt.span().size() == 0); |
| 86 | + |
| 87 | + // After move |
| 88 | + assert(rhsSpSt.span().data() == arr); |
| 89 | + assert(rhsSpSt.span().empty()); |
| 90 | + assert(rhsSpSt.span().size() == 0); |
| 91 | + } |
| 92 | + // Mode: multiple |
| 93 | + { |
| 94 | + SpStream rhsSpSt{sp, std::ios_base::in | std::ios_base::out | std::ios_base::binary}; |
| 95 | + assert(rhsSpSt.span().data() == arr); |
| 96 | + assert(rhsSpSt.span().empty()); |
| 97 | + assert(rhsSpSt.span().size() == 0); |
| 98 | + |
| 99 | + SpStream spSt = std::move(rhsSpSt); |
| 100 | + assert(spSt.span().data() == arr); |
| 101 | + assert(spSt.span().empty()); |
| 102 | + assert(spSt.span().size() == 0); |
| 103 | + |
| 104 | + // After move |
| 105 | + assert(rhsSpSt.span().data() == arr); |
| 106 | + assert(rhsSpSt.span().empty()); |
| 107 | + assert(rhsSpSt.span().size() == 0); |
| 108 | + } |
| 109 | + // Mode `ate` |
| 110 | + { |
| 111 | + SpStream rhsSpSt{sp, std::ios_base::out | std::ios_base::ate}; |
| 112 | + assert(rhsSpSt.span().data() == arr); |
| 113 | + assert(!rhsSpSt.span().empty()); |
| 114 | + assert(rhsSpSt.span().size() == 4); |
| 115 | + |
| 116 | + SpStream spSt = std::move(rhsSpSt); |
| 117 | + assert(spSt.span().data() == arr); |
| 118 | + assert(!spSt.span().empty()); |
| 119 | + assert(spSt.span().size() == 4); |
| 120 | + |
| 121 | + // After move |
| 122 | + assert(rhsSpSt.span().data() == arr); |
| 123 | + assert(!rhsSpSt.span().empty()); |
| 124 | + assert(rhsSpSt.span().size() == 4); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +int main(int, char**) { |
| 129 | +#ifndef TEST_HAS_NO_NASTY_STRING |
| 130 | + test<nasty_char, nasty_char_traits>(); |
| 131 | +#endif |
| 132 | + test<char>(); |
| 133 | + test<char, constexpr_char_traits<char>>(); |
| 134 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 135 | + test<wchar_t>(); |
| 136 | + test<wchar_t, constexpr_char_traits<wchar_t>>(); |
| 137 | +#endif |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
0 commit comments