Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / maps / src / main / rust / src / transit_part12.rs

transit_part12.rs215 lines · 8.0 KiBRaw
1 // --- Corridor-bundled rail lines + trip itineraries (`transit_part11`) ---
2
3 #[test]
4 fn rail_lines_serve_a_shaped_route_with_its_colour() {
5 let idx = shaped_route_pack().index();
6 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
7 assert_eq!(lines.len(), 1, "the one shaped route serves the bbox");
8 let l = &lines[0];
9 assert_eq!(l.name, "N");
10 assert_eq!(l.color, 0x0000FF);
11 assert_eq!(l.feed, "sfmuni");
12 // The fitted shape detours east: 5 vertices = 10 doubles, and the
13 // second point must swing off the stops' shared meridian.
14 assert_eq!(l.coords.len(), 10);
15 assert!(l.coords[2] > -122.400, "on the eastward detour");
16 }
17
18 #[test]
19 fn rail_lines_skip_a_bbox_no_stop_serves() {
20 let idx = shaped_route_pack().index();
21 let lines = rail_lines(&idx, 40.0, -75.0, 41.0, -74.0);
22 assert!(lines.is_empty(), "far from the SF stops: nothing to draw");
23 }
24
25 #[test]
26 fn trip_itinerary_lists_every_stop_with_times() {
27 let idx = one_route_pack().index();
28 // Vehicle id for route 0, trip 0, today: the packing active_vehicles uses.
29 let id = (0i64 << 32) | (0i64 << 1) | 0;
30 let itin =
31 trip_itinerary(&idx, id, sched(wednesday())).expect("trip 0 runs Wednesday");
32 assert_eq!(itin.route_name, "N");
33 assert_eq!(itin.headsign, "Downtown");
34 assert!(!itin.cancelled);
35 assert_eq!(itin.stops.len(), 3);
36 assert_eq!(itin.stops[0].name, "Alpha");
37 assert_eq!(itin.stops[2].name, "Gamma");
38 assert_eq!(
39 (itin.stops[0].dep_secs, itin.stops[2].arr_secs),
40 (28_800, 29_400)
41 );
42 // Dwell at Beta: arrives 29_100, departs 29_160.
43 assert_eq!(
44 (itin.stops[1].arr_secs, itin.stops[1].dep_secs),
45 (29_100, 29_160)
46 );
47 }
48
49 #[test]
50 fn trip_itinerary_refuses_an_unknown_trip() {
51 let idx = one_route_pack().index();
52 // Route 9 does not exist; trip 99 neither.
53 assert!(
54 trip_itinerary(&idx, (9i64 << 32) | (0i64 << 1), sched(wednesday())).is_none()
55 );
56 assert!(
57 trip_itinerary(&idx, (0i64 << 32) | (99i64 << 1), sched(wednesday())).is_none()
58 );
59 }
60
61 /// Two routes over one track, in different agency colours, fan into
62 /// adjacent lanes rather than stacking.
63 fn two_colour_pack() -> Pack {
64 let mut pack = one_route_pack();
65 pack.routes.push(Route {
66 name: "M",
67 color: 0xFF0000,
68 route_type: 0,
69 feed: 0,
70 pattern: vec![0, 1, 2],
71 trips: vec![Trip {
72 start: 28_800,
73 stoptimes: vec![(28_800, 28_800), (29_100, 29_160), (29_400, 29_400)],
74 service: 0,
75 headsign: "Downtown",
76 }],
77 // Same alignment, surveyed a metre east: one corridor, not two.
78 shape: Some(Shape {
79 points: vec![
80 (37.700, -122.39999),
81 (37.705, -122.38999),
82 (37.710, -122.39999),
83 (37.715, -122.38999),
84 (37.720, -122.39999),
85 ],
86 stop_vertices: vec![0, 2, 4],
87 }),
88 });
89 pack.routes[0].shape = Some(Shape {
90 points: vec![
91 (37.700, -122.400),
92 (37.705, -122.390),
93 (37.710, -122.400),
94 (37.715, -122.390),
95 (37.720, -122.400),
96 ],
97 stop_vertices: vec![0, 2, 4],
98 });
99 pack
100 }
101
102 #[test]
103 fn shared_track_fans_into_adjacent_lanes() {
104 let idx = two_colour_pack().index();
105 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
106 assert!(!lines.is_empty(), "both routes serve the bbox");
107 // Every span of the two colours shares one corridor: same lane
108 // count, complementary ordinals.
109 let mut by_color: std::collections::HashMap<u32, Vec<(u8, u8)>> =
110 std::collections::HashMap::new();
111 for l in &lines {
112 by_color.entry(l.color).or_default().push((l.ordinal, l.lanes));
113 }
114 assert_eq!(by_color.len(), 2, "both agency colours draw: {by_color:?}");
115 for (color, spans) in &by_color {
116 assert!(
117 spans.iter().any(|&(_, lanes)| lanes == 2),
118 "colour {color:06X} fans as one of two: {spans:?}"
119 );
120 }
121 let ordinals: std::collections::HashSet<u8> =
122 lines.iter().filter(|l| l.lanes == 2).map(|l| l.ordinal).collect();
123 assert_eq!(ordinals, std::collections::HashSet::from([0, 1]), "{ordinals:?}");
124 }
125
126 #[test]
127 fn a_byte_identical_republication_draws_once() {
128 let shape_points = vec![
129 (37.700, -122.400),
130 (37.705, -122.390),
131 (37.710, -122.400),
132 (37.715, -122.390),
133 (37.720, -122.400),
134 ];
135 let mut pack = one_route_pack();
136 pack.routes[0].shape = Some(Shape {
137 points: shape_points.clone(),
138 stop_vertices: vec![0, 2, 4],
139 });
140 // Same geometry, same colour, new name: a republished feed.
141 pack.routes.push(Route {
142 name: "N-dup",
143 color: 0x0000FF,
144 route_type: 0,
145 feed: 0,
146 pattern: vec![0, 1, 2],
147 trips: vec![Trip {
148 start: 28_800,
149 stoptimes: vec![(28_800, 28_800), (29_100, 29_160), (29_400, 29_400)],
150 service: 0,
151 headsign: "Downtown",
152 }],
153 shape: Some(Shape { points: shape_points, stop_vertices: vec![0, 2, 4] }),
154 });
155 let idx = pack.index();
156 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
157 let blue: Vec<_> = lines.iter().filter(|l| l.color == 0x0000FF).collect();
158 assert_eq!(blue.len(), 1, "one blue line, not two: {lines:?}");
159 }
160
161 #[test]
162 fn stop_lines_name_only_serving_routes_and_keep_buses() {
163 // The shaped tram plus a crosstown line through the same stops and a
164 // bus serving the middle stop: tapping that stop draws all three
165 // (buses included), tapping far away draws nothing.
166 let mut pack = shaped_route_pack();
167 pack.routes.push(Route {
168 name: "Crosstown",
169 color: 0x00FF00,
170 route_type: 1,
171 feed: 0,
172 pattern: vec![0, 1, 2],
173 trips: vec![Trip {
174 start: 28_800,
175 stoptimes: vec![(28_800, 28_800), (29_100, 29_160), (29_400, 29_400)],
176 service: 0,
177 headsign: "East",
178 }],
179 shape: None,
180 });
181 pack.routes.push(Route {
182 name: "38",
183 color: 0xFF8800,
184 route_type: 3,
185 feed: 0,
186 pattern: vec![1, 2],
187 trips: vec![Trip {
188 start: 28_800,
189 stoptimes: vec![(28_800, 28_800), (29_400, 29_400)],
190 service: 0,
191 headsign: "Downtown",
192 }],
193 shape: None,
194 });
195 let idx = pack.index();
196 // Beta (37.710) sits on all three patterns (shared platforms). Assert
197 // on the route-name set, not the span count: corridor fanning may cut
198 // one route into several spans.
199 let lines = routes_for_stop(&idx, 37.710, -122.400);
200 let names: std::collections::HashSet<&str> =
201 lines.iter().map(|l| l.name.as_str()).collect();
202 assert_eq!(
203 names,
204 std::collections::HashSet::from(["N", "Crosstown", "38"]),
205 "every route serving Beta draws: {lines:?}"
206 );
207 assert!(
208 lines.iter().any(|l| l.name == "38"),
209 "the selected-stop case keeps buses: {lines:?}"
210 );
211 // Far from every stop: nothing to draw.
212 let far = routes_for_stop(&idx, 40.0, -75.0);
213 assert!(far.is_empty(), "no stop near: nothing to draw");
214 }
215