• 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;
13 
14 // constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;
15 // Returns: (ym.year() + dy) / ym.month().
16 //
17 // constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;
18 // Returns: ym + dy.
19 //
20 // constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;
21 // Returns: A year_month value z such that z - ym == dm.
22 // Complexity: O(1) with respect to the value of dm.
23 //
24 // constexpr year_month operator+(const months& dm, const year_month& ym) 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 ym)35 constexpr bool testConstexprYears(std::chrono::year_month 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 ym)45 constexpr bool testConstexprMonths(std::chrono::year_month 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 year       = std::chrono::year;
58     using years      = std::chrono::years;
59     using month      = std::chrono::month;
60     using months     = std::chrono::months;
61     using year_month = std::chrono::year_month;
62 
63     {   // year_month + years
64     ASSERT_NOEXCEPT(std::declval<year_month>() + std::declval<years>());
65     ASSERT_NOEXCEPT(std::declval<years>() + std::declval<year_month>());
66 
67     ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() + std::declval<years>()));
68     ASSERT_SAME_TYPE(year_month, decltype(std::declval<years>() + std::declval<year_month>()));
69 
70     static_assert(testConstexprYears (year_month{year{1}, month{1}}), "");
71 
72     year_month ym{year{1234}, std::chrono::January};
73     for (int i = 0; i <= 10; ++i)
74     {
75         year_month ym1 = ym + years{i};
76         year_month ym2 = years{i} + ym;
77         assert(static_cast<int>(ym1.year()) == i + 1234);
78         assert(static_cast<int>(ym2.year()) == i + 1234);
79         assert(ym1.month() == std::chrono::January);
80         assert(ym2.month() == std::chrono::January);
81         assert(ym1 == ym2);
82     }
83     }
84 
85     {   // year_month + months
86     ASSERT_NOEXCEPT(std::declval<year_month>() + std::declval<months>());
87     ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month>());
88 
89     ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() + std::declval<months>()));
90     ASSERT_SAME_TYPE(year_month, decltype(std::declval<months>() + std::declval<year_month>()));
91 
92     static_assert(testConstexprMonths(year_month{year{1}, month{1}}), "");
93 
94     year_month ym{year{1234}, std::chrono::January};
95     for (int i = 0; i <= 10; ++i)  // TODO test wrap-around
96     {
97         year_month ym1 = ym + months{i};
98         year_month ym2 = months{i} + ym;
99         assert(static_cast<int>(ym1.year()) == 1234);
100         assert(static_cast<int>(ym2.year()) == 1234);
101         assert(ym1.month() == month(1 + i));
102         assert(ym2.month() == month(1 + i));
103         assert(ym1 == ym2);
104     }
105     }
106 }
107