elevator-core 18.0.0

Engine-agnostic elevator simulation library with pluggable dispatch strategies
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Boundary-mutation coverage for the reposition strategies and the
//! sweep helper that backs SCAN/LOOK dispatch (#660).
//!
//! Targets the boundary operators (`>`, `>=`, `<`, `<=`) and the
//! EPSILON-band tolerance checks that the existing strategy tests
//! traverse but don't pin down. Each test name encodes the boundary
//! condition it isolates so a surviving mutation says exactly which
//! comparison flipped.
//!
//! Hotspots from `mutants.out` covered here:
//! - `dispatch/reposition.rs:36-228` — `RepositionCooldowns`,
//!   `SpreadEvenly`, `ReturnToLobby`, into `DemandWeighted`.
//! - `dispatch/look.rs:91-92` — degenerate request shapes that exercise
//!   `pair_is_useful` + `sweep::rank`.

use crate::components::{Accel, Elevator, ElevatorPhase, Position, Speed, Stop, Velocity, Weight};
use crate::dispatch::reposition::{
    DEFAULT_REPOSITION_COOLDOWN_TICKS, RepositionCooldowns, ReturnToLobby, SpreadEvenly,
};
use crate::dispatch::sweep::{self, EPSILON, SweepDirection, SweepMode};
use crate::dispatch::{
    DispatchManifest, DispatchStrategy, ElevatorGroup, LineInfo, RankContext, RepositionStrategy,
    look::LookDispatch,
};
use crate::door::DoorState;
use crate::entity::EntityId;
use crate::ids::GroupId;
use crate::world::World;
use std::collections::HashSet;

// ===== Helpers =====

fn world_with_stops(positions: &[f64]) -> (World, Vec<EntityId>) {
    let mut world = World::new();
    let stops = positions
        .iter()
        .enumerate()
        .map(|(i, &p)| {
            let eid = world.spawn();
            world.set_stop(
                eid,
                Stop {
                    name: format!("Stop {i}"),
                    position: p,
                },
            );
            eid
        })
        .collect();
    (world, stops)
}

fn idle_elevator_at(world: &mut World, position: f64) -> EntityId {
    let eid = world.spawn();
    world.set_position(eid, Position { value: position });
    world.set_velocity(eid, Velocity { value: 0.0 });
    world.set_elevator(
        eid,
        Elevator {
            phase: ElevatorPhase::Idle,
            door: DoorState::Closed,
            max_speed: Speed::from(2.0),
            acceleration: Accel::from(1.5),
            deceleration: Accel::from(2.0),
            weight_capacity: Weight::from(800.0),
            current_load: Weight::from(0.0),
            riders: vec![],
            target_stop: None,
            door_transition_ticks: 5,
            door_open_ticks: 10,
            line: EntityId::default(),
            repositioning: false,
            restricted_stops: HashSet::new(),
            inspection_speed_factor: 0.25,
            going_up: true,
            going_down: true,
            move_count: 0,
            door_command_queue: Vec::new(),
            manual_target_velocity: None,
            bypass_load_up_pct: None,
            bypass_load_down_pct: None,
            home_stop: None,
        },
    );
    eid
}

fn group(stops: &[EntityId], elevators: Vec<EntityId>) -> ElevatorGroup {
    ElevatorGroup::new(
        GroupId(0),
        "test".into(),
        vec![LineInfo::new(
            EntityId::default(),
            elevators,
            stops.to_vec(),
        )],
    )
}

// ===== RepositionCooldowns boundary =====
//
// `is_cooling_down` uses `tick < eligible`. The boundary mutation that
// flips `<` to `<=` would let a tick equal to `eligible` count as still
// cooling down — these three cases pin every comparator.

#[test]
fn cooldown_one_tick_before_eligible_is_cooling_down() {
    let mut cd = RepositionCooldowns::default();
    let car = EntityId::default();
    cd.record_arrival(car, 0);
    let eligible = DEFAULT_REPOSITION_COOLDOWN_TICKS;
    assert!(
        cd.is_cooling_down(car, eligible - 1),
        "tick = eligible - 1 must still be cooling down (`<` boundary)"
    );
}

#[test]
fn cooldown_at_exact_eligible_tick_is_no_longer_cooling_down() {
    let mut cd = RepositionCooldowns::default();
    let car = EntityId::default();
    cd.record_arrival(car, 0);
    let eligible = DEFAULT_REPOSITION_COOLDOWN_TICKS;
    assert!(
        !cd.is_cooling_down(car, eligible),
        "tick == eligible must NOT be cooling down (strict `<`, not `<=`)"
    );
}

#[test]
fn cooldown_one_tick_after_eligible_is_no_longer_cooling_down() {
    let mut cd = RepositionCooldowns::default();
    let car = EntityId::default();
    cd.record_arrival(car, 0);
    let eligible = DEFAULT_REPOSITION_COOLDOWN_TICKS;
    assert!(
        !cd.is_cooling_down(car, eligible + 1),
        "tick > eligible must not be cooling down"
    );
}

#[test]
fn cooldown_no_entry_is_never_cooling_down() {
    let cd = RepositionCooldowns::default();
    let car = EntityId::default();
    assert!(
        !cd.is_cooling_down(car, 0),
        "fresh state has no cooldown for any car"
    );
    assert!(
        !cd.is_cooling_down(car, u64::MAX),
        "fresh state stays cooldown-free at any tick"
    );
}

// ===== SpreadEvenly EPSILON-band: `(stop - elev).abs() > 1e-6` =====
//
// Lines 145, 206 of reposition.rs filter out moves where the elevator
// is already "essentially at" the stop. The mutation that flips `>` to
// `>=` (or to `<`) would either suppress legitimate moves or emit
// no-op moves to the car's current position. These tests pin both
// sides of the 1e-6 boundary.

#[test]
fn spread_evenly_omits_no_op_when_already_at_target_stop() {
    // Two idle cars collocated at stop[1] force `SpreadEvenly` to emit
    // exactly one move (the second car must go to stop[0] to spread).
    // The first car stays put: pinning the no-op suppression at the
    // exact `1e-6` boundary requires that the emitted moves never
    // include a target equal to the car's current stop, AND that
    // exactly one move comes out — otherwise a mutation suppressing
    // all moves would survive vacuously.
    let (mut world, stops) = world_with_stops(&[0.0, 10.0]);
    let elev_a = idle_elevator_at(&mut world, 10.0);
    let elev_b = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev_a, elev_b]);
    let stop_pos = vec![(stops[0], 0.0), (stops[1], 10.0)];
    let idle = vec![(elev_a, 10.0), (elev_b, 10.0)];

    let mut out = Vec::new();
    SpreadEvenly.reposition(&idle, &stop_pos, &g, &world, &mut out);
    assert_eq!(
        out.len(),
        1,
        "two cars at stop[1] must spread one to stop[0]; got {out:?}"
    );
    let (_, target) = out[0];
    assert_eq!(
        target, stops[0],
        "the emitted move must target the empty stop[0], not a no-op back to stop[1]"
    );
}

#[test]
fn spread_evenly_within_epsilon_of_stop_omits_move() {
    // Distance just below the 1e-6 threshold — the car is "already
    // there" by the strategy's own definition.
    let (mut world, stops) = world_with_stops(&[0.0, 10.0]);
    // Single stop on the line so spread has only one candidate; the
    // sub-epsilon offset must still register as "already at" stop[0].
    let elev = idle_elevator_at(&mut world, 5e-7);
    let g = group(&stops[..1], vec![elev]);
    let stop_pos = vec![(stops[0], 0.0)];
    let idle = vec![(elev, 5e-7)];

    let mut out = Vec::new();
    SpreadEvenly.reposition(&idle, &stop_pos, &g, &world, &mut out);
    assert!(
        out.is_empty(),
        "elevator within reposition threshold (5e-7 < 1e-6) of stop must not emit a move; got {out:?}"
    );
}

#[test]
fn spread_evenly_just_past_epsilon_emits_move() {
    // Distance just past the 1e-6 threshold — the car must move.
    let (mut world, stops) = world_with_stops(&[0.0, 10.0]);
    let elev = idle_elevator_at(&mut world, 2e-6);
    let g = group(&stops[..1], vec![elev]);
    let stop_pos = vec![(stops[0], 0.0)];
    let idle = vec![(elev, 2e-6)];

    let mut out = Vec::new();
    SpreadEvenly.reposition(&idle, &stop_pos, &g, &world, &mut out);
    assert_eq!(
        out.len(),
        1,
        "elevator 2e-6 from stop (> 1e-6 boundary) must emit one move; got {out:?}"
    );
    assert_eq!(out[0], (elev, stops[0]));
}

// ===== ReturnToLobby epsilon: `(pos - home_pos).abs() > 1e-6` =====

#[test]
fn return_to_lobby_within_epsilon_of_home_omits_move() {
    let (mut world, stops) = world_with_stops(&[0.0, 10.0]);
    let elev = idle_elevator_at(&mut world, 5e-7);
    let g = group(&stops, vec![elev]);
    let stop_pos = vec![(stops[0], 0.0), (stops[1], 10.0)];
    let idle = vec![(elev, 5e-7)];

    let mut out = Vec::new();
    ReturnToLobby::new().reposition(&idle, &stop_pos, &g, &world, &mut out);
    assert!(
        out.is_empty(),
        "car within 1e-6 of home stop must not be repositioned; got {out:?}"
    );
}

#[test]
fn return_to_lobby_just_past_epsilon_emits_move() {
    let (mut world, stops) = world_with_stops(&[0.0, 10.0]);
    let elev = idle_elevator_at(&mut world, 2e-6);
    let g = group(&stops, vec![elev]);
    let stop_pos = vec![(stops[0], 0.0), (stops[1], 10.0)];
    let idle = vec![(elev, 2e-6)];

    let mut out = Vec::new();
    ReturnToLobby::new().reposition(&idle, &stop_pos, &g, &world, &mut out);
    assert_eq!(out, vec![(elev, stops[0])]);
}

// ===== Empty / single-stop input shapes (acceptance criteria) =====

#[test]
fn spread_evenly_empty_idle_returns_empty() {
    let (world, stops) = world_with_stops(&[0.0, 10.0]);
    let g = group(&stops, vec![]);
    let stop_pos = vec![(stops[0], 0.0), (stops[1], 10.0)];

    let mut out = Vec::new();
    SpreadEvenly.reposition(&[], &stop_pos, &g, &world, &mut out);
    assert!(out.is_empty());
}

#[test]
fn spread_evenly_empty_stops_returns_empty() {
    let (mut world, _stops) = world_with_stops(&[]);
    let elev = idle_elevator_at(&mut world, 0.0);
    let g = group(&[], vec![elev]);
    let idle = vec![(elev, 0.0)];

    let mut out = Vec::new();
    SpreadEvenly.reposition(&idle, &[], &g, &world, &mut out);
    assert!(out.is_empty(), "no stops means no targets");
}

// ===== sweep::rank boundary =====
//
// Pins every arm of the (mode, direction) match against the EPSILON
// band so a flipped `>` / `<` or a perturbed `EPSILON` is caught.
//
// Recall:
//   Strict  + Up   ⇒ stop > car + EPSILON
//   Strict  + Down ⇒ stop < car - EPSILON
//   Lenient + Up   ⇒ stop > car - EPSILON
//   Lenient + Down ⇒ stop < car + EPSILON

#[test]
fn sweep_rank_strict_up_rejects_at_or_within_epsilon() {
    let car = 10.0;
    assert!(
        sweep::rank(SweepMode::Strict, SweepDirection::Up, car, car).is_none(),
        "Strict Up rejects stop == car"
    );
    assert!(
        sweep::rank(
            SweepMode::Strict,
            SweepDirection::Up,
            car,
            car + EPSILON / 2.0
        )
        .is_none(),
        "Strict Up rejects stop within EPSILON of car"
    );
}

#[test]
fn sweep_rank_strict_up_accepts_just_past_epsilon() {
    let car = 10.0;
    let stop = EPSILON.mul_add(2.0, car);
    let cost = sweep::rank(SweepMode::Strict, SweepDirection::Up, car, stop)
        .expect("Strict Up accepts stop > car + EPSILON");
    assert!((cost - (stop - car)).abs() < EPSILON);
}

#[test]
fn sweep_rank_strict_down_rejects_at_or_within_epsilon() {
    let car = 10.0;
    assert!(sweep::rank(SweepMode::Strict, SweepDirection::Down, car, car).is_none());
    assert!(
        sweep::rank(
            SweepMode::Strict,
            SweepDirection::Down,
            car,
            car - EPSILON / 2.0
        )
        .is_none()
    );
}

#[test]
fn sweep_rank_strict_down_accepts_just_past_epsilon() {
    let car = 10.0;
    let stop = EPSILON.mul_add(-2.0, car);
    let cost = sweep::rank(SweepMode::Strict, SweepDirection::Down, car, stop)
        .expect("Strict Down accepts stop < car - EPSILON");
    assert!((cost - (car - stop)).abs() < EPSILON);
}

#[test]
fn sweep_rank_lenient_up_accepts_just_below_car() {
    // Lenient Up accepts the half-sweep including just-behind-car.
    let car = 10.0;
    let stop = car - EPSILON / 2.0;
    assert!(
        sweep::rank(SweepMode::Lenient, SweepDirection::Up, car, stop).is_some(),
        "Lenient Up accepts stop > car - EPSILON (i.e. inclusive of car position)"
    );
}

#[test]
fn sweep_rank_lenient_up_rejects_well_below_car() {
    let car = 10.0;
    let stop = car - 1.0; // far below — outside the lenient half-sweep
    assert!(sweep::rank(SweepMode::Lenient, SweepDirection::Up, car, stop).is_none());
}

#[test]
fn sweep_rank_lenient_down_accepts_just_above_car() {
    let car = 10.0;
    let stop = car + EPSILON / 2.0;
    assert!(sweep::rank(SweepMode::Lenient, SweepDirection::Down, car, stop).is_some());
}

#[test]
fn sweep_rank_lenient_down_rejects_well_above_car() {
    let car = 10.0;
    let stop = car + 1.0; // far above — outside the lenient half-sweep
    assert!(sweep::rank(SweepMode::Lenient, SweepDirection::Down, car, stop).is_none());
}

#[test]
fn sweep_rank_cost_is_absolute_distance_regardless_of_direction() {
    // Cost is `(car - stop).abs()` in all four arms — pins the abs vs
    // signed difference mutation.
    let car = 10.0;
    let stop_above = 15.0;
    let stop_below = 5.0;

    let up_cost =
        sweep::rank(SweepMode::Strict, SweepDirection::Up, car, stop_above).expect("up accepts");
    let down_cost = sweep::rank(SweepMode::Strict, SweepDirection::Down, car, stop_below)
        .expect("down accepts");
    assert!((up_cost - 5.0).abs() < EPSILON);
    assert!((down_cost - 5.0).abs() < EPSILON);
}

// ===== strict_demand_ahead boundary =====

fn manifest_with_demand(world: &mut World, stops: &[EntityId]) -> DispatchManifest {
    use crate::components::Weight;
    use crate::dispatch::RiderInfo;
    let mut m = DispatchManifest::default();
    for &s in stops {
        let dummy = world.spawn();
        m.waiting_at_stop.entry(s).or_default().push(RiderInfo {
            id: dummy,
            destination: None,
            weight: Weight::from(70.0),
            wait_ticks: 0,
        });
    }
    m
}

#[test]
fn strict_demand_ahead_up_stop_at_car_position_is_not_ahead() {
    // Demand exactly at the car position must not count as "strictly
    // ahead" in either direction — pins the EPSILON-band mutation on
    // line 59/60 of sweep.rs.
    let (mut world, stops) = world_with_stops(&[10.0]);
    let elev = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev]);
    let m = manifest_with_demand(&mut world, &stops);

    assert!(!sweep::strict_demand_ahead(
        SweepDirection::Up,
        10.0,
        &g,
        &m,
        &world
    ));
    assert!(!sweep::strict_demand_ahead(
        SweepDirection::Down,
        10.0,
        &g,
        &m,
        &world
    ));
}

#[test]
fn strict_demand_ahead_up_rejects_demand_below_car() {
    let (mut world, stops) = world_with_stops(&[5.0]);
    let elev = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev]);
    let m = manifest_with_demand(&mut world, &stops);

    assert!(!sweep::strict_demand_ahead(
        SweepDirection::Up,
        10.0,
        &g,
        &m,
        &world
    ));
    assert!(sweep::strict_demand_ahead(
        SweepDirection::Down,
        10.0,
        &g,
        &m,
        &world
    ));
}

#[test]
fn strict_demand_ahead_up_accepts_demand_strictly_above_car() {
    let (mut world, stops) = world_with_stops(&[15.0]);
    let elev = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev]);
    let m = manifest_with_demand(&mut world, &stops);

    assert!(sweep::strict_demand_ahead(
        SweepDirection::Up,
        10.0,
        &g,
        &m,
        &world
    ));
    assert!(!sweep::strict_demand_ahead(
        SweepDirection::Down,
        10.0,
        &g,
        &m,
        &world
    ));
}

// ===== LookDispatch degenerate request shapes =====

#[allow(
    clippy::too_many_arguments,
    reason = "test helper threading rank context"
)]
fn rank_via_look(
    look: &LookDispatch,
    car: EntityId,
    car_pos: f64,
    stop: EntityId,
    stop_pos: f64,
    g: &ElevatorGroup,
    m: &DispatchManifest,
    world: &World,
) -> Option<f64> {
    let ctx = RankContext {
        car,
        car_position: car_pos,
        stop,
        stop_position: stop_pos,
        group: g,
        manifest: m,
        world,
    };
    look.rank(&ctx)
}

#[test]
fn look_all_demand_strictly_above_car_keeps_up_sweep() {
    let (mut world, stops) = world_with_stops(&[15.0, 20.0, 25.0]);
    let elev = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev]);
    let m = manifest_with_demand(&mut world, &stops);

    let mut look = LookDispatch::new();
    look.prepare_car(elev, 10.0, &g, &m, &world);
    // Sweep stays Up: a stop above the car must rank, the (synthetic)
    // self-stop at the car's position must not.
    let above = rank_via_look(&look, elev, 10.0, stops[0], 15.0, &g, &m, &world);
    assert!(above.is_some(), "Up sweep accepts stop above car");
}

#[test]
fn look_all_demand_strictly_below_car_reverses_to_down() {
    let (mut world, stops) = world_with_stops(&[0.0, 5.0]);
    let elev = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev]);
    let m = manifest_with_demand(&mut world, &stops);

    let mut look = LookDispatch::new();
    // Default direction is Up but no demand is up → prepare_car
    // reverses to Down + Lenient.
    look.prepare_car(elev, 10.0, &g, &m, &world);
    let below = rank_via_look(&look, elev, 10.0, stops[0], 0.0, &g, &m, &world);
    assert!(
        below.is_some(),
        "after reversal, a stop below the car must rank (Down sweep accepts it)"
    );
}

#[test]
fn look_repeated_stops_at_same_position_all_rank_equally() {
    // Degenerate "all stops collocated" — each must produce the same
    // cost (boundary mutation that swapped which stop is preferred
    // would split the cost).
    let (mut world, stops) = world_with_stops(&[20.0, 20.0, 20.0]);
    let elev = idle_elevator_at(&mut world, 10.0);
    let g = group(&stops, vec![elev]);
    let m = manifest_with_demand(&mut world, &stops);

    let mut look = LookDispatch::new();
    look.prepare_car(elev, 10.0, &g, &m, &world);

    let costs: Vec<_> = stops
        .iter()
        .map(|&s| rank_via_look(&look, elev, 10.0, s, 20.0, &g, &m, &world))
        .collect();
    assert!(costs.iter().all(Option::is_some));
    let first = costs[0].unwrap();
    for c in &costs[1..] {
        assert!((c.unwrap() - first).abs() < EPSILON);
    }
}