Skip to content

Commit 55e5d57

Browse files
philnik777mahesh-attarde
authored andcommitted
[libc++] Refactor some .fail.cpp tests and fix time_point_cast not SFINAEing away (llvm#159288)
All of the `.fail.cpp` tests are actually testing constraints, so we should just test that the overloads SFINAE away correctly.
1 parent 674905b commit 55e5d57

File tree

12 files changed

+49
-140
lines changed

12 files changed

+49
-140
lines changed

libcxx/include/__chrono/time_point.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ struct common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_C
9595

9696
namespace chrono {
9797

98-
template <class _ToDuration, class _Clock, class _Duration>
98+
template <class _ToDuration, class _Clock, class _Duration, __enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
9999
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
100100
time_point_cast(const time_point<_Clock, _Duration>& __t) {
101101
return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));

libcxx/include/__cxx03/__chrono/time_point.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _
8181

8282
namespace chrono {
8383

84-
template <class _ToDuration, class _Clock, class _Duration>
84+
template <class _ToDuration, class _Clock, class _Duration, __enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
8585
inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, _ToDuration> time_point_cast(const time_point<_Clock, _Duration>& __t) {
8686
return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
8787
}

libcxx/test/std/time/time.duration/time.duration.alg/abs.compile.fail.cpp

Lines changed: 0 additions & 28 deletions
This file was deleted.

libcxx/test/std/time/time.duration/time.duration.alg/abs.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@
1919
#include <cassert>
2020
#include <ratio>
2121
#include <type_traits>
22+
#include <utility>
2223

2324
#include "test_macros.h"
2425

26+
template <class T, class = void>
27+
inline constexpr bool has_abs_v = false;
28+
29+
template <class T>
30+
inline constexpr bool has_abs_v<T, decltype((void)std::chrono::abs(std::declval<T>()))> = true;
31+
32+
static_assert(has_abs_v<std::chrono::milliseconds>);
33+
static_assert(!has_abs_v<std::chrono::duration<unsigned>>);
34+
2535
template <class Duration>
2636
void
2737
test(const Duration& f, const Duration& d)

libcxx/test/std/time/time.point/time.point.cast/ceil.compile.fail.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

libcxx/test/std/time/time.point/time.point.cast/ceil.pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
#include "test_macros.h"
2323

24+
template <class T, class = void>
25+
inline constexpr bool has_ceil_v = false;
26+
27+
template <class T>
28+
inline constexpr bool has_ceil_v<T, decltype((void)std::chrono::ceil<T>(std::chrono::system_clock::now()))> = true;
29+
30+
static_assert(has_ceil_v<std::chrono::seconds>);
31+
static_assert(!has_ceil_v<int>);
32+
2433
template <class FromDuration, class ToDuration>
2534
void
2635
test(const FromDuration& df, const ToDuration& d)

libcxx/test/std/time/time.point/time.point.cast/floor.compile.fail.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

libcxx/test/std/time/time.point/time.point.cast/floor.pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
#include "test_macros.h"
2323

24+
template <class T, class = void>
25+
inline constexpr bool has_floor_v = false;
26+
27+
template <class T>
28+
inline constexpr bool has_floor_v<T, decltype((void)std::chrono::floor<T>(std::chrono::system_clock::now()))> = true;
29+
30+
static_assert(has_floor_v<std::chrono::seconds>);
31+
static_assert(!has_floor_v<int>);
32+
2433
template <class FromDuration, class ToDuration>
2534
void
2635
test(const FromDuration& df, const ToDuration& d)

libcxx/test/std/time/time.point/time.point.cast/round.compile.fail.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

libcxx/test/std/time/time.point/time.point.cast/round.pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
#include "test_macros.h"
2323

24+
template <class T, class = void>
25+
inline constexpr bool has_round_v = false;
26+
27+
template <class T>
28+
inline constexpr bool has_round_v<T, decltype((void)std::chrono::round<T>(std::chrono::system_clock::now()))> = true;
29+
30+
static_assert(has_round_v<std::chrono::seconds>);
31+
static_assert(!has_round_v<int>);
32+
2433
template <class FromDuration, class ToDuration>
2534
void
2635
test(const FromDuration& df, const ToDuration& d)

0 commit comments

Comments
 (0)