mirror of
https://github.com/HowardHinnant/date.git
synced 2024-12-27 08:31:03 +08:00
Add tests for time_of_day and make_time
This commit is contained in:
parent
2841bc69a7
commit
9b79ad2ce2
2
date.h
2
date.h
@ -3017,6 +3017,7 @@ namespace detail
|
||||
|
||||
enum class classify
|
||||
{
|
||||
not_valid,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
@ -3027,6 +3028,7 @@ template <class Duration>
|
||||
struct classify_duration
|
||||
{
|
||||
static CONSTDATA classify value =
|
||||
Duration{1} >= days{1} ? classify::not_valid :
|
||||
Duration{1} >= std::chrono::hours{1} ? classify::hour :
|
||||
Duration{1} >= std::chrono::minutes{1} ? classify::minute :
|
||||
Duration{1} >= std::chrono::seconds{1} ? classify::second :
|
||||
|
164
test/date_test/make_time.pass.cpp
Normal file
164
test/date_test/make_time.pass.cpp
Normal file
@ -0,0 +1,164 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// template <class Rep, class Period,
|
||||
// class = typename std::enable_if
|
||||
// <!std::chrono::treat_as_floating_point<Rep>::value>::type>
|
||||
// constexpr
|
||||
// time_of_day<std::chrono::duration<Rep, Period>>
|
||||
// make_time(std::chrono::duration<Rep, Period> d) noexcept;
|
||||
|
||||
// constexpr
|
||||
// time_of_day<std::chrono::hours>
|
||||
// make_time(std::chrono::hours h, unsigned md) noexcept;
|
||||
|
||||
// constexpr
|
||||
// time_of_day<std::chrono::minutes>
|
||||
// make_time(std::chrono::hours h, std::chrono::minutes m, unsigned md) noexcept;
|
||||
|
||||
// constexpr
|
||||
// time_of_day<std::chrono::seconds>
|
||||
// make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
|
||||
// unsigned md) noexcept;
|
||||
|
||||
// template <class Rep, class Period,
|
||||
// class = typename std::enable_if<std::ratio_less<Period,
|
||||
// std::ratio<1>>::value>::type>
|
||||
// constexpr
|
||||
// time_of_day<std::chrono::duration<Rep, Period>>
|
||||
// make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
|
||||
// std::chrono::duration<Rep, Period> sub_s, unsigned md) noexcept;
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(nanoseconds{18429000000022})),
|
||||
time_of_day<nanoseconds>>{}, "");
|
||||
auto tod = make_time(nanoseconds{18429000000022});
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.subseconds() == nanoseconds{22});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(microseconds{18429000022})),
|
||||
time_of_day<microseconds>>{}, "");
|
||||
auto tod = make_time(microseconds{18429000022});
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.subseconds() == microseconds{22});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(seconds{18429})),
|
||||
time_of_day<seconds>>{}, "");
|
||||
auto tod = make_time(seconds{18429});
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(minutes{307})),
|
||||
time_of_day<minutes>>{}, "");
|
||||
auto tod = make_time(minutes{307});
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5})),
|
||||
time_of_day<hours>>{}, "");
|
||||
auto tod = make_time(hours{5});
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5}, minutes{7}, seconds{9},
|
||||
nanoseconds{22}, pm)),
|
||||
time_of_day<nanoseconds>>{}, "");
|
||||
auto tod = make_time(hours{5}, minutes{7}, seconds{9}, nanoseconds{22}, pm);
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.subseconds() == nanoseconds{22});
|
||||
assert(tod.mode() == pm);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5}, minutes{7}, seconds{9},
|
||||
microseconds{22}, 0)),
|
||||
time_of_day<microseconds>>{}, "");
|
||||
auto tod = make_time(hours{5}, minutes{7}, seconds{9}, microseconds{22}, 0);
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.subseconds() == microseconds{22});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5}, minutes{7}, seconds{9},
|
||||
milliseconds{22}, am)),
|
||||
time_of_day<milliseconds>>{}, "");
|
||||
auto tod = make_time(hours{5}, minutes{7}, seconds{9}, milliseconds{22}, am);
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.subseconds() == milliseconds{22});
|
||||
assert(tod.mode() == am);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5}, minutes{7}, seconds{9}, am)),
|
||||
time_of_day<seconds>>{}, "");
|
||||
auto tod = make_time(hours{5}, minutes{7}, seconds{9}, am);
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.seconds() == seconds{9});
|
||||
assert(tod.mode() == am);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5}, minutes{7}, pm)),
|
||||
time_of_day<minutes>>{}, "");
|
||||
auto tod = make_time(hours{5}, minutes{7}, pm);
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.minutes() == minutes{7});
|
||||
assert(tod.mode() == pm);
|
||||
}
|
||||
{
|
||||
static_assert(is_same<decltype(make_time(hours{5}, 0)),
|
||||
time_of_day<hours>>{}, "");
|
||||
auto tod = make_time(hours{5}, 0);
|
||||
assert(tod.hours() == hours{5});
|
||||
assert(tod.mode() == 0);
|
||||
}
|
||||
}
|
105
test/date_test/time_of_day_hours.pass.cpp
Normal file
105
test/date_test/time_of_day_hours.pass.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// enum {am = 1, pm};
|
||||
|
||||
// class time_of_day<std::chrono::hours>
|
||||
// {
|
||||
// public:
|
||||
// using precision = std::chrono::hours;
|
||||
//
|
||||
// constexpr explicit time_of_day(std::chrono::hours since_midnight) noexcept;
|
||||
// constexpr time_of_day(std::chrono::hours h, unsigned md) noexcept;
|
||||
//
|
||||
// constexpr std::chrono::hours hours() const noexcept;
|
||||
// constexpr unsigned mode() const noexcept;
|
||||
//
|
||||
// constexpr explicit operator precision() const noexcept;
|
||||
// constexpr precision to_duration() const noexcept;
|
||||
//
|
||||
// void make24() noexcept;
|
||||
// void make12() noexcept;
|
||||
// };
|
||||
|
||||
// std::ostream& operator<<(std::ostream& os, const time_of_day<std::chrono::hours>& t);
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
using tod = time_of_day<hours>;
|
||||
|
||||
static_assert(is_same<tod::precision, hours>{}, "");
|
||||
|
||||
static_assert( is_trivially_destructible<tod>{}, "");
|
||||
static_assert(!is_default_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_assignable<tod>{}, "");
|
||||
static_assert( is_trivially_move_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_move_assignable<tod>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod, hours>{}, "");
|
||||
static_assert(!is_convertible<hours, tod>{}, "");
|
||||
static_assert(is_nothrow_constructible<tod, hours, unsigned>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod::precision, tod>{}, "");
|
||||
static_assert(!is_convertible<tod, tod::precision>{}, "");
|
||||
|
||||
constexpr tod t1 = tod{hours{13}};
|
||||
static_assert(t1.hours() == hours{13}, "");
|
||||
static_assert(t1.mode() == 0, "");
|
||||
#if __cplusplus >= 201402
|
||||
static_assert(static_cast<tod::precision>(t1) == hours{13}, "");
|
||||
static_assert(t1.to_duration() == hours{13}, "");
|
||||
#endif
|
||||
|
||||
auto t2 = t1;
|
||||
assert(t2.hours() == t1.hours());
|
||||
assert(t2.mode() == t1.mode());
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
ostringstream os;
|
||||
os << t2;
|
||||
assert(os.str() == "1300");
|
||||
t2.make12();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{1});
|
||||
assert(t2.mode() == pm);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "1pm");
|
||||
t2.make24();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{13});
|
||||
assert(t2.mode() == 0);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "1300");
|
||||
}
|
128
test/date_test/time_of_day_milliseconds.pass.cpp
Normal file
128
test/date_test/time_of_day_milliseconds.pass.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// enum {am = 1, pm};
|
||||
|
||||
// template <class Rep, class Period>
|
||||
// class time_of_day<std::chrono::duration<Rep, Period>>
|
||||
// {
|
||||
// public:
|
||||
// using precision = std::chrono::std::chrono::duration<Rep, Period>;
|
||||
//
|
||||
// constexpr explicit time_of_day(precision since_midnight) noexcept;
|
||||
// constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
|
||||
// std::chrono::seconds s, precision sub_s,
|
||||
// unsigned md) noexcept;
|
||||
//
|
||||
// constexpr std::chrono::hours hours() const noexcept;
|
||||
// constexpr std::chrono::minutes minutes() const noexcept;
|
||||
// constexpr std::chrono::seconds seconds() const noexcept;
|
||||
// constexpr precision subseconds() const noexcept;
|
||||
// constexpr unsigned mode() const noexcept;
|
||||
//
|
||||
// constexpr explicit operator precision() const noexcept;
|
||||
// constexpr precision to_duration() const noexcept;
|
||||
//
|
||||
// void make24() noexcept;
|
||||
// void make12() noexcept;
|
||||
// };
|
||||
|
||||
// template <class Rep, class Period>
|
||||
// std::ostream&
|
||||
// operator<<(std::ostream& os, const time_of_day<std::chrono::duration<Rep, Period>>& t);
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
using tod = time_of_day<milliseconds>;
|
||||
|
||||
static_assert(is_same<tod::precision, milliseconds>{}, "");
|
||||
|
||||
static_assert( is_trivially_destructible<tod>{}, "");
|
||||
static_assert(!is_default_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_assignable<tod>{}, "");
|
||||
static_assert( is_trivially_move_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_move_assignable<tod>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod, milliseconds>{}, "");
|
||||
static_assert(!is_convertible<milliseconds, tod>{}, "");
|
||||
static_assert(is_nothrow_constructible<tod, hours, minutes, seconds, milliseconds,
|
||||
unsigned>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod::precision, tod>{}, "");
|
||||
static_assert(!is_convertible<tod, tod::precision>{}, "");
|
||||
|
||||
constexpr tod t1 = tod{hours{13} + minutes{7} + seconds{5} + milliseconds{22}};
|
||||
static_assert(t1.hours() == hours{13}, "");
|
||||
static_assert(t1.minutes() == minutes{7}, "");
|
||||
static_assert(t1.seconds() == seconds{5}, "");
|
||||
static_assert(t1.subseconds() == milliseconds{22}, "");
|
||||
static_assert(t1.mode() == 0, "");
|
||||
#if __cplusplus >= 201402
|
||||
static_assert(static_cast<tod::precision>(t1) == hours{13} + minutes{7}
|
||||
+ seconds{5} + milliseconds{22}, "");
|
||||
static_assert(t1.to_duration() == hours{13} + minutes{7} + seconds{5}
|
||||
+ milliseconds{22}, "");
|
||||
#endif
|
||||
|
||||
auto t2 = t1;
|
||||
assert(t2.hours() == t1.hours());
|
||||
assert(t2.minutes() == t1.minutes());
|
||||
assert(t2.seconds() == t1.seconds());
|
||||
assert(t2.subseconds() == t1.subseconds());
|
||||
assert(t2.mode() == t1.mode());
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
ostringstream os;
|
||||
os << t2;
|
||||
assert(os.str() == "13:07:05.022");
|
||||
t2.make12();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{1});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.seconds() == seconds{5});
|
||||
assert(t2.subseconds() == milliseconds{22});
|
||||
assert(t2.mode() == pm);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "1:07:05.022pm");
|
||||
t2.make24();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{13});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.seconds() == seconds{5});
|
||||
assert(t2.subseconds() == milliseconds{22});
|
||||
assert(t2.mode() == 0);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "13:07:05.022");
|
||||
}
|
111
test/date_test/time_of_day_minutes.pass.cpp
Normal file
111
test/date_test/time_of_day_minutes.pass.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// enum {am = 1, pm};
|
||||
|
||||
// class time_of_day<std::chrono::minutes>
|
||||
// {
|
||||
// public:
|
||||
// using precision = std::chrono::minutes;
|
||||
//
|
||||
// constexpr explicit time_of_day(std::chrono::minutes since_midnight) noexcept;
|
||||
// constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
|
||||
// unsigned md) noexcept;
|
||||
//
|
||||
// constexpr std::chrono::hours hours() const noexcept;
|
||||
// constexpr std::chrono::minutes minutes() const noexcept;
|
||||
// constexpr unsigned mode() const noexcept;
|
||||
//
|
||||
// constexpr explicit operator precision() const noexcept;
|
||||
// constexpr precision to_duration() const noexcept;
|
||||
//
|
||||
// void make24() noexcept;
|
||||
// void make12() noexcept;
|
||||
// };
|
||||
|
||||
// std::ostream& operator<<(std::ostream& os, const time_of_day<std::chrono::minutes>& t);
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
using tod = time_of_day<minutes>;
|
||||
|
||||
static_assert(is_same<tod::precision, minutes>{}, "");
|
||||
|
||||
static_assert( is_trivially_destructible<tod>{}, "");
|
||||
static_assert(!is_default_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_assignable<tod>{}, "");
|
||||
static_assert( is_trivially_move_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_move_assignable<tod>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod, minutes>{}, "");
|
||||
static_assert(!is_convertible<minutes, tod>{}, "");
|
||||
static_assert(is_nothrow_constructible<tod, hours, minutes, unsigned>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod::precision, tod>{}, "");
|
||||
static_assert(!is_convertible<tod, tod::precision>{}, "");
|
||||
|
||||
constexpr tod t1 = tod{hours{13} + minutes{7}};
|
||||
static_assert(t1.hours() == hours{13}, "");
|
||||
static_assert(t1.minutes() == minutes{7}, "");
|
||||
static_assert(t1.mode() == 0, "");
|
||||
#if __cplusplus >= 201402
|
||||
static_assert(static_cast<tod::precision>(t1) == hours{13} + minutes{7}, "");
|
||||
static_assert(t1.to_duration() == hours{13} + minutes{7}, "");
|
||||
#endif
|
||||
|
||||
auto t2 = t1;
|
||||
assert(t2.hours() == t1.hours());
|
||||
assert(t2.minutes() == t1.minutes());
|
||||
assert(t2.mode() == t1.mode());
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
ostringstream os;
|
||||
os << t2;
|
||||
assert(os.str() == "13:07");
|
||||
t2.make12();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{1});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.mode() == pm);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "1:07pm");
|
||||
t2.make24();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{13});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.mode() == 0);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "13:07");
|
||||
}
|
128
test/date_test/time_of_day_nanoseconds.pass.cpp
Normal file
128
test/date_test/time_of_day_nanoseconds.pass.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// enum {am = 1, pm};
|
||||
|
||||
// template <class Rep, class Period>
|
||||
// class time_of_day<std::chrono::duration<Rep, Period>>
|
||||
// {
|
||||
// public:
|
||||
// using precision = std::chrono::std::chrono::duration<Rep, Period>;
|
||||
//
|
||||
// constexpr explicit time_of_day(precision since_midnight) noexcept;
|
||||
// constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
|
||||
// std::chrono::seconds s, precision sub_s,
|
||||
// unsigned md) noexcept;
|
||||
//
|
||||
// constexpr std::chrono::hours hours() const noexcept;
|
||||
// constexpr std::chrono::minutes minutes() const noexcept;
|
||||
// constexpr std::chrono::seconds seconds() const noexcept;
|
||||
// constexpr precision subseconds() const noexcept;
|
||||
// constexpr unsigned mode() const noexcept;
|
||||
//
|
||||
// constexpr explicit operator precision() const noexcept;
|
||||
// constexpr precision to_duration() const noexcept;
|
||||
//
|
||||
// void make24() noexcept;
|
||||
// void make12() noexcept;
|
||||
// };
|
||||
|
||||
// template <class Rep, class Period>
|
||||
// std::ostream&
|
||||
// operator<<(std::ostream& os, const time_of_day<std::chrono::duration<Rep, Period>>& t);
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
using tod = time_of_day<nanoseconds>;
|
||||
|
||||
static_assert(is_same<tod::precision, nanoseconds>{}, "");
|
||||
|
||||
static_assert( is_trivially_destructible<tod>{}, "");
|
||||
static_assert(!is_default_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_assignable<tod>{}, "");
|
||||
static_assert( is_trivially_move_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_move_assignable<tod>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod, nanoseconds>{}, "");
|
||||
static_assert(!is_convertible<nanoseconds, tod>{}, "");
|
||||
static_assert(is_nothrow_constructible<tod, hours, minutes, seconds, nanoseconds,
|
||||
unsigned>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod::precision, tod>{}, "");
|
||||
static_assert(!is_convertible<tod, tod::precision>{}, "");
|
||||
|
||||
constexpr tod t1 = tod{hours{13} + minutes{7} + seconds{5} + nanoseconds{22}};
|
||||
static_assert(t1.hours() == hours{13}, "");
|
||||
static_assert(t1.minutes() == minutes{7}, "");
|
||||
static_assert(t1.seconds() == seconds{5}, "");
|
||||
static_assert(t1.subseconds() == nanoseconds{22}, "");
|
||||
static_assert(t1.mode() == 0, "");
|
||||
#if __cplusplus >= 201402
|
||||
static_assert(static_cast<tod::precision>(t1) == hours{13} + minutes{7}
|
||||
+ seconds{5} + nanoseconds{22}, "");
|
||||
static_assert(t1.to_duration() == hours{13} + minutes{7} + seconds{5}
|
||||
+ nanoseconds{22}, "");
|
||||
#endif
|
||||
|
||||
auto t2 = t1;
|
||||
assert(t2.hours() == t1.hours());
|
||||
assert(t2.minutes() == t1.minutes());
|
||||
assert(t2.seconds() == t1.seconds());
|
||||
assert(t2.subseconds() == t1.subseconds());
|
||||
assert(t2.mode() == t1.mode());
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
ostringstream os;
|
||||
os << t2;
|
||||
assert(os.str() == "13:07:05.000000022");
|
||||
t2.make12();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{1});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.seconds() == seconds{5});
|
||||
assert(t2.subseconds() == nanoseconds{22});
|
||||
assert(t2.mode() == pm);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "1:07:05.000000022pm");
|
||||
t2.make24();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{13});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.seconds() == seconds{5});
|
||||
assert(t2.subseconds() == nanoseconds{22});
|
||||
assert(t2.mode() == 0);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "13:07:05.000000022");
|
||||
}
|
117
test/date_test/time_of_day_seconds.pass.cpp
Normal file
117
test/date_test/time_of_day_seconds.pass.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// enum {am = 1, pm};
|
||||
|
||||
// class time_of_day<std::chrono::seconds>
|
||||
// {
|
||||
// public:
|
||||
// using precision = std::chrono::seconds;
|
||||
//
|
||||
// constexpr explicit time_of_day(std::chrono::seconds since_midnight) noexcept;
|
||||
// constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
|
||||
// std::chrono::seconds s, unsigned md) noexcept;
|
||||
//
|
||||
// constexpr std::chrono::hours hours() const noexcept;
|
||||
// constexpr std::chrono::minutes minutes() const noexcept;
|
||||
// constexpr std::chrono::seconds seconds() const noexcept;
|
||||
// constexpr unsigned mode() const noexcept;
|
||||
//
|
||||
// constexpr explicit operator precision() const noexcept;
|
||||
// constexpr precision to_duration() const noexcept;
|
||||
//
|
||||
// void make24() noexcept;
|
||||
// void make12() noexcept;
|
||||
// };
|
||||
|
||||
// std::ostream& operator<<(std::ostream& os, const time_of_day<std::chrono::seconds>& t);
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
using tod = time_of_day<seconds>;
|
||||
|
||||
static_assert(is_same<tod::precision, seconds>{}, "");
|
||||
|
||||
static_assert( is_trivially_destructible<tod>{}, "");
|
||||
static_assert(!is_default_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_copy_assignable<tod>{}, "");
|
||||
static_assert( is_trivially_move_constructible<tod>{}, "");
|
||||
static_assert( is_trivially_move_assignable<tod>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod, seconds>{}, "");
|
||||
static_assert(!is_convertible<seconds, tod>{}, "");
|
||||
static_assert(is_nothrow_constructible<tod, hours, minutes, seconds, unsigned>{}, "");
|
||||
|
||||
static_assert(is_nothrow_constructible<tod::precision, tod>{}, "");
|
||||
static_assert(!is_convertible<tod, tod::precision>{}, "");
|
||||
|
||||
constexpr tod t1 = tod{hours{13} + minutes{7} + seconds{5}};
|
||||
static_assert(t1.hours() == hours{13}, "");
|
||||
static_assert(t1.minutes() == minutes{7}, "");
|
||||
static_assert(t1.seconds() == seconds{5}, "");
|
||||
static_assert(t1.mode() == 0, "");
|
||||
#if __cplusplus >= 201402
|
||||
static_assert(static_cast<tod::precision>(t1) == hours{13} + minutes{7}
|
||||
+ seconds{5}, "");
|
||||
static_assert(t1.to_duration() == hours{13} + minutes{7} + seconds{5}, "");
|
||||
#endif
|
||||
|
||||
auto t2 = t1;
|
||||
assert(t2.hours() == t1.hours());
|
||||
assert(t2.minutes() == t1.minutes());
|
||||
assert(t2.seconds() == t1.seconds());
|
||||
assert(t2.mode() == t1.mode());
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
ostringstream os;
|
||||
os << t2;
|
||||
assert(os.str() == "13:07:05");
|
||||
t2.make12();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{1});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.seconds() == seconds{5});
|
||||
assert(t2.mode() == pm);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "1:07:05pm");
|
||||
t2.make24();
|
||||
os.str("");
|
||||
assert(t2.hours() == hours{13});
|
||||
assert(t2.minutes() == minutes{7});
|
||||
assert(t2.seconds() == seconds{5});
|
||||
assert(t2.mode() == 0);
|
||||
assert(t2.to_duration() == t1.to_duration());
|
||||
os << t2;
|
||||
assert(os.str() == "13:07:05");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user