Home | History | Annotate | Download | only in time.cal.ymdlast.nonmembers
      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_day_last;
     13 
     14 // constexpr year_month_day_last
     15 //   operator-(const year_month_day_last& ymdl, const months& dm) noexcept;
     16 //
     17 //   Returns: ymdl + (-dm).
     18 //
     19 // constexpr year_month_day_last
     20 //   operator-(const year_month_day_last& ymdl, const years& dy) noexcept;
     21 //
     22 //   Returns: ymdl + (-dy).
     23 
     24 
     25 #include <chrono>
     26 #include <type_traits>
     27 #include <cassert>
     28 
     29 #include "test_macros.h"
     30 
     31 #include <iostream>
     32 
     33 constexpr bool testConstexprYears (std::chrono::year_month_day_last ymdl)
     34 {
     35     std::chrono::year_month_day_last ym1 = ymdl - std::chrono::years{10};
     36     return
     37         ym1.year()  == std::chrono::year{static_cast<int>(ymdl.year()) - 10}
     38      && ym1.month() == ymdl.month()
     39         ;
     40 }
     41 
     42 constexpr bool testConstexprMonths (std::chrono::year_month_day_last ymdl)
     43 {
     44     std::chrono::year_month_day_last ym1 = ymdl - std::chrono::months{6};
     45     return
     46         ym1.year()  == ymdl.year()
     47      && ym1.month() == std::chrono::month{static_cast<unsigned>(ymdl.month()) - 6}
     48         ;
     49 }
     50 
     51 int main()
     52 {
     53     using year                = std::chrono::year;
     54     using month               = std::chrono::month;
     55     using month_day_last      = std::chrono::month_day_last;
     56     using year_month_day_last = std::chrono::year_month_day_last;
     57     using months              = std::chrono::months;
     58     using years               = std::chrono::years;
     59 
     60     constexpr month December = std::chrono::December;
     61 
     62     { // year_month_day_last - years
     63     ASSERT_NOEXCEPT(                               std::declval<year_month_day_last>() - std::declval<years>());
     64     ASSERT_SAME_TYPE(year_month_day_last, decltype(std::declval<year_month_day_last>() - std::declval<years>()));
     65 
     66     static_assert(testConstexprYears(year_month_day_last{year{1234}, month_day_last{December}}), "");
     67     year_month_day_last ym{year{1234}, month_day_last{December}};
     68     for (int i = 0; i <= 10; ++i)
     69     {
     70         year_month_day_last ym1 = ym - years{i};
     71         assert(static_cast<int>(ym1.year()) == 1234 - i);
     72         assert(ym1.month() == December);
     73     }
     74     }
     75 
     76     { // year_month_day_last - months
     77     ASSERT_NOEXCEPT(                               std::declval<year_month_day_last>() - std::declval<months>());
     78     ASSERT_SAME_TYPE(year_month_day_last, decltype(std::declval<year_month_day_last>() - std::declval<months>()));
     79 
     80     static_assert(testConstexprMonths(year_month_day_last{year{1234}, month_day_last{December}}), "");
     81 //  TODO test wrapping
     82     year_month_day_last ym{year{1234}, month_day_last{December}};
     83     for (unsigned i = 0; i <= 10; ++i)
     84     {
     85         year_month_day_last ym1 = ym - months{i};
     86         assert(static_cast<int>(ym1.year()) == 1234);
     87         assert(static_cast<unsigned>(ym1.month()) == 12U-i);
     88     }
     89     }
     90 
     91 }
     92