| 1 | // Copyright (C) 2019-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++11 } } |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <sstream> |
| 22 | #include <iterator> |
| 23 | |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void test01() |
| 27 | { |
| 28 | std::stringstream ss("12345"); |
| 29 | |
| 30 | std::string ostr(5, '0'); |
| 31 | typedef std::istreambuf_iterator<char> istrb_ite; |
| 32 | auto res = std::copy_n(istrb_ite(ss), 0, ostr.begin()); |
| 33 | VERIFY( res == ostr.begin() ); |
| 34 | VERIFY( ostr.front() == '0' ); |
| 35 | |
| 36 | res = std::copy_n(istrb_ite(ss), 2, ostr.begin()); |
| 37 | VERIFY( res == ostr.begin() + 2 ); |
| 38 | VERIFY( ostr == "12000" ); |
| 39 | |
| 40 | res = std::copy_n(istrb_ite(ss), 3, ostr.begin() + 2); |
| 41 | VERIFY( res == ostr.begin() + 5 ); |
| 42 | VERIFY( ostr == "12345" ); |
| 43 | } |
| 44 | |
| 45 | void test02() |
| 46 | { |
| 47 | std::stringstream ss("12345"); |
| 48 | |
| 49 | std::string ostr(5, '0'); |
| 50 | typedef std::istreambuf_iterator<char> istrb_ite; |
| 51 | |
| 52 | istrb_ite ibfit(ss); |
| 53 | auto res = std::copy_n(ibfit, 3, std::copy_n(ibfit, 2, ostr.begin())); |
| 54 | VERIFY( res == ostr.begin() + 5 ); |
| 55 | VERIFY( ostr == "12345" ); |
| 56 | } |
| 57 | |
| 58 | void test03() |
| 59 | { |
| 60 | std::string ostr(5, '0'); |
| 61 | typedef std::istreambuf_iterator<char> istrb_ite; |
| 62 | |
| 63 | auto res = std::copy_n(istrb_ite(), 0, ostr.begin()); |
| 64 | VERIFY( res == ostr.begin() ); |
| 65 | } |
| 66 | |
| 67 | int main() |
| 68 | { |
| 69 | test01(); |
| 70 | test02(); |
| 71 | test03(); |
| 72 | return 0; |
| 73 | } |