| 1 | // Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++14 } } |
| 19 | |
| 20 | #include <experimental/buffer> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | namespace net = std::experimental::net; |
| 24 | |
| 25 | template<typename T> |
| 26 | bool is_mutable(const T&) |
| 27 | { return std::is_same<T, net::mutable_buffer>::value; } |
| 28 | |
| 29 | template<typename T> |
| 30 | bool is_const(const T&) |
| 31 | { return std::is_same<T, net::const_buffer>::value; } |
| 32 | |
| 33 | void |
| 34 | test01() |
| 35 | { |
| 36 | bool test = false; |
| 37 | |
| 38 | auto b1 = net::buffer((void*)&test, sizeof(test)); |
| 39 | VERIFY( is_mutable(b1) ); |
| 40 | VERIFY( b1.data() == &test ); |
| 41 | VERIFY( b1.size() == sizeof(test) ); |
| 42 | |
| 43 | auto b2 = net::buffer((const void*)&test, sizeof(test)); |
| 44 | VERIFY( is_const(b2) ); |
| 45 | VERIFY( b2.data() == &test ); |
| 46 | VERIFY( b1.size() == sizeof(test) ); |
| 47 | |
| 48 | auto b3 = net::buffer(b1); |
| 49 | VERIFY( is_mutable(b3) ); |
| 50 | VERIFY( b3.data() == b1.data() ); |
| 51 | VERIFY( b3.size() == b1.size() ); |
| 52 | |
| 53 | auto b4 = net::buffer(b2); |
| 54 | VERIFY( is_const(b4) ); |
| 55 | VERIFY( b4.data() == b2.data() ); |
| 56 | VERIFY( b4.size() == b2.size() ); |
| 57 | |
| 58 | auto b5 = net::buffer(b1, 0); |
| 59 | VERIFY( is_mutable(b5) ); |
| 60 | VERIFY( b5.data() == b1.data() ); |
| 61 | VERIFY( b5.size() == 0 ); |
| 62 | |
| 63 | auto b6 = net::buffer(b2, 0); |
| 64 | VERIFY( is_const(b6) ); |
| 65 | VERIFY( b6.data() == b2.data() ); |
| 66 | VERIFY( b6.size() == 0 ); |
| 67 | |
| 68 | int a7[7]; |
| 69 | auto b7 = net::buffer(a7); |
| 70 | VERIFY( is_mutable(b7) ); |
| 71 | VERIFY( b7.data() == a7 ); |
| 72 | VERIFY( b7.size() == sizeof(a7) ); |
| 73 | |
| 74 | auto b7x = net::buffer(a7, 2); |
| 75 | VERIFY( is_mutable(b7x) ); |
| 76 | VERIFY( b7x.data() == a7 ); |
| 77 | VERIFY( b7x.size() == sizeof(a7[0]) * 2 ); |
| 78 | |
| 79 | const short a8[8] = { }; |
| 80 | auto b8 = net::buffer(a8); |
| 81 | VERIFY( is_const(b8) ); |
| 82 | VERIFY( b8.data() == a8 ); |
| 83 | VERIFY( b8.size() == sizeof(a8) ); |
| 84 | |
| 85 | auto b8x = net::buffer(a8, 3); |
| 86 | VERIFY( is_const(b8x) ); |
| 87 | VERIFY( b8x.data() == a8 ); |
| 88 | VERIFY( b8x.size() == sizeof(a8[0]) * 3 ); |
| 89 | |
| 90 | std::array<short, 9> a9; |
| 91 | auto b9 = net::buffer(a9); |
| 92 | VERIFY( is_mutable(b9) ); |
| 93 | VERIFY( b9.data() == a9.data() ); |
| 94 | VERIFY( b9.size() == sizeof(a9) ); |
| 95 | |
| 96 | auto b9x = net::buffer(a9, 4); |
| 97 | VERIFY( is_mutable(b9x) ); |
| 98 | VERIFY( b9x.data() == a9.data() ); |
| 99 | VERIFY( b9x.size() == sizeof(a9[0]) * 4 ); |
| 100 | |
| 101 | const std::array<long long, 10> a10{}; |
| 102 | auto b10 = net::buffer(a10); |
| 103 | VERIFY( is_const(b10) ); |
| 104 | VERIFY( b10.data() == a10.data() ); |
| 105 | VERIFY( b10.size() == sizeof(a10) ); |
| 106 | |
| 107 | auto b10x = net::buffer(a10, 5); |
| 108 | VERIFY( is_const(b10x) ); |
| 109 | VERIFY( b10x.data() == a10.data() ); |
| 110 | VERIFY( b10x.size() == sizeof(a10[0]) * 5 ); |
| 111 | |
| 112 | std::array<const int, 11> a11{}; |
| 113 | auto b11 = net::buffer(a11); |
| 114 | VERIFY( is_const(b11) ); |
| 115 | VERIFY( b11.data() == a11.data() ); |
| 116 | VERIFY( b11.size() == sizeof(a11) ); |
| 117 | |
| 118 | auto b11x = net::buffer(a11, 6); |
| 119 | VERIFY( is_const(b11x) ); |
| 120 | VERIFY( b11x.data() == a11.data() ); |
| 121 | VERIFY( b11x.size() == sizeof(a11[0]) * 6 ); |
| 122 | |
| 123 | std::vector<short> a12(12); |
| 124 | auto b12 = net::buffer(a12); |
| 125 | VERIFY( is_mutable(b12) ); |
| 126 | VERIFY( b12.data() == a12.data() ); |
| 127 | VERIFY( b12.size() == sizeof(a12[0]) * a12.size() ); |
| 128 | |
| 129 | auto b12x = net::buffer(a12, 7); |
| 130 | VERIFY( is_mutable(b12x) ); |
| 131 | VERIFY( b12x.data() == a12.data() ); |
| 132 | VERIFY( b12x.size() == sizeof(a12[0]) * 7 ); |
| 133 | |
| 134 | const std::vector<long long> a13(13); |
| 135 | auto b13 = net::buffer(a13); |
| 136 | VERIFY( is_const(b13) ); |
| 137 | VERIFY( b13.data() == a13.data() ); |
| 138 | VERIFY( b13.size() == sizeof(a13[0]) * a13.size() ); |
| 139 | |
| 140 | auto b13x = net::buffer(a13, 7); |
| 141 | VERIFY( is_const(b13x) ); |
| 142 | VERIFY( b13x.data() == a13.data() ); |
| 143 | VERIFY( b13x.size() == sizeof(a13[0]) * 7 ); |
| 144 | |
| 145 | std::u32string a14(14, ' '); |
| 146 | auto b14 = net::buffer(a14); |
| 147 | VERIFY( is_mutable(b14) ); |
| 148 | VERIFY( b14.data() == a14.data() ); |
| 149 | VERIFY( b14.size() == sizeof(a14[0]) * a14.size() ); |
| 150 | |
| 151 | auto b14x = net::buffer(a14, 8); |
| 152 | VERIFY( is_mutable(b14x) ); |
| 153 | VERIFY( b14x.data() == a14.data() ); |
| 154 | VERIFY( b14x.size() == sizeof(a14[0]) * 8 ); |
| 155 | |
| 156 | const std::u16string a15(15, ' '); |
| 157 | auto b15 = net::buffer(std::experimental::u16string_view(a15)); |
| 158 | VERIFY( is_const(b15) ); |
| 159 | VERIFY( b15.data() == a15.data() ); |
| 160 | VERIFY( b15.size() == sizeof(a15[0]) * a15.size() ); |
| 161 | |
| 162 | auto b15x = net::buffer(std::experimental::u16string_view(a15), 9); |
| 163 | VERIFY( is_const(b15x) ); |
| 164 | VERIFY( b15x.data() == a15.data() ); |
| 165 | VERIFY( b15x.size() == sizeof(a15[0]) * 9 ); |
| 166 | } |
| 167 | |
| 168 | int |
| 169 | main() |
| 170 | { |
| 171 | test01(); |
| 172 | } |