// Copyright (C) 2020-2025 Free Software Foundation, Inc. // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING3. If not see // . // { dg-do run { target c++20 } } #include #include #include #include #include #include #include #include #include #include #include using __gnu_test::test_forward_range; namespace ranges = std::ranges; template void test01(const T& value) { static_assert(std::equality_comparable); for (int k = 0; k < 6; k++) { constexpr int size = 1024; auto buffer = std::unique_ptr(new char[sizeof(T)*size]); std::span rx((T *)buffer.get(), size); auto i = rx.begin(); if (k == 0) i = ranges::uninitialized_fill(rx.begin(), rx.end(), value); else if (k == 1) i = ranges::uninitialized_fill(rx, value); else if (k == 2) i = ranges::uninitialized_fill_n(rx.begin(), 1024, value); else if (k == 3) i = ranges::uninitialized_fill(rx.begin(), rx.end(), value); else if (k == 4) i = ranges::uninitialized_fill(std::as_const(rx), value); else if (k == 5) i = ranges::uninitialized_fill_n(rx.begin(), 1024, value); else __builtin_abort(); VERIFY( i == rx.end() ); VERIFY( ranges::find_if(rx, [&value](const T& v) { return value != v; }) == i ); ranges::destroy(rx); } } struct X { static constexpr int limit = 67; static inline int construct_count = 0; static inline int destruct_count = 0; struct exception {}; bool live = false; X(int) { if (construct_count >= limit) throw exception{}; construct_count++; live = true; } ~X() { VERIFY( live ); live = false; destruct_count++; } }; template void test02() { constexpr int size = 100; auto buffer = std::unique_ptr(new char[sizeof(X)*size]); test_forward_range rx((X *)buffer.get(), (X *)buffer.get() + size); int value = 5; try { X::construct_count = 0; X::destruct_count = 0; if constexpr (test_sized) ranges::uninitialized_fill_n(rx.begin(), size, value); else ranges::uninitialized_fill(rx, value); VERIFY( false && "exception not thrown" ); } catch (const X::exception&) { VERIFY( X::construct_count == X::limit ); VERIFY( X::destruct_count == X::limit ); } } int main() { test01(5); test01(3); test01(17); test01(2.18); test01(3.98); test01>({'a', 'b', 'c', 'd'}); test01("hello"); test01>({1.1,2.1,3.1}); test01>>>({{{3.4},{1}},{{7.9}}}); test02(); test02(); }