1 // { dg-do compile { target c++20 } }
3 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
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)
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.
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/>.
20 // Class template day [time.cal.weekday]
28 using namespace std::chrono
;
38 static_assert(weekday
{3}[2].weekday() == weekday
{3});
39 static_assert(weekday
{3}[last
].weekday() == weekday
{3});
41 // Test for UB (overflow).
43 using rep
= days::rep
;
44 using std::numeric_limits
;
46 auto constexpr days_min
= days
{numeric_limits
<rep
>::min()};
47 auto constexpr weekday_from_sysdays_min
= weekday
{sys_days
{days_min
}};
48 auto constexpr weekday_000_plus_days_min
= weekday
{ 0 } + days_min
;
49 auto constexpr weekday_255_plus_days_min
= weekday
{255} + days_min
;
50 auto constexpr weekday_000_minus_days_min
= weekday
{ 0 } - days_min
;
51 auto constexpr weekday_255_minus_days_min
= weekday
{255} - days_min
;
53 auto constexpr days_max
= days
{numeric_limits
<rep
>::max()};
54 auto constexpr weekday_from_sysdays_max
= weekday
{sys_days
{days_max
}};
55 auto constexpr weekday_000_plus_days_max
= weekday
{ 0 } + days_max
;
56 auto constexpr weekday_255_plus_days_max
= weekday
{255} + days_max
;
57 auto constexpr weekday_000_minus_days_max
= weekday
{ 0 } - days_max
;
58 auto constexpr weekday_255_minus_days_max
= weekday
{255} - days_max
;
61 static_assert(weekday
{sys_days
{1900y
/January
/1}} == Monday
);
62 static_assert(weekday
{sys_days
{1970y
/January
/1}} == Thursday
);
63 static_assert(weekday
{sys_days
{2020y
/August
/21}} == Friday
);
65 static_assert(weekday
{local_days
{1900y
/January
/1}} == Monday
);
66 static_assert(weekday
{local_days
{1970y
/January
/1}} == Thursday
);
67 static_assert(weekday
{local_days
{2020y
/August
/21}} == Friday
);
69 static_assert(++weekday
{3} == weekday
{4});
70 static_assert(weekday
{3}++ == weekday
{3});
71 static_assert(--weekday
{3} == weekday
{2});
72 static_assert(weekday
{3}-- == weekday
{3});
73 static_assert((weekday
{3} += days
{3}) == weekday
{6});
74 static_assert((weekday
{3} -= days
{3}) == weekday
{0});
76 static_assert(Monday
+ days
{7000} == Monday
);
77 static_assert(Monday
+ days
{-7000} == Monday
);
78 static_assert(days
{7001} + Monday
== Tuesday
);
79 static_assert(days
{-7001} + Monday
== Sunday
);
80 static_assert(Monday
- days
{7000} == Monday
);
81 static_assert(Monday
- days
{-7000} == Monday
);
82 static_assert(Monday
- days
{7001} == Sunday
);
85 constexpr unsigned diff_tbl
[7][7]
86 = { { 0, 6, 5, 4, 3, 2, 1},
87 { 1, 0, 6, 5, 4, 3, 2},
88 { 2, 1, 0, 6, 5, 4, 3},
89 { 3, 2, 1, 0, 6, 5, 4},
90 { 4, 3, 2, 1, 0, 6, 5},
91 { 5, 4, 3, 2, 1, 0, 6},
92 { 6, 5, 4, 3, 2, 1, 0} };
93 for (unsigned x
= 0; x
< 7; x
++)
94 for (unsigned y
= 0; y
< 7; y
++)
96 if (weekday
{x
} - weekday
{y
} != days
{diff_tbl
[x
][y
]})
98 if (weekday
{x
} - days
{diff_tbl
[x
][y
]} != weekday
{y
})
100 if (weekday
{x
} != weekday
{y
} + days
{diff_tbl
[x
][y
]})
102 if (weekday
{x
} != days
{diff_tbl
[x
][y
]} + weekday
{y
})
108 static_assert(Sunday
.c_encoding() == 0);
109 static_assert(Sunday
.iso_encoding() == 7);
110 static_assert(Monday
.c_encoding() == 1);
111 static_assert(Monday
.iso_encoding() == 1);
113 static_assert(!weekday
{127}.ok());
114 static_assert(weekday
{0}.ok());
115 static_assert(weekday
{6}.ok());
116 static_assert(weekday
{7}.ok()); // Ctor wraps 7 to 0.
117 static_assert(!weekday
{8}.ok());
119 static_assert(weekday
{7} == weekday
{0});
120 static_assert(!(weekday
{0} == weekday
{1}));
121 static_assert( (weekday
{0} != weekday
{2}));