|
|
1 |
#include "wvtest.h" |
|
|
2 |
#include "tripgraph.h" |
|
|
3 |
|
|
|
4 |
using namespace std; |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
WVTEST_MAIN("basic_graph_pathfinding") |
|
|
8 |
{ |
|
|
9 |
TripGraph g; |
|
|
10 |
|
|
|
11 |
// simple path, just walking |
|
|
12 |
g.add_tripstop(0, TripStop::OSM, 0.0f, 0.0f); |
|
|
13 |
g.add_tripstop(1, TripStop::OSM, 1.0f, 0.0f); |
|
|
14 |
g.add_walkhop(0, 1); |
|
|
15 |
|
|
|
16 |
{ |
|
|
17 |
TripPath *p = g.find_path(0, false, 0.0, 0.0, 1.0, 0.0); |
|
|
18 |
|
|
|
19 |
std::deque<TripAction> actions = p->get_actions(); |
|
|
20 |
WVPASSEQ(actions.size(), 1); |
|
|
21 |
|
|
|
22 |
TripAction action = actions.front(); |
|
|
23 |
WVPASSEQ(action.src_id, 0); |
|
|
24 |
WVPASSEQ(action.dest_id, 1); |
|
|
25 |
|
|
|
26 |
delete p; |
|
|
27 |
} |
|
|
28 |
|
|
|
29 |
// take the triphop if we have it |
|
|
30 |
{ |
|
|
31 |
ServicePeriod s(0, 0, 0, 0, 7, 0, 100, 2000, true, true, true); |
|
|
32 |
g.add_service_period(s); |
|
|
33 |
g.add_triphop(500, 1000, 0, 1, 1, 1, 0); |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
{ |
|
|
37 |
TripPath *p = g.find_path(0, false, 0.0, 0.0, 1.0, 0.0); |
|
|
38 |
|
|
|
39 |
std::deque<TripAction> actions = p->get_actions(); |
|
|
40 |
WVPASSEQ(actions.size(), 1); |
|
|
41 |
|
|
|
42 |
TripAction action = actions.front(); |
|
|
43 |
WVPASSEQ(action.src_id, 0); |
|
|
44 |
WVPASSEQ(action.dest_id, 1); |
|
|
45 |
WVPASSEQ(action.start_time, 500.0f); |
|
|
46 |
WVPASSEQ(action.end_time, 1000.0f); |
|
|
47 |
|
|
|
48 |
delete p; |
|
|
49 |
} |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
WVTEST_MAIN("basic_graph_saveload") |
|
|
54 |
{ |
|
|
55 |
TripGraph g; |
|
|
56 |
g.add_tripstop(0, TripStop::OSM, 0.0f, 0.0f); |
|
|
57 |
g.add_tripstop(1, TripStop::OSM, 1.0f, 0.0f); |
|
|
58 |
g.add_walkhop(0, 1); |
|
|
59 |
|
|
|
60 |
ServicePeriod s(0, 1, 0, 0, 7, 0, 100, 2000, true, true, true); |
|
|
61 |
g.add_service_period(s); |
|
|
62 |
g.add_triphop(500, 1000, 0, 1, 1, 1, 0); |
|
|
63 |
|
|
|
64 |
char *tmpgraphname = tmpnam(NULL); // security issues in unit tests? bah. |
|
|
65 |
unlink(tmpgraphname); |
|
|
66 |
g.save(tmpgraphname); |
|
|
67 |
|
|
|
68 |
TripGraph g2; |
|
|
69 |
g2.load(tmpgraphname); |
|
|
70 |
|
|
|
71 |
// verify that we have two tripstops |
|
|
72 |
for (int i=0; i<2; i++) |
|
|
73 |
{ |
|
|
74 |
TripStop ts = g2.get_tripstop(i); |
|
|
75 |
WVPASSEQ(ts.type, TripStop::OSM); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
// verify that we can still solve a basic path |
|
|
79 |
{ |
|
|
80 |
TripPath *p = g.find_path(0, false, 0.0, 0.0, 1.0, 0.0); |
|
|
81 |
|
|
|
82 |
std::deque<TripAction> actions = p->get_actions(); |
|
|
83 |
WVPASSEQ(actions.size(), 1); |
|
|
84 |
|
|
|
85 |
TripAction action = actions.front(); |
|
|
86 |
WVPASSEQ(action.src_id, 0); |
|
|
87 |
WVPASSEQ(action.dest_id, 1); |
|
|
88 |
WVPASSEQ(action.start_time, 500.0f); |
|
|
89 |
WVPASSEQ(action.end_time, 1000.0f); |
|
|
90 |
|
|
|
91 |
delete p; |
|
|
92 |
} |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
WVTEST_MAIN("impossible_path") |
|
|
97 |
{ |
|
|
98 |
TripGraph g; |
|
|
99 |
g.add_tripstop(0, TripStop::OSM, 0.0f, 0.0f); |
|
|
100 |
g.add_tripstop(1, TripStop::OSM, 1.0f, 0.0f); |
|
|
101 |
g.add_tripstop(2, TripStop::OSM, 0.0f, 1.0f); |
|
|
102 |
g.add_walkhop(0, 1); |
|
|
103 |
|
|
|
104 |
TripPath *p = g.find_path(0, false, 0.0, 0.0, 0.0, 1.0); |
|
|
105 |
WVPASS(!p); |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
WVTEST_MAIN("tripstops_in_range") |
|
|
110 |
{ |
|
|
111 |
TripGraph g; |
|
|
112 |
// north and agricola |
|
|
113 |
g.add_tripstop(0, TripStop::GTFS, 44.6554236f, -63.5936968f); |
|
|
114 |
// north and robie (just north of north&agricola) |
|
|
115 |
g.add_tripstop(1, TripStop::OSM, 44.6546407f, -63.5948438f); |
|
|
116 |
// north and northwood (just south of north&agricola) |
|
|
117 |
g.add_tripstop(2, TripStop::GTFS, 44.6567144f, -63.5919115f); |
|
|
118 |
// Quinpool and Connaught (a few kms away from north&agricola) |
|
|
119 |
g.add_tripstop(3, TripStop::GTFS, 44.6432423f, -63.6045261f); |
|
|
120 |
|
|
|
121 |
{ |
|
|
122 |
vector<TripStop> v = g.find_tripstops_in_range(44.6554236f, |
|
|
123 |
-63.5936968f, |
|
|
124 |
TripStop::GTFS, |
|
|
125 |
500.0f); |
|
|
126 |
WVPASSEQ(v.size(), 2); |
|
|
127 |
WVPASS(v[0].id == 0 || v[0].id == 2); |
|
|
128 |
WVPASS(v[1].id == 0 || v[1].id == 2); |
|
|
129 |
} |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
|
|
|
133 |
static time_t get_time_t(int tm_mday, int tm_mon, int tm_year) |
|
|
134 |
{ |
|
|
135 |
struct tm t; |
|
|
136 |
t.tm_sec = 0; |
|
|
137 |
t.tm_min = 0; |
|
|
138 |
t.tm_hour = 0; |
|
|
139 |
t.tm_mday = tm_mday; |
|
|
140 |
t.tm_mon = tm_mon; |
|
|
141 |
t.tm_year = tm_year; |
|
|
142 |
t.tm_wday = -1; |
|
|
143 |
t.tm_yday = -1; |
|
|
144 |
t.tm_isdst = -1; |
|
|
145 |
|
|
|
146 |
return mktime(&t); |
|
|
147 |
} |
|
|
148 |
|
|
|
149 |
WVTEST_MAIN("service_periods") |
|
|
150 |
{ |
|
|
151 |
TripGraph g; |
|
|
152 |
|
|
|
153 |
// from the 1st to the 7th (i.e. 1st saturday only) |
|
|
154 |
{ |
|
|
155 |
ServicePeriod s(0, 1, 0, 108, 7, 0, 108, 2000, false, true, false); |
|
|
156 |
g.add_service_period(s); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
// test something that's within a supported service period |
|
|
160 |
// Saturday Midnight Jan 5th 2008 |
|
|
161 |
{ |
|
|
162 |
vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 0, 108)); |
|
|
163 |
WVPASSEQ(vsp.size(), 1); |
|
|
164 |
WVPASSEQ(vsp[0].first, 0); |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
// test something outside a supported service period: day |
|
|
168 |
// Saturday Midnight Jan 11th 2008 |
|
|
169 |
{ |
|
|
170 |
vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(11, 0, 108)); |
|
|
171 |
WVPASSEQ(vsp.size(), 0); |
|
|
172 |
} |
|
|
173 |
// test something outside a supported service period: month |
|
|
174 |
// Saturday Midnight Feb 5th 2008 |
|
|
175 |
{ |
|
|
176 |
vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 1, 108)); |
|
|
177 |
WVPASSEQ(vsp.size(), 0); |
|
|
178 |
} |
|
|
179 |
|
|
|
180 |
// test something outside a supported service period: year |
|
|
181 |
// Saturday Midnight Jan 11th 2009 |
|
|
182 |
{ |
|
|
183 |
vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 1, 109)); |
|
|
184 |
WVPASSEQ(vsp.size(), 0); |
|
|
185 |
} |
|
|
186 |
|
|
|
187 |
// add another service period (saturdays for month of january) |
|
|
188 |
{ |
|
|
189 |
ServicePeriod s(1, 1, 0, 108, 31, 0, 108, 2000, false, true, |
|
|
190 |
false); |
|
|
191 |
g.add_service_period(s); |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
// test something that's within _two_ supported service periods |
|
|
195 |
// Saturday Midnight Jan 5th 2008 |
|
|
196 |
{ |
|
|
197 |
vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 0, 108)); |
|
|
198 |
WVPASSEQ(vsp.size(), 2); |
|
|
199 |
WVPASS(vsp[0].first==0 || vsp[0].first==1); |
|
|
200 |
WVPASS(vsp[1].first==0 || vsp[1].first==1); |
|
|
201 |
WVFAILEQ(vsp[0].first, vsp[1].first); |
|
|
202 |
} |
|
|
203 |
|
|
|
204 |
// save graph, reload, make sure service periods are still there |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
|
|
|
208 |
WVTEST_MAIN("service_periods_overlapping") |
|
|
209 |
{ |
|
|
210 |
TripGraph g; |
|
|
211 |
|
|
|
212 |
// from the 1st to the 7th (i.e. 1st saturday only) |
|
|
213 |
// (weekday and saturday schedules) |
|
|
214 |
{ |
|
|
215 |
ServicePeriod s1(0, 1, 0, 108, 7, 0, 108, 90000, false, true, false); |
|
|
216 |
g.add_service_period(s1); |
|
|
217 |
ServicePeriod s2(1, 1, 0, 108, 7, 0, 108, 90000, true, false, false); |
|
|
218 |
g.add_service_period(s2); |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 0, 108)); |
|
|
222 |
WVPASSEQ(vsp.size(), 2); |
|
|
223 |
WVPASS(vsp[0].first==0 || vsp[0].first==1); |
|
|
224 |
WVPASS(vsp[1].first==0 || vsp[1].first==1); |
|
|
225 |
WVFAILEQ(vsp[0].first, vsp[1].first); |
|
|
226 |
|
|
|
227 |
int weekday_index = (vsp[0].first == 1) ? 0 : 1; |
|
|
228 |
WVPASSEQ(vsp[weekday_index].second, 86400); |
|
|
229 |
} |
|
|
230 |
|
|
|
231 |
|
|
|
232 |
WVTEST_MAIN("service_periods_turned_on_or_off") |
|
|
233 |
{ |
|
|
234 |
TripGraph g; |
|
|
235 |
|
|
|
236 |
// from the 1st to the 7th (i.e. 1st saturday only) |
|
|
237 |
// turn off weekday service on the 2nd (wednesday) |
|
|