Files
pp-jobboard-sim/tests/spec_validation.rs
T

215 lines
7.4 KiB
Rust

use std::collections::HashSet;
use pp_jobboard_sim::generator::{BoardScratch, compute_pay, generate_board, partition_and_sort};
use pp_jobboard_sim::geo::NM;
use pp_jobboard_sim::model::{
BoardOptions, CompanyBase, CompanyState, EventBox, EventMode, EventRule, EventState,
FilterMode, JobKind, ProjectState, SortMode,
};
use pp_jobboard_sim::rng::{BUCKET_MS, board_salt, bucket_of, hash31, rnd2};
use pp_jobboard_sim::synthetic::generate_synthetic_world;
use pp_jobboard_sim::world::WorldIndex;
#[test]
fn test_hash31_and_rnd2_exact() {
// Java-style hash31("KSAN") = ((('K'*31+'S')*31+'A')*31+'N')
let expected = (((('K' as i32) * 31 + ('S' as i32)) * 31 + ('A' as i32)) * 31) + ('N' as i32);
assert_eq!(hash31("KSAN"), expected);
// rnd2 must be deterministic and in [0, 1]
let v1 = rnd2(expected, 12345);
let v2 = rnd2(expected, 12345);
assert_eq!(v1, v2);
assert!((0.0..=1.0).contains(&v1));
// Offline SHA-256 board_salt deterministic
let s1 = board_salt("offline", 997_815);
let s2 = board_salt("offline", 997_815);
let s3 = board_salt("offline", 997_816);
assert_eq!(s1, s2);
assert_ne!(s1, s3);
}
#[test]
fn test_pay_formula_matches_spec() {
// Section 3.4: 25 nm, 1 pax, pax(1.0), level 0, not short:
// base = 60*(15 + 25/3) = 1400; capacity=1 -> 0.6+0.4*1=1.0 -> 1400
assert_eq!(compute_pay(25.0, 1, 0, JobKind::Pax, 1.0, 0, false), 1400);
// 50 nm, 1 pax -> 60*(15+50/3) = 1900
assert_eq!(compute_pay(50.0, 1, 0, JobKind::Pax, 1.0, 0, false), 1900);
// 100 nm, 1 pax -> 60*(15+100/3) = 2900
assert_eq!(compute_pay(100.0, 1, 0, JobKind::Pax, 1.0, 0, false), 2900);
// 400 nm, 36 pax -> 60*(15+400/3)*(0.6+0.4*36^0.6) ≈ 35910
let p400_36 = compute_pay(400.0, 36, 0, JobKind::Pax, 1.0, 0, false);
assert!((p400_36 - 35910).abs() <= 20, "got {p400_36}");
// Short field +10%, level +5% per level, always multiple of 10
let p_short = compute_pay(100.0, 1, 0, JobKind::Pax, 1.0, 2, true);
assert_eq!(p_short % 10, 0);
assert!(p_short > 2900);
}
#[test]
fn test_determinism_and_invariants() {
let data = generate_synthetic_world(42, 600);
let world = WorldIndex::build(data, true);
let epoch_ms: i64 = 1_796_067_000_000;
let bucket = bucket_of(epoch_ms);
let expected_exp = (bucket + 1) * BUCKET_MS;
let opts = BoardOptions {
salt: 123_456_789,
day_salt: 987_654,
level: 2,
..Default::default()
};
let mut scratch = BoardScratch::with_capacity();
let planes: Vec<_> = world.plane_list.iter().collect();
for ap_idx in 0..30 {
let origin = &world.airports[ap_idx];
for &plane in &planes {
let b1 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts, &mut scratch);
let b2 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts, &mut scratch);
// Property 1: byte-identical JSON for identical inputs
let j1 = serde_json::to_string(&b1).unwrap();
let j2 = serde_json::to_string(&b2).unwrap();
assert_eq!(j1, j2, "non-deterministic board for {} / {}", origin.raw.code, plane.id);
// Property 3, 4, 5: unique IDs, no duplicate fixed-wing dest, valid expiry, pay % 10 == 0
let mut ids = HashSet::new();
let mut fw_dests = HashSet::new();
for job in &b1 {
assert!(ids.insert(job.id.clone()), "duplicate id {}", job.id);
assert_eq!(job.expires, expected_exp);
assert!(job.pay >= 0 && job.pay % 10 == 0, "invalid pay {}", job.pay);
if job.to != job.from && !job.big_charter && !job.water {
assert!(
fw_dests.insert(job.to.clone()),
"duplicate dest {} on {}/{}",
job.to,
origin.raw.code,
plane.id
);
}
}
// Property 2: adjacent buckets share no IDs; salt change reshuffles
let next_epoch = epoch_ms + BUCKET_MS;
let b_next = generate_board(&world, ap_idx, next_epoch, &[plane], &opts, &mut scratch);
for job in &b_next {
assert!(!ids.contains(&job.id), "adjacent bucket shared id {}", job.id);
}
}
}
}
#[test]
fn test_salt_change_reshuffles_and_events_company_projects() {
let data = generate_synthetic_world(99, 500);
let world = WorldIndex::build(data, true);
let epoch_ms: i64 = 1_796_067_000_000;
let plane = world.planes.get("utility_turboprop").unwrap();
let ap_idx = world
.airports
.iter()
.position(|a| a.raw.size == 0)
.unwrap();
let origin = &world.airports[ap_idx];
let other_hub = world
.airports
.iter()
.find(|a| a.raw.size == 0 && a.raw.code != origin.raw.code)
.unwrap();
let ev = EventState {
id: "wk-test".into(),
mode: EventMode::Inside,
bbox: EventBox {
min_lat: origin.raw.lat - 10.0,
max_lat: origin.raw.lat + 10.0,
min_lon: origin.raw.lon - 10.0,
max_lon: origin.raw.lon + 10.0,
},
what: "Relief Cargo".into(),
kind: JobKind::Cargo,
met: true,
rule: EventRule { min_kg: Some(200.0) },
};
let co = CompanyState {
id: "CO1".into(),
name: "Test Air".into(),
bases: vec![
CompanyBase {
code: origin.raw.code.clone(),
name: origin.raw.name.clone(),
lat: origin.raw.lat,
lon: origin.raw.lon,
gates: 2,
low: false,
},
CompanyBase {
code: other_hub.raw.code.clone(),
name: other_hub.raw.name.clone(),
lat: other_hub.raw.lat,
lon: other_hub.raw.lon,
gates: 1,
low: true,
},
],
};
let proj = ProjectState {
code: other_hub.raw.code.clone(),
name: other_hub.raw.name.clone(),
lat: other_hub.raw.lat,
lon: other_hub.raw.lon,
what: "Runway lights".into(),
sling: false,
};
let opts1 = BoardOptions {
salt: 11111,
day_salt: 22222,
level: 1,
event: Some(&ev),
company: Some(&co),
projects: &[proj],
..Default::default()
};
let opts2 = BoardOptions {
salt: 99999,
..opts1.clone()
};
let mut scratch = BoardScratch::with_capacity();
let b1 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts1, &mut scratch);
let b2 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts2, &mut scratch);
let dests1: Vec<_> = b1.iter().map(|j| (&j.to, j.pay)).collect();
let dests2: Vec<_> = b2.iter().map(|j| (&j.to, j.pay)).collect();
assert_ne!(dests1, dests2, "salt change should reshuffle board");
// Verify presentation partition & sort
let part = partition_and_sort(
&world,
origin,
plane,
b1,
epoch_ms,
true,
SortMode::NetPerHour,
&FilterMode::All,
);
assert!(!part.ok.is_empty());
for w in part.ok.windows(2) {
assert!(w[0].net_per_hour >= w[1].net_per_hour);
}
assert!(part.refresh_mins > 0 && part.refresh_mins <= 30);
// Sanity check NM constant
assert_eq!(NM, 1852.0);
}