year_month_weekday.pass.cpp (6152B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 10 11 // <chrono> 12 // class year_month_weekday; 13 14 // constexpr year_month_weekday 15 // operator/(const year_month& ym, const weekday_indexed& wdi) noexcept; 16 // Returns: {ym.year(), ym.month(), wdi}. 17 // 18 // constexpr year_month_weekday 19 // operator/(const year& y, const month_weekday& mwd) noexcept; 20 // Returns: {y, mwd.month(), mwd.weekday_indexed()}. 21 // 22 // constexpr year_month_weekday 23 // operator/(int y, const month_weekday& mwd) noexcept; 24 // Returns: year(y) / mwd. 25 // 26 // constexpr year_month_weekday 27 // operator/(const month_weekday& mwd, const year& y) noexcept; 28 // Returns: y / mwd. 29 // 30 // constexpr year_month_weekday 31 // operator/(const month_weekday& mwd, int y) noexcept; 32 // Returns: year(y) / mwd. 33 34 #include <chrono> 35 #include <type_traits> 36 #include <cassert> 37 38 #include "test_macros.h" 39 #include "test_comparisons.h" 40 41 int main() 42 { 43 using year = std::chrono::year; 44 using year_month = std::chrono::year_month; 45 using month_weekday = std::chrono::month_weekday; 46 using year_month_weekday = std::chrono::year_month_weekday; 47 using month = std::chrono::month; 48 using weekday = std::chrono::weekday; 49 using weekday = std::chrono::weekday; 50 using weekday_indexed = std::chrono::weekday_indexed; 51 52 constexpr weekday Tuesday = std::chrono::Tuesday; 53 constexpr month February = std::chrono::February; 54 55 56 { // operator/(const year_month& ym, const weekday_indexed& wdi) 57 constexpr year_month Feb2018{year{2018}, February}; 58 59 ASSERT_NOEXCEPT ( Feb2018/weekday_indexed{Tuesday, 2}); 60 ASSERT_SAME_TYPE(year_month_weekday, decltype(Feb2018/weekday_indexed{Tuesday, 2})); 61 62 static_assert((Feb2018/weekday_indexed{Tuesday, 2}).year() == year{2018}, "" ); 63 static_assert((Feb2018/weekday_indexed{Tuesday, 2}).month() == February, "" ); 64 static_assert((Feb2018/weekday_indexed{Tuesday, 2}).weekday() == Tuesday, "" ); 65 66 for (int i = 1000; i < 1010; ++i) 67 for (int j = 1; j <= 12; ++j) 68 for (unsigned k = 0; k <= 6; ++k) 69 for (unsigned l = 1; l <= 5; ++l) 70 { 71 year y(i); 72 month m(j); 73 weekday wd{k}; 74 year_month_weekday ymd = year_month{y,m}/weekday_indexed{wd,l}; 75 assert(ymd.year() == y); 76 assert(ymd.month() == m); 77 assert(ymd.weekday() == wd); 78 } 79 } 80 81 { // operator/(const year& y, const month_weekday& mwd) (and switched) 82 constexpr month_weekday Feb1stTues{February, weekday_indexed{Tuesday, 1}}; 83 ASSERT_NOEXCEPT ( year{2018}/Feb1stTues); 84 ASSERT_SAME_TYPE(year_month_weekday, decltype(year{2018}/Feb1stTues)); 85 ASSERT_NOEXCEPT ( Feb1stTues/year{2018}); 86 ASSERT_SAME_TYPE(year_month_weekday, decltype(Feb1stTues/year{2018})); 87 88 static_assert((year{2018}/Feb1stTues).year() == year{2018}, "" ); 89 static_assert((year{2018}/Feb1stTues).month() == February, "" ); 90 static_assert((year{2018}/Feb1stTues).weekday() == Tuesday, "" ); 91 92 for (int i = 1000; i < 1010; ++i) 93 for (int j = 1; j <= 12; ++j) 94 for (unsigned k = 0; k <= 6; ++k) 95 for (unsigned l = 1; l <= 5; ++l) 96 { 97 year y(i); 98 month m(j); 99 weekday wd{k}; 100 month_weekday mwd{m, weekday_indexed{weekday{k}, l}}; 101 year_month_weekday ymd1 = y/mwd; 102 year_month_weekday ymd2 = mwd/y; 103 assert(ymd1.year() == y); 104 assert(ymd2.year() == y); 105 assert(ymd1.month() == m); 106 assert(ymd2.month() == m); 107 assert(ymd1.weekday() == wd); 108 assert(ymd2.weekday() == wd); 109 assert(ymd1 == ymd2); 110 } 111 } 112 113 114 { // operator/(int y, const month_weekday& mwd) (and switched) 115 constexpr month_weekday Feb1stTues{February, weekday_indexed{Tuesday, 1}}; 116 ASSERT_NOEXCEPT ( 2018/Feb1stTues); 117 ASSERT_SAME_TYPE(year_month_weekday, decltype(2018/Feb1stTues)); 118 ASSERT_NOEXCEPT ( Feb1stTues/2018); 119 ASSERT_SAME_TYPE(year_month_weekday, decltype(Feb1stTues/2018)); 120 121 static_assert((2018/Feb1stTues).year() == year{2018}, "" ); 122 static_assert((2018/Feb1stTues).month() == February, "" ); 123 static_assert((2018/Feb1stTues).weekday() == Tuesday, "" ); 124 125 for (int i = 1000; i < 1010; ++i) 126 for (int j = 1; j <= 12; ++j) 127 for (unsigned k = 0; k <= 6; ++k) 128 for (unsigned l = 1; l <= 5; ++l) 129 { 130 year y(i); 131 month m(j); 132 weekday wd{k}; 133 month_weekday mwd{m, weekday_indexed{weekday{k}, l}}; 134 year_month_weekday ymd1 = i/mwd; 135 year_month_weekday ymd2 = mwd/i; 136 assert(ymd1.year() == y); 137 assert(ymd2.year() == y); 138 assert(ymd1.month() == m); 139 assert(ymd2.month() == m); 140 assert(ymd1.weekday() == wd); 141 assert(ymd2.weekday() == wd); 142 assert(ymd1 == ymd2); 143 } 144 } 145 }