From cb3ffd10e0815401e33313e4009cdec98883f64f Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Fri, 14 Aug 2015 13:50:54 -0400 Subject: [PATCH] Add tests for year --- test/date_test/year.pass.cpp | 146 +++++++++++++++++++++++++++ test/date_test/year_p_year.fail.cpp | 32 ++++++ test/date_test/years_m_year.fail.cpp | 32 ++++++ 3 files changed, 210 insertions(+) create mode 100644 test/date_test/year.pass.cpp create mode 100644 test/date_test/year_p_year.fail.cpp create mode 100644 test/date_test/years_m_year.fail.cpp diff --git a/test/date_test/year.pass.cpp b/test/date_test/year.pass.cpp new file mode 100644 index 0000000..39ded20 --- /dev/null +++ b/test/date_test/year.pass.cpp @@ -0,0 +1,146 @@ +// 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. + +// class year +// { +// public: +// explicit constexpr year(int y) noexcept; +// +// year& operator++() noexcept; +// year operator++(int) noexcept; +// year& operator--() noexcept; +// year operator--(int) noexcept; +// +// year& operator+=(const years& y) noexcept; +// year& operator-=(const years& y) noexcept; +// +// constexpr bool is_leap() const noexcept; +// +// constexpr explicit operator int() const noexcept; +// constexpr bool ok() const noexcept; +// +// static constexpr year min() noexcept; +// static constexpr year max() noexcept; +// }; + +// constexpr bool operator==(const year& x, const year& y) noexcept; +// constexpr bool operator!=(const year& x, const year& y) noexcept; +// constexpr bool operator< (const year& x, const year& y) noexcept; +// constexpr bool operator> (const year& x, const year& y) noexcept; +// constexpr bool operator<=(const year& x, const year& y) noexcept; +// constexpr bool operator>=(const year& x, const year& y) noexcept; + +// constexpr year operator+(const year& x, const years& y) noexcept; +// constexpr year operator+(const years& x, const year& y) noexcept; +// constexpr year operator-(const year& x, const years& y) noexcept; +// constexpr years operator-(const year& x, const year& y) noexcept; + +// constexpr year operator "" _y(unsigned long long y) noexcept; +// std::ostream& operator<<(std::ostream& os, const year& y); + +#include "date.h" + +#include +#include +#include + +static_assert( std::is_trivially_destructible{}, ""); +static_assert(!std::is_default_constructible{}, ""); +static_assert( std::is_trivially_copy_constructible{}, ""); +static_assert( std::is_trivially_copy_assignable{}, ""); +static_assert( std::is_trivially_move_constructible{}, ""); +static_assert( std::is_trivially_move_assignable{}, ""); + +static_assert( std::is_nothrow_constructible{}, ""); +static_assert(!std::is_convertible{}, ""); +static_assert( std::is_nothrow_constructible{}, ""); +static_assert(!std::is_convertible{}, ""); +static_assert(static_cast(date::year{-1}) == -1, ""); + +template +constexpr +inline +std::chrono::duration +as(std::chrono::duration d) +{ + return d; +} + +int +main() +{ + using namespace date; + using namespace std::chrono; + using int64_t = std::int64_t; + + static_assert(year{2015} == 2015_y, ""); + static_assert(year{2015} != 2016_y, ""); + static_assert(year{2015} < 2016_y, ""); + static_assert(year{2016} > 2015_y, ""); + static_assert(year{2015} <= 2015_y, ""); + static_assert(year{2016} >= 2015_y, ""); + + static_assert(!year{2015}.is_leap(), ""); + static_assert( year{2016}.is_leap(), ""); + + static_assert(year::min().ok(), ""); + static_assert(year{2015}.ok(), ""); + static_assert(year{2016}.ok(), ""); + static_assert(year::max().ok(), ""); + +#if __cplusplus >= 201402 + static_assert(day_point(year::min()/jan/1) - day_point(1970_y/jan/1) + >= as(days::min()), ""); + static_assert(day_point(year::min()/jan/1) - day_point(1970_y/jan/1) + >= as(hours::min()), ""); + static_assert(day_point(year::min()/jan/1) - day_point(1970_y/jan/1) + >= as(minutes::min()), ""); + static_assert(day_point(year::min()/jan/1) - day_point(1970_y/jan/1) + >= as(seconds::min()), ""); + static_assert(day_point(year::min()/jan/1) - day_point(1970_y/jan/1) + >= as(milliseconds::min()), ""); + static_assert(day_point(year::min()/jan/1) - day_point(1970_y/jan/1) + >= as(microseconds::min()), ""); + static_assert(day_point(year::max()/dec/31) - day_point(1970_y/jan/1) + <= as(microseconds::max()), ""); + static_assert(day_point(year::max()/dec/31) - day_point(1970_y/jan/1) + <= as(milliseconds::max()), ""); + static_assert(day_point(year::max()/dec/31) - day_point(1970_y/jan/1) + <= as(seconds::max()), ""); + static_assert(day_point(year::max()/dec/31) - day_point(1970_y/jan/1) + <= as(minutes::max()), ""); + static_assert(day_point(year::max()/dec/31) - day_point(1970_y/jan/1) + <= as(hours::max()), ""); + static_assert(day_point(year::max()/dec/31) - day_point(1970_y/jan/1) + <= as(days::max()), ""); +#endif + + static_assert(2015_y - 2010_y == years{5}, ""); + static_assert(2015_y - years{5} == 2010_y, ""); + static_assert(2015_y == years{5} + 2010_y, ""); + static_assert(2015_y == 2010_y + years{5}, ""); + + auto y = 2015_y; + std::ostringstream os; + os << y; + assert(os.str() == "2015"); +} diff --git a/test/date_test/year_p_year.fail.cpp b/test/date_test/year_p_year.fail.cpp new file mode 100644 index 0000000..130676b --- /dev/null +++ b/test/date_test/year_p_year.fail.cpp @@ -0,0 +1,32 @@ +// 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. + +// year + year not allowed + +#include "date.h" + +int +main() +{ + using namespace date; + auto x = 2015_y + 2015_y; +} diff --git a/test/date_test/years_m_year.fail.cpp b/test/date_test/years_m_year.fail.cpp new file mode 100644 index 0000000..60fd839 --- /dev/null +++ b/test/date_test/years_m_year.fail.cpp @@ -0,0 +1,32 @@ +// 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. + +// years - year not allowed + +#include "date.h" + +int +main() +{ + using namespace date; + auto x = years{3} - 2015_y; +}