From 7796894a1cdd744ce69d54f97eef8254d33d046f Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sun, 9 Aug 2015 17:37:33 -0400 Subject: [PATCH] Add tests for year_month --- test/date_test/months_m_year_month.fail.cpp | 33 ++++ test/date_test/year_month.pass.cpp | 142 ++++++++++++++++++ .../year_month_p_year_month.fail.cpp | 33 ++++ test/date_test/years_m_year_month.fail.cpp | 33 ++++ 4 files changed, 241 insertions(+) create mode 100644 test/date_test/months_m_year_month.fail.cpp create mode 100644 test/date_test/year_month.pass.cpp create mode 100644 test/date_test/year_month_p_year_month.fail.cpp create mode 100644 test/date_test/years_m_year_month.fail.cpp diff --git a/test/date_test/months_m_year_month.fail.cpp b/test/date_test/months_m_year_month.fail.cpp new file mode 100644 index 0000000..2eaee8b --- /dev/null +++ b/test/date_test/months_m_year_month.fail.cpp @@ -0,0 +1,33 @@ +// 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. + +// months - year_month not allowed + +#include "date.h" + +int +main() +{ + using namespace date; + + auto x = months{1} - year_month{2015_y, jan}; +} diff --git a/test/date_test/year_month.pass.cpp b/test/date_test/year_month.pass.cpp new file mode 100644 index 0000000..f4463ab --- /dev/null +++ b/test/date_test/year_month.pass.cpp @@ -0,0 +1,142 @@ +// 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_month +// { +// public: +// constexpr year_month(const date::year& y, const date::month& m) noexcept; +// +// constexpr date::year year() const noexcept; +// constexpr date::month month() const noexcept; +// +// year_month& operator+=(const months& dm) noexcept; +// year_month& operator-=(const months& dm) noexcept; +// year_month& operator+=(const years& dy) noexcept; +// year_month& operator-=(const years& dy) noexcept; +// +// constexpr bool ok() const noexcept; +// }; + +// constexpr bool operator==(const year_month& x, const year_month& y) noexcept; +// constexpr bool operator!=(const year_month& x, const year_month& y) noexcept; +// constexpr bool operator< (const year_month& x, const year_month& y) noexcept; +// constexpr bool operator> (const year_month& x, const year_month& y) noexcept; +// constexpr bool operator<=(const year_month& x, const year_month& y) noexcept; +// constexpr bool operator>=(const year_month& x, const year_month& y) noexcept; + +// constexpr year_month operator+(const year_month& ym, const months& dm) noexcept; +// constexpr year_month operator+(const months& dm, const year_month& ym) noexcept; +// constexpr year_month operator-(const year_month& ym, const months& dm) noexcept; + +// constexpr months operator-(const year_month& x, const year_month& y) noexcept; +// constexpr year_month operator+(const year_month& ym, const years& dy) noexcept; +// constexpr year_month operator+(const years& dy, const year_month& ym) noexcept; +// constexpr year_month operator-(const year_month& ym, const years& dy) noexcept; + +// std::ostream& operator<<(std::ostream& os, const year_month& ym); + +#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{}, ""); + +void +test_arithmetic() +{ + using namespace date; + using namespace std::chrono; + + for (int y1 = 2010; y1 <= 2015; ++y1) + { + for (int y2 = 2010; y2 <= 2015; ++y2) + { + for (unsigned m1 = 1; m1 <= 12; ++m1) + { + for (unsigned m2 = 1; m2 <= 12; ++m2) + { + year_month ym1{year{y1}, month{m1}}; + year_month ym2 = {year{y2}, month{m2}}; + months dm = ym1 - ym2; + assert((dm < months{0}) == (ym1 < ym2)); + assert((dm == months{0}) == (ym1 == ym2)); + assert((dm > months{0}) == (ym1 > ym2)); + assert(dm + ym2 == ym1); + assert(ym2 + dm == ym1); + assert(ym1 - dm == ym2); + years dy{y1-y2}; + assert((ym1 + dy == year_month{ym1.year() + dy, ym1.month()})); + assert((dy + ym1 == year_month{ym1.year() + dy, ym1.month()})); + assert((ym1 - dy == year_month{ym1.year() - dy, ym1.month()})); + assert((year_month{ym2} += dm) == ym1); + assert((year_month{ym1} -= dm) == ym2); + assert(((year_month{ym1} += dy) == + year_month{ym1.year() + dy, ym1.month()})); + assert(((year_month{ym1} -= dy) == + year_month{ym1.year() - dy, ym1.month()})); + } + } + } + } +} + +int +main() +{ + using namespace date; + + constexpr year_month ym1 = {2015_y, jun}; + static_assert(ym1.year() == year{2015}, ""); + static_assert(ym1.month() == jun, ""); + static_assert(ym1.ok(), ""); + + constexpr year_month ym2 = {2016_y, may}; + static_assert(ym2.year() == year{2016}, ""); + static_assert(ym2.month() == may, ""); + static_assert(ym2.ok(), ""); + + static_assert(ym1 == ym1, ""); + static_assert(ym1 != ym2, ""); + static_assert(ym1 < ym2, ""); + static_assert(ym1 <= ym2, ""); + static_assert(ym2 > ym1, ""); + static_assert(ym2 >= ym2, ""); + + static_assert(ym2 - ym1 == months{11}, ""); + static_assert(ym1 - ym2 == -months{11}, ""); + + test_arithmetic(); + + std::ostringstream os; + os << ym1; + assert(os.str() == "2015/Jun"); +} diff --git a/test/date_test/year_month_p_year_month.fail.cpp b/test/date_test/year_month_p_year_month.fail.cpp new file mode 100644 index 0000000..6e70842 --- /dev/null +++ b/test/date_test/year_month_p_year_month.fail.cpp @@ -0,0 +1,33 @@ +// 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_month + year_month not allowed + +#include "date.h" + +int +main() +{ + using namespace date; + + auto x = year_month{2015_y, jan} + year_month{2015_y, jan}; +} diff --git a/test/date_test/years_m_year_month.fail.cpp b/test/date_test/years_m_year_month.fail.cpp new file mode 100644 index 0000000..d1fbb4f --- /dev/null +++ b/test/date_test/years_m_year_month.fail.cpp @@ -0,0 +1,33 @@ +// 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_month not allowed + +#include "date.h" + +int +main() +{ + using namespace date; + + auto x = years{1} - year_month{2015_y, jan}; +}