Merge branch 'feature/#757-Add-time-date-classes' of https://github.com/ETLCPP/etl-21 into dual-licence/feature/#757-Add-time-date-classes

# Conflicts:
#	include/etl/chrono.h
#	include/etl/private/chrono/day.h
#	include/etl/private/chrono/duration.h
#	test/CMakeLists.txt
#	test/test_chrono_day.cpp
#	test/vs2022/etl.vcxproj
#	test/vs2022/etl.vcxproj.filters
This commit is contained in:
John Wellbelove 2024-09-07 21:24:27 +01:00
commit d74d8ad3b5
12 changed files with 1012 additions and 58 deletions

View File

@ -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 <stdint.h>
#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

View File

@ -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);

View File

@ -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 <stdint.h>
#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 <typename TToDuration, typename TValue2, typename TPeriod2>
ETL_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::duration<TValue2, TPeriod2>& 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<unsigned>(d1) - static_cast<unsigned>(d2));
return etl::chrono::days(static_cast<int>(static_cast<unsigned>(d1)) -
static_cast<int>(static_cast<unsigned>(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<unsigned>(d));
}
}
}
}
#endif
#endif
#endif

View File

@ -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 <typename TToDuration, typename TValue, typename TPeriod>
ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration<TValue, TPeriod>& d)
ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration<TValue, TPeriod>& d) ETL_NOEXCEPT
{
using to_value_type = typename TToDuration::value_type;
using to_period = typename TToDuration::period;

View File

@ -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
}
}

View File

@ -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 <b>true</b> 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<etl::chrono::weekday_indexed>
{
size_t operator()(const etl::chrono::weekday_indexed& wdi) const
{
return etl::hash<etl::chrono::weekday>()(wdi.weekday()) ^ etl::hash<unsigned>()(wdi.index());
}
};
#endif
}

View File

@ -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 <b>true</b> 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<etl::chrono::weekday_last>
{
size_t operator()(const etl::chrono::weekday_last& wdl) const
{
return etl::hash<etl::chrono::weekday>()(wdl.weekday());
}
};
#endif
}

View File

@ -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

View File

@ -37,6 +37,8 @@ SOFTWARE.
#include "etl/chrono.h"
#include <chrono>
#include <array>
#include <algorithm>
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<size_t> hashes;
for (int i = 0; i < 256; ++i)
{
hashes.push_back(etl::hash<etl::chrono::day>()(etl::chrono::day(i)));
}
std::sort(hashes.begin(), hashes.end());
(void)std::unique(hashes.begin(), hashes.end());
CHECK_EQUAL(256U, hashes.size());
}
};
}

385
test/test_chrono_month.cpp Normal file
View File

@ -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 <chrono>
#include <array>
#include <algorithm>
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<size_t> hashes;
for (int i = 0; i < 256; ++i)
{
hashes.push_back(etl::hash<etl::chrono::month>()(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<unsigned>(std::chrono::January), static_cast<unsigned>(etl::chrono::January));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::February), static_cast<unsigned>(etl::chrono::February));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::March), static_cast<unsigned>(etl::chrono::March));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::April), static_cast<unsigned>(etl::chrono::April));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::May), static_cast<unsigned>(etl::chrono::May));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::June), static_cast<unsigned>(etl::chrono::June));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::July), static_cast<unsigned>(etl::chrono::July));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::August), static_cast<unsigned>(etl::chrono::August));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::September), static_cast<unsigned>(etl::chrono::September));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::October), static_cast<unsigned>(etl::chrono::October));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::November), static_cast<unsigned>(etl::chrono::November));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::December), static_cast<unsigned>(etl::chrono::December));
}
};
}
#endif

View File

@ -3093,6 +3093,13 @@
<ClInclude Include="..\..\include\etl\private\bitset_legacy.h" />
<ClInclude Include="..\..\include\etl\private\bitset_new.h" />
<ClInclude Include="..\..\include\etl\private\chrono\day.h" />
<ClInclude Include="..\..\include\etl\private\chrono\duration.h" />
<ClInclude Include="..\..\include\etl\private\chrono\last_spec.h" />
<ClInclude Include="..\..\include\etl\private\chrono\month.h" />
<ClInclude Include="..\..\include\etl\private\chrono\weekday.h" />
<ClInclude Include="..\..\include\etl\private\chrono\weekday_indexed.h" />
<ClInclude Include="..\..\include\etl\private\chrono\weekday_last.h" />
<ClInclude Include="..\..\include\etl\private\chrono\year.h" />
<ClInclude Include="..\..\include\etl\private\diagnostic_array_bounds_push.h" />
<ClInclude Include="..\..\include\etl\private\diagnostic_cxx_20_compat_push.h" />
<ClInclude Include="..\..\include\etl\private\diagnostic_deprecated_push.h" />
@ -7950,6 +7957,9 @@
<ClCompile Include="..\test_callback_timer_locked.cpp" />
<ClCompile Include="..\test_char_traits.cpp" />
<ClCompile Include="..\test_chrono_day.cpp" />
<ClCompile Include="..\test_chrono_month.cpp" />
<ClCompile Include="..\test_chrono_weekday.cpp" />
<ClCompile Include="..\test_chrono_year.cpp" />
<ClCompile Include="..\test_circular_buffer.cpp" />
<ClCompile Include="..\test_circular_buffer_external_buffer.cpp" />
<ClCompile Include="..\test_circular_iterator.cpp" />

View File

@ -1380,6 +1380,30 @@
<ClInclude Include="..\..\include\etl\chrono.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\duration.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\day.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\month.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\year.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\weekday.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\weekday_indexed.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\weekday_last.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\last_spec.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\day.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
@ -2501,6 +2525,15 @@
<ClCompile Include="..\test_chrono_day.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_month.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_year.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_weekday.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\syntax_check\absolute.h.t.cpp">
<Filter>Tests\Syntax Checks\Source</Filter>
</ClCompile>