1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9
10 // <chrono>
11 // class year;
12
13 // constexpr year& operator+=(const years& d) noexcept;
14 // constexpr year& operator-=(const years& d) noexcept;
15
16 #include <chrono>
17 #include <type_traits>
18 #include <cassert>
19
20 #include "test_macros.h"
21
22 template <typename Y, typename Ys>
testConstexpr()23 constexpr bool testConstexpr()
24 {
25 Y y1{1};
26 if (static_cast<int>(y1 += Ys{ 1}) != 2) return false;
27 if (static_cast<int>(y1 += Ys{ 2}) != 4) return false;
28 if (static_cast<int>(y1 += Ys{ 8}) != 12) return false;
29 if (static_cast<int>(y1 -= Ys{ 1}) != 11) return false;
30 if (static_cast<int>(y1 -= Ys{ 2}) != 9) return false;
31 if (static_cast<int>(y1 -= Ys{ 8}) != 1) return false;
32 return true;
33 }
34
main(int,char **)35 int main(int, char**)
36 {
37 using year = std::chrono::year;
38 using years = std::chrono::years;
39
40 ASSERT_NOEXCEPT(std::declval<year&>() += std::declval<years>());
41 ASSERT_NOEXCEPT(std::declval<year&>() -= std::declval<years>());
42
43 ASSERT_SAME_TYPE(year&, decltype(std::declval<year&>() += std::declval<years>()));
44 ASSERT_SAME_TYPE(year&, decltype(std::declval<year&>() -= std::declval<years>()));
45
46 static_assert(testConstexpr<year, years>(), "");
47
48 for (int i = 10000; i <= 10020; ++i)
49 {
50 year yr(i);
51 assert(static_cast<int>(yr += years{10}) == i + 10);
52 assert(static_cast<int>(yr) == i + 10);
53 assert(static_cast<int>(yr -= years{ 9}) == i + 1);
54 assert(static_cast<int>(yr) == i + 1);
55 }
56
57 return 0;
58 }
59