llvm-project/libcxx/test/support/test_comparisons.h
Chandler Carruth 57b08b0944 Update more file headers across all of the LLVM projects in the monorepo
to reflect the new license. These used slightly different spellings that
defeated my regular expressions.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351648
2019-01-19 10:56:40 +00:00

175 lines
6.5 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// A set of routines for testing the comparison operators of a type
//
// XXXX6 tests all six comparison operators
// XXXX2 tests only op== and op!=
//
// AssertComparisonsXAreNoexcept static_asserts that the operations are all noexcept.
// AssertComparisonsXReturnBool static_asserts that the operations return bool.
// AssertComparisonsXConvertibleToBool static_asserts that the operations return something convertible to bool.
#ifndef TEST_COMPARISONS_H
#define TEST_COMPARISONS_H
#include <type_traits>
#include "test_macros.h"
// Test all six comparison operations for sanity
template <class T, class U = T>
TEST_CONSTEXPR_CXX14 bool testComparisons6(const T& t1, const U& t2, bool isEqual, bool isLess)
{
if (isEqual)
{
if (!(t1 == t2)) return false;
if (!(t2 == t1)) return false;
if ( (t1 != t2)) return false;
if ( (t2 != t1)) return false;
if ( (t1 < t2)) return false;
if ( (t2 < t1)) return false;
if (!(t1 <= t2)) return false;
if (!(t2 <= t1)) return false;
if ( (t1 > t2)) return false;
if ( (t2 > t1)) return false;
if (!(t1 >= t2)) return false;
if (!(t2 >= t1)) return false;
}
else if (isLess)
{
if ( (t1 == t2)) return false;
if ( (t2 == t1)) return false;
if (!(t1 != t2)) return false;
if (!(t2 != t1)) return false;
if (!(t1 < t2)) return false;
if ( (t2 < t1)) return false;
if (!(t1 <= t2)) return false;
if ( (t2 <= t1)) return false;
if ( (t1 > t2)) return false;
if (!(t2 > t1)) return false;
if ( (t1 >= t2)) return false;
if (!(t2 >= t1)) return false;
}
else /* greater */
{
if ( (t1 == t2)) return false;
if ( (t2 == t1)) return false;
if (!(t1 != t2)) return false;
if (!(t2 != t1)) return false;
if ( (t1 < t2)) return false;
if (!(t2 < t1)) return false;
if ( (t1 <= t2)) return false;
if (!(t2 <= t1)) return false;
if (!(t1 > t2)) return false;
if ( (t2 > t1)) return false;
if (!(t1 >= t2)) return false;
if ( (t2 >= t1)) return false;
}
return true;
}
// Easy call when you can init from something already comparable.
template <class T, class Param>
TEST_CONSTEXPR_CXX14 bool testComparisons6Values(Param val1, Param val2)
{
const bool isEqual = val1 == val2;
const bool isLess = val1 < val2;
return testComparisons6(T(val1), T(val2), isEqual, isLess);
}
template <class T, class U = T>
void AssertComparisons6AreNoexcept()
{
ASSERT_NOEXCEPT(std::declval<const T&>() == std::declval<const U&>());
ASSERT_NOEXCEPT(std::declval<const T&>() != std::declval<const U&>());
ASSERT_NOEXCEPT(std::declval<const T&>() < std::declval<const U&>());
ASSERT_NOEXCEPT(std::declval<const T&>() <= std::declval<const U&>());
ASSERT_NOEXCEPT(std::declval<const T&>() > std::declval<const U&>());
ASSERT_NOEXCEPT(std::declval<const T&>() >= std::declval<const U&>());
}
template <class T, class U = T>
void AssertComparisons6ReturnBool()
{
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() == std::declval<const U&>()), bool);
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() != std::declval<const U&>()), bool);
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() < std::declval<const U&>()), bool);
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() <= std::declval<const U&>()), bool);
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() > std::declval<const U&>()), bool);
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() >= std::declval<const U&>()), bool);
}
template <class T, class U = T>
void AssertComparisons6ConvertibleToBool()
{
static_assert((std::is_convertible<decltype(std::declval<const T&>() == std::declval<const U&>()), bool>::value), "");
static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const U&>()), bool>::value), "");
static_assert((std::is_convertible<decltype(std::declval<const T&>() < std::declval<const U&>()), bool>::value), "");
static_assert((std::is_convertible<decltype(std::declval<const T&>() <= std::declval<const U&>()), bool>::value), "");
static_assert((std::is_convertible<decltype(std::declval<const T&>() > std::declval<const U&>()), bool>::value), "");
static_assert((std::is_convertible<decltype(std::declval<const T&>() >= std::declval<const U&>()), bool>::value), "");
}
// Test all two comparison operations for sanity
template <class T, class U = T>
TEST_CONSTEXPR_CXX14 bool testComparisons2(const T& t1, const U& t2, bool isEqual)
{
if (isEqual)
{
if (!(t1 == t2)) return false;
if (!(t2 == t1)) return false;
if ( (t1 != t2)) return false;
if ( (t2 != t1)) return false;
}
else /* not equal */
{
if ( (t1 == t2)) return false;
if ( (t2 == t1)) return false;
if (!(t1 != t2)) return false;
if (!(t2 != t1)) return false;
}
return true;
}
// Easy call when you can init from something already comparable.
template <class T, class Param>
TEST_CONSTEXPR_CXX14 bool testComparisons2Values(Param val1, Param val2)
{
const bool isEqual = val1 == val2;
return testComparisons2(T(val1), T(val2), isEqual);
}
template <class T, class U = T>
void AssertComparisons2AreNoexcept()
{
ASSERT_NOEXCEPT(std::declval<const T&>() == std::declval<const U&>());
ASSERT_NOEXCEPT(std::declval<const T&>() != std::declval<const U&>());
}
template <class T, class U = T>
void AssertComparisons2ReturnBool()
{
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() == std::declval<const U&>()), bool);
ASSERT_SAME_TYPE(decltype(std::declval<const T&>() != std::declval<const U&>()), bool);
}
template <class T, class U = T>
void AssertComparisons2ConvertibleToBool()
{
static_assert((std::is_convertible<decltype(std::declval<const T&>() == std::declval<const U&>()), bool>::value), "");
static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const U&>()), bool>::value), "");
}
#endif // TEST_COMPARISONS_H