Implement LWG#2688: 'clamp misses preconditions and has extraneous condition on result'. We already did this, just added tests

llvm-svn: 274252
This commit is contained in:
Marshall Clow 2016-06-30 17:52:51 +00:00
parent 078d8f69b6
commit e46c0885ff
3 changed files with 136 additions and 5 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -290,7 +290,7 @@
<tr><td><a href="http://wg21.link/LWG2684">2684</a></td><td>priority_queue lacking comparator typedef</td><td>Oulu</td><td>Complete</td></tr>
<tr><td><a href="http://wg21.link/LWG2685">2685</a></td><td>shared_ptr deleters must not throw on move construction</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2687">2687</a></td><td>{inclusive,exclusive}_scan misspecified</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2688">2688</a></td><td>clamp misses preconditions and has extraneous condition on result</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2688">2688</a></td><td>clamp misses preconditions and has extraneous condition on result</td><td>Oulu</td><td>Complete</td></tr>
<tr><td><a href="http://wg21.link/LWG2689">2689</a></td><td>Parallel versions of std::copy and std::move shouldn't be in order</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2698">2698</a></td><td>Effect of assign() on iterators/pointers/references</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2704">2704</a></td><td>recursive_directory_iterator's members should require '*this is dereferenceable'</td><td>Oulu</td><td>Complete</td></tr>