From patchwork Wed Jun 10 19:32:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1307162 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Oyj8nnk6; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49hxws0RRmz9sRN for ; Thu, 11 Jun 2020 05:33:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 30CD1388F05C; Wed, 10 Jun 2020 19:33:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 30CD1388F05C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1591817590; bh=3Mc3aQ7Af1mvnNe8/AbAIR3WEnm1qyjp+0CzvUMci8A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Oyj8nnk63H0QTZfcRurRPyu5xxPcxtqdyb5hHtKMnE14ziTAUcgEe2F19dUuDpWu+ UZIUCjre3t2IALyVJKt4fKLj7n+7PWTZ9NoyY0WkEKSQzuZKT6x/klkr8X1g2CMfTV jyrpJvNc7Ue+2vZLmvjWj6qPyRhlq7Lr93G90S7o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id CF0533851C04 for ; Wed, 10 Jun 2020 19:33:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CF0533851C04 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-412-Mn-2VfsnMFq0npROCtdvBQ-1; Wed, 10 Jun 2020 15:32:59 -0400 X-MC-Unique: Mn-2VfsnMFq0npROCtdvBQ-1 Received: by mail-qk1-f199.google.com with SMTP id m29so3027183qkm.17 for ; Wed, 10 Jun 2020 12:32:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3Mc3aQ7Af1mvnNe8/AbAIR3WEnm1qyjp+0CzvUMci8A=; b=lejDPdGf9usa4e7aHwTEyTzsCdsmRZsTOIjlTBeKJN1FZIx1js73Jr6LjflQKzgLyE L3Ruz7wReRWp4I66wB/NpDx1R4w0FwFRufCEbbR8uhtRHBTUvzZIvOpCUuzov3JGgy9m Kg+5h8eM22HKTIONQM9QOyzRuxwB987HXGYeW4iR8+7o4JKwtX7ypyAxvfMwKOM0HW1t Tgmc9HhquqJIsHWqPpMkRfHg5yaQTXDmZ5krJXn2UM4sU665xEUUb0tFLnO35Jbez5ZC nQd09HvokRfg6Pd30TFOOZDVDU7FjIcXm4KeQ0aHOuCQGBYdWjP6RF+MJYgysohqfya5 N0jg== X-Gm-Message-State: AOAM5311jK8gZ86MgwGqjh+pyWXkua97eE0bPNFvT5Tm8HeAGRHhmAYS BLphyCfLn/6MBOscgiYhurSgA15D5OacC/lnt3tcZsp2k7O+FxcmCRcb/eh0c/QGSzbNHt48SZt WzXnXb/zzx8hhJkZogw== X-Received: by 2002:a05:620a:15e8:: with SMTP id p8mr4852615qkm.333.1591817577737; Wed, 10 Jun 2020 12:32:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1DBobBG9/zfNXN63gwV9sKzFyKVUyoqGDMEp5JgMwnTYcJ/cSmKNQogaG/t11+blAzlZw3g== X-Received: by 2002:a05:620a:15e8:: with SMTP id p8mr4852575qkm.333.1591817577222; Wed, 10 Jun 2020 12:32:57 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id g5sm769116qta.46.2020.06.10.12.32.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jun 2020 12:32:56 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Fix some ranges algos optimizations [PR95578] Date: Wed, 10 Jun 2020 15:32:54 -0400 Message-Id: <20200610193254.3901135-1-ppalka@redhat.com> X-Mailer: git-send-email 2.27.0.83.g0313f36c6e MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-18.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" ranges::copy and a number of other ranges algorithms have unwrapping optimizations for iterators of type __normal_iterator, move_iterator and reverse_iterator. But in the checks that guard these optimizations we currently only test that the iterator of the iterator/sentinel pair has the appropriate type before proceeding with the corresponding optimization, and we fail to also test the sentinel type. This breaks the testcase in this PR because this testcase constructs via range adaptors a range whose begin() is a __normal_iterator and whose end() is a custom sentinel type, and then does ranges::copy on it. From there we bogusly perform the __normal_iterator unwrapping optimization on this iterator/sentinel pair, which immediately leads to a constraint failure since the custom sentinel type does not model sentinel_for. This patch fixes this issue by augmenting each of the problematic checks to also test that the iterator and sentinel types are the same before applying the corresponding unwrapping optimization. Tested on x86_64-pc-linux-gnu, does this look OK to commit to master to and to the 10.2 branch? libstdc++-v3/ChangeLog: PR libstdc++/95578 * include/bits/ranges_algo.h (__lexicographical_compare_fn): Also check that the iterator and sentinel have the same type before applying the unwrapping optimization for __normal_iterator. Split the check into two, one for the first iterator/sentinel pair and another for second iterator/sentinel pair. Stop using __niter_base and stop doing std::move on a __normal_iterator. * include/bits/ranges_algobase.c (__equal_fn): Likewise. (__copy_or_move): Likewise. Perform similar adjustments for the reverse_iterator and move_iterator optimizations. Inline the checks into the if-constexprs, and use using-declarations to make them less visually noisy. Don't use __niter_wrap. (__copy_or_move_backward): Likewise. * testsuite/25_algorithms/copy/95578.cc: New test. * testsuite/25_algorithms/copy_backward/95578.cc: New test. * testsuite/25_algorithms/equal/95578.cc: New test. * testsuite/25_algorithms/lexicographical_compare/95578.cc: New test. * testsuite/25_algorithms/move/95578.cc: New test. * testsuite/25_algorithms/move_backward/95578.cc: New test. --- libstdc++-v3/include/bits/ranges_algo.h | 14 +-- libstdc++-v3/include/bits/ranges_algobase.h | 88 ++++++++++--------- .../testsuite/25_algorithms/copy/95578.cc | 74 ++++++++++++++++ .../25_algorithms/copy_backward/95578.cc | 62 +++++++++++++ .../testsuite/25_algorithms/equal/95578.cc | 74 ++++++++++++++++ .../lexicographical_compare/95578.cc | 74 ++++++++++++++++ .../testsuite/25_algorithms/move/95578.cc | 62 +++++++++++++ .../25_algorithms/move_backward/95578.cc | 62 +++++++++++++ 8 files changed, 465 insertions(+), 45 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/copy/95578.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/copy_backward/95578.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/equal/95578.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/95578.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/move/95578.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/move_backward/95578.cc diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index c038a505afa..94ca7b6488d 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -3452,11 +3452,15 @@ namespace ranges _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { if constexpr (__detail::__is_normal_iterator<_Iter1> - || __detail::__is_normal_iterator<_Iter2>) - return (*this)(std::__niter_base(std::move(__first1)), - std::__niter_base(std::move(__last1)), - std::__niter_base(std::move(__first2)), - std::__niter_base(std::move(__last2)), + && same_as<_Iter1, _Sent1>) + return (*this)(__first1.base(), __last1.base(), + std::move(__first2), std::move(__last2), + std::move(__comp), + std::move(__proj1), std::move(__proj2)); + else if constexpr (__detail::__is_normal_iterator<_Iter2> + && same_as<_Iter2, _Sent2>) + return (*this)(std::move(__first1), std::move(__last1), + __first2.base(), __last2.base(), std::move(__comp), std::move(__proj1), std::move(__proj2)); else diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index 49ca5ed4155..3bdc7cc471b 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -85,11 +85,15 @@ namespace ranges // TODO: implement more specializations to at least have parity with // std::equal. if constexpr (__detail::__is_normal_iterator<_Iter1> - || __detail::__is_normal_iterator<_Iter2>) - return (*this)(std::__niter_base(std::move(__first1)), - std::__niter_base(std::move(__last1)), - std::__niter_base(std::move(__first2)), - std::__niter_base(std::move(__last2)), + && same_as<_Iter1, _Sent1>) + return (*this)(__first1.base(), __last1.base(), + std::move(__first2), std::move(__last2), + std::move(__pred), + std::move(__proj1), std::move(__proj2)); + else if constexpr (__detail::__is_normal_iterator<_Iter2> + && same_as<_Iter2, _Sent2>) + return (*this)(std::move(__first1), std::move(__last1), + __first2.base(), __last2.base(), std::move(__pred), std::move(__proj1), std::move(__proj2)); else if constexpr (sized_sentinel_for<_Sent1, _Iter1> @@ -211,14 +215,10 @@ namespace ranges { // TODO: implement more specializations to be at least on par with // std::copy/std::move. - constexpr bool __normal_iterator_p - = (__detail::__is_normal_iterator<_Iter> - || __detail::__is_normal_iterator<_Out>); - constexpr bool __reverse_p - = (__detail::__is_reverse_iterator<_Iter> - && __detail::__is_reverse_iterator<_Out>); - constexpr bool __move_iterator_p = __detail::__is_move_iterator<_Iter>; - if constexpr (__move_iterator_p) + using __detail::__is_move_iterator; + using __detail::__is_reverse_iterator; + using __detail::__is_normal_iterator; + if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>) { auto [__in, __out] = ranges::__copy_or_move(std::move(__first).base(), @@ -226,23 +226,28 @@ namespace ranges std::move(__result)); return {move_iterator{std::move(__in)}, std::move(__out)}; } - else if constexpr (__reverse_p) + else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent> + && __is_reverse_iterator<_Out>) { auto [__in,__out] - = ranges::__copy_or_move_backward<_IsMove>(__last.base(), - __first.base(), - __result.base()); + = ranges::__copy_or_move_backward<_IsMove>(std::move(__last).base(), + std::move(__first).base(), + std::move(__result).base()); return {reverse_iterator{std::move(__in)}, reverse_iterator{std::move(__out)}}; } - else if constexpr (__normal_iterator_p) + else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>) + { + auto [__in,__out] + = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(), + __result); + return {decltype(__first){__in}, std::move(__out)}; + } + else if constexpr (__is_normal_iterator<_Out>) { auto [__in,__out] - = ranges::__copy_or_move<_IsMove>(std::__niter_base(__first), - std::__niter_base(__last), - std::__niter_base(__result)); - return {std::__niter_wrap(__first, std::move(__in)), - std::__niter_wrap(__result, std::move(__out))}; + = ranges::__copy_or_move<_IsMove>(__first, __last, __result.base()); + return {std::move(__in), decltype(__result){__out}}; } else if constexpr (sized_sentinel_for<_Sent, _Iter>) { @@ -353,30 +358,33 @@ namespace ranges { // TODO: implement more specializations to be at least on par with // std::copy_backward/std::move_backward. - constexpr bool __normal_iterator_p - = (__detail::__is_normal_iterator<_Iter> - || __detail::__is_normal_iterator<_Out>); - constexpr bool __reverse_p - = (__detail::__is_reverse_iterator<_Iter> - && __detail::__is_reverse_iterator<_Out>); - if constexpr (__reverse_p) + using __detail::__is_reverse_iterator; + using __detail::__is_normal_iterator; + if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent> + && __is_reverse_iterator<_Out>) { auto [__in,__out] - = ranges::__copy_or_move<_IsMove>(__last.base(), - __first.base(), - __result.base()); + = ranges::__copy_or_move<_IsMove>(std::move(__last).base(), + std::move(__first).base(), + std::move(__result).base()); return {reverse_iterator{std::move(__in)}, reverse_iterator{std::move(__out)}}; } - else if constexpr (__normal_iterator_p) + else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>) { auto [__in,__out] - = ranges::__copy_or_move_backward<_IsMove> - (std::__niter_base(__first), - std::__niter_base(__last), - std::__niter_base(__result)); - return {std::__niter_wrap(__first, std::move(__in)), - std::__niter_wrap(__result, std::move(__out))}; + = ranges::__copy_or_move_backward<_IsMove>(__first.base(), + __last.base(), + std::move(__result)); + return {decltype(__first){__in}, std::move(__out)}; + } + else if constexpr (__is_normal_iterator<_Out>) + { + auto [__in,__out] + = ranges::__copy_or_move_backward<_IsMove>(std::move(__first), + std::move(__last), + __result.base()); + return {std::move(__in), decltype(__result){__out}}; } else if constexpr (sized_sentinel_for<_Sent, _Iter>) { diff --git a/libstdc++-v3/testsuite/25_algorithms/copy/95578.cc b/libstdc++-v3/testsuite/25_algorithms/copy/95578.cc new file mode 100644 index 00000000000..7aaefba454c --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy/95578.cc @@ -0,0 +1,74 @@ +// Copyright (C) 2020 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-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template +struct sentinel +{ + It it; + + friend bool + operator==(It x, sentinel y) + { return x == y.it; } +}; + +void +test01() +{ + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr = {v.begin(), sentinel{v.end()}}; + ranges::copy(sr, w.begin()); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +void +test02() +{ + using std::reverse_iterator; + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr + = {reverse_iterator{v.end()}, sentinel{reverse_iterator{v.begin()}}}; + ranges::copy(sr, reverse_iterator{w.end()}); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +void +test03() +{ + using std::move_iterator; + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr + = {move_iterator{v.begin()}, sentinel{move_iterator{v.end()}}}; + ranges::copy(sr, w.begin()); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_backward/95578.cc b/libstdc++-v3/testsuite/25_algorithms/copy_backward/95578.cc new file mode 100644 index 00000000000..6e3d1b62401 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_backward/95578.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2020 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-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template +struct sentinel +{ + It it; + + friend bool + operator==(It x, sentinel y) + { return x == y.it; } +}; + +void +test01() +{ + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr = {v.begin(), sentinel{v.end()}}; + ranges::copy_backward(sr, w.end()); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +void +test02() +{ + using std::reverse_iterator; + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr + = {reverse_iterator{v.end()}, sentinel{reverse_iterator{v.begin()}}}; + ranges::copy_backward(sr, reverse_iterator{w.begin()}); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +int +main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/equal/95578.cc b/libstdc++-v3/testsuite/25_algorithms/equal/95578.cc new file mode 100644 index 00000000000..b69d877e4e3 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/equal/95578.cc @@ -0,0 +1,74 @@ +// Copyright (C) 2020 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-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template +struct sentinel +{ + It it; + + friend bool + operator==(It x, sentinel y) + { return x == y.it; } +}; + +void +test01() +{ + std::vector v = {1,2,3,4,5}; + ranges::subrange sr = {v.begin(), sentinel{v.end()}}; + VERIFY( ranges::equal(sr, v) ); + VERIFY( ranges::equal(v, sr) ); +} + +void +test02() +{ + using std::reverse_iterator; + std::vector v = {1,2,3,4,5}; + ranges::subrange sr + = {reverse_iterator{v.end()}, sentinel{reverse_iterator{v.begin()}}}; + VERIFY( ranges::equal(sr, (int[]){5,4,3,2,1}) ); + VERIFY( ranges::equal((int[]){5,4,3,2,1}, sr) ); +} + +void +test03() +{ + using std::move_iterator; + std::vector v = {1,2,3,4,5}; + ranges::subrange sr + = {move_iterator{v.begin()}, sentinel{move_iterator{v.end()}}}; + VERIFY( ranges::equal(sr, v) ); + VERIFY( ranges::equal(v, sr) ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/95578.cc b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/95578.cc new file mode 100644 index 00000000000..693506826bc --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/95578.cc @@ -0,0 +1,74 @@ +// Copyright (C) 2020 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-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template +struct sentinel +{ + It it; + + friend bool + operator==(It x, sentinel y) + { return x == y.it; } +}; + +void +test01() +{ + std::vector v = {1,2,3,4,5}; + ranges::subrange sr = {v.begin(), sentinel{v.end()}}; + VERIFY( ranges::lexicographical_compare(sr, (int[]){1,2,3,5,5}) ); + VERIFY( !ranges::lexicographical_compare((int[]){1,2,3,5,5}, sr) ); +} + +void +test02() +{ + using std::reverse_iterator; + std::vector v = {1,2,3,4,5}; + ranges::subrange sr + = {reverse_iterator{v.end()}, sentinel{reverse_iterator{v.begin()}}}; + VERIFY( ranges::lexicographical_compare(sr, (int[]){5,4,3,2,2}) ); + VERIFY( !ranges::lexicographical_compare((int[]){5,4,3,2,2}, sr) ); +} + +void +test03() +{ + using std::move_iterator; + std::vector v = {1,2,3,4,5}; + ranges::subrange sr + = {move_iterator{v.begin()}, sentinel{move_iterator{v.end()}}}; + VERIFY( ranges::lexicographical_compare(sr, (int[]){1,2,3,5,5}) ); + VERIFY( !ranges::lexicographical_compare((int[]){1,2,3,5,5}, sr) ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/move/95578.cc b/libstdc++-v3/testsuite/25_algorithms/move/95578.cc new file mode 100644 index 00000000000..e5c494d55d2 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/move/95578.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2020 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-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template +struct sentinel +{ + It it; + + friend bool + operator==(It x, sentinel y) + { return x == y.it; } +}; + +void +test01() +{ + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr = {v.begin(), sentinel{v.end()}}; + ranges::move(sr, w.begin()); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +void +test02() +{ + using std::reverse_iterator; + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr + = {reverse_iterator{v.end()}, sentinel{reverse_iterator{v.begin()}}}; + ranges::move(sr, reverse_iterator{w.end()}); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +int +main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/move_backward/95578.cc b/libstdc++-v3/testsuite/25_algorithms/move_backward/95578.cc new file mode 100644 index 00000000000..694e4a75c83 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/move_backward/95578.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2020 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-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template +struct sentinel +{ + It it; + + friend bool + operator==(It x, sentinel y) + { return x == y.it; } +}; + +void +test01() +{ + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr = {v.begin(), sentinel{v.end()}}; + ranges::move_backward(sr, w.end()); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +void +test02() +{ + using std::reverse_iterator; + std::vector v = {1,2,3,4,5}, w = {0,0,0,0,0}; + ranges::subrange sr + = {reverse_iterator{v.end()}, sentinel{reverse_iterator{v.begin()}}}; + ranges::move_backward(sr, reverse_iterator{w.begin()}); + VERIFY( ranges::equal(w, (int[]){1,2,3,4,5}) ); +} + +int +main() +{ + test01(); + test02(); +}