From 906f5eb4f99a3ba32ead2154a8aaedd02928c62e Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Mon, 21 Dec 2015 21:33:50 -0500 Subject: [PATCH] Tests for iso_week::weeknum --- test/iso_week/weeknum.pass.cpp | 121 +++++++++++++++++++++++ test/iso_week/weeknum_p_weeknum.fail.cpp | 32 ++++++ 2 files changed, 153 insertions(+) create mode 100644 test/iso_week/weeknum.pass.cpp create mode 100644 test/iso_week/weeknum_p_weeknum.fail.cpp diff --git a/test/iso_week/weeknum.pass.cpp b/test/iso_week/weeknum.pass.cpp new file mode 100644 index 0000000..1289d32 --- /dev/null +++ b/test/iso_week/weeknum.pass.cpp @@ -0,0 +1,121 @@ +// 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 weeknum +// { +// unsigned char wn_; +// +// public: +// explicit constexpr weeknum(unsigned wn) noexcept; +// +// weeknum& operator++() noexcept; +// weeknum operator++(int) noexcept; +// weeknum& operator--() noexcept; +// weeknum operator--(int) noexcept; +// +// weeknum& operator+=(const weeks& y) noexcept; +// weeknum& operator-=(const weeks& y) noexcept; +// +// constexpr explicit operator unsigned() const noexcept; +// constexpr bool ok() const noexcept; +// }; +// +// constexpr bool operator==(const weeknum& x, const weeknum& y) noexcept; +// constexpr bool operator!=(const weeknum& x, const weeknum& y) noexcept; +// constexpr bool operator< (const weeknum& x, const weeknum& y) noexcept; +// constexpr bool operator> (const weeknum& x, const weeknum& y) noexcept; +// constexpr bool operator<=(const weeknum& x, const weeknum& y) noexcept; +// constexpr bool operator>=(const weeknum& x, const weeknum& y) noexcept; +// +// constexpr weeknum operator+(const weeknum& x, const weeks& y) noexcept; +// constexpr weeknum operator+(const weeks& x, const weeknum& y) noexcept; +// constexpr weeknum operator-(const weeknum& x, const weeks& y) noexcept; +// constexpr weeks operator-(const weeknum& x, const weeknum& y) noexcept; +// +// std::ostream& operator<<(std::ostream& os, const weeknum& wn); +// +// constexpr weeknum operator "" _w(unsigned long long wn) noexcept; + +#include "iso_week.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_trivially_copyable{}, ""); +static_assert(std::is_standard_layout{}, ""); +static_assert(std::is_literal_type{}, ""); + +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(iso_week::weeknum{3}) == 3, ""); + +int +main() +{ + using namespace iso_week; + using namespace std::chrono; + + static_assert(weeknum{2} == 2_w, ""); + static_assert(unsigned{weeknum{2}} == 2, ""); + static_assert(unsigned{weeknum{3}} == 3, ""); + static_assert(weeknum{2} != 3_w, ""); + static_assert(weeknum{2} < 3_w, ""); + static_assert(weeknum{3} > 2_w, ""); + static_assert(weeknum{2} <= 2_w, ""); + static_assert(weeknum{3} >= 2_w, ""); + + auto wn = weeknum{4}; + assert(++wn == weeknum{5}); + assert(wn == weeknum{5}); + assert(wn++ == weeknum{5}); + assert(wn == weeknum{6}); + assert(--wn == weeknum{5}); + assert(wn == weeknum{5}); + assert(wn-- == weeknum{5}); + assert(wn == weeknum{4}); + + static_assert(!weeknum{0}.ok(), ""); + static_assert( weeknum{1}.ok(), ""); + static_assert( weeknum{53}.ok(), ""); + static_assert(!weeknum{54}.ok(), ""); + + static_assert(15_w - 10_w == weeks{5}, ""); + static_assert(15_w - weeks{5} == 10_w, ""); + static_assert(15_w == weeks{5} + 10_w, ""); + static_assert(15_w == 10_w + weeks{5}, ""); + static_assert(10_w - 15_w == -weeks{5}, ""); + + auto w = 5_w; + std::ostringstream os; + os << w; + assert(os.str() == "W05"); +} diff --git a/test/iso_week/weeknum_p_weeknum.fail.cpp b/test/iso_week/weeknum_p_weeknum.fail.cpp new file mode 100644 index 0000000..8d2b0db --- /dev/null +++ b/test/iso_week/weeknum_p_weeknum.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. + +// weeknum + weeknum not allowed + +#include "iso_week.h" + +int +main() +{ + using namespace iso_week; + auto x = 3_w + 4_w; +}