From e46c0885ff8b4e4b6088847ae15c6b4bc31596d0 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 30 Jun 2016 17:52:51 +0000 Subject: Implement LWG#2688: 'clamp misses preconditions and has extraneous condition on result'. We already did this, just added tests llvm-svn: 274252 --- .../alg.sorting/alg.clamp/clamp.comp.pass.cpp | 70 +++++++++++++++++++++- .../alg.sorting/alg.clamp/clamp.pass.cpp | 69 ++++++++++++++++++++- 2 files changed, 135 insertions(+), 4 deletions(-) (limited to 'libcxx/test/std/algorithms/alg.sorting') diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp index 7067227119c..3fec12b6b53 100644 --- a/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp @@ -18,6 +18,20 @@ #include #include +struct Tag { + Tag() : val(0), tag("Default") {} + Tag(int a, const char *b) : val(a), tag(b) {} + ~Tag() {} + + int val; + const char *tag; + }; + +bool eq(const Tag& rhs, const Tag& lhs) { return rhs.val == lhs.val && rhs.tag == lhs.tag; } +// bool operator==(const Tag& rhs, const Tag& lhs) { return rhs.val == lhs.val; } +bool comp (const Tag& rhs, const Tag& lhs) { return rhs.val < lhs.val; } + + template void test(const T& v, const T& lo, const T& hi, C c, const T& x) @@ -48,7 +62,60 @@ int main() test(x, y, z, std::greater(), y); test(y, x, z, std::greater(), y); } -#if _LIBCPP_STD_VER > 11 + + { +// If they're all the same, we should get the value back. + Tag x{0, "Zero-x"}; + Tag y{0, "Zero-y"}; + Tag z{0, "Zero-z"}; + assert(eq(std::clamp(x, y, z, comp), x)); + assert(eq(std::clamp(y, x, z, comp), y)); + } + + { +// If it's the same as the lower bound, we get the value back. + Tag x{0, "Zero-x"}; + Tag y{0, "Zero-y"}; + Tag z{1, "One-z"}; + assert(eq(std::clamp(x, y, z, comp), x)); + assert(eq(std::clamp(y, x, z, comp), y)); + } + + { +// If it's the same as the upper bound, we get the value back. + Tag x{1, "One-x"}; + Tag y{0, "Zero-y"}; + Tag z{1, "One-z"}; + assert(eq(std::clamp(x, y, z, comp), x)); + assert(eq(std::clamp(z, y, x, comp), z)); + } + + { +// If the value is between, we should get the value back + Tag x{1, "One-x"}; + Tag y{0, "Zero-y"}; + Tag z{2, "Two-z"}; + assert(eq(std::clamp(x, y, z, comp), x)); + assert(eq(std::clamp(y, x, z, comp), x)); + } + + { +// If the value is less than the 'lo', we should get the lo back. + Tag x{0, "Zero-x"}; + Tag y{1, "One-y"}; + Tag z{2, "Two-z"}; + assert(eq(std::clamp(x, y, z, comp), y)); + assert(eq(std::clamp(y, x, z, comp), y)); + } + { +// If the value is greater than 'hi', we should get hi back. + Tag x{2, "Two-x"}; + Tag y{0, "Zero-y"}; + Tag z{1, "One-z"}; + assert(eq(std::clamp(x, y, z, comp), z)); + assert(eq(std::clamp(y, z, x, comp), z)); + } + { typedef int T; constexpr T x = 1; @@ -57,5 +124,4 @@ int main() static_assert(std::clamp(x, y, z, std::greater()) == y, "" ); static_assert(std::clamp(y, x, z, std::greater()) == y, "" ); } -#endif } diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp index 2592f904fd2..779c41827c9 100644 --- a/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp @@ -17,6 +17,19 @@ #include #include +struct Tag { + Tag() : val(0), tag("Default") {} + Tag(int a, const char *b) : val(a), tag(b) {} + ~Tag() {} + + int val; + const char *tag; + }; + +bool eq(const Tag& rhs, const Tag& lhs) { return rhs.val == lhs.val && rhs.tag == lhs.tag; } +// bool operator==(const Tag& rhs, const Tag& lhs) { return rhs.val == lhs.val; } +bool operator< (const Tag& rhs, const Tag& lhs) { return rhs.val < lhs.val; } + template void test(const T& a, const T& lo, const T& hi, const T& x) @@ -47,7 +60,60 @@ int main() test(x, y, z, x); test(y, x, z, x); } -#if _LIBCPP_STD_VER > 11 + + { +// If they're all the same, we should get the value back. + Tag x{0, "Zero-x"}; + Tag y{0, "Zero-y"}; + Tag z{0, "Zero-z"}; + assert(eq(std::clamp(x, y, z), x)); + assert(eq(std::clamp(y, x, z), y)); + } + + { +// If it's the same as the lower bound, we get the value back. + Tag x{0, "Zero-x"}; + Tag y{0, "Zero-y"}; + Tag z{1, "One-z"}; + assert(eq(std::clamp(x, y, z), x)); + assert(eq(std::clamp(y, x, z), y)); + } + + { +// If it's the same as the upper bound, we get the value back. + Tag x{1, "One-x"}; + Tag y{0, "Zero-y"}; + Tag z{1, "One-z"}; + assert(eq(std::clamp(x, y, z), x)); + assert(eq(std::clamp(z, y, x), z)); + } + + { +// If the value is between, we should get the value back + Tag x{1, "One-x"}; + Tag y{0, "Zero-y"}; + Tag z{2, "Two-z"}; + assert(eq(std::clamp(x, y, z), x)); + assert(eq(std::clamp(y, x, z), x)); + } + + { +// If the value is less than the 'lo', we should get the lo back. + Tag x{0, "Zero-x"}; + Tag y{1, "One-y"}; + Tag z{2, "Two-z"}; + assert(eq(std::clamp(x, y, z), y)); + assert(eq(std::clamp(y, x, z), y)); + } + { +// If the value is greater than 'hi', we should get hi back. + Tag x{2, "Two-x"}; + Tag y{0, "Zero-y"}; + Tag z{1, "One-z"}; + assert(eq(std::clamp(x, y, z), z)); + assert(eq(std::clamp(y, z, x), z)); + } + { typedef int T; constexpr T x = 1; @@ -56,5 +122,4 @@ int main() static_assert(std::clamp(x, y, z) == x, "" ); static_assert(std::clamp(y, x, z) == x, "" ); } -#endif } -- cgit v1.2.3