diff --git a/include/etl/chrono.h b/include/etl/chrono.h index 0cf26e5b..8972d49e 100644 --- a/include/etl/chrono.h +++ b/include/etl/chrono.h @@ -31,9 +31,22 @@ SOFTWARE. #ifndef ETL_CHRONO_INCLUDED #define ETL_CHRONO_INCLUDED -#include "platform.h" +#define ETL_IN_CHRONO_H +#include "platform.h" +#include "hash.h" + +#include + +#include "private/chrono/last_spec.h" #include "private/chrono/duration.h" #include "private/chrono/day.h" +#include "private/chrono/weekday.h" +#include "private/chrono/weekday_indexed.h" +#include "private/chrono/weekday_last.h" +#include "private/chrono/month.h" +#include "private/chrono/year.h" +#undef ETL_IN_CHRONO_H + #endif diff --git a/include/etl/platform.h b/include/etl/platform.h index e66a0e08..361fade2 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -271,6 +271,38 @@ SOFTWARE. #define ETL_HAS_VIRTUAL_MESSAGES 1 #endif +//************************************* +// 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 + //************************************* // The macros below are dependent on the profile. // C++11 @@ -512,6 +544,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 e2118172..91557a95 100644 --- a/include/etl/private/chrono/day.h +++ b/include/etl/private/chrono/day.h @@ -28,15 +28,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_CHRONO_DAY_INCLUDED -#define ETL_CHRONO_DAY_INCLUDED - -#include "../../platform.h" -#include "../../hash.h" - -#include "duration.h" - -#include +#ifndef ETL_IN_CHRONO_H + #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H +#endif namespace etl { @@ -50,7 +44,9 @@ namespace etl public: //*********************************************************************** - ETL_CONSTEXPR day() + /// Default constructor + //*********************************************************************** + ETL_CONSTEXPR day() ETL_NOEXCEPT : value(0) { } @@ -64,13 +60,17 @@ namespace etl } //*********************************************************************** - ETL_CONSTEXPR day(const etl::chrono::day& other) + /// Copy constructor + //*********************************************************************** + ETL_CONSTEXPR day(const etl::chrono::day& other) ETL_NOEXCEPT : value(other.value) { } //*********************************************************************** - ETL_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::day& rhs) + /// Assignment operator + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::day& rhs) ETL_NOEXCEPT { value = rhs.value; @@ -78,6 +78,7 @@ namespace etl } //*********************************************************************** + /// Pre-increment operator template ETL_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::duration& rhs) { @@ -94,6 +95,8 @@ namespace etl return *this; } + //*********************************************************************** + /// Post-increment operator //*********************************************************************** ETL_CONSTEXPR14 etl::chrono::day operator ++(int) ETL_NOEXCEPT { @@ -103,6 +106,8 @@ namespace etl return temp; } + //*********************************************************************** + /// Pre-decrement operator //*********************************************************************** ETL_CONSTEXPR etl::chrono::day& operator --() ETL_NOEXCEPT { @@ -111,6 +116,8 @@ namespace etl return *this; } + //*********************************************************************** + /// Post-decrement operator //*********************************************************************** ETL_CONSTEXPR14 etl::chrono::day operator --(int) ETL_NOEXCEPT { @@ -120,6 +127,8 @@ namespace etl return temp; } + //*********************************************************************** + /// Plus-equals operator adding etl::chrono::days //*********************************************************************** ETL_CONSTEXPR etl::chrono::day& operator +=(const etl::chrono::days& ds) ETL_NOEXCEPT { @@ -128,6 +137,8 @@ namespace etl return *this; } + //*********************************************************************** + /// Minus-equals operator subtracting etl::chrono::days //*********************************************************************** ETL_CONSTEXPR etl::chrono::day& operator -=(const etl::chrono::days& ds) ETL_NOEXCEPT { @@ -144,6 +155,8 @@ namespace etl return (value >= 1U) && (value <= 31U); } + //*********************************************************************** + /// Conversion operator to unsigned int //*********************************************************************** ETL_CONSTEXPR operator unsigned() const ETL_NOEXCEPT { @@ -274,14 +287,13 @@ namespace etl //*********************************************************************** ETL_CONSTEXPR etl::chrono::days operator -(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT { - etl::chrono::days result; - - return etl::chrono::days(static_cast(d1) - static_cast(d2)); + return etl::chrono::days(static_cast(static_cast(d1)) - + static_cast(static_cast(d2))); } } //************************************************************************* - /// Hash function. + /// Hash function for etl::chrono::day //************************************************************************* #if ETL_USING_8BIT_TYPES template <> @@ -298,6 +310,8 @@ namespace etl #endif } +#if ETL_HAS_CHRONO_LITERALS_DAY +#if ETL_USING_CPP11 namespace etl { namespace literals @@ -305,12 +319,14 @@ namespace etl namespace chrono_literals { //*********************************************************************** - ETL_CONSTEXPR etl::chrono::day operator ""_d(unsigned long long d) ETL_NOEXCEPT + /// Literal for days + //*********************************************************************** + constexpr etl::chrono::day operator ""_day(unsigned long long d) noexcept { - return etl::chrono::day(d); + return etl::chrono::day(static_cast(d)); } } } } - -#endif \ No newline at end of file +#endif +#endif diff --git a/include/etl/private/chrono/duration.h b/include/etl/private/chrono/duration.h index fdb8d9b5..b499f0f4 100644 --- a/include/etl/private/chrono/duration.h +++ b/include/etl/private/chrono/duration.h @@ -28,10 +28,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_CHRONO_DURATION_INCLUDED -#define ETL_CHRONO_DURATION_INCLUDED +#ifndef ETL_IN_CHRONO_H + #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H +#endif -#include "../../platform.h" #include "../../ratio.h" #include "../../static_assert.h" #include "../../limits.h" @@ -50,6 +50,7 @@ namespace etl //*********************************************************************** ETL_CONSTEXPR duration() ETL_NOEXCEPT + : value(0) { } @@ -101,7 +102,6 @@ namespace etl private: TValue value; - //TPeriod period; }; //*********************************************************************** @@ -129,7 +129,7 @@ namespace etl /// duration_cast //*********************************************************************** template - ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration& d) + ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration& d) ETL_NOEXCEPT { using to_value_type = typename TToDuration::value_type; using to_period = typename TToDuration::period; diff --git a/include/etl/private/chrono/last_spec.h b/include/etl/private/chrono/last_spec.h new file mode 100644 index 00000000..71bda5fe --- /dev/null +++ b/include/etl/private/chrono/last_spec.h @@ -0,0 +1,52 @@ +///\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_IN_CHRONO_H + #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H +#endif + +namespace etl +{ + namespace chrono + { + struct last_spec + { + ETL_CONSTEXPR explicit last_spec() + { + } + }; + +#if ETL_USING_CPP17 + inline constexpr last_spec last{}; +#else + static ETL_CONSTANT last_spec last{}; +#endif + } +} \ No newline at end of file diff --git a/include/etl/private/chrono/weekday_indexed.h b/include/etl/private/chrono/weekday_indexed.h new file mode 100644 index 00000000..6c2f37e3 --- /dev/null +++ b/include/etl/private/chrono/weekday_indexed.h @@ -0,0 +1,151 @@ +///\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_IN_CHRONO_H + #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H +#endif + +namespace etl +{ + namespace chrono + { + //*********************************************************************** + /// weekday_indexed + //*********************************************************************** + class weekday_indexed + { + public: + + //*********************************************************************** + /// Default constructor + //*********************************************************************** + ETL_CONSTEXPR weekday_indexed() ETL_NOEXCEPT + { + } + + //*********************************************************************** + /// Construct from unsigned + //*********************************************************************** + ETL_CONSTEXPR weekday_indexed(const etl::chrono::weekday& wd_, unsigned index_) ETL_NOEXCEPT + : wd(wd_) + , i(index_) + { + } + + //*********************************************************************** + /// Copy constructor + //*********************************************************************** + ETL_CONSTEXPR weekday_indexed(const etl::chrono::weekday_indexed& other) ETL_NOEXCEPT + : wd(other.wd) + , i(other.i) + { + } + + //*********************************************************************** + /// Assignment operator + //*********************************************************************** + ETL_CONSTEXPR14 etl::chrono::weekday_indexed& operator =(const etl::chrono::weekday_indexed& rhs) ETL_NOEXCEPT + { + wd = rhs.wd; + i = rhs.i; + + return *this; + } + + //*********************************************************************** + /// Get weekday + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR etl::chrono::weekday weekday() const ETL_NOEXCEPT + { + return wd; + } + + //*********************************************************************** + /// Get index + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR unsigned index() const ETL_NOEXCEPT + { + return i; + } + + //*********************************************************************** + /// Returns true if the weekday and index are valid + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT + { + return wd.ok() && (i >= 1U) && (i <= 5); + } + + private: + + etl::chrono::weekday wd; + unsigned i; + }; + + //*********************************************************************** + /// Equality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator ==(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT + { + return (wd1.weekday() == wd2.weekday()) && + (wd1.index() == wd2.index()); + } + + //*********************************************************************** + /// Inequality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator !=(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT + { + return !(wd1 == wd2); + } + + //*********************************************************************** + /// weekday index operator from index + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday_indexed etl::chrono::weekday::operator[](unsigned index) const ETL_NOEXCEPT + { + return etl::chrono::weekday_indexed(*this, index); + } + } + + //************************************************************************* + /// Hash function for etl::chrono::weekday_indexed + //************************************************************************* +#if ETL_USING_8BIT_TYPES + template <> + struct hash + { + size_t operator()(const etl::chrono::weekday_indexed& wdi) const + { + return etl::hash()(wdi.weekday()) ^ etl::hash()(wdi.index()); + } + }; +#endif +} diff --git a/include/etl/private/chrono/weekday_last.h b/include/etl/private/chrono/weekday_last.h new file mode 100644 index 00000000..e61890f9 --- /dev/null +++ b/include/etl/private/chrono/weekday_last.h @@ -0,0 +1,138 @@ +///\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_IN_CHRONO_H + #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H +#endif + +namespace etl +{ + namespace chrono + { + //*********************************************************************** + /// weekday_last + //*********************************************************************** + class weekday_last + { + public: + + //*********************************************************************** + /// Default constructor + //*********************************************************************** + ETL_CONSTEXPR weekday_last() ETL_NOEXCEPT + { + } + + //*********************************************************************** + /// Construct from unsigned + //*********************************************************************** + ETL_CONSTEXPR weekday_last(const etl::chrono::weekday& wd_) ETL_NOEXCEPT + : wd(wd_) + { + } + + //*********************************************************************** + /// Copy constructor + //*********************************************************************** + ETL_CONSTEXPR weekday_last(const etl::chrono::weekday_last& other) ETL_NOEXCEPT + : wd(other.wd) + { + } + + //*********************************************************************** + /// Assignment operator + //*********************************************************************** + ETL_CONSTEXPR14 etl::chrono::weekday_last& operator =(const etl::chrono::weekday_last& rhs) ETL_NOEXCEPT + { + wd = rhs.wd; + + return *this; + } + + //*********************************************************************** + /// Get weekday + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR etl::chrono::weekday weekday() const ETL_NOEXCEPT + { + return wd; + } + + //*********************************************************************** + /// Returns true if the weekday is valid + //*********************************************************************** + ETL_NODISCARD ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT + { + return wd.ok(); + } + + private: + + etl::chrono::weekday wd; + }; + + //*********************************************************************** + /// Equality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator ==(const etl::chrono::weekday_last& wd1, const etl::chrono::weekday_last& wd2) ETL_NOEXCEPT + { + return (wd1.weekday() == wd2.weekday()); + } + + //*********************************************************************** + /// Inequality operator + //*********************************************************************** + ETL_CONSTEXPR bool operator !=(const etl::chrono::weekday_last& wd1, const etl::chrono::weekday_last& wd2) ETL_NOEXCEPT + { + return !(wd1 == wd2); + } + + //*********************************************************************** + /// weekday index operator from index + //*********************************************************************** + ETL_CONSTEXPR etl::chrono::weekday_last etl::chrono::weekday::operator[](etl::chrono::last_spec) const ETL_NOEXCEPT + { + return etl::chrono::weekday_last(*this); + } + } + + //************************************************************************* + /// Hash function for etl::chrono::weekday_last + //************************************************************************* +#if ETL_USING_8BIT_TYPES + template <> + struct hash + { + size_t operator()(const etl::chrono::weekday_last& wdl) const + { + return etl::hash()(wdl.weekday()); + } + }; +#endif +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 029c0443..648d4c4f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -60,6 +60,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/test_chrono_day.cpp b/test/test_chrono_day.cpp index f8a3d453..815cb4f4 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 { @@ -154,62 +156,177 @@ namespace } //************************************************************************* - TEST(test_plus_days) + TEST(test_day_plus_days) { - std::chrono::day std_day1(0); - std::chrono::day std_day2(0); - etl::chrono::day day1(0); - etl::chrono::day day2(0); - - std::chrono::days std_days(2); - etl::chrono::days days(2); - - for (int i = 0; i < 128; ++i) + for (int d = 0; d < 256; ++d) { - std_day1 = std_days + std_day1; - std_day2 = std_day2 + std_days; - - day1 = days + day1; - day2 = day2 + days; + for (int ds = 0; ds < 256; ++ds) + { + std::chrono::day std_day(d); + etl::chrono::day day(d); - CHECK_EQUAL(std_day1.ok(), day1.ok()); - CHECK_EQUAL(std_day2.ok(), day2.ok()); - CHECK_EQUAL(unsigned(std_day1), unsigned(day1)); - CHECK_EQUAL(unsigned(std_day2), unsigned(day2)); + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_day = std_day + std_days; + day = day + days; + + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } + } + } + + //************************************************************************* + TEST(test_days_plus_day) + { + 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(ds); + etl::chrono::days days(ds); + + std_day = std_days + std_day; + day = days + 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); - - std::chrono::days std_days(2); - etl::chrono::days days(2); - - for (int i = 0; i < 128; ++i) + for (int d = 0; d <= 256; ++d) { - std_day -= std_days; - day -= days; + for (int ds = 0; ds <= 256; ++ds) + { + std::chrono::day std_day(d); + etl::chrono::day day(d); - CHECK_EQUAL(std_day.ok(), day.ok()); - CHECK_EQUAL(unsigned(std_day), unsigned(day)); + std::chrono::days std_days(ds); + etl::chrono::days days(ds); + + std_day -= std_days; + day -= days; + + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } } } //************************************************************************* - TEST(test_literal_days) + TEST(test_day_minus_days) + { + 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(ds); + etl::chrono::days days(ds); + + std_day = std_day - std_days; + day = day - days; + + CHECK_EQUAL(std_day.ok(), day.ok()); + CHECK_EQUAL(unsigned(std_day), unsigned(day)); + } + } + } + + //************************************************************************* + TEST(test_day_minus_day) + { + for (int d = 0; d < 256; ++d) + { + std::chrono::day std_day1(d); + std::chrono::day std_day2(255 - d); + + std::chrono::day day1(d); + std::chrono::day day2(255 - d); + + auto std_days12 = std_day1 - std_day2; + auto std_days21 = std_day2 - std_day1; + + auto days12 = day1 - day2; + auto days21 = day2 - day1; + + CHECK_EQUAL(std_days12.count(), days12.count()); + CHECK_EQUAL(std_days21.count(), days21.count()); + } + } + + //************************************************************************* + TEST(test_min_max_day) + { + CHECK_EQUAL(1U, etl::chrono::day::min()); + CHECK_EQUAL(31U, etl::chrono::day::max()); + } + + //************************************************************************* + TEST(test_literal_day) { using namespace std::literals::chrono_literals; 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)); } + + //************************************************************************* + TEST(test_day_comparison_operators) + { + etl::chrono::day day10(10); + etl::chrono::day day20(20); + + CHECK_TRUE(day10 == day10); + CHECK_FALSE(day10 != day10); + CHECK_TRUE(day10 < day20); + CHECK_FALSE(day10 < day10); + CHECK_FALSE(day20 < day10); + CHECK_TRUE(day10 <= day20); + CHECK_TRUE(day10 <= day10); + CHECK_FALSE(day20 <= day10); + CHECK_FALSE(day10 > day20); + CHECK_FALSE(day10 > day10); + CHECK_TRUE(day20 > day10); + CHECK_FALSE(day10 >= day20); + CHECK_TRUE(day10 >= day10); + CHECK_TRUE(day20 >= day10); + +#if ETL_USING_CPP20 + CHECK_TRUE((day10 <=> day10) == 0); + CHECK_TRUE((day10 <=> day20) < 0); + CHECK_TRUE((day20 <=> day10) > 0); +#endif + } + + //************************************************************************* + TEST(test_day_hashes_are_unique) + { + std::vector hashes; + + for (int i = 0; i < 256; ++i) + { + hashes.push_back(etl::hash()(etl::chrono::day(i))); + } + + 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 new file mode 100644 index 00000000..2dcc7187 --- /dev/null +++ b/test/test_chrono_month.cpp @@ -0,0 +1,385 @@ +/****************************************************************************** +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_month) + { + //************************************************************************* + TEST(test_default_constructor) + { + std::chrono::month std_month; + etl::chrono::month month; + + CHECK_EQUAL(std_month.ok(), month.ok()); + } + + //************************************************************************* + TEST(test_constructor_in_range) + { + for (unsigned i = 0U; i < 256; ++i) + { + std::chrono::month std_month(i); + etl::chrono::month month(i); + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + + //************************************************************************* + TEST(test_pre_increment) + { + std::chrono::month std_month(0); + etl::chrono::month month(0); + + for (int i = 0; i < 255; ++i) + { + ++std_month; + ++month; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + + //************************************************************************* + TEST(test_post_increment) + { + std::chrono::month std_month(0); + etl::chrono::month month(0); + + 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(unsigned(std_last_month), unsigned(last_month)); + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + + //************************************************************************* + TEST(test_pre_decrement) + { + std::chrono::month std_month(255); + etl::chrono::month month(255); + + for (int i = 0; i < 256; ++i) + { + --std_month; + --month; + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + + //************************************************************************* + TEST(test_post_decrement) + { + std::chrono::month std_month(255); + etl::chrono::month month(255); + + 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(unsigned(std_last_month), unsigned(last_month)); + + CHECK_EQUAL(std_month.ok(), month.ok()); + CHECK_EQUAL(unsigned(std_month), unsigned(month)); + } + } + + //************************************************************************* + 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) + { + CHECK_EQUAL(1U, etl::chrono::month::min()); + CHECK_EQUAL(12U, etl::chrono::month::max()); + } + + //************************************************************************* + TEST(test_literal_month) + { + using namespace etl::literals::chrono_literals; + + 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()); + CHECK_TRUE(month3.ok()); + CHECK_TRUE(month4.ok()); + CHECK_TRUE(month5.ok()); + CHECK_TRUE(month6.ok()); + CHECK_TRUE(month7.ok()); + CHECK_TRUE(month8.ok()); + CHECK_TRUE(month9.ok()); + CHECK_TRUE(month10.ok()); + CHECK_TRUE(month11.ok()); + CHECK_TRUE(month12.ok()); + + CHECK_EQUAL(1U, unsigned(month1)); + CHECK_EQUAL(2U, unsigned(month2)); + CHECK_EQUAL(3U, unsigned(month3)); + CHECK_EQUAL(4U, unsigned(month4)); + CHECK_EQUAL(5U, unsigned(month5)); + CHECK_EQUAL(6U, unsigned(month6)); + CHECK_EQUAL(7U, unsigned(month7)); + CHECK_EQUAL(8U, unsigned(month8)); + CHECK_EQUAL(9U, unsigned(month9)); + CHECK_EQUAL(10U, unsigned(month10)); + CHECK_EQUAL(11U, unsigned(month11)); + CHECK_EQUAL(12U, unsigned(month12)); + } + + //************************************************************************* + TEST(test_month_comparison_operators) + { + etl::chrono::month month1(1); + etl::chrono::month month2(2); + + CHECK_TRUE(month1 == month1); + CHECK_FALSE(month1 != month1); + CHECK_TRUE(month1 < month2); + CHECK_FALSE(month1 < month1); + CHECK_FALSE(month2 < month1); + CHECK_TRUE(month1 <= month2); + CHECK_TRUE(month1 <= month1); + CHECK_FALSE(month2 <= month1); + CHECK_FALSE(month1 > month2); + CHECK_FALSE(month1 > month1); + CHECK_TRUE(month2 > month1); + CHECK_FALSE(month1 >= month2); + CHECK_TRUE(month1 >= month1); + CHECK_TRUE(month2 >= month1); + +#if ETL_USING_CPP20 + CHECK_TRUE((month1 <=> month1) == 0); + CHECK_TRUE((month1 <=> month2) < 0); + CHECK_TRUE((month2 <=> month1) > 0); +#endif + } + + //************************************************************************* + TEST(test_month_hashes_are_unique) + { + std::vector hashes; + + for (int i = 0; i < 256; ++i) + { + hashes.push_back(etl::hash()(etl::chrono::month(i))); + } + + std::sort(hashes.begin(), hashes.end()); + (void)std::unique(hashes.begin(), hashes.end()); + CHECK_EQUAL(256U, hashes.size()); + } + + //************************************************************************* + 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)); + } + }; +} + +#endif \ No newline at end of file diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index 8c675a44..20c4b6d4 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -3093,6 +3093,13 @@ + + + + + + + @@ -7950,6 +7957,9 @@ + + + diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index 470fb776..6ab086e7 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -1380,6 +1380,30 @@ ETL\Utilities + + ETL\Private\chrono + + + ETL\Private\chrono + + + ETL\Private\chrono + + + ETL\Private\chrono + + + ETL\Private\chrono + + + ETL\Private\chrono + + + ETL\Private\chrono + + + ETL\Private\chrono + ETL\Utilities @@ -2501,6 +2525,15 @@ Tests\Chrono + + Tests\Chrono + + + Tests\Chrono + + + Tests\Chrono + Tests\Syntax Checks\Source