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 weekday
24 // {
25 //     unsigned char wd_;
26 // public:
27 //     explicit constexpr weekday(unsigned wd) noexcept;
28 //     constexpr weekday(date::weekday wd) noexcept;
29 //     constexpr weekday(const sys_days& dp) noexcept;
30 //
31 //     weekday& operator++()    noexcept;
32 //     weekday  operator++(int) noexcept;
33 //     weekday& operator--()    noexcept;
34 //     weekday  operator--(int) noexcept;
35 //
36 //     weekday& operator+=(const days& d) noexcept;
37 //     weekday& operator-=(const days& d) noexcept;
38 //
39 //     constexpr explicit operator unsigned() const noexcept;
40 //     constexpr operator date::weekday() const noexcept;
41 //     constexpr bool ok() const noexcept;
42 //
43 //     // tested with weekday_indexed
44 //     constexpr weekday_indexed operator[](unsigned index) const noexcept;
45 //     // tested with weekday_last
46 //     constexpr weekday_last    operator[](last_spec)      const noexcept;
47 // };
48 
49 // constexpr bool operator==(const weekday& x, const weekday& y) noexcept;
50 // constexpr bool operator!=(const weekday& x, const weekday& y) noexcept;
51 
52 // constexpr weekday operator+(const weekday& x, const days&    y) noexcept;
53 // constexpr weekday operator+(const days&    x, const weekday& y) noexcept;
54 // constexpr weekday operator-(const weekday& x, const days&    y) noexcept;
55 // constexpr days    operator-(const weekday& x, const weekday& y) noexcept;
56 
57 // std::ostream& operator<<(std::ostream& os, const weekday& wd);
58 
59 // constexpr weekday sun{7};
60 // constexpr weekday mon{1};
61 // constexpr weekday tue{2};
62 // constexpr weekday wed{3};
63 // constexpr weekday thu{4};
64 // constexpr weekday fri{5};
65 // constexpr weekday sat{6};
66 
67 #include "iso_week.h"
68 #include "date.h"
69 
70 #include <cassert>
71 #include <sstream>
72 #include <type_traits>
73 
74 #include <iostream>
75 
76 static_assert( std::is_trivially_destructible<iso_week::weekday>{}, "");
77 static_assert(!std::is_default_constructible<iso_week::weekday>{}, "");
78 static_assert( std::is_trivially_copy_constructible<iso_week::weekday>{}, "");
79 static_assert( std::is_trivially_copy_assignable<iso_week::weekday>{}, "");
80 static_assert( std::is_trivially_move_constructible<iso_week::weekday>{}, "");
81 static_assert( std::is_trivially_move_assignable<iso_week::weekday>{}, "");
82 
83 static_assert(std::is_trivially_copyable<iso_week::weekday>{}, "");
84 static_assert(std::is_standard_layout<iso_week::weekday>{}, "");
85 static_assert(std::is_literal_type<iso_week::weekday>{}, "");
86 
87 static_assert( std::is_nothrow_constructible<iso_week::weekday, unsigned>{}, "");
88 static_assert( std::is_nothrow_constructible<iso_week::weekday, iso_week::sys_days>{}, "");
89 static_assert( std::is_nothrow_constructible<iso_week::weekday, date::weekday>{}, "");
90 static_assert( std::is_nothrow_constructible<date::weekday, iso_week::weekday>{}, "");
91 static_assert( std::is_nothrow_constructible<unsigned, iso_week::weekday>{}, "");
92 static_assert(!std::is_convertible<unsigned, iso_week::weekday>{}, "");
93 static_assert( std::is_convertible<iso_week::sys_days, iso_week::weekday>{}, "");
94 static_assert( std::is_convertible<iso_week::weekday, date::weekday>{}, "");
95 static_assert( std::is_convertible<date::weekday, iso_week::weekday>{}, "");
96 static_assert(!std::is_convertible<iso_week::weekday, unsigned>{}, "");
97 static_assert(static_cast<unsigned>(iso_week::weekday{1u}) == 1, "");
98 
99 static_assert(!iso_week::weekday{0u}.ok(), "");
100 static_assert( iso_week::weekday{1u}.ok(), "");
101 static_assert( iso_week::weekday{2u}.ok(), "");
102 static_assert( iso_week::weekday{3u}.ok(), "");
103 static_assert( iso_week::weekday{4u}.ok(), "");
104 static_assert( iso_week::weekday{5u}.ok(), "");
105 static_assert( iso_week::weekday{6u}.ok(), "");
106 static_assert( iso_week::weekday{7u}.ok(), "");
107 
108 void
test_weekday_arithmetic()109 test_weekday_arithmetic()
110 {
111     using namespace iso_week;
112     constexpr unsigned a[7][7] =
113     {// -    Mon Tue Wed Thu Fri Sat Sun
114      /*Mon*/ {0,  6,  5,  4,  3,  2,  1},
115      /*Tue*/ {1,  0,  6,  5,  4,  3,  2},
116      /*Wed*/ {2,  1,  0,  6,  5,  4,  3},
117      /*Thu*/ {3,  2,  1,  0,  6,  5,  4},
118      /*Fri*/ {4,  3,  2,  1,  0,  6,  5},
119      /*Sat*/ {5,  4,  3,  2,  1,  0,  6},
120      /*Sun*/ {6,  5,  4,  3,  2,  1,  0}
121     };
122     for (unsigned x = 1; x <= 7; ++x)
123     {
124         for (unsigned y = 1; y <= 7; ++y)
125         {
126             assert(weekday{x} - weekday{y} == days{a[x-1][y-1]});
127             assert(weekday{x} - days{a[x-1][y-1]} == weekday{y});
128             assert(weekday{x} == weekday{y} + days{a[x-1][y-1]});
129             assert(weekday{x} == days{a[x-1][y-1]} + weekday{y});
130         }
131     }
132     for (unsigned x = 1; x <= 7; ++x)
133     {
134         for (int y = -21; y < 21; ++y)
135         {
136             weekday wx{x};
137             days dy{y};
138             wx += dy;
139             weekday wz = weekday{x} + days{y};
140             assert(wx.ok());
141             assert(wz.ok());
142             assert(wx == wz);
143             assert(wx - weekday{x} == days{y % 7 + (y % 7 < 0 ? 7 : 0)});
144         }
145     }
146     for (unsigned x = 1; x <= 7; ++x)
147     {
148         for (int y = -21; y < 21; ++y)
149         {
150             weekday wx{x};
151             days dy{y};
152             wx -= dy;
153             assert(wx == weekday{x} + days{-y});
154         }
155     }
156     for (unsigned x = 1; x <= 7; ++x)
157     {
158         weekday wx{x};
159         assert(++wx - weekday{x} == days{1});
160         assert(wx++ - weekday{x} == days{1});
161         assert(wx - weekday{x} == days{2});
162         assert(wx-- - weekday{x} == days{2});
163         assert(wx - weekday{x} == days{1});
164         assert(--wx - weekday{x} == days{0});
165     }
166 }
167 
168 void
test_with_date_weekday()169 test_with_date_weekday()
170 {
171     auto constexpr d1 = iso_week::sun;
172     static_assert(unsigned{d1} == 7, "");
173     auto constexpr d2 = date::weekday{d1};
174     static_assert(d2 == date::Sunday, "");
175     auto constexpr d3 = iso_week::weekday{d2};
176     static_assert(unsigned{d3} == 7, "");
177 }
178 
179 int
main()180 main()
181 {
182     using namespace iso_week;
183 
184     static_assert(sun == weekday{7u}, "");
185     static_assert(mon == weekday{1u}, "");
186     static_assert(tue == weekday{2u}, "");
187     static_assert(wed == weekday{3u}, "");
188     static_assert(thu == weekday{4u}, "");
189     static_assert(fri == weekday{5u}, "");
190     static_assert(sat == weekday{6u}, "");
191 
192     static_assert(!(sun != sun), "");
193     static_assert(  sun != mon, "");
194     static_assert(  mon != sun, "");
195 
196     test_weekday_arithmetic();
197     test_with_date_weekday();
198 
199     std::ostringstream os;
200 
201     os << sun;
202     assert(os.str() == "Sun");
203     os.str("");
204 
205     os << mon;
206     assert(os.str() == "Mon");
207     os.str("");
208 
209     os << tue;
210     assert(os.str() == "Tue");
211     os.str("");
212 
213     os << wed;
214     assert(os.str() == "Wed");
215     os.str("");
216 
217     os << thu;
218     assert(os.str() == "Thu");
219     os.str("");
220 
221     os << fri;
222     assert(os.str() == "Fri");
223     os.str("");
224 
225     os << sat;
226     assert(os.str() == "Sat");
227 }
228