1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Documentation:
Copyright(c) 2024 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"
#include "unit_test_framework.h"
#include "etl/chrono.h"
#include <vector>
#include <algorithm>
// Set to 0 to reference against std::chrono
#define ETL_USING_ETL_CHRONO 1
#if ETL_USING_ETL_CHRONO
#define Chrono etl::chrono
#else
#if ETL_USING_CPP20
#include <chrono>
#define Chrono std::chrono
#else
#error std::chrono not supported
#endif
#endif
namespace
{
SUITE(test_chrono_month_day)
{
//*************************************************************************
TEST(test_default_constructor)
{
Chrono::month_day md;
CHECK_FALSE(md.ok()); // Default-constructed month_day is not valid
}
//*************************************************************************
TEST(test_constructor_with_month_and_day)
{
Chrono::month_day md{Chrono::January, Chrono::day{15}};
CHECK_TRUE(md.ok()); // Valid month_day
CHECK_EQUAL(Chrono::January, md.month());
CHECK_EQUAL(Chrono::day{15}, md.day());
}
//*************************************************************************
TEST(test_invalid_month_day)
{
Chrono::month_day md{Chrono::month{13}, Chrono::day{15}}; // Invalid month (13)
CHECK_FALSE(md.ok()); // Invalid month_day
}
//*************************************************************************
TEST(test_invalid_day_in_month_day)
{
Chrono::month_day md{Chrono::January, Chrono::day{32}}; // Invalid day (32)
CHECK_FALSE(md.ok()); // Invalid month_day
}
#if ETL_USING_CPP20
//*************************************************************************
TEST(test_month_day_spaceship_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{15}};
CHECK_TRUE((md1 <=> md3) == std::strong_ordering::equal); // Same month and day
CHECK_TRUE((md1 <=> md2) == std::strong_ordering::less); // Different month and day
CHECK_TRUE((md2 <=> md1) == std::strong_ordering::greater); // Same month and day
}
#endif
#if ETL_USING_ETL_CHRONO
//*************************************************************************
TEST(test_month_day_compare)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{15}};
CHECK_TRUE(md1.compare(md3) == 0); // Same month and day
CHECK_TRUE(md1.compare(md2) == -1); // Different month and day
CHECK_TRUE(md2.compare(md1) == 1); // Same month and day
}
#endif
//*************************************************************************
TEST(test_month_day_equality_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{20}};
CHECK_TRUE(md1 == md1); // January == January
CHECK_FALSE(md1 == md2); // January != February
CHECK_FALSE(md1 == md3); // 20th != 15th in the same month
}
//*************************************************************************
TEST(test_month_day_not_equality_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{20}};
CHECK_FALSE(md1 != md1); // January == January
CHECK_TRUE(md1 != md2); // January != February
CHECK_TRUE(md1 != md3); // 20th != 15th in the same month
}
//*************************************************************************
TEST(test_month_day_less_than_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{20}};
CHECK_TRUE(md1 < md2); // January < February
CHECK_TRUE(md1 < md3); // 15th < 20th in the same month
CHECK_FALSE(md2 < md1); // February !< January
CHECK_FALSE(md3 < md1); // 20th !< 15th in the same month
}
//*************************************************************************
TEST(test_month_day_less_than_equal_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{20}};
CHECK_TRUE(md1 <= md1); // January <= January
CHECK_TRUE(md1 <= md2); // January <= February
CHECK_TRUE(md1 <= md3); // 15th <= 20th in the same month
CHECK_FALSE(md2 <= md1); // February !<= January
CHECK_FALSE(md3 <= md1); // 20th !<= 15th in the same month
}
//*************************************************************************
TEST(test_month_day_greater_than_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{20}};
CHECK_TRUE(md2 > md1); // February > January
CHECK_TRUE(md3 > md1); // 20th > 15th in the same month
CHECK_FALSE(md1 > md2); // January !> February
CHECK_FALSE(md1 > md3); // 15th !> 20th in the same month
}
//*************************************************************************
TEST(test_month_day_greater_than_equal_operator)
{
Chrono::month_day md1{Chrono::January, Chrono::day{15}};
Chrono::month_day md2{Chrono::February, Chrono::day{10}};
Chrono::month_day md3{Chrono::January, Chrono::day{20}};
CHECK_TRUE(md1 >= md1); // January >= January
CHECK_TRUE(md2 >= md1); // February >= January
CHECK_TRUE(md3 >= md1); // 20th >= 15th in the same month
CHECK_FALSE(md1 >= md2); // January !>= February
CHECK_FALSE(md1 >= md3); // 15th !>= 20th in the same month
}
//*************************************************************************
TEST(test_month_day_min_max)
{
Chrono::month_day md_min{Chrono::January, Chrono::day{1}};
Chrono::month_day md_max{Chrono::December, Chrono::day{31}};
CHECK_TRUE(md_min.ok());
CHECK_TRUE(md_max.ok());
CHECK_EQUAL(Chrono::January, md_min.month());
CHECK_EQUAL(Chrono::day{1}, md_min.day());
CHECK_EQUAL(Chrono::December, md_max.month());
CHECK_EQUAL(Chrono::day{31}, md_max.day());
}
};
}
|