summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/25_algorithms/clamp
diff options
context:
space:
mode:
authorEdward Smith-Rowland <3dw4rd@verizon.net>2016-07-15 17:16:32 +0000
committerEdward Smith-Rowland <emsr@gcc.gnu.org>2016-07-15 17:16:32 +0000
commit4db1cb44be3a9121ccbd517824cb280bbf62d833 (patch)
tree54ebeb5d20114eb85cee86260ec2b09fada95261 /libstdc++-v3/testsuite/25_algorithms/clamp
parent4a1248da3997c0d1120cf283e4765e42a8e94dd8 (diff)
downloadgcc-4db1cb44be3a9121ccbd517824cb280bbf62d833.tar.gz
Implement C++17 P0025 clamp.
2016-07-15 Edward Smith-Rowland <3dw4rd@verizon.net> Implement C++17 P0025 clamp. * include/bits/algorithmfwd.h: Declare clamp overloads. * include/bits/stl_algo.h: Implement clamp. Feature __cpp_lib_clamp. * testsuite/25_algorithms/clamp/1.cc: New test. * testsuite/25_algorithms/clamp/2.cc: New test. * testsuite/25_algorithms/clamp/constexpr.cc: New test. * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ 1.cc: New test. * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ pod.cc: New test. From-SVN: r238383
Diffstat (limited to 'libstdc++-v3/testsuite/25_algorithms/clamp')
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/clamp/1.cc48
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/clamp/2.cc102
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc31
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc44
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc38
5 files changed, 263 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/1.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/1.cc
new file mode 100644
index 00000000000..cf2e13150ac
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/clamp/1.cc
@@ -0,0 +1,48 @@
+// { dg-options "-std=gnu++17" }
+
+// Copyright (C) 2016 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/>.
+
+#include <algorithm>
+#include <functional>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ const int x = std::clamp(1, 2, 4);
+ const int y = std::clamp(3, 2, 4);
+ const int z = std::clamp(5, 2, 4);
+ VERIFY( x == 2 );
+ VERIFY( y == 3 );
+ VERIFY( z == 4 );
+
+ const int xc = std::clamp(1, 2, 4, std::greater<int>());
+ const int yc = std::clamp(3, 2, 4, std::greater<int>());
+ const int zc = std::clamp(5, 2, 4, std::greater<int>());
+ VERIFY( xc == 4 );
+ VERIFY( yc == 2 );
+ VERIFY( zc == 2 );
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/2.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/2.cc
new file mode 100644
index 00000000000..50c1a518ff2
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/clamp/2.cc
@@ -0,0 +1,102 @@
+// { dg-options "-std=gnu++17" }
+
+// Copyright (C) 2000-2016 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/>.
+
+#include <algorithm>
+#include <functional>
+#include <testsuite_hooks.h>
+
+template<typename T>
+ struct A { static const T a; };
+
+template<typename T>
+const T A<T>::a = T(3);
+
+void test02()
+{
+ bool test __attribute__((unused)) = true;
+
+ VERIFY( 3 == std::clamp(A<int>::a, 2, 4) );
+ VERIFY( 2 == std::clamp(A<int>::a, 1, 2) );
+ VERIFY( 4 == std::clamp(A<int>::a, 4, 6) );
+
+ VERIFY( 3u == std::clamp(A<unsigned int>::a, 2u, 4u) );
+ VERIFY( 2u == std::clamp(A<unsigned int>::a, 1u, 2u) );
+ VERIFY( 4u == std::clamp(A<unsigned int>::a, 4u, 6u) );
+
+ VERIFY( 3l == std::clamp(A<long>::a, 2l, 4l) );
+ VERIFY( 2l == std::clamp(A<long>::a, 1l, 2l) );
+ VERIFY( 4l == std::clamp(A<long>::a, 4l, 6l) );
+
+ VERIFY( 3ul == std::clamp(A<unsigned long>::a, 2ul, 4ul) );
+ VERIFY( 2ul == std::clamp(A<unsigned long>::a, 1ul, 2ul) );
+ VERIFY( 4ul == std::clamp(A<unsigned long>::a, 4ul, 6ul) );
+
+#ifdef _GLIBCXX_USE_LONG_LONG
+ VERIFY( 3ll == std::clamp(A<long long>::a, 2ll, 4ll) );
+ VERIFY( 2ll == std::clamp(A<long long>::a, 1ll, 2ll) );
+ VERIFY( 4ll == std::clamp(A<long long>::a, 4ll, 6ll) );
+
+ VERIFY( 3ull == std::clamp(A<unsigned long long>::a, 2ull, 4ull) );
+ VERIFY( 2ull == std::clamp(A<unsigned long long>::a, 1ull, 2ull) );
+ VERIFY( 4ull == std::clamp(A<unsigned long long>::a, 4ull, 6ull) );
+#endif
+
+ VERIFY( (short)3 == std::clamp(A<short>::a, (short)2, (short)4) );
+ VERIFY( (short)2 == std::clamp(A<short>::a, (short)1, (short)2) );
+ VERIFY( (short)4 == std::clamp(A<short>::a, (short)4, (short)6) );
+
+ VERIFY( (unsigned short)3 == std::clamp(A<unsigned short>::a, (unsigned short)2, (unsigned short)4) );
+ VERIFY( (unsigned short)2 == std::clamp(A<unsigned short>::a, (unsigned short)1, (unsigned short)2) );
+ VERIFY( (unsigned short)4 == std::clamp(A<unsigned short>::a, (unsigned short)4, (unsigned short)6) );
+
+ VERIFY( (char)3 == std::clamp(A<char>::a, (char)2, (char)4) );
+ VERIFY( (char)2 == std::clamp(A<char>::a, (char)1, (char)2) );
+ VERIFY( (char)4 == std::clamp(A<char>::a, (char)4, (char)6) );
+
+ VERIFY( (signed char)3 == std::clamp(A<signed char>::a, (signed char)2, (signed char)4) );
+ VERIFY( (signed char)2 == std::clamp(A<signed char>::a, (signed char)1, (signed char)2) );
+ VERIFY( (signed char)4 == std::clamp(A<signed char>::a, (signed char)4, (signed char)6) );
+
+ VERIFY( (unsigned char)3 == std::clamp(A<unsigned char>::a, (unsigned char)2, (unsigned char)4) );
+ VERIFY( (unsigned char)2 == std::clamp(A<unsigned char>::a, (unsigned char)1, (unsigned char)2) );
+ VERIFY( (unsigned char)4 == std::clamp(A<unsigned char>::a, (unsigned char)4, (unsigned char)6) );
+
+ VERIFY( (wchar_t)3 == std::clamp(A<wchar_t>::a, (wchar_t)2, (wchar_t)4) );
+ VERIFY( (wchar_t)2 == std::clamp(A<wchar_t>::a, (wchar_t)1, (wchar_t)2) );
+ VERIFY( (wchar_t)4 == std::clamp(A<wchar_t>::a, (wchar_t)4, (wchar_t)6) );
+
+ VERIFY( 3.0 == std::clamp(A<double>::a, 2.0, 4.0) );
+ VERIFY( 2.0 == std::clamp(A<double>::a, 1.0, 2.0) );
+ VERIFY( 4.0 == std::clamp(A<double>::a, 4.0, 6.0) );
+
+ VERIFY( 3.0f == std::clamp(A<float>::a, 2.0f, 4.0f) );
+ VERIFY( 2.0f == std::clamp(A<float>::a, 1.0f, 2.0f) );
+ VERIFY( 4.0f == std::clamp(A<float>::a, 4.0f, 6.0f) );
+
+ VERIFY( 3.0l == std::clamp(A<long double>::a, 2.0l, 4.0l) );
+ VERIFY( 2.0l == std::clamp(A<long double>::a, 1.0l, 2.0l) );
+ VERIFY( 4.0l == std::clamp(A<long double>::a, 4.0l, 6.0l) );
+}
+
+int
+main()
+{
+ test02();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc
new file mode 100644
index 00000000000..4ff8798cfae
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc
@@ -0,0 +1,31 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do compile }
+
+// Copyright (C) 2016 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/>.
+
+#include <algorithm>
+#include <functional>
+
+#ifndef __cpp_lib_clamp
+# error "Feature-test macro for clamp missing"
+#elif __cpp_lib_clamp != 201603
+# error "Feature-test macro for clamp has wrong value"
+#endif
+
+static_assert(std::clamp(2, 0, 1) == 1, "");
+static_assert(std::clamp(2, 0, 1, std::greater<int>()) == 0, "");
diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc
new file mode 100644
index 00000000000..d8bf8d6dcee
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc
@@ -0,0 +1,44 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do compile }
+
+// Copyright (C) 2016 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/>.
+
+#include <algorithm>
+#include <functional>
+#include <testsuite_api.h>
+
+#ifndef __cpp_lib_clamp
+# error "Feature-test macro for clamp missing"
+#elif __cpp_lib_clamp != 201603
+# error "Feature-test macro for clamp has wrong value"
+#endif
+
+namespace std
+{
+ using __gnu_test::NonDefaultConstructible;
+
+ typedef NonDefaultConstructible value_type;
+ typedef value_type* iterator_type;
+ typedef std::less<value_type> compare_type;
+
+ template const value_type& clamp(const value_type&,
+ const value_type&, const value_type&);
+ template const value_type& clamp(const value_type&,
+ const value_type&, const value_type&,
+ compare_type);
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc
new file mode 100644
index 00000000000..acb6d0f26b8
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc
@@ -0,0 +1,38 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do compile }
+
+// Copyright (C) 2016 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/>.
+
+
+#include <algorithm>
+#include <testsuite_character.h>
+
+namespace std
+{
+ using __gnu_test::pod_int;
+
+ typedef pod_int value_type;
+ typedef value_type* iterator_type;
+ typedef std::less<value_type> compare_type;
+
+ template const value_type& clamp(const value_type&,
+ const value_type&, const value_type&);
+ template const value_type& clamp(const value_type&,
+ const value_type&, const value_type&,
+ compare_type);
+}