diff options
author | Marshall Clow <mclow.lists@gmail.com> | 2016-06-30 17:52:51 +0000 |
---|---|---|
committer | Marshall Clow <mclow.lists@gmail.com> | 2016-06-30 17:52:51 +0000 |
commit | e46c0885ff8b4e4b6088847ae15c6b4bc31596d0 (patch) | |
tree | 4803477b659820c5d11f0409ae13959bd2215f18 /libcxx/test/std/algorithms | |
parent | 078d8f69b6e62a6f68c40cc7d2e8b5ef632fb2ba (diff) | |
download | bcm5719-llvm-e46c0885ff8b4e4b6088847ae15c6b4bc31596d0.tar.gz bcm5719-llvm-e46c0885ff8b4e4b6088847ae15c6b4bc31596d0.zip |
Implement LWG#2688: 'clamp misses preconditions and has extraneous condition on result'. We already did this, just added tests
llvm-svn: 274252
Diffstat (limited to 'libcxx/test/std/algorithms')
-rw-r--r-- | libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp | 70 | ||||
-rw-r--r-- | libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp | 69 |
2 files changed, 135 insertions, 4 deletions
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 <functional> #include <cassert> +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 <class T, class C> 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<int>(), y); test(y, x, z, std::greater<int>(), 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<T>()) == y, "" ); static_assert(std::clamp(y, x, z, std::greater<T>()) == 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 <algorithm> #include <cassert> +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 <class T> 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 } |