Merge branch 'master' of ssh://apples.lambdacomplex.org/git/bus
[bus.git] / origin-src / tripgraph.t.cc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "wvtest.h"
#include "tripgraph.h"

using namespace std;


WVTEST_MAIN("basic_graph_pathfinding")
{
    TripGraph g;
    
    // simple path, just walking
    g.add_tripstop(0, TripStop::OSM, 0.0f, 0.0f);
    g.add_tripstop(1, TripStop::OSM, 1.0f, 0.0f);
    g.add_walkhop(0, 1);

    {
        TripPath *p = g.find_path(0, false, 0.0, 0.0, 1.0, 0.0);
        
        std::deque<TripAction> actions = p->get_actions();
        WVPASSEQ(actions.size(), 1);
        
        TripAction action = actions.front();
        WVPASSEQ(action.src_id, 0);
        WVPASSEQ(action.dest_id, 1);

        delete p;
    }

    // take the triphop if we have it
    {
        ServicePeriod s(0, 0, 0, 0, 7, 0, 100, 2000, true, true, true);
        g.add_service_period(s);
        g.add_triphop(500, 1000, 0, 1, 1, 1, 0);
    }

    {
        TripPath *p = g.find_path(0, false, 0.0, 0.0, 1.0, 0.0);
        
        std::deque<TripAction> actions = p->get_actions();
        WVPASSEQ(actions.size(), 1);
        
        TripAction action = actions.front();
        WVPASSEQ(action.src_id, 0);
        WVPASSEQ(action.dest_id, 1);
        WVPASSEQ(action.start_time, 500.0f);
        WVPASSEQ(action.end_time, 1000.0f);

        delete p;
    }
}


WVTEST_MAIN("basic_graph_saveload")
{
    TripGraph g;
    g.add_tripstop(0, TripStop::OSM, 0.0f, 0.0f);
    g.add_tripstop(1, TripStop::OSM, 1.0f, 0.0f);
    g.add_walkhop(0, 1);

    ServicePeriod s(0, 1, 0, 0, 7, 0, 100, 2000, true, true, true);
    g.add_service_period(s);
    g.add_triphop(500, 1000, 0, 1, 1, 1, 0);

    char *tmpgraphname = tmpnam(NULL); // security issues in unit tests? bah.
    unlink(tmpgraphname);
    g.save(tmpgraphname);

    TripGraph g2;
    g2.load(tmpgraphname);

    // verify that we have two tripstops
    for (int i=0; i<2; i++) 
    {
        TripStop ts = g2.get_tripstop(i);
        WVPASSEQ(ts.type, TripStop::OSM);
    }

    // verify that we can still solve a basic path
    {
        TripPath *p = g.find_path(0, false, 0.0, 0.0, 1.0, 0.0);
        
        std::deque<TripAction> actions = p->get_actions();
        WVPASSEQ(actions.size(), 1);

        TripAction action = actions.front();
        WVPASSEQ(action.src_id, 0);
        WVPASSEQ(action.dest_id, 1);
        WVPASSEQ(action.start_time, 500.0f);
        WVPASSEQ(action.end_time, 1000.0f);
        
        delete p;
    }    
}


WVTEST_MAIN("impossible_path")
{
    TripGraph g;
    g.add_tripstop(0, TripStop::OSM, 0.0f, 0.0f);
    g.add_tripstop(1, TripStop::OSM, 1.0f, 0.0f);
    g.add_tripstop(2, TripStop::OSM, 0.0f, 1.0f);
    g.add_walkhop(0, 1);

    TripPath *p = g.find_path(0, false, 0.0, 0.0, 0.0, 1.0);
    WVPASS(!p);
}


WVTEST_MAIN("tripstops_in_range")
{
    TripGraph g;
    // north and agricola
    g.add_tripstop(0, TripStop::GTFS, 44.6554236f, -63.5936968f);
    // north and robie (just north of north&agricola)
    g.add_tripstop(1, TripStop::OSM, 44.6546407f, -63.5948438f);
    // north and northwood (just south of north&agricola)
    g.add_tripstop(2, TripStop::GTFS, 44.6567144f, -63.5919115f);
    // Quinpool and Connaught (a few kms away from north&agricola)
    g.add_tripstop(3, TripStop::GTFS, 44.6432423f, -63.6045261f);

    {
        vector<TripStop> v = g.find_tripstops_in_range(44.6554236f, 
                                                       -63.5936968f, 
                                                       TripStop::GTFS, 
                                                       500.0f);      
        WVPASSEQ(v.size(), 2);
        WVPASS(v[0].id == 0 || v[0].id == 2);
        WVPASS(v[1].id == 0 || v[1].id == 2);
    }
}


static time_t get_time_t(int tm_mday, int tm_mon, int tm_year)
{
    struct tm t;
    t.tm_sec = 0;
    t.tm_min = 0;
    t.tm_hour = 0;
    t.tm_mday = tm_mday;
    t.tm_mon = tm_mon;
    t.tm_year = tm_year;
    t.tm_wday = -1;
    t.tm_yday = -1;
    t.tm_isdst = -1;

    return mktime(&t);
}

WVTEST_MAIN("service_periods")
{
    TripGraph g;

    // from the 1st to the 7th (i.e. 1st saturday only)
    {
        ServicePeriod s(0, 1, 0, 108, 7, 0, 108, 2000, false, true, false);
        g.add_service_period(s);
    }

    // test something that's within a supported service period
    // Saturday Midnight Jan 5th 2008
    {
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 0, 108));
        WVPASSEQ(vsp.size(), 1);
        WVPASSEQ(vsp[0].first, 0);
    }

    // test something outside a supported service period: day
    // Saturday Midnight Jan 11th 2008
    {
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(11, 0, 108));
        WVPASSEQ(vsp.size(), 0);
    }
    // test something outside a supported service period: month
    // Saturday Midnight Feb 5th 2008
    {
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 1, 108));
        WVPASSEQ(vsp.size(), 0);
    }

    // test something outside a supported service period: year
    // Saturday Midnight Jan 11th 2009
    {
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 1, 109));
        WVPASSEQ(vsp.size(), 0);
    }

    // add another service period (saturdays for month of january)
    {
        ServicePeriod s(1, 1, 0, 108, 31, 0, 108, 2000, false, true,
                        false);
        g.add_service_period(s);
    }

    // test something that's within _two_ supported service periods
    // Saturday Midnight Jan 5th 2008
    {
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 0, 108));
        WVPASSEQ(vsp.size(), 2);
        WVPASS(vsp[0].first==0 || vsp[0].first==1);
        WVPASS(vsp[1].first==0 || vsp[1].first==1);
        WVFAILEQ(vsp[0].first, vsp[1].first);
    }    

    // save graph, reload, make sure service periods are still there
}


WVTEST_MAIN("service_periods_overlapping")
{
    TripGraph g;

    // from the 1st to the 7th (i.e. 1st saturday only)
    // (weekday and saturday schedules)
    {
        ServicePeriod s1(0, 1, 0, 108, 7, 0, 108, 90000, false, true, false);
        g.add_service_period(s1);
        ServicePeriod s2(1, 1, 0, 108, 7, 0, 108, 90000, true, false, false);
        g.add_service_period(s2);
    }

    vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(5, 0, 108));
    WVPASSEQ(vsp.size(), 2);
    WVPASS(vsp[0].first==0 || vsp[0].first==1);
    WVPASS(vsp[1].first==0 || vsp[1].first==1);
    WVFAILEQ(vsp[0].first, vsp[1].first);
    
    int weekday_index = (vsp[0].first == 1) ? 0 : 1;
    WVPASSEQ(vsp[weekday_index].second, 86400);
}


WVTEST_MAIN("service_periods_turned_on_or_off")
{
    TripGraph g;

    // from the 1st to the 7th (i.e. 1st saturday only)
    // turn off weekday service on the 2nd (wednesday)
    // turn on saturday service on the 3rd (keeping weekday service)
    {
        ServicePeriod s1(0, 1, 0, 108, 7, 0, 108, 80000, false, true, false);
        s1.add_exception_on(3, 0, 108);
        WVPASSEQ(s1.is_turned_on(3, 0, 108), true);
        WVPASSEQ(s1.is_turned_on(4, 0, 108), false);
        g.add_service_period(s1);
        ServicePeriod s2(1, 1, 0, 108, 7, 0, 108, 80000, true, false, false);
        s2.add_exception_off(2, 0, 108);
        WVPASSEQ(s2.is_turned_off(2, 0, 108), true);
        WVPASSEQ(s2.is_turned_off(3, 0, 108), false);
        g.add_service_period(s2);
    }

    {    
        // should be no service on the 2nd
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(2, 0, 108));
        WVPASSEQ(vsp.size(), 0);
    }

    {
        // should be two service periods on the 3rd (saturday and weekday)
        vector<pair<int, int> > vsp = g.get_service_period_ids_for_time(get_time_t(3, 0, 108));
        WVPASSEQ(vsp.size(), 2);
        WVPASS(vsp[0].first==0 || vsp[0].first==1);
        WVPASS(vsp[1].first==0 || vsp[1].first==1);
        WVFAILEQ(vsp[0].first, vsp[1].first);
    }    
}


WVTEST_MAIN("service_periods_save_load")
{
    TripGraph g;

    // use the same setup as the previous test: saturday and weekday schedules 
    // with a few exceptions

    // from the 1st to the 7th (i.e. 1st saturday only)
    // turn off weekday service on the 2nd (wednesday)
    // turn on saturday service on the 3rd (keeping weekday service)
    {
        ServicePeriod s1(0, 1, 0, 108, 7, 0, 108, 80000, false, true, false);
        s1.add_exception_on(3, 0, 108);
        g.add_service_period(s1);
        ServicePeriod s2(1, 1, 0, 108, 7, 0, 108, 80000, true, false, false);
        s2.add_exception_off(2, 0, 108);
        g.add_service_period(s2);
    }

    char *tmpgraphname = tmpnam(NULL); // security issues in unit tests? bah.
    unlink(tmpgraphname);
    g.save(tmpgraphname);

    TripGraph g2;
    g2.load(tmpgraphname);

    {    
        // should be no service on the 2nd
        vector<pair<int, int> > vsp = g2.get_service_period_ids_for_time(get_time_t(2, 0, 108));
        WVPASSEQ(vsp.size(), 0);
    }

    {
        // should be two service periods on the 3rd (saturday and weekday)
        vector<pair<int, int> > vsp = g2.get_service_period_ids_for_time(get_time_t(3, 0, 108));
        WVPASSEQ(vsp.size(), 2);
        WVPASS(vsp[0].first==0 || vsp[0].first==1);
        WVPASS(vsp[1].first==0 || vsp[1].first==1);
        WVFAILEQ(vsp[0].first, vsp[1].first);
    }    
}