From e02e54c3df13f31a32c30923c1ad2809dbbbbdb2 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 15 Oct 2024 20:29:07 +0200 Subject: [PATCH] fix split for negative whole day deltas (#138) * fix split for negative whole day deltas * mority's suggestion implemented https://github.com/motis-project/nigiri/pull/138#issuecomment-2373295572 --- include/nigiri/common/delta_t.h | 11 ++--------- test/delta_t_test.cc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 test/delta_t_test.cc diff --git a/include/nigiri/common/delta_t.h b/include/nigiri/common/delta_t.h index c4c355da5..98a5eb0c7 100644 --- a/include/nigiri/common/delta_t.h +++ b/include/nigiri/common/delta_t.h @@ -51,15 +51,8 @@ inline std::pair split_day_mam( day_idx_t const base, delta_t const x) { assert(x != std::numeric_limits::min()); assert(x != std::numeric_limits::max()); - if (x < 0) { - auto const t = -x / 1440 + 1; - auto const min = x + (t * 1440); - return {static_cast(static_cast(to_idx(base)) - t), - minutes_after_midnight_t{min}}; - } else { - return {static_cast(static_cast(to_idx(base)) + x / 1440), - minutes_after_midnight_t{x % 1440}}; - } + auto const minutes = base.v_ * 1440 + x; + return {day_idx_t{minutes / 1440}, minutes_after_midnight_t{minutes % 1440}}; } } // namespace nigiri \ No newline at end of file diff --git a/test/delta_t_test.cc b/test/delta_t_test.cc new file mode 100644 index 000000000..fea812be2 --- /dev/null +++ b/test/delta_t_test.cc @@ -0,0 +1,33 @@ +#include "gtest/gtest.h" + +#include "nigiri/common/delta_t.h" + +using namespace nigiri; + +TEST(DeltaT, NegDeltaValue) { + { + auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{0}); + EXPECT_EQ(day, day_idx_t{10}); + EXPECT_EQ(mam.count(), 0); + } + { + auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{1441}); + EXPECT_EQ(day, day_idx_t{11}); + EXPECT_EQ(mam.count(), 1); + } + { + auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{1440}); + EXPECT_EQ(day, day_idx_t{11}); + EXPECT_EQ(mam.count(), 0); + } + { + auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{-1441}); + EXPECT_EQ(day, day_idx_t{8}); + EXPECT_EQ(mam.count(), 1439); + } + { + auto [day, mam] = split_day_mam(day_idx_t{10}, delta_t{-1440}); + EXPECT_EQ(day, day_idx_t{9}); + EXPECT_EQ(mam.count(), 0); + } +} \ No newline at end of file