Commit 0ff15d21 by Jonathan Wakely Committed by Jonathan Wakely

libsupc++: Implement comparison algorithms for C++20

This is incomplete because std::strong_order doesn't support
floating-point types.

The partial_order and weak_order tests use VERIFY instead of
static_assert because of PR 92431.

	* libsupc++/compare (strong_order, weak_order, partial_order)
	(compare_strong_order_fallback, compare_weak_order_fallback)
	(compare_partial_order_fallback): Define customization point objects
	for C++20.
	* testsuite/18_support/comparisons/algorithms/partial_order.cc: New
	test.
	* testsuite/18_support/comparisons/algorithms/strong_order.cc: New
	test.
	* testsuite/18_support/comparisons/algorithms/weak_order.cc: New test.

From-SVN: r278149
parent 5d462877
2019-11-13 Jonathan Wakely <jwakely@redhat.com>
* libsupc++/compare (strong_order, weak_order, partial_order)
(compare_strong_order_fallback, compare_weak_order_fallback)
(compare_partial_order_fallback): Define customization point objects
for C++20.
* testsuite/18_support/comparisons/algorithms/partial_order.cc: New
test.
* testsuite/18_support/comparisons/algorithms/strong_order.cc: New
test.
* testsuite/18_support/comparisons/algorithms/weak_order.cc: New test.
2019-11-11 Gerald Pfeifer <gerald@pfeifer.com>
* doc/xml/gnu/gpl-3.0.xml: Adjust link to "Why not LGPL".
......
// Copyright (C) 2019 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }
#include <compare>
#include <limits>
#include <testsuite_hooks.h>
using std::partial_order;
using std::partial_ordering;
void
test01()
{
int one = 1, two = 2;
VERIFY( partial_order(one, two) == partial_ordering::less );
VERIFY( partial_order(one, one) == partial_ordering::equivalent );
VERIFY( partial_order(two, one) == partial_ordering::greater );
static_assert( noexcept(partial_order(1, 1)) );
}
constexpr partial_ordering different_cv_quals(int i, const int j)
{
return partial_order(i, j);
}
void
test02()
{
int fortytwo = 42, nines = 999, lots = 1000;
VERIFY( different_cv_quals(fortytwo, nines) == partial_ordering::less );
VERIFY( different_cv_quals(-nines, -nines) == partial_ordering::equivalent );
VERIFY( different_cv_quals(-nines, -lots) == partial_ordering::greater );
}
void
test03()
{
double zero = 0.0;
VERIFY( partial_order(zero, zero) == partial_ordering::equivalent );
VERIFY( partial_order(-zero, -zero) == partial_ordering::equivalent );
VERIFY( partial_order(-zero, zero) == partial_ordering::equivalent );
VERIFY( partial_order(zero, -zero) == partial_ordering::equivalent );
static_assert( noexcept(partial_order(zero, 1.0)) );
static_assert( partial_order(0.0, 1.0) == std::partial_ordering::less );
double min = std::numeric_limits<double>::lowest();
double max = std::numeric_limits<double>::max();
double nan = std::numeric_limits<double>::quiet_NaN();
double inf = std::numeric_limits<double>::infinity();
double denorm = std::numeric_limits<double>::denorm_min();
double smallest = std::numeric_limits<double>::min();
double epsilon = std::numeric_limits<double>::epsilon();
VERIFY( partial_order(denorm, smallest) == partial_ordering::less );
VERIFY( partial_order(denorm, 0.0) == partial_ordering::greater );
VERIFY( partial_order(0.0, nan) == partial_ordering::unordered );
VERIFY( partial_order(nan, nan) == partial_ordering::unordered );
VERIFY( partial_order(nan, 0.0) == partial_ordering::unordered );
VERIFY( partial_order(-nan, 0.0) == partial_ordering::unordered );
VERIFY( partial_order(-nan, min) == partial_ordering::unordered );
VERIFY( partial_order(-inf, min) == partial_ordering::less );
VERIFY( partial_order(-nan, -inf) == partial_ordering::unordered );
VERIFY( partial_order(-inf, -nan) == partial_ordering::unordered );
VERIFY( partial_order(max, inf) == partial_ordering::less );
VERIFY( partial_order(inf, max) == partial_ordering::greater );
VERIFY( partial_order(inf, nan) == partial_ordering::unordered );
VERIFY( partial_order(1.0, 1.0+epsilon) == partial_ordering::less );
}
namespace N
{
struct X { int i; };
constexpr partial_ordering operator<=>(X l, X r)
{
if (l.i < 0 && r.i < 0)
return partial_ordering::equivalent;
return r.i <=> l.i;
}
}
void
test04()
{
using N::X;
X one{1};
X negone{-1};
VERIFY( partial_order(one, X{1}) == partial_ordering::equivalent );
VERIFY( partial_order(negone, X{-2}) == partial_ordering::equivalent );
VERIFY( partial_order(one, X{2}) == partial_ordering::greater );
static_assert( !noexcept(partial_order(X{1}, X{2})) );
}
int main()
{
test01();
test02();
test03();
test04();
}
// Copyright (C) 2019 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <compare>
#include <limits>
using std::strong_order;
using std::strong_ordering;
static_assert( strong_order(1, 2) == strong_ordering::less );
static_assert( strong_order(1, 1) == strong_ordering::equal );
static_assert( strong_order(2, 1) == strong_ordering::greater );
static_assert( noexcept(strong_order(1, 1)) );
constexpr strong_ordering different_cv_quals(int i, const int j)
{
return strong_order(i, j);
}
static_assert( different_cv_quals(42, 999) == strong_ordering::less );
static_assert( different_cv_quals(-999, -999) == strong_ordering::equal );
static_assert( different_cv_quals(-99, -111) == strong_ordering::greater );
namespace N
{
struct X { int i; };
constexpr strong_ordering operator<=>(X l, X r)
{
if (l.i < 0 && r.i < 0)
return strong_ordering::equivalent;
return r.i <=> l.i;
}
}
using N::X;
static_assert( strong_order(X{1}, X{1}) == strong_ordering::equal );
static_assert( strong_order(X{-1}, X{-2}) == strong_ordering::equivalent );
static_assert( strong_order(X{1}, X{2}) == strong_ordering::greater );
static_assert( !noexcept(strong_order(X{1}, X{2})) );
// Copyright (C) 2019 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }
#include <compare>
#include <limits>
#include <testsuite_hooks.h>
using std::weak_order;
using std::weak_ordering;
void
test01()
{
int one = 1, two = 2;
VERIFY( weak_order(one, two) == weak_ordering::less );
VERIFY( weak_order(one, one) == weak_ordering::equivalent );
VERIFY( weak_order(two, one) == weak_ordering::greater );
static_assert( noexcept(weak_order(1, 1)) );
}
constexpr weak_ordering different_cv_quals(int i, const int j)
{
return weak_order(i, j);
}
void
test02()
{
int fortytwo = 42, nines = 999, lots = 1000;
VERIFY( different_cv_quals(fortytwo, nines) == weak_ordering::less );
VERIFY( different_cv_quals(-nines, -nines) == weak_ordering::equivalent );
VERIFY( different_cv_quals(-nines, -lots) == weak_ordering::greater );
}
void
test03()
{
double zero = 0.0;
VERIFY( weak_order(zero, zero) == weak_ordering::equivalent );
VERIFY( weak_order(-zero, -zero) == weak_ordering::equivalent );
VERIFY( weak_order(-zero, zero) == weak_ordering::equivalent );
VERIFY( weak_order(zero, -zero) == weak_ordering::equivalent );
double min = std::numeric_limits<double>::lowest();
double max = std::numeric_limits<double>::max();
double nan = std::numeric_limits<double>::quiet_NaN();
double inf = std::numeric_limits<double>::infinity();
double denorm = std::numeric_limits<double>::denorm_min();
double smallest = std::numeric_limits<double>::min();
double epsilon = std::numeric_limits<double>::epsilon();
VERIFY( weak_order(denorm, smallest) == weak_ordering::less );
VERIFY( weak_order(denorm, 0.0) == weak_ordering::greater );
VERIFY( weak_order(0.0, nan) == weak_ordering::less );
VERIFY( weak_order(nan, nan) == weak_ordering::equivalent );
VERIFY( weak_order(nan, -nan) == weak_ordering::greater );
VERIFY( weak_order(-nan, nan) == weak_ordering::less );
VERIFY( weak_order(nan, 0.0) == weak_ordering::greater );
VERIFY( weak_order(-nan, 0.0) == weak_ordering::less );
VERIFY( weak_order(-nan, min) == weak_ordering::less );
VERIFY( weak_order(-inf, min) == weak_ordering::less );
VERIFY( weak_order(-nan, -inf) == weak_ordering::less );
VERIFY( weak_order(-inf, -nan) == weak_ordering::greater );
VERIFY( weak_order(max, inf) == weak_ordering::less );
VERIFY( weak_order(inf, max) == weak_ordering::greater );
VERIFY( weak_order(inf, nan) == weak_ordering::less );
VERIFY( weak_order(1.0, 1.0+epsilon) == weak_ordering::less );
}
namespace N
{
struct X { int i; };
constexpr weak_ordering operator<=>(X l, X r)
{
if (l.i < 0 && r.i < 0)
return weak_ordering::equivalent;
return r.i <=> l.i;
}
}
void
test04()
{
using N::X;
X one{1};
X negone{-1};
VERIFY( weak_order(one, X{1}) == weak_ordering::equivalent );
VERIFY( weak_order(negone, X{-2}) == weak_ordering::equivalent );
VERIFY( weak_order(one, X{2}) == weak_ordering::greater );
static_assert( !noexcept(weak_order(X{1}, X{2})) );
}
int main()
{
test01();
test02();
test03();
test04();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment