1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015, 2016 Howard Hinnant
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 // class weeknum
24 // {
25 //     unsigned char wn_;
26 //
27 // public:
28 //     explicit constexpr weeknum(unsigned wn) noexcept;
29 //
30 //     weeknum& operator++()    noexcept;
31 //     weeknum  operator++(int) noexcept;
32 //     weeknum& operator--()    noexcept;
33 //     weeknum  operator--(int) noexcept;
34 //
35 //     weeknum& operator+=(const weeks& y) noexcept;
36 //     weeknum& operator-=(const weeks& y) noexcept;
37 //
38 //     constexpr explicit operator unsigned() const noexcept;
39 //     constexpr bool ok() const noexcept;
40 // };
41 //
42 // constexpr bool operator==(const weeknum& x, const weeknum& y) noexcept;
43 // constexpr bool operator!=(const weeknum& x, const weeknum& y) noexcept;
44 // constexpr bool operator< (const weeknum& x, const weeknum& y) noexcept;
45 // constexpr bool operator> (const weeknum& x, const weeknum& y) noexcept;
46 // constexpr bool operator<=(const weeknum& x, const weeknum& y) noexcept;
47 // constexpr bool operator>=(const weeknum& x, const weeknum& y) noexcept;
48 //
49 // constexpr weeknum  operator+(const weeknum& x, const weeks&   y) noexcept;
50 // constexpr weeknum  operator+(const weeks&   x, const weeknum& y) noexcept;
51 // constexpr weeknum  operator-(const weeknum& x, const weeks&   y) noexcept;
52 // constexpr weeks    operator-(const weeknum& x, const weeknum& y) noexcept;
53 //
54 // std::ostream& operator<<(std::ostream& os, const weeknum& wn);
55 //
56 // constexpr weeknum operator "" _w(unsigned long long wn) noexcept;
57 
58 #include "iso_week.h"
59 
60 #include <cassert>
61 #include <sstream>
62 #include <type_traits>
63 
64 static_assert( std::is_trivially_destructible<iso_week::weeknum>{}, "");
65 static_assert(!std::is_default_constructible<iso_week::weeknum>{}, "");
66 static_assert( std::is_trivially_copy_constructible<iso_week::weeknum>{}, "");
67 static_assert( std::is_trivially_copy_assignable<iso_week::weeknum>{}, "");
68 static_assert( std::is_trivially_move_constructible<iso_week::weeknum>{}, "");
69 static_assert( std::is_trivially_move_assignable<iso_week::weeknum>{}, "");
70 
71 static_assert(std::is_trivially_copyable<iso_week::weeknum>{}, "");
72 static_assert(std::is_standard_layout<iso_week::weeknum>{}, "");
73 static_assert(std::is_literal_type<iso_week::weeknum>{}, "");
74 
75 static_assert( std::is_nothrow_constructible<iso_week::weeknum, unsigned>{}, "");
76 static_assert(!std::is_convertible<unsigned, iso_week::weeknum>{}, "");
77 static_assert( std::is_nothrow_constructible<unsigned, iso_week::weeknum>{}, "");
78 static_assert(!std::is_convertible<iso_week::weeknum, unsigned>{}, "");
79 static_assert(static_cast<unsigned>(iso_week::weeknum{3}) == 3, "");
80 
81 int
main()82 main()
83 {
84     using namespace iso_week;
85     using namespace std::chrono;
86 
87     static_assert(weeknum{2} == 2_w, "");
88     static_assert(unsigned{weeknum{2}} == 2, "");
89     static_assert(unsigned{weeknum{3}} == 3, "");
90     static_assert(weeknum{2} != 3_w, "");
91     static_assert(weeknum{2} < 3_w, "");
92     static_assert(weeknum{3} > 2_w, "");
93     static_assert(weeknum{2} <= 2_w, "");
94     static_assert(weeknum{3} >= 2_w, "");
95 
96     auto wn = weeknum{4};
97     assert(++wn == weeknum{5});
98     assert(wn == weeknum{5});
99     assert(wn++ == weeknum{5});
100     assert(wn == weeknum{6});
101     assert(--wn == weeknum{5});
102     assert(wn == weeknum{5});
103     assert(wn-- == weeknum{5});
104     assert(wn == weeknum{4});
105 
106     static_assert(!weeknum{0}.ok(), "");
107     static_assert( weeknum{1}.ok(), "");
108     static_assert( weeknum{53}.ok(), "");
109     static_assert(!weeknum{54}.ok(), "");
110 
111     static_assert(15_w - 10_w == weeks{5}, "");
112     static_assert(15_w - weeks{5} == 10_w, "");
113     static_assert(15_w == weeks{5} + 10_w, "");
114     static_assert(15_w == 10_w + weeks{5}, "");
115     static_assert(10_w - 15_w == -weeks{5}, "");
116 
117     auto w = 5_w;
118     std::ostringstream os;
119     os << w;
120     assert(os.str() == "W05");
121 }
122