Skip to content

Commit

Permalink
Remove code duplication in test cases (#168)
Browse files Browse the repository at this point in the history
* remove code duplication

* remove code duplication
  • Loading branch information
mority authored Jan 28, 2025
1 parent 7988877 commit 119e5de
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 166 deletions.
45 changes: 6 additions & 39 deletions test/routing/clasz_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "../raptor_search.h"
#include "../rt/util.h"
#include "results_to_string.h"

using namespace nigiri;
using namespace date;
Expand Down Expand Up @@ -70,7 +71,6 @@ leg 0: (A, A) [2024-03-01 09:00] -> (C, C) [2024-03-01 10:00]
0: A A............................................... d: 01.03 09:00 [01.03 10:00] [{name=X, day=2024-03-01, id=AIR, src=0}]
1: C C............................................... a: 01.03 10:00 [01.03 11:00]
)"sv;

constexpr auto const expected_1 =
Expand All @@ -88,7 +88,6 @@ leg 2: (B, B) [2024-03-01 10:05] -> (C, C) [2024-03-01 11:00]
0: B B............................................... d: 01.03 10:05 [01.03 11:05] [{name=2, day=2024-03-01, id=T2, src=0}]
1: C C............................................... a: 01.03 11:00 [01.03 12:00]
)"sv;

constexpr auto const expected_rt =
Expand All @@ -101,7 +100,6 @@ leg 0: (A, A) [2024-03-01 09:10] -> (C, C) [2024-03-01 10:10]
0: A A............................................... d: 01.03 09:00 [01.03 10:00] [{name=X, day=2024-03-01, id=AIR, src=0}]
1: C C............................................... a: 01.03 10:00 [01.03 11:00]
)"sv;

constexpr auto const expected_rt_1 =
Expand All @@ -119,7 +117,6 @@ leg 2: (B, B) [2024-03-01 10:15] -> (C, C) [2024-03-01 11:10]
0: B B............................................... d: 01.03 10:05 [01.03 11:05] [{name=2, day=2024-03-01, id=T2, src=0}]
1: C C............................................... a: 01.03 11:00 [01.03 12:00]
)"sv;

} // namespace
Expand All @@ -146,41 +143,23 @@ TEST(routing, clasz_filter_test) {
raptor_search(tt, nullptr, "A", "C", tt.date_range_,
direction::kForward, routing::all_clasz_allowed());

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(expected, ss.str());
EXPECT_EQ(expected, to_string(tt, results));
}

{ // All available classes.
auto const results = raptor_search(
tt, nullptr, "A", "C", tt.date_range_, direction::kForward,
make_mask(clasz::kCoach, clasz::kRegionalFast, clasz::kAir));

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(expected, ss.str());
EXPECT_EQ(expected, to_string(tt, results));
}

{ // No plane - one transfer, 2h
auto const results = raptor_search(
tt, nullptr, "A", "C", tt.date_range_, direction::kForward,
make_mask(clasz::kCoach, clasz::kRegionalFast));

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(expected_1, ss.str());
EXPECT_EQ(expected_1, to_string(tt, results));
}

{ // No connection.
Expand Down Expand Up @@ -214,26 +193,14 @@ TEST(routing, clasz_filter_test) {
tt, &rtt, "A", "C", tt.date_range_, direction::kForward,
make_mask(clasz::kCoach, clasz::kRegionalFast, clasz::kAir));

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(expected_rt, ss.str());
EXPECT_EQ(expected_rt, to_string(tt, results));
}

{ // No plane - one transfer, 2h
auto const results =
raptor_search(tt, &rtt, "A", "C", tt.date_range_, direction::kForward,
make_mask(clasz::kCoach, clasz::kRegionalFast));

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(expected_rt_1, ss.str());
EXPECT_EQ(expected_rt_1, to_string(tt, results));
}
}
29 changes: 3 additions & 26 deletions test/routing/raptor_intermodal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "../loader/hrd/hrd_timetable.h"
#include "../raptor_search.h"
#include "results_to_string.h"

using namespace date;
using namespace nigiri;
Expand All @@ -30,7 +31,6 @@ leg 3: (B, 0000002) [2020-03-30 06:45] -> (C, 0000003) [2020-03-30 07:45]
leg 4: (C, 0000003) [2020-03-30 07:45] -> (END, END) [2020-03-30 08:00]
MUMO (id=77, duration=15)
[2020-03-30 05:50, 2020-03-30 08:30]
TRANSFERS: 1
FROM: (START, START) [2020-03-30 05:50]
Expand All @@ -48,7 +48,6 @@ leg 3: (B, 0000002) [2020-03-30 07:15] -> (C, 0000003) [2020-03-30 08:15]
leg 4: (C, 0000003) [2020-03-30 08:15] -> (END, END) [2020-03-30 08:30]
MUMO (id=77, duration=15)
)";

TEST(routing, raptor_intermodal_forward) {
Expand All @@ -70,14 +69,7 @@ TEST(routing, raptor_intermodal_forward) {
unixtime_t{sys_days{2020_y / March / 30}} + 6_hours},
nigiri::direction::kForward);

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
std::cout << ss.str() << "\n";
EXPECT_EQ(std::string_view{fwd_journeys}, ss.str());
EXPECT_EQ(std::string_view{fwd_journeys}, to_string(tt, results));
};

constexpr auto const bwd_journeys = R"(
Expand All @@ -98,7 +90,6 @@ leg 3: (B, 0000002) [2020-03-30 01:45] -> (C, 0000003) [2020-03-30 02:45]
leg 4: (C, 0000003) [2020-03-30 02:45] -> (START, START) [2020-03-30 03:00]
MUMO (id=99, duration=15)
[2020-03-30 00:50, 2020-03-30 03:30]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 00:50]
Expand All @@ -116,7 +107,6 @@ leg 3: (B, 0000002) [2020-03-30 02:15] -> (C, 0000003) [2020-03-30 03:15]
leg 4: (C, 0000003) [2020-03-30 03:15] -> (START, START) [2020-03-30 03:30]
MUMO (id=99, duration=15)
[2020-03-30 01:20, 2020-03-30 04:00]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 01:20]
Expand All @@ -134,7 +124,6 @@ leg 3: (B, 0000002) [2020-03-30 02:45] -> (C, 0000003) [2020-03-30 03:45]
leg 4: (C, 0000003) [2020-03-30 03:45] -> (START, START) [2020-03-30 04:00]
MUMO (id=99, duration=15)
[2020-03-30 01:50, 2020-03-30 04:30]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 01:50]
Expand All @@ -152,7 +141,6 @@ leg 3: (B, 0000002) [2020-03-30 03:15] -> (C, 0000003) [2020-03-30 04:15]
leg 4: (C, 0000003) [2020-03-30 04:15] -> (START, START) [2020-03-30 04:30]
MUMO (id=99, duration=15)
[2020-03-30 02:20, 2020-03-30 05:00]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 02:20]
Expand All @@ -170,7 +158,6 @@ leg 3: (B, 0000002) [2020-03-30 03:45] -> (C, 0000003) [2020-03-30 04:45]
leg 4: (C, 0000003) [2020-03-30 04:45] -> (START, START) [2020-03-30 05:00]
MUMO (id=99, duration=15)
[2020-03-30 02:50, 2020-03-30 05:30]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 02:50]
Expand All @@ -188,7 +175,6 @@ leg 3: (B, 0000002) [2020-03-30 04:15] -> (C, 0000003) [2020-03-30 05:15]
leg 4: (C, 0000003) [2020-03-30 05:15] -> (START, START) [2020-03-30 05:30]
MUMO (id=99, duration=15)
[2020-03-30 03:20, 2020-03-30 06:00]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 03:20]
Expand All @@ -206,7 +192,6 @@ leg 3: (B, 0000002) [2020-03-30 04:45] -> (C, 0000003) [2020-03-30 05:45]
leg 4: (C, 0000003) [2020-03-30 05:45] -> (START, START) [2020-03-30 06:00]
MUMO (id=99, duration=15)
[2020-03-30 03:50, 2020-03-30 06:30]
TRANSFERS: 1
FROM: (END, END) [2020-03-30 03:50]
Expand All @@ -224,7 +209,6 @@ leg 3: (B, 0000002) [2020-03-30 05:15] -> (C, 0000003) [2020-03-30 06:15]
leg 4: (C, 0000003) [2020-03-30 06:15] -> (START, START) [2020-03-30 06:30]
MUMO (id=99, duration=15)
)";

TEST(routing, raptor_intermodal_backward) {
Expand All @@ -246,12 +230,5 @@ TEST(routing, raptor_intermodal_backward) {
unixtime_t{sys_days{2020_y / March / 30}} + 6_hours},
nigiri::direction::kBackward, 3U, true, true);

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}

EXPECT_EQ(std::string_view{bwd_journeys}, ss.str());
EXPECT_EQ(std::string_view{bwd_journeys}, to_string(tt, results));
}
21 changes: 3 additions & 18 deletions test/routing/raptor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../loader/hrd/hrd_timetable.h"

#include "../raptor_search.h"
#include "results_to_string.h"

using namespace date;
using namespace nigiri;
Expand All @@ -27,7 +28,6 @@ leg 2: (B, 0000002) [2020-03-30 06:15] -> (C, 0000003) [2020-03-30 07:15]
0: 0000002 B............................................... d: 30.03 06:15 [30.03 08:15] [{name=RE 7331, day=2020-03-30, id=7331/0000002/375/0000003/435/, src=0}]
1: 0000003 C............................................... a: 30.03 07:15 [30.03 09:15]
[2020-03-30 05:30, 2020-03-30 07:45]
TRANSFERS: 1
FROM: (A, 0000001) [2020-03-30 05:30]
Expand All @@ -41,7 +41,6 @@ leg 2: (B, 0000002) [2020-03-30 06:45] -> (C, 0000003) [2020-03-30 07:45]
0: 0000002 B............................................... d: 30.03 06:45 [30.03 08:45] [{name=RE 7331, day=2020-03-30, id=7331/0000002/405/0000003/465/, src=0}]
1: 0000003 C............................................... a: 30.03 07:45 [30.03 09:45]
)";

TEST(routing, raptor_forward) {
Expand All @@ -57,13 +56,7 @@ TEST(routing, raptor_forward) {
interval{unixtime_t{sys_days{2020_y / March / 30}} + 5_hours,
unixtime_t{sys_days{2020_y / March / 30}} + 6_hours});

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(std::string_view{fwd_journeys}, ss.str());
EXPECT_EQ(std::string_view{fwd_journeys}, to_string(tt, results));
}

constexpr auto const bwd_journeys = R"(
Expand All @@ -80,7 +73,6 @@ leg 2: (B, 0000002) [2020-03-30 04:15] -> (C, 0000003) [2020-03-30 05:15]
0: 0000002 B............................................... d: 30.03 04:15 [30.03 06:15] [{name=RE 7331, day=2020-03-30, id=7331/0000002/255/0000003/315/, src=0}]
1: 0000003 C............................................... a: 30.03 05:15 [30.03 07:15]
[2020-03-30 03:30, 2020-03-30 05:45]
TRANSFERS: 1
FROM: (A, 0000001) [2020-03-30 03:30]
Expand All @@ -94,7 +86,6 @@ leg 2: (B, 0000002) [2020-03-30 04:45] -> (C, 0000003) [2020-03-30 05:45]
0: 0000002 B............................................... d: 30.03 04:45 [30.03 06:45] [{name=RE 7331, day=2020-03-30, id=7331/0000002/285/0000003/345/, src=0}]
1: 0000003 C............................................... a: 30.03 05:45 [30.03 07:45]
)";

TEST(routing, raptor_backward) {
Expand All @@ -114,11 +105,5 @@ TEST(routing, raptor_backward) {

EXPECT_EQ(2U, results.size());

std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt);
ss << "\n\n";
}
EXPECT_EQ(std::string_view{bwd_journeys}, ss.str());
EXPECT_EQ(std::string_view{bwd_journeys}, to_string(tt, results));
}
29 changes: 29 additions & 0 deletions test/routing/results_to_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <sstream>
#include <string>

#include "nigiri/routing/journey.h"
#include "nigiri/routing/pareto_set.h"
#include "nigiri/timetable.h"

inline std::string to_string(
nigiri::timetable const& tt,
nigiri::rt_timetable const* rtt,
nigiri::pareto_set<nigiri::routing::journey> const& results,
bool const debug = false) {
std::stringstream ss;
ss << "\n";
for (auto const& x : results) {
x.print(ss, tt, rtt, debug);
ss << "\n";
}
return ss.str();
}

inline std::string to_string(
nigiri::timetable const& tt,
nigiri::pareto_set<nigiri::routing::journey> const& results,
bool const debug = false) {
return to_string(tt, nullptr, results, debug);
}
25 changes: 9 additions & 16 deletions test/routing/rt_routing_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../loader/hrd/hrd_timetable.h"

#include "../raptor_search.h"
#include "results_to_string.h"

using namespace date;
using namespace nigiri;
Expand Down Expand Up @@ -69,7 +70,8 @@ T_RE2,01:00:00,01:00:00,D,3,0,0
)");
}

constexpr auto const fwd_journeys = R"([2019-05-02 23:00, 2019-05-03 01:00]
constexpr auto const fwd_journeys = R"(
[2019-05-02 23:00, 2019-05-03 01:00]
TRANSFERS: 1
FROM: (A, A) [2019-05-02 23:00]
TO: (D, D) [2019-05-03 01:00]
Expand Down Expand Up @@ -131,16 +133,12 @@ TEST(routing, rt_raptor_forward) {

auto const results =
raptor_search(tt, &rtt, "A", "D", sys_days{May / 2 / 2019} + 23h);
std::stringstream ss;
for (auto const& x : results) {
x.print(ss, tt, &rtt);
ss << "\n";
}
std::cout << ss.str() << "\n";
EXPECT_EQ(std::string_view{fwd_journeys}, ss.str());

EXPECT_EQ(std::string_view{fwd_journeys}, to_string(tt, &rtt, results));
}

constexpr auto const bwd_journeys = R"([2019-05-02 23:00, 2019-05-03 02:00]
constexpr auto const bwd_journeys = R"(
[2019-05-02 23:00, 2019-05-03 02:00]
TRANSFERS: 1
FROM: (A, A) [2019-05-02 23:00]
TO: (D, D) [2019-05-03 01:00]
Expand Down Expand Up @@ -201,11 +199,6 @@ TEST(routing, rt_raptor_backward) {
auto const results =
raptor_search(tt, &rtt, "D", "A", sys_days{May / 3 / 2019} + 2h,
nigiri::direction::kBackward);
std::stringstream ss;
for (auto const& x : results) {
x.print(ss, tt, &rtt);
ss << "\n";
}
std::cout << ss.str() << "\n";
EXPECT_EQ(std::string_view{bwd_journeys}, ss.str());

EXPECT_EQ(std::string_view{bwd_journeys}, to_string(tt, &rtt, results));
}
Loading

0 comments on commit 119e5de

Please sign in to comment.