| 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.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 | |
| 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; |
| 52 | |
| 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; |
| 59 | } |
| 60 | |
| 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); |
| 64 | |
| 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); |
| 68 | |
| 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}); |
| 75 | |
| 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); |
| 83 | |
| 84 | static_assert([] { |
| 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++) |
| 95 | { |
| 96 | if (weekday{x} - weekday{y} != days{diff_tbl[x][y]}) |
| 97 | return false; |
| 98 | if (weekday{x} - days{diff_tbl[x][y]} != weekday{y}) |
| 99 | return false; |
| 100 | if (weekday{x} != weekday{y} + days{diff_tbl[x][y]}) |
| 101 | return false; |
| 102 | if (weekday{x} != days{diff_tbl[x][y]} + weekday{y}) |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | }()); |
| 107 | |
| 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); |
| 112 | |
| 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()); |
| 118 | |
| 119 | static_assert(weekday{7} == weekday{0}); |
| 120 | static_assert(!(weekday{0} == weekday{1})); |
| 121 | static_assert( (weekday{0} != weekday{2})); |
| 122 | } |