Debian Codegraph @

libstdc++-v3/testsuite/std/time/month/1.cc

raw
1 // { dg-do compile { target c++20 } }
2
3 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // Class template day [time.cal.month]
21
22 #include <chrono>
23 #include <limits>
24
25 constexpr void
26 constexpr_month()
27 {
28 using namespace std::chrono;
29
30 month dm{};
31 ++(++dm);
32 dm++;
33 --(--dm);
34 dm--;
35 dm += months{3};
36 dm -= months{3};
37
38 // Test for UB (overflow).
39 {
40 using rep = months::rep;
41 using std::numeric_limits;
42
43 auto constexpr months_min = months{numeric_limits<rep>::min()};
44 auto constexpr month_000_plus_months_min = month{ 0 } + months_min;
45 auto constexpr month_255_plus_months_min = month{255} + months_min;
46 auto constexpr month_000_minus_months_min = month{ 0 } - months_min;
47 auto constexpr month_255_minus_months_min = month{255} - months_min;
48
49 auto constexpr months_max = months{numeric_limits<rep>::max()};
50 auto constexpr month_000_plus_months_max = month{ 0 } + months_max;
51 auto constexpr month_255_plus_months_max = month{255} + months_max;
52 auto constexpr month_000_minus_months_max = month{ 0 } - months_max;
53 auto constexpr month_255_minus_months_max = month{255} - months_max;
54 }
55
56 static_assert(February + months{11} == January);
57 static_assert(January + months{1200} == January);
58 static_assert(January + months{1201} == February);
59 static_assert(months{-1200} + January == January);
60 static_assert(months{-1201} + January == December);
61 static_assert(January - months{1200} == January);
62 static_assert(January - months{-1200} == January);
63 static_assert(January - months{1201} == December);
64
65 static_assert(January - February == months{11});
66 static_assert(February - January == months{1});
67 static_assert(June - June == months{});
68
69 static_assert(++month{4} == month{5});
70 static_assert(month{4}++ == month{4});
71 static_assert(--month{4} == month{3});
72 static_assert(month{4}-- == month{4});
73 static_assert((month{4} += months{3}) == month{7});
74 static_assert((month{4} -= months{3}) == month{1});
75
76 static_assert(!month{}.ok());
77 static_assert(month{1}.ok());
78 static_assert(month{12}.ok());
79 static_assert(!month{13}.ok());
80
81 static_assert(unsigned{month{7}} == 7);
82
83 static_assert(!(month{0} == month{1}));
84 static_assert( (month{0} != month{2}));
85 static_assert( (month{0} < month{3}));
86 static_assert(!(month{0} > month{4}));
87 static_assert( (month{0} <= month{5}));
88 static_assert(!(month{0} >= month{6}));
89
90 static_assert(month{0} <=> month{1} == std::strong_ordering::less);
91 static_assert(month{3} <=> month{3} == std::strong_ordering::equal);
92 static_assert(month{5} <=> month{2} == std::strong_ordering::greater);
93 }