• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10 
11 // <chrono>
12 // class year_month_day;
13 
14 // constexpr year_month_day operator+(const year_month_day& ymd, const months& dm) noexcept;
15 //   Returns: (ymd.year() / ymd.month() + dm) / ymd.day().
16 //
17 // constexpr year_month_day operator+(const months& dm, const year_month_day& ymd) noexcept;
18 //   Returns: ymd + dm.
19 //
20 //
21 // constexpr year_month_day operator+(const year_month_day& ymd, const years& dy) noexcept;
22 //   Returns: (ymd.year() + dy) / ymd.month() / ymd.day().
23 //
24 // constexpr year_month_day operator+(const years& dy, const year_month_day& ymd) noexcept;
25 //   Returns: ym + dm.
26 
27 
28 
29 #include <chrono>
30 #include <type_traits>
31 #include <cassert>
32 
33 #include "test_macros.h"
34 
testConstexprYears(std::chrono::year_month_day ym)35 constexpr bool testConstexprYears(std::chrono::year_month_day ym)
36 {
37     std::chrono::years offset{23};
38     if (static_cast<int>((ym         ).year()) !=  1) return false;
39     if (static_cast<int>((ym + offset).year()) != 24) return false;
40     if (static_cast<int>((offset + ym).year()) != 24) return false;
41     return true;
42 }
43 
44 
testConstexprMonths(std::chrono::year_month_day ym)45 constexpr bool testConstexprMonths(std::chrono::year_month_day ym)
46 {
47     std::chrono::months offset{6};
48     if (static_cast<unsigned>((ym         ).month()) !=  1) return false;
49     if (static_cast<unsigned>((ym + offset).month()) !=  7) return false;
50     if (static_cast<unsigned>((offset + ym).month()) !=  7) return false;
51     return true;
52 }
53 
54 
main()55 int main()
56 {
57     using day        = std::chrono::day;
58     using year       = std::chrono::year;
59     using years      = std::chrono::years;
60     using month      = std::chrono::month;
61     using months     = std::chrono::months;
62     using year_month_day = std::chrono::year_month_day;
63 
64     {   // year_month_day + months
65     ASSERT_NOEXCEPT(std::declval<year_month_day>() + std::declval<months>());
66     ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month_day>());
67 
68     ASSERT_SAME_TYPE(year_month_day, decltype(std::declval<year_month_day>() + std::declval<months>()));
69     ASSERT_SAME_TYPE(year_month_day, decltype(std::declval<months>() + std::declval<year_month_day>()));
70 
71     static_assert(testConstexprMonths(year_month_day{year{1}, month{1}, day{1}}), "");
72 
73     year_month_day ym{year{1234}, std::chrono::January, day{12}};
74     for (int i = 0; i <= 10; ++i)  // TODO test wrap-around
75     {
76         year_month_day ym1 = ym + months{i};
77         year_month_day ym2 = months{i} + ym;
78         assert(static_cast<int>(ym1.year()) == 1234);
79         assert(static_cast<int>(ym2.year()) == 1234);
80         assert(ym1.month() == month(1 + i));
81         assert(ym2.month() == month(1 + i));
82         assert(ym1.day()   == day{12});
83         assert(ym2.day()   == day{12});
84         assert(ym1 == ym2);
85     }
86     }
87 
88     {   // year_month_day + years
89     ASSERT_NOEXCEPT(std::declval<year_month_day>() + std::declval<years>());
90     ASSERT_NOEXCEPT(std::declval<years>() + std::declval<year_month_day>());
91 
92     ASSERT_SAME_TYPE(year_month_day, decltype(std::declval<year_month_day>() + std::declval<years>()));
93     ASSERT_SAME_TYPE(year_month_day, decltype(std::declval<years>() + std::declval<year_month_day>()));
94 
95     static_assert(testConstexprYears (year_month_day{year{1}, month{1}, day{1}}), "");
96 
97     year_month_day ym{year{1234}, std::chrono::January, day{12}};
98     for (int i = 0; i <= 10; ++i)
99     {
100         year_month_day ym1 = ym + years{i};
101         year_month_day ym2 = years{i} + ym;
102         assert(static_cast<int>(ym1.year()) == i + 1234);
103         assert(static_cast<int>(ym2.year()) == i + 1234);
104         assert(ym1.month() == std::chrono::January);
105         assert(ym2.month() == std::chrono::January);
106         assert(ym1.day()   == day{12});
107         assert(ym2.day()   == day{12});
108         assert(ym1 == ym2);
109     }
110     }
111 
112 }
113