]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/time/weekday/1.cc
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
;
45 constexpr weekday max
{sys_days
{days
{numeric_limits
<rep
>::max()}}};
46 constexpr weekday min
{sys_days
{days
{numeric_limits
<rep
>::min()}}};
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
);
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
);
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});
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
);
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
++)
84 if (weekday
{x
} - weekday
{y
} != days
{diff_tbl
[x
][y
]})
86 if (weekday
{x
} - days
{diff_tbl
[x
][y
]} != weekday
{y
})
88 if (weekday
{x
} != weekday
{y
} + days
{diff_tbl
[x
][y
]})
90 if (weekday
{x
} != days
{diff_tbl
[x
][y
]} + weekday
{y
})
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);
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());
107 static_assert(weekday
{7} == weekday
{0});
108 static_assert(!(weekday
{0} == weekday
{1}));
109 static_assert( (weekday
{0} != weekday
{2}));