Implement n3469 - constexpr for chrono

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@187517 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2013-07-31 19:32:19 +00:00
parent f890d9bfaa
commit 832b304076
10 changed files with 178 additions and 30 deletions

View File

@@ -26,11 +26,27 @@ test(const FromDuration& df, const ToDuration& d)
typedef std::chrono::system_clock Clock;
typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
{
FromTimePoint f(df);
ToTimePoint t(d);
typedef decltype(std::chrono::time_point_cast<ToDuration>(f)) R;
static_assert((std::is_same<R, ToTimePoint>::value), "");
assert(std::chrono::time_point_cast<ToDuration>(f) == t);
}
}
template<class FromDuration, long long From, class ToDuration, long long To>
void test_constexpr ()
{
typedef std::chrono::system_clock Clock;
typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
{
constexpr FromTimePoint f{FromDuration{From}};
constexpr ToTimePoint t{ToDuration{To}};
static_assert(std::chrono::time_point_cast<ToDuration>(f) == t, "");
}
}
int main()
@@ -45,4 +61,16 @@ int main()
std::chrono::duration<double, std::ratio<3600> >(7265./3600));
test(std::chrono::duration<int, std::ratio<2, 3> >(9),
std::chrono::duration<int, std::ratio<3, 5> >(10));
#if _LIBCPP_STD_VER > 11
{
test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours, 2> ();
test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();
test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();
test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();
test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();
test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();
typedef std::chrono::duration<int, std::ratio<3, 5>> T1;
test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();
}
#endif
}

View File

@@ -54,4 +54,31 @@ int main()
assert(!(t1 == t2));
assert( (t1 != t2));
}
#if _LIBCPP_STD_VER > 11
{
constexpr T1 t1(Duration1(3));
constexpr T1 t2(Duration1(3));
static_assert( (t1 == t2), "");
static_assert(!(t1 != t2), "");
}
{
constexpr T1 t1(Duration1(3));
constexpr T1 t2(Duration1(4));
static_assert(!(t1 == t2), "");
static_assert( (t1 != t2), "");
}
{
constexpr T1 t1(Duration1(3));
constexpr T2 t2(Duration2(3000));
static_assert( (t1 == t2), "");
static_assert(!(t1 != t2), "");
}
{
constexpr T1 t1(Duration1(3));
constexpr T2 t2(Duration2(3001));
static_assert(!(t1 == t2), "");
static_assert( (t1 != t2), "");
}
#endif
}

View File

@@ -70,4 +70,39 @@ int main()
assert( (t1 <= t2));
assert(!(t1 >= t2));
}
#if _LIBCPP_STD_VER > 11
{
constexpr T1 t1(Duration1(3));
constexpr T1 t2(Duration1(3));
static_assert(!(t1 < t2), "");
static_assert(!(t1 > t2), "");
static_assert( (t1 <= t2), "");
static_assert( (t1 >= t2), "");
}
{
constexpr T1 t1(Duration1(3));
constexpr T1 t2(Duration1(4));
static_assert( (t1 < t2), "");
static_assert(!(t1 > t2), "");
static_assert( (t1 <= t2), "");
static_assert(!(t1 >= t2), "");
}
{
constexpr T1 t1(Duration1(3));
constexpr T2 t2(Duration2(3000));
static_assert(!(t1 < t2), "");
static_assert(!(t1 > t2), "");
static_assert( (t1 <= t2), "");
static_assert( (t1 >= t2), "");
}
{
constexpr T1 t1(Duration1(3));
constexpr T2 t2(Duration2(3001));
static_assert( (t1 < t2), "");
static_assert(!(t1 > t2), "");
static_assert( (t1 <= t2), "");
static_assert(!(t1 >= t2), "");
}
#endif
}

View File

@@ -27,4 +27,11 @@ int main()
std::chrono::time_point<Clock, Duration1> t1 = t2;
assert(t1.time_since_epoch() == Duration1(3000));
}
#if _LIBCPP_STD_VER > 11
{
constexpr std::chrono::time_point<Clock, Duration2> t2(Duration2(3));
constexpr std::chrono::time_point<Clock, Duration1> t1 = t2;
static_assert(t1.time_since_epoch() == Duration1(3000), "");
}
#endif
}

View File

@@ -22,6 +22,14 @@ int main()
{
typedef std::chrono::system_clock Clock;
typedef std::chrono::duration<Rep, std::milli> Duration;
{
std::chrono::time_point<Clock, Duration> t;
assert(t.time_since_epoch() == Duration::zero());
}
#if _LIBCPP_STD_VER > 11
{
constexpr std::chrono::time_point<Clock, Duration> t;
static_assert(t.time_since_epoch() == Duration::zero(), "");
}
#endif
}

View File

@@ -28,4 +28,14 @@ int main()
std::chrono::time_point<Clock, Duration> t(std::chrono::seconds(3));
assert(t.time_since_epoch() == Duration(3000));
}
#if _LIBCPP_STD_VER > 11
{
constexpr std::chrono::time_point<Clock, Duration> t(Duration(3));
static_assert(t.time_since_epoch() == Duration(3), "");
}
{
constexpr std::chrono::time_point<Clock, Duration> t(std::chrono::seconds(3));
static_assert(t.time_since_epoch() == Duration(3000), "");
}
#endif
}

View File

@@ -27,9 +27,20 @@ int main()
typedef std::chrono::system_clock Clock;
typedef std::chrono::milliseconds Duration1;
typedef std::chrono::microseconds Duration2;
{
std::chrono::time_point<Clock, Duration1> t1(Duration1(3));
std::chrono::time_point<Clock, Duration2> t2 = t1 + Duration2(5);
assert(t2.time_since_epoch() == Duration2(3005));
t2 = Duration2(6) + t1;
assert(t2.time_since_epoch() == Duration2(3006));
}
#if _LIBCPP_STD_VER > 11
{
constexpr std::chrono::time_point<Clock, Duration1> t1(Duration1(3));
constexpr std::chrono::time_point<Clock, Duration2> t2 = t1 + Duration2(5);
static_assert(t2.time_since_epoch() == Duration2(3005), "");
constexpr std::chrono::time_point<Clock, Duration2> t3 = Duration2(6) + t1;
static_assert(t3.time_since_epoch() == Duration2(3006), "");
}
#endif
}

View File

@@ -23,7 +23,16 @@ int main()
typedef std::chrono::system_clock Clock;
typedef std::chrono::milliseconds Duration1;
typedef std::chrono::microseconds Duration2;
{
std::chrono::time_point<Clock, Duration1> t1(Duration1(3));
std::chrono::time_point<Clock, Duration2> t2 = t1 - Duration2(5);
assert(t2.time_since_epoch() == Duration2(2995));
}
#if _LIBCPP_STD_VER > 11
{
constexpr std::chrono::time_point<Clock, Duration1> t1(Duration1(3));
constexpr std::chrono::time_point<Clock, Duration2> t2 = t1 - Duration2(5);
static_assert(t2.time_since_epoch() == Duration2(2995), "");
}
#endif
}

View File

@@ -23,7 +23,16 @@ int main()
typedef std::chrono::system_clock Clock;
typedef std::chrono::milliseconds Duration1;
typedef std::chrono::microseconds Duration2;
{
std::chrono::time_point<Clock, Duration1> t1(Duration1(3));
std::chrono::time_point<Clock, Duration2> t2(Duration2(5));
assert((t1 - t2) == Duration2(2995));
}
#if _LIBCPP_STD_VER > 11
{
constexpr std::chrono::time_point<Clock, Duration1> t1(Duration1(3));
constexpr std::chrono::time_point<Clock, Duration2> t2(Duration2(5));
static_assert((t1 - t2) == Duration2(2995), "");
}
#endif
}