]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/time/weekday/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / time / weekday / 1.cc
1 // { dg-do compile { target c++20 } }
2
3 // Copyright (C) 2020-2024 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.weekday]
21
22 #include <chrono>
23 #include <limits>
24
25 constexpr void
26 constexpr_weekday()
27 {
28 using namespace std::chrono;
29
30 weekday dwd{};
31 ++dwd;
32 dwd++;
33 --dwd;
34 dwd--;
35 dwd += days{3};
36 dwd -= days{3};
37
38 static_assert(weekday{3}[2].weekday() == weekday{3});
39 static_assert(weekday{3}[last].weekday() == weekday{3});
40
41 // Test for UB (overflow).
42 {
43 using rep = days::rep;
44 using std::numeric_limits;
45 constexpr weekday max{sys_days{days{numeric_limits<rep>::max()}}};
46 constexpr weekday min{sys_days{days{numeric_limits<rep>::min()}}};
47 }
48
49 static_assert(weekday{sys_days{1900y/January/1}} == Monday);
50 static_assert(weekday{sys_days{1970y/January/1}} == Thursday);
51 static_assert(weekday{sys_days{2020y/August/21}} == Friday);
52
53 static_assert(weekday{local_days{1900y/January/1}} == Monday);
54 static_assert(weekday{local_days{1970y/January/1}} == Thursday);
55 static_assert(weekday{local_days{2020y/August/21}} == Friday);
56
57 static_assert(++weekday{3} == weekday{4});
58 static_assert(weekday{3}++ == weekday{3});
59 static_assert(--weekday{3} == weekday{2});
60 static_assert(weekday{3}-- == weekday{3});
61 static_assert((weekday{3} += days{3}) == weekday{6});
62 static_assert((weekday{3} -= days{3}) == weekday{0});
63
64 static_assert(Monday + days{7000} == Monday);
65 static_assert(Monday + days{-7000} == Monday);
66 static_assert(days{7001} + Monday == Tuesday);
67 static_assert(days{-7001} + Monday == Sunday);
68 static_assert(Monday - days{7000} == Monday);
69 static_assert(Monday - days{-7000} == Monday);
70 static_assert(Monday - days{7001} == Sunday);
71
72 static_assert([] {
73 constexpr unsigned diff_tbl[7][7]
74 = { { 0, 6, 5, 4, 3, 2, 1},
75 { 1, 0, 6, 5, 4, 3, 2},
76 { 2, 1, 0, 6, 5, 4, 3},
77 { 3, 2, 1, 0, 6, 5, 4},
78 { 4, 3, 2, 1, 0, 6, 5},
79 { 5, 4, 3, 2, 1, 0, 6},
80 { 6, 5, 4, 3, 2, 1, 0} };
81 for (unsigned x = 0; x < 7; x++)
82 for (unsigned y = 0; y < 7; y++)
83 {
84 if (weekday{x} - weekday{y} != days{diff_tbl[x][y]})
85 return false;
86 if (weekday{x} - days{diff_tbl[x][y]} != weekday{y})
87 return false;
88 if (weekday{x} != weekday{y} + days{diff_tbl[x][y]})
89 return false;
90 if (weekday{x} != days{diff_tbl[x][y]} + weekday{y})
91 return false;
92 }
93 return true;
94 }());
95
96 static_assert(Sunday.c_encoding() == 0);
97 static_assert(Sunday.iso_encoding() == 7);
98 static_assert(Monday.c_encoding() == 1);
99 static_assert(Monday.iso_encoding() == 1);
100
101 static_assert(!weekday{127}.ok());
102 static_assert(weekday{0}.ok());
103 static_assert(weekday{6}.ok());
104 static_assert(weekday{7}.ok()); // Ctor wraps 7 to 0.
105 static_assert(!weekday{8}.ok());
106
107 static_assert(weekday{7} == weekday{0});
108 static_assert(!(weekday{0} == weekday{1}));
109 static_assert( (weekday{0} != weekday{2}));
110 }