]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/time/time.cal/time.cal.month/time.cal.month.members/plus_minus_equal.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / utilities / time / time.cal / time.cal.month / time.cal.month.members / plus_minus_equal.pass.cpp
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 month;
13
14 // constexpr month& operator+=(const month& d) noexcept;
15 // constexpr month& operator-=(const month& d) noexcept;
16
17 #include <chrono>
18 #include <type_traits>
19 #include <cassert>
20
21 #include "test_macros.h"
22
23 template <typename M, typename Ms>
24 constexpr bool testConstexpr()
25 {
26     M m1{1};
27     if (static_cast<unsigned>(m1 += Ms{ 1}) !=  2) return false;
28     if (static_cast<unsigned>(m1 += Ms{ 2}) !=  4) return false;
29     if (static_cast<unsigned>(m1 += Ms{ 8}) != 12) return false;
30     if (static_cast<unsigned>(m1 -= Ms{ 1}) != 11) return false;
31     if (static_cast<unsigned>(m1 -= Ms{ 2}) !=  9) return false;
32     if (static_cast<unsigned>(m1 -= Ms{ 8}) !=  1) return false;
33     return true;
34 }
35
36 int main()
37 {
38     using month  = std::chrono::month;
39     using months = std::chrono::months;
40
41     ASSERT_NOEXCEPT(std::declval<month&>() += std::declval<months&>());
42     ASSERT_NOEXCEPT(std::declval<month&>() -= std::declval<months&>());
43     ASSERT_SAME_TYPE(month&, decltype(std::declval<month&>() += std::declval<months&>()));
44     ASSERT_SAME_TYPE(month&, decltype(std::declval<month&>() -= std::declval<months&>()));
45
46     static_assert(testConstexpr<month, months>(), "");
47
48     for (unsigned i = 1; i <= 10; ++i)
49     {
50         month month(i);
51         int exp = i + 10;
52         while (exp > 12)
53             exp -= 12;
54         assert(static_cast<unsigned>(month += months{10}) == static_cast<unsigned>(exp));
55         assert(static_cast<unsigned>(month)               == static_cast<unsigned>(exp));
56     }
57
58     for (unsigned i = 1; i <= 10; ++i)
59     {
60         month month(i);
61         int exp = i - 9;
62         while (exp < 1)
63             exp += 12;
64         assert(static_cast<unsigned>(month -= months{ 9}) == static_cast<unsigned>(exp));
65         assert(static_cast<unsigned>(month)               == static_cast<unsigned>(exp));
66     }
67 }