diff --git a/include/etl/chrono.h b/include/etl/chrono.h index 5fc361901..42f97afe4 100644 --- a/include/etl/chrono.h +++ b/include/etl/chrono.h @@ -35,7 +35,9 @@ SOFTWARE. #include "private/chrono/duration.h" #include "private/chrono/day.h" +#include "private/chrono/weekday.h" #include "private/chrono/month.h" #include "private/chrono/year.h" + #endif diff --git a/include/etl/platform.h b/include/etl/platform.h index 048203f3b..2c869a816 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -240,11 +240,35 @@ SOFTWARE. #endif //************************************* -// Indicate if etl::literals::chrono_literals has month (_m) -#if defined(ETL_ENABLE_CHRONO_LITERALS_MONTH) - #define ETL_HAS_CHRONO_LITERALS_MONTH 1 +// Indicate if etl::literals::chrono_literals has weekdays (_day) +#if defined(ETL_DISABLE_CHRONO_LITERALS_WEEKDAY) + #define ETL_HAS_CHRONO_LITERALS_DAY 0 #else + #define ETL_HAS_CHRONO_LITERALS_DAY 1 +#endif + +//************************************* +// Indicate if etl::literals::chrono_literals has weekdays (_weekday) +#if defined(ETL_DISABLE_CHRONO_LITERALS_WEEKDAY) + #define ETL_HAS_CHRONO_LITERALS_WEEKDAY 0 +#else + #define ETL_HAS_CHRONO_LITERALS_WEEKDAY 1 +#endif + +//************************************* +// Indicate if etl::literals::chrono_literals has month (_month) +#if defined(ETL_DISABLE_CHRONO_LITERALS_MONTH) #define ETL_HAS_CHRONO_LITERALS_MONTH 0 +#else + #define ETL_HAS_CHRONO_LITERALS_MONTH 1 +#endif + +//************************************* +// Indicate if etl::literals::chrono_literals has year (_year) +#if defined(ETL_DISABLE_CHRONO_LITERALS_YEAR) + #define ETL_HAS_CHRONO_LITERALS_YEAR 0 +#else + #define ETL_HAS_CHRONO_LITERALS_YEAR 1 #endif //************************************* @@ -484,7 +508,10 @@ namespace etl static ETL_CONSTANT bool has_mutable_array_view = (ETL_HAS_MUTABLE_ARRAY_VIEW == 1); static ETL_CONSTANT bool has_ideque_repair = (ETL_HAS_IDEQUE_REPAIR == 1); static ETL_CONSTANT bool has_virtual_messages = (ETL_HAS_VIRTUAL_MESSAGES == 1); + static ETL_CONSTANT bool has_chrono_literals_day = (ETL_HAS_CHRONO_LITERALS_DAY == 1); + static ETL_CONSTANT bool has_chrono_literals_weekday = (ETL_HAS_CHRONO_LITERALS_WEEKDAY == 1); static ETL_CONSTANT bool has_chrono_literals_month = (ETL_HAS_CHRONO_LITERALS_MONTH == 1); + static ETL_CONSTANT bool has_chrono_literals_year = (ETL_HAS_CHRONO_LITERALS_YEAR == 1); // Is... static ETL_CONSTANT bool is_debug_build = (ETL_IS_DEBUG_BUILD == 1); diff --git a/include/etl/private/chrono/day.h b/include/etl/private/chrono/day.h index b9f4e419b..54ac673e3 100644 --- a/include/etl/private/chrono/day.h +++ b/include/etl/private/chrono/day.h @@ -182,55 +182,6 @@ namespace etl unsigned char value; }; - //*********************************************************************** - /// Add etl::chrono::days to etl::chrono::day - ///\return etl::chrono::day - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT - { - etl::chrono::day result(d); - - result += ds; - - return result; - } - - //*********************************************************************** - /// Add etl::chrono::day to etl::chrono::days - ///\return etl::chrono::day - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::days& ds, const etl::chrono::day& d) ETL_NOEXCEPT - { - etl::chrono::day result(d); - - result += ds; - - return result; - } - - //*********************************************************************** - /// Subtract etl::chrono::days from etl::chrono::day - ///\return etl::chrono::day - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::day operator -(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT - { - etl::chrono::day result(d); - - result -= ds; - - return result; - } - - //*********************************************************************** - /// Subtract etl::chrono::day from etl::chrono::day - ///\return etl::chrono::days - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::days operator -(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT - { - return etl::chrono::days(static_cast(static_cast(d1)) - - static_cast(static_cast(d2))); - } - //*********************************************************************** /// Equality operator //*********************************************************************** @@ -288,6 +239,55 @@ namespace etl return (static_cast(d1) <=> static_cast(d2)); } #endif + + //*********************************************************************** + /// Add etl::chrono::days to etl::chrono::day + ///\return etl::chrono::day + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT + { + etl::chrono::day result(d); + + result += ds; + + return result; + } + + //*********************************************************************** + /// Add etl::chrono::day to etl::chrono::days + ///\return etl::chrono::day + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::days& ds, const etl::chrono::day& d) ETL_NOEXCEPT + { + etl::chrono::day result(d); + + result += ds; + + return result; + } + + //*********************************************************************** + /// Subtract etl::chrono::days from etl::chrono::day + ///\return etl::chrono::day + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::day operator -(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT + { + etl::chrono::day result(d); + + result -= ds; + + return result; + } + + //*********************************************************************** + /// Subtract etl::chrono::day from etl::chrono::day + ///\return etl::chrono::days + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::days operator -(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT + { + return etl::chrono::days(static_cast(static_cast(d1)) - + static_cast(static_cast(d2))); + } } //************************************************************************* @@ -308,6 +308,7 @@ namespace etl #endif } +#if ETL_HAS_CHRONO_LITERALS_DAY #if ETL_USING_CPP11 namespace etl { @@ -318,7 +319,7 @@ namespace etl //*********************************************************************** /// Literal for days //*********************************************************************** - ETL_CONSTEXPR etl::chrono::day operator ""_d(unsigned long long d) ETL_NOEXCEPT + ETL_CONSTEXPR etl::chrono::day operator ""_day(unsigned long long d) ETL_NOEXCEPT { return etl::chrono::day(static_cast(d)); } @@ -326,5 +327,6 @@ namespace etl } } #endif +#endif #endif \ No newline at end of file diff --git a/include/etl/private/chrono/month.h b/include/etl/private/chrono/month.h index 1020a4354..10316f837 100644 --- a/include/etl/private/chrono/month.h +++ b/include/etl/private/chrono/month.h @@ -190,11 +190,69 @@ namespace etl unsigned char value; }; + //*********************************************************************** + /// Equality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator ==(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT + { + return (static_cast(d1) == static_cast(d2)); + } + + //*********************************************************************** + /// Inequality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator !=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT + { + return !(d1 == d2); + } + + //*********************************************************************** + /// Less-than operator + //*********************************************************************** + ETL_CONSTEXPR bool operator <(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT + { + return (static_cast(d1) < static_cast(d2)); + } + + //*********************************************************************** + /// Less-than-or-equal operator + //*********************************************************************** + ETL_CONSTEXPR bool operator <=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT + { + return (static_cast(d1) <= static_cast(d2)); + } + + //*********************************************************************** + /// Greater-than operator + //*********************************************************************** + ETL_CONSTEXPR bool operator >(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT + { + return (static_cast(d1) > static_cast(d2)); + } + + //*********************************************************************** + /// Greater-than-or-equal operator + //*********************************************************************** + ETL_CONSTEXPR bool operator >=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT + { + return (static_cast(d1) >= static_cast(d2)); + } + + //*********************************************************************** + /// Spaceship operator + //*********************************************************************** +#if ETL_USING_CPP20 + constexpr auto operator <=>(const etl::chrono::month& d1, const etl::chrono::month& d2) noexcept + { + return (static_cast(d1) <=> static_cast(d2)); + } +#endif + //*********************************************************************** /// Add etl::chrono::months to etl::chrono::month ///\return etl::chrono::month //*********************************************************************** - ETL_CONSTEXPR etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT + ETL_CONSTEXPR14 etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT { unsigned int value = static_cast(m); @@ -238,7 +296,7 @@ namespace etl /// Subtract etl::chrono::month from etl::chrono::month ///\return etl::chrono::months //*********************************************************************** - ETL_CONSTEXPR etl::chrono::months operator -(const etl::chrono::month& m1, const etl::chrono::month& m2) ETL_NOEXCEPT + ETL_CONSTEXPR14 etl::chrono::months operator -(const etl::chrono::month& m1, const etl::chrono::month& m2) ETL_NOEXCEPT { if (m1.ok() && m2.ok()) { @@ -251,68 +309,9 @@ namespace etl } } - return etl::chrono::months(); } - //*********************************************************************** - /// Equality operator - //*********************************************************************** - ETL_CONSTEXPR bool operator ==(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT - { - return (static_cast(d1) == static_cast(d2)); - } - - //*********************************************************************** - /// Inequality operator - //*********************************************************************** - ETL_CONSTEXPR bool operator !=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT - { - return !(d1 == d2); - } - - //*********************************************************************** - /// Less-than operator - //*********************************************************************** - ETL_CONSTEXPR bool operator <(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT - { - return (static_cast(d1) < static_cast(d2)); - } - - //*********************************************************************** - /// Less-than-or-equal operator - //*********************************************************************** - ETL_CONSTEXPR bool operator <=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT - { - return (static_cast(d1) <= static_cast(d2)); - } - - //*********************************************************************** - /// Greater-than operator - //*********************************************************************** - ETL_CONSTEXPR bool operator >(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT - { - return (static_cast(d1) > static_cast(d2)); - } - - //*********************************************************************** - /// Greater-than-or-equal operator - //*********************************************************************** - ETL_CONSTEXPR bool operator >=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT - { - return (static_cast(d1) >= static_cast(d2)); - } - - //*********************************************************************** - /// Spaceship operator - //*********************************************************************** -#if ETL_USING_CPP20 - constexpr auto operator <=>(const etl::chrono::month& d1, const etl::chrono::month& d2) noexcept - { - return (static_cast(d1) <=> static_cast(d2)); - } -#endif - #if ETL_USING_CPP17 inline constexpr etl::chrono::month January{ 1 }; inline constexpr etl::chrono::month February{ 2 }; @@ -326,8 +325,7 @@ namespace etl inline constexpr etl::chrono::month October{ 10 }; inline constexpr etl::chrono::month November{ 11 }; inline constexpr etl::chrono::month December{ 12 }; -#else - +#else static ETL_CONSTANT etl::chrono::month January{ 1 }; static ETL_CONSTANT etl::chrono::month February{ 2 }; static ETL_CONSTANT etl::chrono::month March{ 3 }; @@ -372,7 +370,7 @@ namespace etl //*********************************************************************** /// Literal for months //*********************************************************************** - ETL_CONSTEXPR etl::chrono::month operator ""_m(unsigned long long m) ETL_NOEXCEPT + ETL_CONSTEXPR etl::chrono::month operator ""_month(unsigned long long m) ETL_NOEXCEPT { return etl::chrono::month(m); } diff --git a/include/etl/private/chrono/weekday.h b/include/etl/private/chrono/weekday.h new file mode 100644 index 000000000..fa520737e --- /dev/null +++ b/include/etl/private/chrono/weekday.h @@ -0,0 +1,391 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2023 John Wellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_CHRONO_WEEKDAY_INCLUDED +#define ETL_CHRONO_WEEKDAY_INCLUDED + +#include "../../platform.h" +#include "../../hash.h" + +#include "duration.h" + +#include + +namespace etl +{ + namespace chrono + { + class weekday; + + ETL_CONSTEXPR etl::chrono::weekday operator +(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT; + ETL_CONSTEXPR etl::chrono::weekday operator +(const etl::chrono::days& ds, const etl::chrono::weekday& m) ETL_NOEXCEPT; + ETL_CONSTEXPR etl::chrono::weekday operator -(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT; + + //*********************************************************************** + /// weekday + //*********************************************************************** + class weekday + { + public: + + //*********************************************************************** + /// Default constructor + //*********************************************************************** + ETL_CONSTEXPR weekday() + : value(255U) + { + } + + //*********************************************************************** + /// Construct from unsigned + //*********************************************************************** + ETL_CONSTEXPR explicit weekday(unsigned value_) + : value(value_) + { + } + + //*********************************************************************** + /// Copy constructor + //*********************************************************************** + ETL_CONSTEXPR weekday(const etl::chrono::weekday& other) + : value(other.value) + { + } + + //*********************************************************************** + /// Assignment operator + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday& operator =(const etl::chrono::weekday& rhs) + { + value = rhs.value; + + return *this; + } + + //*********************************************************************** + /// Pre-increment operator + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday& operator ++() ETL_NOEXCEPT + { + *this += etl::chrono::days(1); + + return *this; + } + + //*********************************************************************** + /// Post-increment operator + //*********************************************************************** + ETL_CONSTEXPR14 etl::chrono::weekday operator ++(int) ETL_NOEXCEPT + { + const etl::chrono::weekday temp = *this; + + *this += etl::chrono::days(1); + + return temp; + } + + //*********************************************************************** + /// Pre-decrement operator + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday& operator --() ETL_NOEXCEPT + { + *this -= etl::chrono::days(1); + + return *this; + } + + //*********************************************************************** + /// Post-decrement operator + //*********************************************************************** + ETL_CONSTEXPR14 etl::chrono::weekday operator --(int) ETL_NOEXCEPT + { + etl::chrono::weekday temp = *this; + + *this -= etl::chrono::days(1); + + return temp; + } + + //*********************************************************************** + /// Plus-equals operator adding etl::chrono::days + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday& operator +=(const etl::chrono::days& ds) ETL_NOEXCEPT + { + *this = *this + ds; + + return *this; + } + + //*********************************************************************** + /// Minus-equals operator subtracting etl::chrono::days + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday& operator -=(const etl::chrono::days& ds) ETL_NOEXCEPT + { + *this = *this - ds; + + return *this; + } + + //*********************************************************************** + /// Returns true if the weekday is within the valid 1 to 31 range + //*********************************************************************** + ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT + { + return (c_encoding() <= 6U); + } + + //*********************************************************************** + /// The minimum weekday value for which ok() will return true + //*********************************************************************** + static ETL_NODISCARD ETL_CONSTEXPR etl::chrono::weekday min() ETL_NOEXCEPT + { + return etl::chrono::weekday(0); + } + + //*********************************************************************** + /// The maximum weekday value for which ok() will return true + //*********************************************************************** + static ETL_NODISCARD ETL_CONSTEXPR etl::chrono::weekday max() ETL_NOEXCEPT + { + return etl::chrono::weekday(6); + } + + //*********************************************************************** + /// Get the C encoding of the weekday + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR unsigned c_encoding() const ETL_NOEXCEPT + { + return (value == 7U) ? 0U : value; + } + + //*********************************************************************** + /// Get the ISO encoding of the weekday + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR unsigned iso_encoding() const ETL_NOEXCEPT + { + return (value == 0U) ? 7U : value; + } + + ////*********************************************************************** + ///// Index operator + ////*********************************************************************** + //ETL_CONSTEXPR etl::chrono::weekday_indexed operator[](unsigned index) const ETL_NOEXCEPT + //{ + // etl::chrono::weekday_indexed(); + //} + // + ////*********************************************************************** + ///// Index operator + ////*********************************************************************** + //ETL_CONSTEXPR etl::chrono::weekday_last operator[](etl::chrono::last_spec) const ETL_NOEXCEPT + //{ + // std::chrono::weekday_last(); + //} + + private: + + unsigned char value; + }; + + //*********************************************************************** + /// Equality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator ==(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + return (wd1.c_encoding() == wd2.c_encoding()); + } + + //*********************************************************************** + /// Inequality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator !=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + return !(wd1 == wd2); + } + + //*********************************************************************** + /// Less-than operator + //*********************************************************************** + ETL_CONSTEXPR bool operator <(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + return (wd1.c_encoding() < wd2.c_encoding()); + } + + //*********************************************************************** + /// Less-than-or-equal operator + //*********************************************************************** + ETL_CONSTEXPR bool operator <=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + return (wd1.c_encoding() <= wd2.c_encoding()); + } + + //*********************************************************************** + /// Greater-than operator + //*********************************************************************** + ETL_CONSTEXPR bool operator >(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + return (wd1.c_encoding() > wd2.c_encoding()); + } + + //*********************************************************************** + /// Greater-than-or-equal operator + //*********************************************************************** + ETL_CONSTEXPR bool operator >=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + return (wd1.c_encoding() >= wd2.c_encoding()); + } + + //*********************************************************************** + /// Spaceship operator + //*********************************************************************** +#if ETL_USING_CPP20 + constexpr auto operator <=>(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) noexcept + { + return (wd1.c_encoding() <=> wd2.c_encoding()); + } +#endif + + //*********************************************************************** + /// Add etl::chrono::days to etl::chrono::weekday + ///\return etl::chrono::weekday + //*********************************************************************** + ETL_CONSTEXPR14 etl::chrono::weekday operator +(const etl::chrono::weekday& wd, const etl::chrono::days& ds) ETL_NOEXCEPT + { + unsigned int value = wd.c_encoding(); + + value = value % 7U; + + int delta = ds.count() % 7; + + // Adjust to allow a limited +-7 weekday delta + value += 7U; + value += delta; + value %= 7U; + + return etl::chrono::weekday(value); + } + + //*********************************************************************** + /// Add etl::chrono::weekday to etl::chrono::days + ///\return etl::chrono::weekday + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday operator +(const etl::chrono::days& ds, const etl::chrono::weekday& wd) ETL_NOEXCEPT + { + return wd + ds; + } + + //*********************************************************************** + /// Subtract etl::chrono::days from etl::chrono::weekday + ///\return etl::chrono::weekday + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday operator -(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT + { + return m + etl::chrono::days(-ds.count()); + } + + //*********************************************************************** + /// Subtract etl::chrono::weekday from etl::chrono::weekday + ///\return etl::chrono::days + //*********************************************************************** + ETL_CONSTEXPR14 etl::chrono::days operator -(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT + { + if (wd1.ok() && wd2.ok()) + { + etl::chrono::days ds(static_cast(wd1.c_encoding()) - + static_cast(wd2.c_encoding()) % 12); + + if (wd1 == (wd2 + ds)) + { + return ds; + } + } + + return etl::chrono::days(); + } + +#if ETL_USING_CPP17 + inline constexpr etl::chrono::weekday Sunday{ 0 }; + inline constexpr etl::chrono::weekday Monday{ 1 }; + inline constexpr etl::chrono::weekday Tuesday{ 2 }; + inline constexpr etl::chrono::weekday Wednesday{ 3 }; + inline constexpr etl::chrono::weekday Thursday{ 4 }; + inline constexpr etl::chrono::weekday Friday{ 5 }; + inline constexpr etl::chrono::weekday Saturday{ 6 }; +#else + static ETL_CONSTANT etl::chrono::weekday Sunday{ 0 }; + static ETL_CONSTANT etl::chrono::weekday Monday{ 1 }; + static ETL_CONSTANT etl::chrono::weekday Tuesday{ 2 }; + static ETL_CONSTANT etl::chrono::weekday Wednesday{ 3 }; + static ETL_CONSTANT etl::chrono::weekday Thursday{ 4 }; + static ETL_CONSTANT etl::chrono::weekday Friday{ 5 }; + static ETL_CONSTANT etl::chrono::weekday Saturday{ 6 }; +#endif + } + + //************************************************************************* + /// Hash function for etl::chrono::weekday + //************************************************************************* +#if ETL_USING_8BIT_TYPES + template <> + struct hash + { + size_t operator()(const etl::chrono::weekday& wd) const + { + unsigned value = wd.c_encoding(); + const uint8_t* p = reinterpret_cast(&value); + + return etl::private_hash::generic_hash(p, p + sizeof(unsigned)); + } + }; +#endif +} + +#if ETL_HAS_CHRONO_LITERALS_WEEKDAY +#if ETL_USING_CPP11 +namespace etl +{ + namespace literals + { + namespace chrono_literals + { + //*********************************************************************** + /// Literal for weekdays + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday operator ""_weekday(unsigned long long m) ETL_NOEXCEPT + { + return etl::chrono::weekday(m); + } + } + } +} +#endif +#endif + +#endif \ No newline at end of file diff --git a/include/etl/private/chrono/year.h b/include/etl/private/chrono/year.h index 54a1b1941..c22285b6a 100644 --- a/include/etl/private/chrono/year.h +++ b/include/etl/private/chrono/year.h @@ -190,55 +190,6 @@ namespace etl int16_t value; }; - //*********************************************************************** - /// Add etl::chrono::years to etl::chrono::year - ///\return etl::chrono::year - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::year operator +(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT - { - etl::chrono::year result(y); - - result += ys; - - return result; - } - - //*********************************************************************** - /// Add etl::chrono::year to etl::chrono::years - ///\return etl::chrono::year - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::year operator +(const etl::chrono::years& ys, const etl::chrono::year& y) ETL_NOEXCEPT - { - etl::chrono::year result(y); - - result += ys; - - return result; - } - - //*********************************************************************** - /// Subtract etl::chrono::years from etl::chrono::year - ///\return etl::chrono::year - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::year operator -(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT - { - etl::chrono::year result(y); - - result -= ys; - - return result; - } - - //*********************************************************************** - /// Subtract etl::chrono::year from etl::chrono::year - ///\return etl::chrono::years - //*********************************************************************** - ETL_CONSTEXPR etl::chrono::years operator -(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT - { - return etl::chrono::years(static_cast(static_cast(y1)) - - static_cast(static_cast(y2))); - } - //*********************************************************************** /// Equality operator //*********************************************************************** @@ -296,6 +247,55 @@ namespace etl return (static_cast(y1) <=> static_cast(y2)); } #endif + + //*********************************************************************** + /// Add etl::chrono::years to etl::chrono::year + ///\return etl::chrono::year + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::year operator +(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT + { + etl::chrono::year result(y); + + result += ys; + + return result; + } + + //*********************************************************************** + /// Add etl::chrono::year to etl::chrono::years + ///\return etl::chrono::year + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::year operator +(const etl::chrono::years& ys, const etl::chrono::year& y) ETL_NOEXCEPT + { + etl::chrono::year result(y); + + result += ys; + + return result; + } + + //*********************************************************************** + /// Subtract etl::chrono::years from etl::chrono::year + ///\return etl::chrono::year + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::year operator -(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT + { + etl::chrono::year result(y); + + result -= ys; + + return result; + } + + //*********************************************************************** + /// Subtract etl::chrono::year from etl::chrono::year + ///\return etl::chrono::years + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::years operator -(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT + { + return etl::chrono::years(static_cast(static_cast(y1)) - + static_cast(static_cast(y2))); + } } //************************************************************************* @@ -316,6 +316,7 @@ namespace etl #endif } +#if ETL_HAS_CHRONO_LITERALS_YEAR #if ETL_USING_CPP11 namespace etl { @@ -326,7 +327,7 @@ namespace etl //*********************************************************************** /// Literal for years //*********************************************************************** - constexpr etl::chrono::year operator ""_y(unsigned long long y) noexcept + constexpr etl::chrono::year operator ""_year(unsigned long long y) noexcept { return etl::chrono::year(static_cast(y)); } @@ -334,5 +335,6 @@ namespace etl } } #endif +#endif #endif \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 85265ab18..7adcdb145 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -47,6 +47,9 @@ add_executable(etl_tests test_checksum.cpp test_chrono_day.cpp + test_chrono_weekday.cpp + test_chrono_month.cpp + test_chrono_year.cpp test_circular_buffer.cpp test_circular_buffer_external_buffer.cpp diff --git a/test/etl_profile.h b/test/etl_profile.h index d0f6a6872..c72404220 100644 --- a/test/etl_profile.h +++ b/test/etl_profile.h @@ -74,8 +74,6 @@ SOFTWARE. #define ETL_POLYMORPHIC_VECTOR #define ETL_POLYMORPHIC_INDIRECT_VECTOR -#define ETL_ENABLE_CHRONO_LITERALS_MONTH - #if defined(ETL_FORCE_TEST_CPP03_IMPLEMENTATION) #define ETL_FUNCTION_FORCE_CPP03_IMPLEMENTATION #define ETL_PRIORITY_QUEUE_FORCE_CPP03_IMPLEMENTATION diff --git a/test/test_chrono_day.cpp b/test/test_chrono_day.cpp index cb8415159..815cb4f4a 100644 --- a/test/test_chrono_day.cpp +++ b/test/test_chrono_day.cpp @@ -37,6 +37,8 @@ SOFTWARE. #include "etl/chrono.h" #include +#include +#include namespace { @@ -156,94 +158,110 @@ namespace //************************************************************************* TEST(test_day_plus_days) { - std::chrono::day std_day(0); - etl::chrono::day day(0); + for (int d = 0; d < 256; ++d) + { + for (int ds = 0; ds < 256; ++ds) + { + std::chrono::day std_day(d); + etl::chrono::day day(d); - std::chrono::days std_days(2); - etl::chrono::days days(2); + std::chrono::days std_days(ds); + etl::chrono::days days(ds); - for (int i = 0; i < 128; ++i) - { - std_day = std_day + std_days; - day = day + days; + std_day = std_day + std_days; + day = day + days; - CHECK_EQUAL(std_day.ok(), day.ok()); - CHECK_EQUAL(unsigned(std_day), unsigned(day)); + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } } } //************************************************************************* TEST(test_days_plus_day) { - std::chrono::day std_day(0); - etl::chrono::day day(0); + for (int d = 0; d < 256; ++d) + { + for (int ds = 0; ds < 256; ++ds) + { + std::chrono::day std_day(d); + etl::chrono::day day(d); - std::chrono::days std_days(2); - etl::chrono::days days(2); + std::chrono::days std_days(ds); + etl::chrono::days days(ds); - for (int i = 0; i < 128; ++i) - { - std_day = std_days + std_day; - day = days + day; + std_day = std_days + std_day; + day = days + day; - CHECK_EQUAL(std_day.ok(), day.ok()); - CHECK_EQUAL(unsigned(std_day), unsigned(day)); + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } } } //************************************************************************* TEST(test_minus_equal_days) { - std::chrono::day std_day(256); - etl::chrono::day day(256); + for (int d = 0; d <= 256; ++d) + { + for (int ds = 0; ds <= 256; ++ds) + { + std::chrono::day std_day(d); + etl::chrono::day day(d); - std::chrono::days std_days(2); - etl::chrono::days days(2); + std::chrono::days std_days(ds); + etl::chrono::days days(ds); - for (int i = 0; i < 128; ++i) - { - std_day -= std_days; - day -= days; + std_day -= std_days; + day -= days; - CHECK_EQUAL(std_day.ok(), day.ok()); - CHECK_EQUAL(unsigned(std_day), unsigned(day)); + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } } } //************************************************************************* TEST(test_day_minus_days) { - std::chrono::day std_day(0); - etl::chrono::day day(0); + for (int d = 0; d <= 256; ++d) + { + for (int ds = 0; ds <= 256; ++ds) + { + std::chrono::day std_day(d); + etl::chrono::day day(d); - std::chrono::days std_days(2); - etl::chrono::days days(2); + std::chrono::days std_days(ds); + etl::chrono::days days(ds); - for (int i = 0; i < 128; ++i) - { - std_day = std_day - std_days; - day = day - days; + std_day = std_day - std_days; + day = day - days; - CHECK_EQUAL(std_day.ok(), day.ok()); - CHECK_EQUAL(unsigned(std_day), unsigned(day)); + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } } } //************************************************************************* TEST(test_day_minus_day) { - for (int i = 1; i < 31; ++i) + for (int d = 0; d < 256; ++d) { - std::chrono::day std_day1(i); - std::chrono::day std_day2(31 - i); + std::chrono::day std_day1(d); + std::chrono::day std_day2(255 - d); + + std::chrono::day day1(d); + std::chrono::day day2(255 - d); - etl::chrono::day day1(i); - etl::chrono::day day2(31 - i); + auto std_days12 = std_day1 - std_day2; + auto std_days21 = std_day2 - std_day1; - std::chrono::days std_days = std_day1 - std_day2; - etl::chrono::days days = day1 - day2; + auto days12 = day1 - day2; + auto days21 = day2 - day1; - CHECK_EQUAL(std_days.count(), days.count()); + CHECK_EQUAL(std_days12.count(), days12.count()); + CHECK_EQUAL(std_days21.count(), days21.count()); } } @@ -261,7 +279,7 @@ namespace using namespace etl::literals::chrono_literals; std::chrono::day std_day = 25d; - etl::chrono::day day = 25_d; + etl::chrono::day day = 25_day; CHECK_EQUAL(std_day.ok(), day.ok()); CHECK_EQUAL(unsigned(std_day), unsigned(day)); @@ -296,15 +314,18 @@ namespace } //************************************************************************* - TEST(test_day_hash) + TEST(test_day_hashes_are_unique) { - etl::chrono::day day(10); + std::vector hashes; - size_t h = 0; - - h = etl::hash()(day); + for (int i = 0; i < 256; ++i) + { + hashes.push_back(etl::hash()(etl::chrono::day(i))); + } - CHECK_TRUE(h != 0); + std::sort(hashes.begin(), hashes.end()); + (void)std::unique(hashes.begin(), hashes.end()); + CHECK_EQUAL(256U, hashes.size()); } }; } diff --git a/test/test_chrono_month.cpp b/test/test_chrono_month.cpp index 897c43336..2dcc71870 100644 --- a/test/test_chrono_month.cpp +++ b/test/test_chrono_month.cpp @@ -37,6 +37,8 @@ SOFTWARE. #include "etl/chrono.h" #include +#include +#include namespace { @@ -86,7 +88,7 @@ namespace std::chrono::month std_month(0); etl::chrono::month month(0); - for (int i = 0; i < 255; ++i) + for (int i = 0; i < 256; ++i) { std::chrono::month std_last_month = std_month++; etl::chrono::month last_month = month++; @@ -102,10 +104,10 @@ namespace //************************************************************************* TEST(test_pre_decrement) { - std::chrono::month std_month(256); - etl::chrono::month month(256); + std::chrono::month std_month(255); + etl::chrono::month month(255); - for (int i = 0; i < 255; ++i) + for (int i = 0; i < 256; ++i) { --std_month; --month; @@ -121,131 +123,150 @@ namespace std::chrono::month std_month(255); etl::chrono::month month(255); - for (int i = 0; i < 255; ++i) + for (int i = 0; i < 256; ++i) { std::chrono::month std_last_month = std_month--; etl::chrono::month last_month = month--; -// CHECK_EQUAL(std_last_month.ok(), last_month.ok()); + CHECK_EQUAL(std_last_month.ok(), last_month.ok()); CHECK_EQUAL(unsigned(std_last_month), unsigned(last_month)); -// CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(std_month.ok(), month.ok()); CHECK_EQUAL(unsigned(std_month), unsigned(month)); } } - ////************************************************************************* - //TEST(test_plus_equal_months) - //{ - // std::chrono::month std_month(0); - // etl::chrono::month month(0); - - // std::chrono::months std_months(2); - // etl::chrono::months months(2); - - // for (int i = 0; i < 128; ++i) - // { - // std_month += std_months; - // month += months; - - // CHECK_EQUAL(std_month.ok(), month.ok()); - // CHECK_EQUAL(unsigned(std_month), unsigned(month)); - // } - //} - - ////************************************************************************* - //TEST(test_month_plus_months) - //{ - // std::chrono::month std_month(0); - // etl::chrono::month month(0); - - // std::chrono::months std_months(2); - // etl::chrono::months months(2); - - // for (int i = 0; i < 128; ++i) - // { - // std_month = std_month + std_months; - // month = month + months; - - // CHECK_EQUAL(std_month.ok(), month.ok()); - // CHECK_EQUAL(unsigned(std_month), unsigned(month)); - // } - //} - - ////************************************************************************* - //TEST(test_months_plus_month) - //{ - // std::chrono::month std_month(0); - // etl::chrono::month month(0); - - // std::chrono::months std_months(2); - // etl::chrono::months months(2); - - // for (int i = 0; i < 128; ++i) - // { - // std_month = std_months + std_month; - // month = months + month; - - // CHECK_EQUAL(std_month.ok(), month.ok()); - // CHECK_EQUAL(unsigned(std_month), unsigned(month)); - // } - //} - - ////************************************************************************* - //TEST(test_minus_equal_months) - //{ - // std::chrono::month std_month(256); - // etl::chrono::month month(256); - - // std::chrono::months std_months(2); - // etl::chrono::months months(2); - - // for (int i = 0; i < 128; ++i) - // { - // std_month -= std_months; - // month -= months; - - // CHECK_EQUAL(std_month.ok(), month.ok()); - // CHECK_EQUAL(unsigned(std_month), unsigned(month)); - // } - //} - - ////************************************************************************* - //TEST(test_month_minus_months) - //{ - // std::chrono::month std_month(0); - // etl::chrono::month month(0); - - // std::chrono::months std_months(2); - // etl::chrono::months months(2); - - // for (int i = 0; i < 128; ++i) - // { - // std_month = std_month - std_months; - // month = month - months; - - // CHECK_EQUAL(std_month.ok(), month.ok()); - // CHECK_EQUAL(unsigned(std_month), unsigned(month)); - // } - //} - - ////************************************************************************* - //TEST(test_month_minus_month) - //{ - // for (int i = 1; i < 12; ++i) - // { - // std::chrono::month std_month1(i); - // std::chrono::month std_month2(12 - i); - - // etl::chrono::month month1(i); - // etl::chrono::month month2(12 - i); - - // std::chrono::months std_months = std_month1 - std_month2; - // etl::chrono::months months = month1 - month2; - - // CHECK_EQUAL(std_months.count(), months.count()); - // } - //} + //************************************************************************* + TEST(test_plus_equal_months) + { + for (int m = 0; m <= 12; ++m) + { + for (int ms = 0; ms <= 24; ++ms) + { + std::chrono::month std_month(m); + etl::chrono::month month(m); + + std::chrono::months std_months(ms); + etl::chrono::months months(ms); + + std_month += std_months; + month += months; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + } + + //************************************************************************* + TEST(test_month_plus_months) + { + for (int m = 0; m <= 12; ++m) + { + for (int ms = 0; ms <= 24; ++ms) + { + std::chrono::month std_month(m); + etl::chrono::month month(m); + + std::chrono::months std_months(ms); + etl::chrono::months months(ms); + + std_month = std_month + std_months; + month = month + months; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + } + + //************************************************************************* + TEST(test_months_plus_month) + { + for (int m = 0; m <= 12; ++m) + { + for (int ms = 0; ms <= 24; ++ms) + { + std::chrono::month std_month(m); + etl::chrono::month month(m); + + std::chrono::months std_months(ms); + etl::chrono::months months(ms); + + std_month = std_months + std_month; + month = months + month; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + } + + //************************************************************************* + TEST(test_minus_equal_months) + { + for (int m = 0; m <= 12; ++m) + { + for (int ms = 0; ms <= 24; ++ms) + { + std::chrono::month std_month(m); + etl::chrono::month month(m); + + std::chrono::months std_months(ms); + etl::chrono::months months(ms); + + std_month -= std_months; + month -= months; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + } + + //************************************************************************* + TEST(test_month_minus_months) + { + for (int m = 0; m <= 12; ++m) + { + for (int ms = 0; ms <= 24; ++ms) + { + std::chrono::month std_month(m); + etl::chrono::month month(m); + + std::chrono::months std_months(ms); + etl::chrono::months months(ms); + + std_month = std_month - std_months; + month = month - months; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + } + + //************************************************************************* + TEST(test_month_minus_month) + { + for (int m = 0; m < 256; ++m) + { + std::chrono::month std_month1(m); + std::chrono::month std_month2(255 - m); + + std::chrono::month month1(m); + std::chrono::month month2(255 - m); + + auto std_months12 = std_month1 - std_month2; + auto std_months21 = std_month2 - std_month1; + + auto months12 = month1 - month2; + auto months21 = month2 - month1; + + CHECK_EQUAL(std_months12.count(), months12.count()); + CHECK_EQUAL(std_months21.count(), months21.count()); + } + } //************************************************************************* TEST(test_min_max_month) @@ -259,18 +280,18 @@ namespace { using namespace etl::literals::chrono_literals; - etl::chrono::month month1 = 1_m; - etl::chrono::month month2 = 2_m; - etl::chrono::month month3 = 3_m; - etl::chrono::month month4 = 4_m; - etl::chrono::month month5 = 5_m; - etl::chrono::month month6 = 6_m; - etl::chrono::month month7 = 7_m; - etl::chrono::month month8 = 8_m; - etl::chrono::month month9 = 9_m; - etl::chrono::month month10 = 10_m; - etl::chrono::month month11 = 11_m; - etl::chrono::month month12 = 12_m; + etl::chrono::month month1 = 1_month; + etl::chrono::month month2 = 2_month; + etl::chrono::month month3 = 3_month; + etl::chrono::month month4 = 4_month; + etl::chrono::month month5 = 5_month; + etl::chrono::month month6 = 6_month; + etl::chrono::month month7 = 7_month; + etl::chrono::month month8 = 8_month; + etl::chrono::month month9 = 9_month; + etl::chrono::month month10 = 10_month; + etl::chrono::month month11 = 11_month; + etl::chrono::month month12 = 12_month; CHECK_TRUE(month1.ok()); CHECK_TRUE(month2.ok()); @@ -328,15 +349,35 @@ namespace } //************************************************************************* - TEST(test_month_hash) + TEST(test_month_hashes_are_unique) { - etl::chrono::month month(10); + std::vector hashes; + + for (int i = 0; i < 256; ++i) + { + hashes.push_back(etl::hash()(etl::chrono::month(i))); + } - size_t h = 0; - - h = etl::hash()(month); + std::sort(hashes.begin(), hashes.end()); + (void)std::unique(hashes.begin(), hashes.end()); + CHECK_EQUAL(256U, hashes.size()); + } - CHECK_TRUE(h != 0); + //************************************************************************* + TEST(test_month_types) + { + CHECK_EQUAL(static_cast(std::chrono::January), static_cast(etl::chrono::January)); + CHECK_EQUAL(static_cast(std::chrono::February), static_cast(etl::chrono::February)); + CHECK_EQUAL(static_cast(std::chrono::March), static_cast(etl::chrono::March)); + CHECK_EQUAL(static_cast(std::chrono::April), static_cast(etl::chrono::April)); + CHECK_EQUAL(static_cast(std::chrono::May), static_cast(etl::chrono::May)); + CHECK_EQUAL(static_cast(std::chrono::June), static_cast(etl::chrono::June)); + CHECK_EQUAL(static_cast(std::chrono::July), static_cast(etl::chrono::July)); + CHECK_EQUAL(static_cast(std::chrono::August), static_cast(etl::chrono::August)); + CHECK_EQUAL(static_cast(std::chrono::September), static_cast(etl::chrono::September)); + CHECK_EQUAL(static_cast(std::chrono::October), static_cast(etl::chrono::October)); + CHECK_EQUAL(static_cast(std::chrono::November), static_cast(etl::chrono::November)); + CHECK_EQUAL(static_cast(std::chrono::December), static_cast(etl::chrono::December)); } }; } diff --git a/test/test_chrono_weekday.cpp b/test/test_chrono_weekday.cpp new file mode 100644 index 000000000..0594ea758 --- /dev/null +++ b/test/test_chrono_weekday.cpp @@ -0,0 +1,386 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Documentation: + +Copyright(c) 2023 John Wellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "etl/platform.h" + +#if ETL_USING_CPP20 + +#include "unit_test_framework.h" + +#include "etl/chrono.h" + +#include +#include +#include + +namespace +{ + SUITE(test_chrono_weekday) + { + //************************************************************************* + TEST(test_default_constructor) + { + std::chrono::weekday std_weekday; + etl::chrono::weekday weekday; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + } + + //************************************************************************* + TEST(test_constructor_in_range) + { + for (unsigned i = 0U; i < 256U; ++i) + { + std::chrono::weekday std_weekday(i); + etl::chrono::weekday weekday(i); + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + + //************************************************************************* + TEST(test_encodings) + { + std::chrono::weekday std_weekday; + etl::chrono::weekday weekday; + + for (unsigned i = 0U; i < 256; ++i) + { + std_weekday = std::chrono::weekday(i); + weekday = etl::chrono::weekday(i); + + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + CHECK_EQUAL(std_weekday.iso_encoding(), weekday.iso_encoding()); + } + } + + //************************************************************************* + TEST(test_pre_increment) + { + std::chrono::weekday std_weekday(0); + etl::chrono::weekday weekday(0); + + for (int i = 0; i < 255; ++i) + { + ++std_weekday; + ++weekday; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + + //************************************************************************* + TEST(test_post_increment) + { + std::chrono::weekday std_weekday(0); + etl::chrono::weekday weekday(0); + + for (int i = 0; i < 256; ++i) + { + std::chrono::weekday std_last_weekday = std_weekday++; + etl::chrono::weekday last_weekday = weekday++; + + CHECK_EQUAL(std_last_weekday.ok(), last_weekday.ok()); + CHECK_EQUAL(std_last_weekday.c_encoding(), last_weekday.c_encoding()); + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + + //************************************************************************* + TEST(test_pre_decrement) + { + std::chrono::weekday std_weekday(255); + etl::chrono::weekday weekday(255); + + for (int i = 0; i < 256; ++i) + { + --std_weekday; + --weekday; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + + //************************************************************************* + TEST(test_post_decrement) + { + std::chrono::weekday std_weekday(255); + etl::chrono::weekday weekday(255); + + for (int i = 0; i < 256; ++i) + { + std::chrono::weekday std_last_weekday = std_weekday--; + etl::chrono::weekday last_weekday = weekday--; + + CHECK_EQUAL(std_last_weekday.ok(), last_weekday.ok()); + CHECK_EQUAL(std_last_weekday.c_encoding(), last_weekday.c_encoding()); + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + + //************************************************************************* + TEST(test_plus_equal_days) + { + for (int wd = 0; wd <= 6; ++wd) + { + for (int ds = 0; ds <= 14; ++ds) + { + std::chrono::weekday std_weekday(wd); + etl::chrono::weekday weekday(wd); + + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_weekday += std_days; + weekday += days; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + } + + //************************************************************************* + TEST(test_weekday_plus_days) + { + for (int wd = 0; wd <= 6; ++wd) + { + for (int ds = 0; ds <= 14; ++ds) + { + std::chrono::weekday std_weekday(wd); + etl::chrono::weekday weekday(wd); + + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_weekday = std_weekday + std_days; + weekday = weekday + days; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + } + + //************************************************************************* + TEST(test_days_plus_weekday) + { + for (int wd = 0; wd <= 6; ++wd) + { + for (int ds = 0; ds <= 14; ++ds) + { + std::chrono::weekday std_weekday(wd); + etl::chrono::weekday weekday(wd); + + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_weekday = std_days + std_weekday; + weekday = days + weekday; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + } + + //************************************************************************* + TEST(test_minus_equal_days) + { + for (int wd = 0; wd <= 6; ++wd) + { + for (int ds = 0; ds <= 14; ++ds) + { + std::chrono::weekday std_weekday(wd); + etl::chrono::weekday weekday(wd); + + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_weekday -= std_days; + weekday -= days; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + } + + //************************************************************************* + TEST(test_weekday_minus_days) + { + for (int wd = 0; wd <= 6; ++wd) + { + for (int ds = 0; ds <= 14; ++ds) + { + std::chrono::weekday std_weekday(wd); + etl::chrono::weekday weekday(wd); + + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_weekday = std_weekday - std_days; + weekday = weekday - days; + + CHECK_EQUAL(std_weekday.ok(), weekday.ok()); + CHECK_EQUAL(std_weekday.c_encoding(), weekday.c_encoding()); + } + } + } + + //************************************************************************* + TEST(test_weekday_minus_weekday) + { + for (int m = 0; m < 256; ++m) + { + std::chrono::weekday std_weekday1(m); + std::chrono::weekday std_weekday2(255 - m); + + std::chrono::weekday weekday1(m); + std::chrono::weekday weekday2(255 - m); + + auto std_days12 = std_weekday1 - std_weekday2; + auto std_days21 = std_weekday2 - std_weekday1; + + auto days12 = weekday1 - weekday2; + auto days21 = weekday2 - weekday1; + + CHECK_EQUAL(std_days12.count(), days12.count()); + CHECK_EQUAL(std_days21.count(), days21.count()); + } + } + + //************************************************************************* + TEST(test_min_max_weekday) + { + CHECK_EQUAL(etl::chrono::Sunday.c_encoding(), etl::chrono::weekday::min().c_encoding()); + CHECK_EQUAL(etl::chrono::Saturday.c_encoding(), etl::chrono::weekday::max().c_encoding()); + } + + //************************************************************************* + TEST(test_literal_weekday) + { + using namespace etl::literals::chrono_literals; + + etl::chrono::weekday weekday0 = 0_weekday; + etl::chrono::weekday weekday1 = 1_weekday; + etl::chrono::weekday weekday2 = 2_weekday; + etl::chrono::weekday weekday3 = 3_weekday; + etl::chrono::weekday weekday4 = 4_weekday; + etl::chrono::weekday weekday5 = 5_weekday; + etl::chrono::weekday weekday6 = 6_weekday; + + CHECK_TRUE(weekday0.ok()); + CHECK_TRUE(weekday1.ok()); + CHECK_TRUE(weekday2.ok()); + CHECK_TRUE(weekday3.ok()); + CHECK_TRUE(weekday4.ok()); + CHECK_TRUE(weekday5.ok()); + CHECK_TRUE(weekday6.ok()); + + CHECK_EQUAL(0U, weekday0.c_encoding()); + CHECK_EQUAL(1U, weekday1.c_encoding()); + CHECK_EQUAL(2U, weekday2.c_encoding()); + CHECK_EQUAL(3U, weekday3.c_encoding()); + CHECK_EQUAL(4U, weekday4.c_encoding()); + CHECK_EQUAL(5U, weekday5.c_encoding()); + CHECK_EQUAL(6U, weekday6.c_encoding()); + } + + //************************************************************************* + TEST(test_weekday_comparison_operators) + { + etl::chrono::weekday weekday1(1); + etl::chrono::weekday weekday2(2); + + CHECK_TRUE(weekday1 == weekday1); + CHECK_FALSE(weekday1 != weekday1); + CHECK_TRUE(weekday1 < weekday2); + CHECK_FALSE(weekday1 < weekday1); + CHECK_FALSE(weekday2 < weekday1); + CHECK_TRUE(weekday1 <= weekday2); + CHECK_TRUE(weekday1 <= weekday1); + CHECK_FALSE(weekday2 <= weekday1); + CHECK_FALSE(weekday1 > weekday2); + CHECK_FALSE(weekday1 > weekday1); + CHECK_TRUE(weekday2 > weekday1); + CHECK_FALSE(weekday1 >= weekday2); + CHECK_TRUE(weekday1 >= weekday1); + CHECK_TRUE(weekday2 >= weekday1); + +#if ETL_USING_CPP20 + CHECK_TRUE((weekday1 <=> weekday1) == 0); + CHECK_TRUE((weekday1 <=> weekday2) < 0); + CHECK_TRUE((weekday2 <=> weekday1) > 0); +#endif + } + + //************************************************************************* + TEST(test_weekday_hashes_are_unique) + { + std::vector hashes; + + for (int i = 0; i < 256; ++i) + { + hashes.push_back(etl::hash()(etl::chrono::weekday(i))); + } + + std::sort(hashes.begin(), hashes.end()); + (void)std::unique(hashes.begin(), hashes.end()); + CHECK_EQUAL(256U, hashes.size()); + } + + //************************************************************************* + TEST(test_weekday_types) + { + CHECK_EQUAL(static_cast(std::chrono::January), static_cast(etl::chrono::January)); + CHECK_EQUAL(static_cast(std::chrono::February), static_cast(etl::chrono::February)); + CHECK_EQUAL(static_cast(std::chrono::March), static_cast(etl::chrono::March)); + CHECK_EQUAL(static_cast(std::chrono::April), static_cast(etl::chrono::April)); + CHECK_EQUAL(static_cast(std::chrono::May), static_cast(etl::chrono::May)); + CHECK_EQUAL(static_cast(std::chrono::June), static_cast(etl::chrono::June)); + CHECK_EQUAL(static_cast(std::chrono::July), static_cast(etl::chrono::July)); + CHECK_EQUAL(static_cast(std::chrono::August), static_cast(etl::chrono::August)); + CHECK_EQUAL(static_cast(std::chrono::September), static_cast(etl::chrono::September)); + CHECK_EQUAL(static_cast(std::chrono::October), static_cast(etl::chrono::October)); + CHECK_EQUAL(static_cast(std::chrono::November), static_cast(etl::chrono::November)); + CHECK_EQUAL(static_cast(std::chrono::December), static_cast(etl::chrono::December)); + } + }; +} + +#endif \ No newline at end of file diff --git a/test/test_chrono_year.cpp b/test/test_chrono_year.cpp index c91dce80f..e927668f7 100644 --- a/test/test_chrono_year.cpp +++ b/test/test_chrono_year.cpp @@ -261,7 +261,7 @@ namespace using namespace etl::literals::chrono_literals; std::chrono::year std_year = 25y; - etl::chrono::year year = 25_y; + etl::chrono::year year = 25_year; CHECK_EQUAL(std_year.ok(), year.ok()); CHECK_EQUAL(int(std_year), int(year)); @@ -270,7 +270,7 @@ namespace //************************************************************************* TEST(test_year_comparison_operators) { - etl::chrono::year year10(-10); + etl::chrono::year year10(10); etl::chrono::year year20(20); CHECK_TRUE(year10 == year10); @@ -296,21 +296,18 @@ namespace } //************************************************************************* - TEST(test_year_hash) + TEST(test_year_hashes_are_unique) { - etl::chrono::year year(-32767); - - size_t h = 0; - - h = etl::hash()(year); - - CHECK_TRUE(h != 0); + std::vector hashes; - year = etl::chrono::year(32767); - h = 0; - h = etl::hash()(year); + for (int32_t i = -32767; i < 32768; ++i) + { + hashes.push_back(etl::hash()(etl::chrono::year(i))); + } - CHECK_TRUE(h != 0); + std::sort(hashes.begin(), hashes.end()); + (void)std::unique(hashes.begin(), hashes.end()); + CHECK_EQUAL(65535U, hashes.size()); } }; } diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index a8f6cf587..7a886d9de 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -3004,6 +3004,7 @@ + @@ -14344,6 +14345,7 @@ + diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index 819bd3121..b885808ce 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -1383,6 +1383,9 @@ ETL\Private\chrono + + ETL\Private\chrono + @@ -3455,6 +3458,9 @@ Tests\Chrono + + Tests\Chrono +