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 day
24 // {
25 // unsigned char d_;
26 // public:
27 // explicit constexpr day(unsigned d) noexcept;
28 //
29 // day& operator++() noexcept;
30 // day operator++(int) noexcept;
31 // day& operator--() noexcept;
32 // day operator--(int) noexcept;
33 //
34 // day& operator+=(const days& d) noexcept;
35 // day& operator-=(const days& d) noexcept;
36 //
37 // constexpr explicit operator unsigned() const noexcept;
38 // constexpr bool ok() const noexcept;
39 // };
40 //
41 // constexpr bool operator==(const day& x, const day& y) noexcept;
42 // constexpr bool operator!=(const day& x, const day& y) noexcept;
43 // constexpr bool operator< (const day& x, const day& y) noexcept;
44 // constexpr bool operator> (const day& x, const day& y) noexcept;
45 // constexpr bool operator<=(const day& x, const day& y) noexcept;
46 // constexpr bool operator>=(const day& x, const day& y) noexcept;
47 //
48 // constexpr day operator+(const day& x, const days& y) noexcept;
49 // constexpr day operator+(const days& x, const day& y) noexcept;
50 // constexpr day operator-(const day& x, const days& y) noexcept;
51 // constexpr days operator-(const day& x, const day& y) noexcept;
52 //
53 // constexpr day operator "" _d(unsigned long long d) noexcept;
54 // std::ostream& operator<<(std::ostream& os, const day& d);
55
56 #include "date.h"
57
58 #include <cassert>
59 #include <sstream>
60 #include <type_traits>
61
62 static_assert( std::is_trivially_destructible<date::day>{}, "");
63 static_assert( std::is_default_constructible<date::day>{}, "");
64 static_assert( std::is_trivially_copy_constructible<date::day>{}, "");
65 static_assert( std::is_trivially_copy_assignable<date::day>{}, "");
66 static_assert( std::is_trivially_move_constructible<date::day>{}, "");
67 static_assert( std::is_trivially_move_assignable<date::day>{}, "");
68
69 static_assert( std::is_nothrow_constructible<date::day, unsigned>{}, "");
70 static_assert( std::is_nothrow_constructible<unsigned, date::day>{}, "");
71 static_assert(!std::is_convertible<unsigned, date::day>{}, "");
72 static_assert(!std::is_convertible<date::day, unsigned>{}, "");
73 static_assert(static_cast<unsigned>(date::day{1}) == 1, "");
74
75 static_assert(!date::day{0}.ok(), "");
76 static_assert( date::day{1}.ok(), "");
77 static_assert( date::day{2}.ok(), "");
78 static_assert( date::day{3}.ok(), "");
79 static_assert( date::day{29}.ok(), "");
80 static_assert( date::day{30}.ok(), "");
81 static_assert( date::day{31}.ok(), "");
82 static_assert(!date::day{32}.ok(), "");
83
84 int
main()85 main()
86 {
87 using namespace date;
88 static_assert(std::is_same<decltype(1_d), day>{}, "");
89
90 static_assert(1_d == day{1}, "");
91 static_assert(2_d == day{2}, "");
92
93 static_assert( 1_d == 1_d, "");
94 static_assert(!(1_d == 2_d), "");
95 static_assert(!(2_d == 1_d), "");
96
97 static_assert(!(1_d != 1_d), "");
98 static_assert( 1_d != 2_d, "");
99 static_assert( 2_d != 1_d, "");
100
101 static_assert(!(1_d < 1_d), "");
102 static_assert( 1_d < 2_d, "");
103 static_assert(!(2_d < 1_d), "");
104
105 static_assert( 1_d <= 1_d, "");
106 static_assert( 1_d <= 2_d, "");
107 static_assert(!(2_d <= 1_d), "");
108
109 static_assert(!(1_d > 1_d), "");
110 static_assert(!(1_d > 2_d), "");
111 static_assert( 2_d > 1_d, "");
112
113 static_assert( 1_d >= 1_d, "");
114 static_assert(!(1_d >= 2_d), "");
115 static_assert( 2_d >= 1_d, "");
116
117 static_assert(3_d + days{7} == 10_d, "");
118 static_assert(days{7} + 3_d == 10_d, "");
119 static_assert(3_d + weeks{1} == 10_d, "");
120 static_assert(weeks{1} + 3_d == 10_d, "");
121
122 static_assert(7_d - days{3} == 4_d, "");
123 static_assert(3_d - 7_d == days{-4}, "");
124 static_assert(25_d - 11_d == weeks{2}, "");
125
126 auto d = 1_d;
127 assert(++d == 2_d);
128 assert(d++ == 2_d);
129 assert(d == 3_d);
130 assert(d-- == 3_d);
131 assert(d == 2_d);
132 assert(--d == 1_d);
133 assert((d += days{2}) == 3_d);
134 assert((d -= days{2}) == 1_d);
135
136 std::ostringstream os;
137 os << d;
138 assert(os.str() == "01");
139 d += days{11};
140 os.str("");
141 os << d;
142 assert(os.str() == "12");
143 }
144