libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

year_month_weekday_last.pass.cpp (6437B)


      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_last;
     13 
     14 // constexpr year_month_weekday_last
     15 //   operator/(const year_month& ym, const weekday_last& wdl) noexcept;
     16 // Returns: {ym.year(), ym.month(), wdl}.
     17 //
     18 // constexpr year_month_weekday_last
     19 //   operator/(const year& y, const month_weekday_last& mwdl) noexcept;
     20 // Returns: {y, mwdl.month(), mwdl.weekday_last()}.
     21 //
     22 // constexpr year_month_weekday_last
     23 //   operator/(int y, const month_weekday_last& mwdl) noexcept;
     24 // Returns: year(y) / mwdl.
     25 //
     26 // constexpr year_month_weekday_last
     27 //   operator/(const month_weekday_last& mwdl, const year& y) noexcept;
     28 // Returns: y / mwdl.
     29 //
     30 // constexpr year_month_weekday_last
     31 //   operator/(const month_weekday_last& mwdl, int y) noexcept;
     32 // Returns: year(y) / mwdl.
     33 
     34 
     35 
     36 #include <chrono>
     37 #include <type_traits>
     38 #include <cassert>
     39 
     40 #include "test_macros.h"
     41 #include "test_comparisons.h"
     42 
     43 int main()
     44 {
     45     using year_month         = std::chrono::year_month;
     46     using year               = std::chrono::year;
     47     using month              = std::chrono::month;
     48     using weekday            = std::chrono::weekday;
     49     using weekday_last       = std::chrono::weekday_last;
     50     using month_weekday_last = std::chrono::month_weekday_last;
     51     using year_month_weekday_last = std::chrono::year_month_weekday_last;
     52 
     53     constexpr weekday Tuesday = std::chrono::Tuesday;
     54     constexpr month February = std::chrono::February;
     55 
     56     { // operator/(const year_month& ym, const weekday_last& wdl) (and switched)
     57         constexpr year_month Feb2018{year{2018}, February};
     58 
     59         ASSERT_NOEXCEPT (                                  Feb2018/weekday_last{Tuesday});
     60         ASSERT_SAME_TYPE(year_month_weekday_last, decltype(Feb2018/weekday_last{Tuesday}));
     61 
     62         static_assert((Feb2018/weekday_last{Tuesday}).year()    == year{2018}, "");
     63         static_assert((Feb2018/weekday_last{Tuesday}).month()   == February,   "");
     64         static_assert((Feb2018/weekday_last{Tuesday}).weekday() == Tuesday,    "");
     65 
     66         for (int i = 1000; i < 1010; ++i)
     67             for (unsigned j = 1; j <= 12; ++j)
     68                 for (unsigned k = 0; k <= 6; ++k)
     69                 {
     70                     year y{i};
     71                     month m{j};
     72                     weekday wd{k};
     73                     year_month_weekday_last ymwdl = year_month{y,m}/weekday_last{wd};
     74                     assert(ymwdl.year()    == y);
     75                     assert(ymwdl.month()   == m);
     76                     assert(ymwdl.weekday() == wd);
     77                 }
     78     }
     79 
     80 
     81     { // operator/(const year& y, const month_weekday_last& mwdl) (and switched)
     82         constexpr month_weekday_last FebLastTues{February, weekday_last{Tuesday}};
     83 
     84         ASSERT_NOEXCEPT (                                  year{2018}/FebLastTues);
     85         ASSERT_SAME_TYPE(year_month_weekday_last, decltype(year{2018}/FebLastTues));
     86         ASSERT_NOEXCEPT (                                  FebLastTues/year{2018});
     87         ASSERT_SAME_TYPE(year_month_weekday_last, decltype(FebLastTues/year{2018}));
     88 
     89 
     90         static_assert((year{2018}/FebLastTues).year()    == year{2018}, "");
     91         static_assert((year{2018}/FebLastTues).month()   == February,   "");
     92         static_assert((year{2018}/FebLastTues).weekday() == Tuesday,    "");
     93         static_assert((FebLastTues/year{2018}).year()    == year{2018}, "");
     94         static_assert((FebLastTues/year{2018}).month()   == February,   "");
     95         static_assert((FebLastTues/year{2018}).weekday() == Tuesday,    "");
     96 
     97 
     98         for (int i = 1000; i < 1010; ++i)
     99             for (unsigned j = 1; j <= 12; ++j)
    100                 for (unsigned k = 0; k <= 6; ++k)
    101                 {
    102                     year y{i};
    103                     month m{j};
    104                     weekday wd{k};
    105                     year_month_weekday_last ymwdl1 = y/month_weekday_last{m, weekday_last{wd}};
    106                     year_month_weekday_last ymwdl2 = month_weekday_last{m, weekday_last{wd}}/y;
    107                     assert(ymwdl1.year()    == y);
    108                     assert(ymwdl2.year()    == y);
    109                     assert(ymwdl1.month()   == m);
    110                     assert(ymwdl2.month()   == m);
    111                     assert(ymwdl1.weekday() == wd);
    112                     assert(ymwdl2.weekday() == wd);
    113                     assert(ymwdl1 == ymwdl2);
    114                 }
    115     }
    116 
    117 
    118     { // operator/(int y, const month_weekday_last& mwdl) (and switched)
    119         constexpr month_weekday_last FebLastTues{February, weekday_last{Tuesday}};
    120 
    121         ASSERT_NOEXCEPT (                                  2018/FebLastTues);
    122         ASSERT_SAME_TYPE(year_month_weekday_last, decltype(2018/FebLastTues));
    123         ASSERT_NOEXCEPT (                                  FebLastTues/2018);
    124         ASSERT_SAME_TYPE(year_month_weekday_last, decltype(FebLastTues/2018));
    125 
    126 
    127         static_assert((2018/FebLastTues).year()    == year{2018}, "");
    128         static_assert((2018/FebLastTues).month()   == February,   "");
    129         static_assert((2018/FebLastTues).weekday() == Tuesday,    "");
    130         static_assert((FebLastTues/2018).year()    == year{2018}, "");
    131         static_assert((FebLastTues/2018).month()   == February,   "");
    132         static_assert((FebLastTues/2018).weekday() == Tuesday,    "");
    133 
    134 
    135         for (int i = 1000; i < 1010; ++i)
    136             for (unsigned j = 1; j <= 12; ++j)
    137                 for (unsigned k = 0; k <= 6; ++k)
    138                 {
    139                     year y{i};
    140                     month m{j};
    141                     weekday wd{k};
    142                     year_month_weekday_last ymwdl1 = i/month_weekday_last{m, weekday_last{wd}};
    143                     year_month_weekday_last ymwdl2 = month_weekday_last{m, weekday_last{wd}}/i;
    144                     assert(ymwdl1.year()    == y);
    145                     assert(ymwdl2.year()    == y);
    146                     assert(ymwdl1.month()   == m);
    147                     assert(ymwdl2.month()   == m);
    148                     assert(ymwdl1.weekday() == wd);
    149                     assert(ymwdl2.weekday() == wd);
    150                     assert(ymwdl1 == ymwdl2);
    151                 }
    152     }
    153 }