Skip to content

Commit

Permalink
inline error in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Oct 12, 2023
1 parent c71a194 commit 17787ec
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
2 changes: 2 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ env.Alias(target='install', source=env.InstallVersionedLib(dir=os.path.join(pref

## Tests ...
if test:
env.Append(CXXFLAGS=' -Wno-error=inline')
if '-Winline' in env['CXXFLAGS']: env['CXXFLAGS'].replace('-Winline','')
cmp_error_fn = 'test/unit_tests/test_compilation_error.json'
cerror_dct = {}
if os.path.isfile(cmp_error_fn): os.remove(cmp_error_fn)
Expand Down
45 changes: 40 additions & 5 deletions src/dtfund.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ class day_of_year {
* If users want to check the instance for validity, then they should use the
* ymd_date::is_valid function.
*/
struct ymd_date {
class ymd_date {
public:
/** @brief ymd_date constructor
* No check for validity will be performed. If you want to check the
* validity of the created instance, use ymd_date::is_valid
Expand All @@ -705,11 +706,27 @@ struct ymd_date {
}

/** @brief Transform to year and day-of-year
* Note that no validation checks are performed on the instance. If needed,
* (e.g. before the conversion), use the is_valid method on the instance.
* The function will first check that the instance is a valid date, before
* performing the transformation (to year and day of year). This is done
* because an invalid ymd_date can result in a seamingly valid ydoy_date
* (e.g. constructing a 29/2 date on a non-leap year).
*/
ydoy_date to_ydoy() const noexcept;
ydoy_date to_ydoy() const;

/** get/set year */
constexpr year &yr() noexcept {return __year;}
/** get/set month */
constexpr month &mn() noexcept {return __month;}
/** get/set day of month */
constexpr day_of_month &dm() noexcept {return __dom;}
/** get year */
constexpr year yr() const noexcept {return __year;}
/** get month */
constexpr month mn() const noexcept {return __month;}
/** get day of month */
constexpr day_of_month dm() const noexcept {return __dom;}

private:
year __year; /** the year */
month __month; /** the month */
day_of_month __dom; /** day of month */
Expand All @@ -723,14 +740,22 @@ struct ymd_date {
* If users want to check the instance for validity, then they should use the
* ymd_date::is_valid function.
*/
struct ydoy_date {
class ydoy_date {
public:
/** @brief ymd_date constructor
* No check for validity will be performed. If you want to check the
* validity of the created instance, use ymd_date::is_valid
*/
constexpr ydoy_date(year y = year{}, day_of_year d = day_of_year{}) noexcept
: __year(y), __doy(d) {}

/** @brief Constructor from a Year/Month/DayOfMonth instance
* In case the input argument \p ymd is not a valid date, the constructor
* will throw.
*/
ydoy_date(const ymd_date &ymd)
: __year(ymd.yr()), __doy(ymd.to_ydoy().dy()) {}

/** @brief Check if the date is a valid calendar date
* @return True if the date is valid, false otherwise.
*/
Expand All @@ -753,7 +778,17 @@ struct ydoy_date {
(double)days_in_year;
}
}

/** get/set year */
year &yr() noexcept {return __year;}
/** get/set day of year */
day_of_year &dy() noexcept {return __doy;}
/** get year */
constexpr year yr() const noexcept {return __year;}
/** get day of year */
constexpr day_of_year dy() const noexcept {return __doy;}

private:
year __year; /** the year */
day_of_year __doy; /** day of year */
}; /* ydoy_date */
Expand Down
12 changes: 6 additions & 6 deletions src/ydoy_date.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "dtfund.hpp"

constexpr dso::ymd_date dso::ydoy_date::to_ymd() const noexcept {
ymd_date yd;
int guess = static_cast<int>(__doy.as_underlying_type() * 0.032);
int leap = __year.is_leap();
int more = ((__doy.as_underlying_type() -
int leap = yr().is_leap();
int more = ((dy().as_underlying_type() -
dso::core::month_day[leap][guess + 1]) > 0);
/* assign */
yd.__year = __year;
yd.__month = month{guess + more + 1};
yd.__dom = day_of_month(__doy.as_underlying_type() -
ymd_date yd;
yd.yr() = yr();
yd.mn() = month(guess + more + 1);
yd.dm() = day_of_month(dy().as_underlying_type() -
dso::core::month_day[leap][guess + more]);
return yd;
}
17 changes: 12 additions & 5 deletions src/ymd_date.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#include "dtfund.hpp"
#include <stdexcept>

dso::ydoy_date dso::ymd_date::to_ydoy() const noexcept {
int leap = __year.is_leap();
int md = __month.as_underlying_type() - 1;
return dso::ydoy_date(__year, dso::day_of_year(core::month_day[leap][md] +
__dom.as_underlying_type()));
dso::ydoy_date dso::ymd_date::to_ydoy() const {
if (!is_valid()) {
throw std::invalid_argument(
"[ERROR] Tring to compute year/day_of_year from an invalid "
"year/month/day instance (traceback:" +
std::string(__func__) + ")\n");
}
int leap = yr().is_leap();
int md = mn().as_underlying_type() - 1;
return dso::ydoy_date(yr(), dso::day_of_year(core::month_day[leap][md] +
dm().as_underlying_type()));
}
25 changes: 25 additions & 0 deletions test/unit_tests/ymd_date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,46 @@ using dso::day_of_month;
using dso::month;
using dso::year;
using dso::ymd_date;
using dso::ydoy_date;
using dso::day_of_year;

int main() {

/* note that we can construct an invali date */
ymd_date d1(year(2023), month(13), day_of_month(56));
/* however, we can always validate a ymd_date instance */
assert(!(d1.is_valid()));

for (int y = 1804; y <= 2400; y++) {
if (dso::year(y).is_leap()) {
/* leap year, should have a valid date for YYYY/02/29 */
ymd_date ymd(year(y), month(2), day_of_month(29));
assert(ymd.is_valid());
/* transform to Year - Day of Year */
const ydoy_date ydoy (ymd);
assert((ydoy.yr() == year(y)) && (ydoy.dy() == day_of_year(31 + 29)));
assert(ydoy.is_valid());
} else {
/* non-leap year, should not have a valid date for YYYY/02/29 */
ymd_date iymd(year(y), month(2), day_of_month(29));
assert(!(iymd.is_valid()));
/* transform to Year - Day of Year; since this is an invalid date, the
* construction should throw!
*/
try {
const ydoy_date iydoy(iymd);
/* shit,it didn't throw*/
assert(1 == 2);
} catch (std::exception &) {
; /* good, it throwed */
}
/* ...but should have a valid date for YYYY/02/28 */
ymd_date vymd(year(y), month(2), day_of_month(28));
assert(vymd.is_valid());
/* transform to Year - Day of Year */
const ydoy_date vydoy (vymd);
assert((vydoy.yr() == year(y)) && (vydoy.dy() == day_of_year(31 + 28)));
assert(vydoy.is_valid());
}
}

Expand Down

0 comments on commit 17787ec

Please sign in to comment.