ballistics-engine 0.50.0

High-performance ballistics trajectory engine with professional physics
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
//! Unit tests for the single humidity source of truth in `atmosphere.rs`: the validated
//! [`RelativeHumidity`], [`StationAir`], [`KernelAtmosphere`] and [`air_at`].
//!
//! Two kinds of test live here.
//! - Validation and conversion: every constructor's bounds, NaN and the infinities, exact 0 and
//!   100, the fraction round trips, and impossible air refused by exactly the predicate the
//!   `atmosphere.density_altitude` service applies.
//! - Bit-identity of the refactor: the merged `vapor_mole_fraction`, `StationAir::new` against
//!   `calculate_atmosphere`, and `air_at` against both the legacy `get_local_atmosphere_humid`
//!   wrapper and a verbatim copy of 0.49.0's `TrajectorySolver::calculate_acceleration` air
//!   block. Those comparisons are `f64::to_bits`, never a tolerance.

use super::*;

// =================================================================================================
// Grids
// =================================================================================================

/// Station temperatures (C) over -40..45 C, with 60 C beyond it as a hot edge.
const TEMPS_C: [f64; 12] = [
    -40.0, -12.3, -10.0, 0.0, 7.9, 15.0, 21.7, 30.0, 33.3, 35.0, 45.0, 60.0,
];
/// Station pressures (hPa) spanning 500..1100 hPa.
const PRESSURES_HPA: [f64; 6] = [500.0, 700.0, 850.0, 1000.0, 1013.25, 1100.0];
/// Relative humidities (percent), both exact ends included.
const HUMIDITIES: [f64; 7] = [0.0, 12.5, 37.5, 50.0, 62.5, 90.0, 100.0];
/// Query altitudes relative to the station (m): at, just above, well above, and below it.
const ALT_OFFSETS_M: [f64; 6] = [0.0, 0.5, 50.0, 1_000.0, 12_000.0, -200.0];

fn bits(values: &[f64]) -> Vec<u64> {
    values.iter().map(|v| v.to_bits()).collect()
}

fn local_bits(air: LocalAir) -> [u64; 4] {
    [
        air.density_kg_m3.to_bits(),
        air.speed_of_sound_mps.to_bits(),
        air.temperature_k.to_bits(),
        air.pressure_pa.to_bits(),
    ]
}

fn rh(percent: f64) -> RelativeHumidity {
    RelativeHumidity::try_percent(percent).expect("valid test humidity")
}

/// The difference in units in the last place between two finite, same-sign doubles.
fn ulps_apart(a: f64, b: f64) -> u64 {
    a.to_bits().abs_diff(b.to_bits())
}

// =================================================================================================
// RelativeHumidity
// =================================================================================================

#[test]
fn relative_humidity_constants_are_the_documented_default_and_dry_air() {
    assert_eq!(DEFAULT_HUMIDITY_PERCENT.to_bits(), 50.0_f64.to_bits());
    assert_eq!(
        RelativeHumidity::DEFAULT.percent().to_bits(),
        DEFAULT_HUMIDITY_PERCENT.to_bits()
    );
    assert_eq!(
        RelativeHumidity::DEFAULT.fraction().to_bits(),
        0.5_f64.to_bits()
    );
    assert_eq!(RelativeHumidity::DRY.percent().to_bits(), 0.0_f64.to_bits());
    assert_eq!(
        RelativeHumidity::DRY.fraction().to_bits(),
        0.0_f64.to_bits()
    );
    assert_eq!(rh(50.0), RelativeHumidity::DEFAULT);
    assert_eq!(rh(0.0), RelativeHumidity::DRY);
}

#[test]
fn try_percent_accepts_the_closed_range_and_stores_the_value_verbatim() {
    for percent in [0.0, 1e-300, 0.0001, 12.5, 37.5, 50.0, 99.9999, 100.0] {
        let humidity = RelativeHumidity::try_percent(percent).expect("in range");
        assert_eq!(humidity.percent().to_bits(), percent.to_bits(), "{percent}");
        assert_eq!(
            humidity.fraction().to_bits(),
            (percent / 100.0).to_bits(),
            "{percent}"
        );
    }
    // Exact ends, by bits.
    assert_eq!(rh(0.0).percent().to_bits(), 0.0_f64.to_bits());
    assert_eq!(rh(100.0).percent().to_bits(), 100.0_f64.to_bits());
    assert_eq!(rh(100.0).fraction().to_bits(), 1.0_f64.to_bits());
}

#[test]
fn negative_zero_percent_is_dry_air_with_positive_zero_bits() {
    let humidity = RelativeHumidity::try_percent(-0.0).expect("-0.0 is zero");
    assert_eq!(humidity.percent().to_bits(), 0.0_f64.to_bits());
    assert_eq!(humidity, RelativeHumidity::DRY);
    let humidity = RelativeHumidity::try_fraction(-0.0).expect("-0.0 is zero");
    assert_eq!(humidity.percent().to_bits(), 0.0_f64.to_bits());
}

#[test]
fn try_percent_rejects_out_of_range_and_non_finite_values_without_clamping() {
    // A reading within 5 points of the range is saturated or dry air (the band tests
    // above); only a value beyond the band is refused, and it is refused unchanged.
    let out_of_range = [-5.000_000_1, 105.000_000_1, 150.0, 1e300];
    for percent in out_of_range {
        let error = RelativeHumidity::try_percent(percent).expect_err("out of range");
        assert_eq!(error.field(), "humidity_percent", "{percent}");
        assert_eq!(error.value().to_bits(), percent.to_bits(), "{percent}");
        assert_eq!(
            error.reason(),
            AtmosphereErrorReason::HumidityPercentOutOfRange,
            "{percent}"
        );
    }
    for percent in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        let error = RelativeHumidity::try_percent(percent).expect_err("non-finite");
        assert_eq!(error.field(), "humidity_percent");
        assert_eq!(error.reason(), AtmosphereErrorReason::NotFinite);
        assert!(
            error.value().is_nan() || error.value() == percent,
            "the error echoes the rejected value"
        );
    }
}

#[test]
fn try_fraction_accepts_the_closed_unit_range_and_scales_by_exactly_100() {
    // The scaling is `fraction * 100.0`, the very conversion the SI wire applies today
    // (solve_v1's relative_humidity * 100.0 and the density_altitude service's), so a later
    // migration of those sites through try_fraction is bit-identical.
    for k in 0..=1000 {
        let fraction = f64::from(k) / 1000.0;
        let humidity = RelativeHumidity::try_fraction(fraction).expect("in range");
        assert_eq!(
            humidity.percent().to_bits(),
            (fraction * 100.0).to_bits(),
            "{fraction}"
        );
    }
    assert_eq!(
        RelativeHumidity::try_fraction(0.5).unwrap(),
        RelativeHumidity::try_percent(50.0).unwrap()
    );
    assert_eq!(
        RelativeHumidity::try_fraction(1.0)
            .unwrap()
            .percent()
            .to_bits(),
        100.0_f64.to_bits()
    );
    assert_eq!(
        RelativeHumidity::try_fraction(0.0)
            .unwrap()
            .percent()
            .to_bits(),
        0.0_f64.to_bits()
    );
    assert_eq!(
        RelativeHumidity::try_fraction(0.375).unwrap(),
        RelativeHumidity::try_percent(37.5).unwrap()
    );
}

#[test]
fn try_fraction_rejects_out_of_range_and_non_finite_values_without_clamping() {
    for fraction in [1.5, -0.1, -0.050_000_1, 1.050_000_1, 50.0, 100.0] {
        let error = RelativeHumidity::try_fraction(fraction).expect_err("out of range");
        assert_eq!(error.field(), "relative_humidity", "{fraction}");
        assert_eq!(error.value().to_bits(), fraction.to_bits(), "{fraction}");
        assert_eq!(
            error.reason(),
            AtmosphereErrorReason::HumidityFractionOutOfRange,
            "{fraction}"
        );
    }
    for fraction in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        let error = RelativeHumidity::try_fraction(fraction).expect_err("non-finite");
        assert_eq!(error.field(), "relative_humidity");
        assert_eq!(error.reason(), AtmosphereErrorReason::NotFinite);
    }
}

#[test]
fn fraction_round_trips_are_exact_at_the_ends_and_within_one_ulp_elsewhere() {
    for fraction in [0.0, 0.25, 0.5, 0.75, 1.0] {
        let back = RelativeHumidity::try_fraction(fraction).unwrap().fraction();
        assert_eq!(back.to_bits(), fraction.to_bits(), "{fraction}");
    }
    for k in 0..=10_000 {
        let fraction = f64::from(k) / 10_000.0;
        let back = RelativeHumidity::try_fraction(fraction).unwrap().fraction();
        assert!(
            ulps_apart(back, fraction) <= 1,
            "fraction {fraction} came back as {back}"
        );
        let percent = f64::from(k) / 100.0;
        let back = RelativeHumidity::try_fraction(rh(percent).fraction())
            .unwrap()
            .percent();
        assert!(
            ulps_apart(back, percent) <= 1,
            "percent {percent} came back as {back}"
        );
    }
}

// =================================================================================================
// The sensor-tolerance band (owner decision 2026-11-01)
// =================================================================================================

/// Percents a humidity SENSOR can report around saturated air: within 5 points above 100,
/// they are saturated air, stored as exactly 100.
const SATURATED_READINGS: [f64; 6] = [
    100.000_000_000_001,
    100.000_000_1,
    100.0001,
    101.7,
    104.999_999_9,
    105.0,
];
/// ...and around dry air: within 5 points below 0 (and a negative zero), dry air, stored as
/// exactly +0.0.
const DRY_READINGS: [f64; 7] = [-0.0, -1e-300, -0.0001, -2.0, -3.0, -4.999_999_9, -5.0];

#[test]
fn the_sensor_band_is_five_points_either_side_in_both_units() {
    assert_eq!(
        HUMIDITY_SENSOR_TOLERANCE_PERCENT.to_bits(),
        5.0_f64.to_bits()
    );
    // The band edges are the decimal literals themselves, so 105.0, -5.0, 1.05 and -0.05 are
    // inside and the next double out is not.
    assert_eq!(
        [HUMIDITY_PERCENT_BAND.0, HUMIDITY_PERCENT_BAND.1].map(f64::to_bits),
        [-5.0_f64, 105.0].map(f64::to_bits)
    );
    assert_eq!(
        [HUMIDITY_FRACTION_BAND.0, HUMIDITY_FRACTION_BAND.1].map(f64::to_bits),
        [-0.05_f64, 1.05].map(f64::to_bits)
    );
    for edge in [-5.0_f64, 105.0] {
        assert!(RelativeHumidity::try_percent(edge).is_ok(), "{edge}");
        let beyond = f64::from_bits(if edge > 0.0 {
            edge.to_bits() + 1
        } else {
            (-edge).to_bits().wrapping_add(1) | (1 << 63)
        });
        assert!(RelativeHumidity::try_percent(beyond).is_err(), "{beyond}");
    }
    for edge in [-0.05_f64, 1.05] {
        assert!(RelativeHumidity::try_fraction(edge).is_ok(), "{edge}");
        let beyond = f64::from_bits(if edge > 0.0 {
            edge.to_bits() + 1
        } else {
            (-edge).to_bits().wrapping_add(1) | (1 << 63)
        });
        assert!(RelativeHumidity::try_fraction(beyond).is_err(), "{beyond}");
    }
}

/// The two other places a humidity's range is needed take it from the one rule rather than
/// writing a second one. The MCP schema publishes `RelativeHumidity::FRACTION_BAND`, which is
/// the band `try_fraction` enforces; and the perturbation layer's physical domain is
/// `is_physical_fraction`, the fractions the band leaves where it found them, which is exactly
/// `0.0..=1.0` (the domain that layer had before the band, so its derivatives are unchanged).
#[test]
fn the_published_fraction_band_and_the_physical_domain_come_from_the_one_rule() {
    assert_eq!(
        [
            RelativeHumidity::FRACTION_BAND.0,
            RelativeHumidity::FRACTION_BAND.1
        ]
        .map(f64::to_bits),
        [HUMIDITY_FRACTION_BAND.0, HUMIDITY_FRACTION_BAND.1].map(f64::to_bits)
    );
    let below_one = f64::from_bits(1.0_f64.to_bits() - 1);
    let above_one = f64::from_bits(1.0_f64.to_bits() + 1);
    for fraction in [0.0, -0.0, 1e-300, 0.25, 0.5, below_one, 1.0] {
        assert!(
            RelativeHumidity::is_physical_fraction(fraction),
            "{fraction}"
        );
    }
    for fraction in [
        above_one,
        1.001,
        1.03,
        1.05,
        -1e-300,
        -0.001,
        -0.03,
        -0.05,
        1.06,
        1.5,
        50.0,
        -1.0,
        f64::NAN,
        f64::INFINITY,
        f64::NEG_INFINITY,
    ] {
        assert!(
            !RelativeHumidity::is_physical_fraction(fraction),
            "{fraction}"
        );
    }
    // Exactly the closed unit interval, on a sweep across both band edges.
    for step in -1_200..=1_200 {
        let fraction = f64::from(step) * 1e-3;
        assert_eq!(
            RelativeHumidity::is_physical_fraction(fraction),
            (0.0..=1.0).contains(&fraction),
            "{fraction}"
        );
    }
}

#[test]
fn try_percent_reads_a_sensor_reading_within_five_points_as_saturated_or_dry_air() {
    for percent in SATURATED_READINGS {
        let humidity = RelativeHumidity::try_percent(percent)
            .unwrap_or_else(|e| panic!("{percent} is a saturated-air reading: {e}"));
        assert_eq!(
            humidity.percent().to_bits(),
            100.0_f64.to_bits(),
            "{percent}"
        );
        assert_eq!(
            humidity.fraction().to_bits(),
            1.0_f64.to_bits(),
            "{percent}"
        );
        assert_eq!(humidity, rh(100.0), "{percent}");
    }
    for percent in DRY_READINGS {
        let humidity = RelativeHumidity::try_percent(percent)
            .unwrap_or_else(|e| panic!("{percent} is a dry-air reading: {e}"));
        assert_eq!(humidity.percent().to_bits(), 0.0_f64.to_bits(), "{percent}");
        assert_eq!(humidity, RelativeHumidity::DRY, "{percent}");
    }
    // The exact ends and the inside of the range are untouched, bit for bit.
    for percent in [0.0, 1e-300, 0.0001, 7.0, 55.0, 99.9999, 100.0] {
        assert_eq!(
            rh(percent).percent().to_bits(),
            percent.to_bits(),
            "{percent}"
        );
    }
}

#[test]
fn try_percent_refuses_a_reading_beyond_the_sensor_band_unchanged() {
    for percent in [
        105.000_000_1,
        105.000_000_000_001,
        106.0,
        150.0,
        1e300,
        -5.000_000_1,
        -5.000_000_000_001,
        -20.0,
        -1e300,
    ] {
        let error = RelativeHumidity::try_percent(percent).expect_err("beyond the band");
        assert_eq!(error.field(), "humidity_percent", "{percent}");
        assert_eq!(error.value().to_bits(), percent.to_bits(), "{percent}");
        assert_eq!(
            error.reason(),
            AtmosphereErrorReason::HumidityPercentOutOfRange,
            "{percent}"
        );
    }
    for percent in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        let error = RelativeHumidity::try_percent(percent).expect_err("non-finite");
        assert_eq!(error.reason(), AtmosphereErrorReason::NotFinite);
    }
}

#[test]
fn try_fraction_reads_a_sensor_reading_within_five_points_as_saturated_or_dry_air() {
    // 1.0 is exactly 100 %, and a fraction up to 0.05 above it is saturated air.
    for fraction in [1.0, 1.000_000_000_000_001, 1.0001, 1.03, 1.049_999_9, 1.05] {
        let humidity = RelativeHumidity::try_fraction(fraction)
            .unwrap_or_else(|e| panic!("{fraction} is a saturated-air reading: {e}"));
        assert_eq!(
            humidity.percent().to_bits(),
            100.0_f64.to_bits(),
            "{fraction}"
        );
        assert_eq!(humidity, rh(100.0), "{fraction}");
    }
    for fraction in [-0.0, -1e-300, -0.0001, -0.03, -0.049_999_9, -0.05] {
        let humidity = RelativeHumidity::try_fraction(fraction)
            .unwrap_or_else(|e| panic!("{fraction} is a dry-air reading: {e}"));
        assert_eq!(
            humidity.percent().to_bits(),
            0.0_f64.to_bits(),
            "{fraction}"
        );
        assert_eq!(humidity, RelativeHumidity::DRY, "{fraction}");
    }
}

#[test]
fn try_fraction_refuses_a_reading_beyond_the_sensor_band_unchanged() {
    // A percent sent where a fraction belongs (50.0, 100.0) stays an error.
    for fraction in [
        1.050_000_1,
        1.06,
        1.5,
        50.0,
        100.0,
        -0.050_000_1,
        -0.1,
        -1.0,
    ] {
        let error = RelativeHumidity::try_fraction(fraction).expect_err("beyond the band");
        assert_eq!(error.field(), "relative_humidity", "{fraction}");
        assert_eq!(error.value().to_bits(), fraction.to_bits(), "{fraction}");
        assert_eq!(
            error.reason(),
            AtmosphereErrorReason::HumidityFractionOutOfRange,
            "{fraction}"
        );
    }
    for fraction in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        let error = RelativeHumidity::try_fraction(fraction).expect_err("non-finite");
        assert_eq!(error.reason(), AtmosphereErrorReason::NotFinite);
    }
}

#[test]
fn a_reading_in_the_band_is_the_same_station_and_zone_air_as_the_end_it_reads_as() {
    // The snapped humidity is the end itself, so the station air (density, sound speed, ratio)
    // and a zone built at the reading are the end's to the bit.
    for (reading, end) in [(101.7, 100.0), (105.0, 100.0), (-2.0, 0.0), (-5.0, 0.0)] {
        for (altitude_m, temp_c, pressure_hpa) in [
            (0.0, 15.0, 1013.25),
            (1_500.0, 30.0, 830.0),
            (0.0, -10.0, 1030.0),
        ] {
            let at_reading = StationAir::new(altitude_m, temp_c, pressure_hpa, rh(reading))
                .expect("a reading in the band is a station");
            let at_end = StationAir::new(altitude_m, temp_c, pressure_hpa, rh(end)).unwrap();
            assert_eq!(at_reading, at_end, "{reading} -> {end} at {temp_c} C");
        }
        assert!(AtmoSock::new(vec![(15.0, 1013.25, reading, 300.0)])
            .validate()
            .is_ok());
    }
    // The band does not relax impossible air: saturated air at the boiling point read as 102 %
    // is refused exactly as 100 % is, at the station and in a zone.
    for reading in [100.0, 101.7, 105.0] {
        let error = StationAir::new(0.0, 100.0, 1013.25, rh(reading)).expect_err("steam");
        assert_eq!(
            error.reason(),
            AtmosphereErrorReason::ImpossibleAir,
            "{reading}"
        );
        let error = AtmoSock::new(vec![(100.0, 1013.25, reading, 300.0)])
            .validate()
            .expect_err("steam");
        assert_eq!(
            error.reason(),
            AtmosphereErrorReason::ImpossibleAir,
            "{reading}"
        );
    }
}

// =================================================================================================
// AtmosphereError
// =================================================================================================

#[test]
fn atmosphere_error_names_its_field_and_value_and_can_be_relabelled_at_a_boundary() {
    let error = RelativeHumidity::try_percent(150.0).unwrap_err();
    let text = error.to_string();
    assert!(text.contains("humidity_percent"), "{text}");
    assert!(text.contains("150"), "{text}");
    assert!(text.contains("0") && text.contains("100"), "{text}");

    let relabelled = error.clone().at_field("$.atmosphere.relative_humidity");
    assert_eq!(relabelled.field(), "$.atmosphere.relative_humidity");
    assert_eq!(relabelled.value().to_bits(), error.value().to_bits());
    assert_eq!(relabelled.reason(), error.reason());

    // It is a std error, so `?` can lift it into Box<dyn Error> and friends.
    let boxed: Box<dyn std::error::Error> = Box::new(relabelled);
    assert!(boxed
        .to_string()
        .starts_with("$.atmosphere.relative_humidity"));
}

// =================================================================================================
// One vapour mole fraction for the density and the sound speed
// =================================================================================================

/// 0.49.0's inline vapour mole fraction from `moist_speed_of_sound`, verbatim, before its
/// `.min(1.0)` cap.
fn inline_mole_fraction_0_49(temp_k: f64, pressure_pa: f64, humidity_percent: f64) -> f64 {
    let humidity_clamped = humidity_percent.clamp(0.0, 100.0);
    let temp_c = temp_k - 273.15;
    let p_sv_hpa = enhanced_saturation_vapor_pressure(temp_k);
    let f = enhanced_enhancement_factor(pressure_pa, temp_c);
    let vapor_pressure_pa = humidity_clamped / 100.0 * f * p_sv_hpa * 100.0;
    vapor_pressure_pa / pressure_pa.max(f64::MIN_POSITIVE)
}

/// 0.49.0's `moist_speed_of_sound`, verbatim.
fn moist_speed_of_sound_0_49(temp_k: f64, pressure_pa: f64, humidity_percent: f64) -> f64 {
    let mole_fraction_vapor =
        inline_mole_fraction_0_49(temp_k, pressure_pa, humidity_percent).min(1.0);
    let gamma_moist = GAMMA * (1.0 - mole_fraction_vapor * 0.062);
    let r_moist = R_AIR * (1.0 + 0.378 * mole_fraction_vapor);
    (gamma_moist * r_moist * temp_k).sqrt()
}

#[test]
fn merged_vapor_mole_fraction_is_bit_identical_to_the_old_inline_copy() {
    let mut checked = 0usize;
    for t_step in 0..=85 {
        let temp_c = -40.0 + f64::from(t_step);
        let temp_k = temp_c + 273.15;
        for p_step in 0..=60 {
            let pressure_pa = (500.0 + 10.0 * f64::from(p_step)) * 100.0;
            for h_step in 0..=20 {
                let humidity = 5.0 * f64::from(h_step);
                let merged = moist_air_vapor_mole_fraction(temp_k, pressure_pa, humidity);
                let inline = inline_mole_fraction_0_49(temp_k, pressure_pa, humidity);
                assert_eq!(
                    merged.to_bits(),
                    inline.to_bits(),
                    "x_v at {temp_c} C, {pressure_pa} Pa, {humidity}%"
                );
                assert_eq!(
                    moist_speed_of_sound(temp_k, pressure_pa, humidity).to_bits(),
                    moist_speed_of_sound_0_49(temp_k, pressure_pa, humidity).to_bits(),
                    "c at {temp_c} C, {pressure_pa} Pa, {humidity}%"
                );
                checked += 1;
            }
        }
    }
    assert_eq!(checked, 86 * 61 * 21);
}

#[test]
fn merged_moist_speed_of_sound_keeps_the_old_total_function_guards() {
    // Out-of-range RH, a zero or negative pressure and NaN inputs reach the legacy public
    // function unvalidated; the merge must keep its clamp, its pressure floor and its x_v cap
    // bit-for-bit.
    let cases = [
        (288.15, 101_325.0, -5.0),
        (288.15, 101_325.0, 150.0),
        (288.15, 0.0, 50.0),
        (288.15, -5.0, 50.0),
        (373.15, 101_325.0, 100.0),
        (500.0, 1_000.0, 100.0),
        (f64::NAN, 101_325.0, 50.0),
    ];
    for (temp_k, pressure_pa, humidity) in cases {
        let merged = moist_speed_of_sound(temp_k, pressure_pa, humidity);
        let old = moist_speed_of_sound_0_49(temp_k, pressure_pa, humidity);
        assert_eq!(
            merged.to_bits(),
            old.to_bits(),
            "c at {temp_k} K, {pressure_pa} Pa, {humidity}%: merged {merged} old {old}"
        );
    }
    // The inputs whose water-vapour mole fraction is NaN (a NaN humidity, a NaN pressure, and
    // humid air above water's critical point, 647.096 K) are NaN now. 0.49.0's `x_v.min(1.0)`
    // read the NaN as 1 and returned pure steam's sound speed (603.0 m/s at 700 K, 386.9 m/s at
    // 15 °C), at 0 % too. Dry air above that point holds no vapour: its mole fraction is exactly
    // 0, and its sound speed the dry one, to the bit.
    let old = moist_speed_of_sound_0_49(700.0, 101_325.0, 0.0);
    assert!(old.is_finite(), "0.49.0 flew 700 K / 0 % as steam: {old}");
    assert_eq!(
        moist_speed_of_sound(700.0, 101_325.0, 0.0).to_bits(),
        (GAMMA * R_AIR * 700.0).sqrt().to_bits(),
        "dry air at 700 K has the dry sound speed"
    );
    for (temp_k, pressure_pa, humidity) in [
        (700.0, 101_325.0, 1.0),
        (288.15, 101_325.0, f64::NAN),
        (288.15, f64::NAN, 50.0),
    ] {
        let old = moist_speed_of_sound_0_49(temp_k, pressure_pa, humidity);
        assert!(
            old.is_finite(),
            "0.49.0 flew {temp_k} K / {humidity} % as steam: {old}"
        );
        let merged = moist_speed_of_sound(temp_k, pressure_pa, humidity);
        assert!(
            merged.is_nan(),
            "c at {temp_k} K, {pressure_pa} Pa, {humidity}% is NaN, not steam: {merged}"
        );
    }
}

#[test]
fn dry_sound_speed_is_the_engines_single_dry_constant() {
    for temp_k in [223.15, 250.0, 288.15, 303.15, 318.15] {
        for pressure_pa in [50_000.0, 101_325.0, 110_000.0] {
            assert_eq!(
                moist_speed_of_sound(temp_k, pressure_pa, 0.0).to_bits(),
                (GAMMA * R_AIR * temp_k).sqrt().to_bits(),
                "{temp_k} K {pressure_pa} Pa"
            );
        }
    }
}

// =================================================================================================
// StationAir
// =================================================================================================

#[test]
fn station_air_new_is_calculate_atmosphere_bit_for_bit() {
    for altitude_m in [-100.0, 0.0, 1_500.0, 4_000.0] {
        for temp_c in TEMPS_C {
            for pressure_hpa in PRESSURES_HPA {
                for humidity in HUMIDITIES {
                    let Ok(station) =
                        StationAir::new(altitude_m, temp_c, pressure_hpa, rh(humidity))
                    else {
                        // Only impossible air may be refused on this grid.
                        let x_v =
                            water_vapor_mole_fraction_uncapped(temp_c, pressure_hpa, humidity);
                        assert!(x_v >= 1.0, "{temp_c} C {pressure_hpa} hPa {humidity}%");
                        continue;
                    };
                    let (density, sound) = calculate_atmosphere(
                        altitude_m,
                        Some(temp_c),
                        Some(pressure_hpa),
                        humidity,
                    );
                    assert_eq!(
                        bits(&[
                            station.density_kg_m3(),
                            station.speed_of_sound_mps(),
                            station.density_ratio(),
                        ]),
                        bits(&[density, sound, density / 1.225]),
                        "{altitude_m} m {temp_c} C {pressure_hpa} hPa {humidity}%"
                    );
                    assert_eq!(
                        bits(&[
                            station.altitude_m(),
                            station.temperature_c(),
                            station.pressure_hpa(),
                            station.humidity().percent(),
                        ]),
                        bits(&[altitude_m, temp_c, pressure_hpa, humidity])
                    );
                }
            }
        }
    }
}

#[test]
fn station_air_accepts_exact_zero_and_one_hundred_percent() {
    for humidity in [0.0, 100.0] {
        let station = StationAir::new(0.0, 15.0, 1013.25, rh(humidity)).expect("possible air");
        assert_eq!(station.humidity().percent().to_bits(), humidity.to_bits());
    }
}

#[test]
fn station_air_refuses_non_finite_and_unphysical_state_naming_the_field() {
    let humidity = RelativeHumidity::DEFAULT;
    let cases: [(f64, f64, f64, &str, AtmosphereErrorReason); 11] = [
        (
            f64::NAN,
            15.0,
            1013.25,
            "altitude_m",
            AtmosphereErrorReason::NotFinite,
        ),
        (
            f64::INFINITY,
            15.0,
            1013.25,
            "altitude_m",
            AtmosphereErrorReason::NotFinite,
        ),
        (
            0.0,
            f64::NAN,
            1013.25,
            "temperature_c",
            AtmosphereErrorReason::NotFinite,
        ),
        (
            0.0,
            f64::NEG_INFINITY,
            1013.25,
            "temperature_c",
            AtmosphereErrorReason::NotFinite,
        ),
        (
            0.0,
            -273.15,
            1013.25,
            "temperature_c",
            AtmosphereErrorReason::AtOrBelowAbsoluteZero,
        ),
        (
            0.0,
            -300.0,
            1013.25,
            "temperature_c",
            AtmosphereErrorReason::AtOrBelowAbsoluteZero,
        ),
        (
            0.0,
            15.0,
            f64::NAN,
            "pressure_hpa",
            AtmosphereErrorReason::NotFinite,
        ),
        (
            0.0,
            15.0,
            f64::INFINITY,
            "pressure_hpa",
            AtmosphereErrorReason::NotFinite,
        ),
        (
            0.0,
            15.0,
            0.0,
            "pressure_hpa",
            AtmosphereErrorReason::NonPositivePressure,
        ),
        (
            0.0,
            15.0,
            -0.0,
            "pressure_hpa",
            AtmosphereErrorReason::NonPositivePressure,
        ),
        (
            0.0,
            15.0,
            -1013.25,
            "pressure_hpa",
            AtmosphereErrorReason::NonPositivePressure,
        ),
    ];
    for (altitude_m, temp_c, pressure_hpa, field, reason) in cases {
        let error = StationAir::new(altitude_m, temp_c, pressure_hpa, humidity)
            .expect_err("unphysical state");
        assert_eq!(error.field(), field, "{altitude_m} {temp_c} {pressure_hpa}");
        assert_eq!(
            error.reason(),
            reason,
            "{altitude_m} {temp_c} {pressure_hpa}"
        );
        let error = StationAir::with_density_ratio(altitude_m, temp_c, pressure_hpa, 1.0, humidity)
            .expect_err("unphysical state");
        assert_eq!(error.field(), field, "{altitude_m} {temp_c} {pressure_hpa}");
        assert_eq!(
            error.reason(),
            reason,
            "{altitude_m} {temp_c} {pressure_hpa}"
        );
    }
    // Just above absolute zero is a state the model can evaluate.
    assert!(StationAir::new(0.0, -273.0, 1013.25, RelativeHumidity::DRY).is_ok());
}

#[test]
fn station_air_refuses_the_impossible_air_that_density_altitude_refuses() {
    // Saturated air at 100 C and 1013.25 hPa (boiling water at sea level) and at 95 C and
    // 500 hPa: each needs a water-vapour mole fraction of 1 or more, so every RH-carrying
    // constructor refuses it.
    for (temp_c, pressure_hpa) in [(100.0, 1013.25), (95.0, 500.0)] {
        let error = StationAir::new(0.0, temp_c, pressure_hpa, rh(100.0)).expect_err("steam");
        assert_eq!(error.field(), "humidity_percent");
        assert_eq!(error.reason(), AtmosphereErrorReason::ImpossibleAir);
        assert_eq!(error.value().to_bits(), 100.0_f64.to_bits());
        let error = StationAir::with_density_ratio(0.0, temp_c, pressure_hpa, 0.6, rh(100.0))
            .expect_err("steam");
        assert_eq!(error.reason(), AtmosphereErrorReason::ImpossibleAir);
    }

    // Parity with the service's predicate (atmosphere_service.rs: NaN or >= 1 is refused) over
    // a grid that crosses the boundary, up past water's critical point.
    let temps_c = [
        -40.0, 0.0, 20.0, 40.0, 60.0, 80.0, 90.0, 95.0, 99.0, 100.0, 101.0, 110.0, 150.0, 200.0,
        300.0, 373.0, 374.0, 380.0, 500.0,
    ];
    let pressures_hpa = [300.0, 500.0, 700.0, 850.0, 1013.25, 1100.0, 2000.0];
    let humidities = [0.0, 0.5, 1.0, 50.0, 99.0, 100.0];
    let (mut refused, mut accepted) = (0, 0);
    for temp_c in temps_c {
        for pressure_hpa in pressures_hpa {
            for humidity in humidities {
                let x_v = water_vapor_mole_fraction_uncapped(temp_c, pressure_hpa, humidity);
                let service_refuses = x_v.is_nan() || x_v >= 1.0;
                match StationAir::new(0.0, temp_c, pressure_hpa, rh(humidity)) {
                    Ok(_) => {
                        assert!(
                            !service_refuses,
                            "accepted impossible air: {temp_c} C {pressure_hpa} hPa {humidity}%"
                        );
                        accepted += 1;
                    }
                    Err(error) => {
                        assert!(
                            service_refuses,
                            "refused possible air: {temp_c} C {pressure_hpa} hPa {humidity}%: \
                             {error}"
                        );
                        assert_eq!(error.reason(), AtmosphereErrorReason::ImpossibleAir);
                        // Above water's critical point the vapour formula itself is undefined
                        // (NaN at any RH), which is the temperature's fault, not the humidity's.
                        let field = if x_v.is_nan() {
                            "temperature_c"
                        } else {
                            "humidity_percent"
                        };
                        assert_eq!(error.field(), field, "{temp_c} C {pressure_hpa} hPa");
                        refused += 1;
                    }
                }
            }
        }
    }
    assert!(
        refused > 20 && accepted > 20,
        "the grid crosses the boundary"
    );
}

#[test]
fn with_density_ratio_keeps_the_callers_ratio_verbatim() {
    for ratio in [1e-9, 0.5, 0.95, 1.0, 1.25, 1.999_999] {
        for humidity in [0.0, 37.5, 100.0] {
            let station = StationAir::with_density_ratio(250.0, 21.0, 980.0, ratio, rh(humidity))
                .expect("valid ratio");
            assert_eq!(station.density_ratio().to_bits(), ratio.to_bits());
            assert_eq!(station.density_kg_m3().to_bits(), (ratio * 1.225).to_bits());
            // The station sound speed is the RH-carrying one, the same as `new` would give.
            let reference = StationAir::new(250.0, 21.0, 980.0, rh(humidity)).unwrap();
            assert_eq!(
                station.speed_of_sound_mps().to_bits(),
                reference.speed_of_sound_mps().to_bits()
            );
            assert_eq!(station.humidity(), rh(humidity));
            assert_eq!(
                bits(&[
                    station.altitude_m(),
                    station.temperature_c(),
                    station.pressure_hpa()
                ]),
                bits(&[250.0, 21.0, 980.0])
            );
        }
    }
}

#[test]
fn with_density_ratio_refuses_ratios_outside_zero_to_two() {
    for ratio in [
        0.0,
        -0.0,
        -1.0,
        2.0,
        2.5,
        f64::NAN,
        f64::INFINITY,
        f64::NEG_INFINITY,
    ] {
        let error =
            StationAir::with_density_ratio(0.0, 15.0, 1013.25, ratio, RelativeHumidity::DRY)
                .expect_err("bad ratio");
        assert_eq!(error.field(), "density_ratio", "{ratio}");
        let reason = if ratio.is_finite() {
            AtmosphereErrorReason::DensityRatioOutOfRange
        } else {
            AtmosphereErrorReason::NotFinite
        };
        assert_eq!(error.reason(), reason, "{ratio}");
    }
}

#[test]
fn the_solver_station_is_station_air_new_and_refuses_what_0_49_clamped() {
    // TrajectorySolver resolves its station through StationAir::new, the interim
    // crate-private `legacy_clamped` constructor is gone. A valid station is still exactly
    // calculate_atmosphere at the given humidity, to the bit; the inputs 0.49.0 flew clamped or
    // capped (an RH beyond the sensor band around [0, 100], impossible air, a pressure of 0) are
    // refused instead.
    for (altitude_m, temp_c, pressure_hpa, humidity) in [
        (0.0, 15.0, 1013.25, 50.0),
        (1_500.0, 25.0, 830.0, 0.0),
        (0.0, 30.0, 1013.25, 100.0),
    ] {
        let station = StationAir::new(altitude_m, temp_c, pressure_hpa, rh(humidity))
            .expect("a valid station");
        let (density, sound) =
            calculate_atmosphere(altitude_m, Some(temp_c), Some(pressure_hpa), humidity);
        assert_eq!(
            bits(&[
                station.density_kg_m3(),
                station.speed_of_sound_mps(),
                station.density_ratio()
            ]),
            bits(&[density, sound, density / 1.225]),
            "{temp_c} C {pressure_hpa} hPa {humidity}%"
        );
        assert_eq!(station.humidity().percent().to_bits(), humidity.to_bits());
    }
    for humidity in [150.0, -5.000_000_1, f64::NAN] {
        let error = RelativeHumidity::try_percent(humidity).expect_err("not a humidity");
        assert_eq!(error.field(), "humidity_percent", "{humidity}");
    }
    let error = StationAir::new(0.0, 100.0, 1013.25, rh(100.0)).expect_err("impossible air");
    assert_eq!(error.reason(), AtmosphereErrorReason::ImpossibleAir);
    let error = StationAir::new(0.0, 15.0, 0.0, rh(50.0)).expect_err("no pressure");
    assert_eq!(error.reason(), AtmosphereErrorReason::NonPositivePressure);
}

// =================================================================================================
// KernelAtmosphere
// =================================================================================================

#[test]
fn uniform_accepts_todays_direct_mode_range() {
    for (density, sound) in [(1e-9, 200.000_001), (1.225, 340.294), (1.999_999, 1_500.0)] {
        match KernelAtmosphere::uniform(density, sound).expect("valid direct air") {
            KernelAtmosphere::Uniform {
                density_kg_m3,
                speed_of_sound_mps,
            } => {
                assert_eq!(density_kg_m3.to_bits(), density.to_bits());
                assert_eq!(speed_of_sound_mps.to_bits(), sound.to_bits());
            }
            other => panic!("expected Uniform, got {other:?}"),
        }
    }
}

#[test]
fn uniform_rejects_what_the_direct_mode_kernels_reject() {
    // 0 and -0 are a vacuum, which 0.49.0's compute_derivatives flew, so
    // they are accepted (tests/kernel_atmosphere_legacy_tuples.rs pins the vacuum's drag at
    // exactly 0); the smallest negative density is refused, as every negative one is.
    for vacuum in [0.0, -0.0] {
        let atmosphere = KernelAtmosphere::uniform(vacuum, 340.0).expect("a vacuum");
        match atmosphere {
            KernelAtmosphere::Uniform { density_kg_m3, .. } => {
                assert_eq!(density_kg_m3.to_bits(), 0.0f64.to_bits(), "{vacuum}")
            }
            other => panic!("expected Uniform, got {other:?}"),
        }
    }
    let bad_density = [
        -f64::from_bits(1),
        -1.0,
        2.0,
        2.5,
        f64::NAN,
        f64::INFINITY,
        f64::NEG_INFINITY,
    ];
    for density in bad_density {
        let error = KernelAtmosphere::uniform(density, 340.0).expect_err("bad density");
        assert_eq!(error.field(), "density_kg_m3", "{density}");
        let reason = if density.is_finite() {
            AtmosphereErrorReason::UniformDensityOutOfRange
        } else {
            AtmosphereErrorReason::NotFinite
        };
        assert_eq!(error.reason(), reason, "{density}");
    }
    for sound in [200.0, 199.0, 0.0, -340.0, f64::NAN, f64::INFINITY] {
        let error = KernelAtmosphere::uniform(1.2, sound).expect_err("bad sound speed");
        assert_eq!(error.field(), "speed_of_sound_mps", "{sound}");
        let reason = if sound.is_finite() {
            AtmosphereErrorReason::UniformSpeedOfSoundOutOfRange
        } else {
            AtmosphereErrorReason::NotFinite
        };
        assert_eq!(error.reason(), reason, "{sound}");
    }
}

#[test]
fn from_legacy_params_maps_the_direct_sentinel_to_uniform() {
    let atmosphere =
        KernelAtmosphere::from_legacy_params((1.1, 330.0, 0.0, 0.0), RelativeHumidity::DEFAULT)
            .expect("direct sentinel");
    assert_eq!(atmosphere, KernelAtmosphere::uniform(1.1, 330.0).unwrap());
    // -0.0 sentinels compare equal to 0.0, as the kernels' `== 0.0` tests do.
    let atmosphere =
        KernelAtmosphere::from_legacy_params((1.1, 330.0, -0.0, -0.0), RelativeHumidity::DRY)
            .expect("direct sentinel");
    assert_eq!(atmosphere, KernelAtmosphere::uniform(1.1, 330.0).unwrap());
    // A density of exactly 0 is the vacuum 0.49.0's compute_derivatives flew,
    // so it converts; the nearest negative density does not.
    assert_eq!(
        KernelAtmosphere::from_legacy_params((0.0, 340.0, 0.0, 0.0), RelativeHumidity::DRY)
            .expect("the vacuum sentinel"),
        KernelAtmosphere::uniform(0.0, 340.0).unwrap()
    );
    // A sentinel with out-of-range direct values is refused, not reinterpreted.
    for params in [
        (2.5, 340.0, 0.0, 0.0),
        (1.2, 150.0, 0.0, 0.0),
        (-f64::from_bits(1), 340.0, 0.0, 0.0),
    ] {
        assert!(
            KernelAtmosphere::from_legacy_params(params, RelativeHumidity::DRY).is_err(),
            "{params:?}"
        );
    }
}

#[test]
fn from_legacy_params_maps_standard_mode_to_a_station_with_the_ratio_verbatim() {
    let humidity = rh(37.5);
    let atmosphere = KernelAtmosphere::from_legacy_params((1_500.0, 25.0, 830.0, 0.93), humidity)
        .expect("standard mode");
    assert_eq!(
        atmosphere,
        KernelAtmosphere::Station(
            StationAir::with_density_ratio(1_500.0, 25.0, 830.0, 0.93, humidity).unwrap()
        )
    );
    // A nonpositive ratio means "not supplied" and falls back to 1.0 (MBA-1157).
    for missing in [0.0, -0.0, -1.0, -1e300] {
        let KernelAtmosphere::Station(station) =
            KernelAtmosphere::from_legacy_params((0.0, 15.0, 1013.25, missing), humidity)
                .expect("missing ratio")
        else {
            panic!("standard mode must give a station");
        };
        assert_eq!(
            station.density_ratio().to_bits(),
            1.0_f64.to_bits(),
            "{missing}"
        );
    }
    // A supplied ratio at or above 2 is refused, as fast_integrate refuses it today.
    for ratio in [2.0, 3.0, f64::NAN, f64::INFINITY] {
        let error = KernelAtmosphere::from_legacy_params((0.0, 15.0, 1013.25, ratio), humidity)
            .expect_err("overlarge ratio");
        assert_eq!(error.field(), "density_ratio", "{ratio}");
    }
    // A zero station pressure that is not the direct sentinel is refused.
    let error = KernelAtmosphere::from_legacy_params((0.0, 15.0, 0.0, 1.0), humidity)
        .expect_err("zero pressure");
    assert_eq!(error.field(), "pressure_hpa");
    // Impossible air is refused through the station constructor.
    let error = KernelAtmosphere::from_legacy_params((0.0, 100.0, 1013.25, 0.6), rh(100.0))
        .expect_err("steam");
    assert_eq!(error.reason(), AtmosphereErrorReason::ImpossibleAir);
}

// =================================================================================================
// air_at
// =================================================================================================

#[test]
fn air_at_uniform_returns_the_callers_air_everywhere_and_ignores_zones() {
    let atmosphere = KernelAtmosphere::uniform(1.1, 330.0).unwrap();
    let zones = AtmoSock::new(vec![
        (35.0, 900.0, 90.0, 100.0),
        (-5.0, 1050.0, 10.0, 500.0),
    ]);
    for zone in [None, Some(&zones)] {
        for downrange_m in [0.0, 99.0, 500.0, 2_000.0] {
            for altitude_m in [-200.0, 0.0, 1_000.0, 12_000.0] {
                let air = air_at(&atmosphere, zone, downrange_m, altitude_m);
                assert_eq!(air.density_kg_m3.to_bits(), 1.1_f64.to_bits());
                assert_eq!(air.speed_of_sound_mps.to_bits(), 330.0_f64.to_bits());
                assert_eq!(
                    air.temperature_k.to_bits(),
                    (330.0 * 330.0 / (GAMMA * R_AIR)).to_bits()
                );
                assert_eq!(air.pressure_pa.to_bits(), 0.0_f64.to_bits());
            }
        }
    }
}

#[test]
fn air_at_station_is_the_legacy_humid_lookup_bit_for_bit() {
    for base_alt in [0.0, 1_500.0] {
        for temp_c in [-10.0, 15.0, 35.0] {
            for pressure_hpa in [850.0, 1013.25] {
                for humidity in [0.0, 50.0, 100.0] {
                    let station = StationAir::new(base_alt, temp_c, pressure_hpa, rh(humidity))
                        .expect("possible air");
                    let atmosphere = KernelAtmosphere::Station(station);
                    for offset in ALT_OFFSETS_M {
                        let altitude_m = base_alt + offset;
                        let (density, sound) = get_local_atmosphere_humid(
                            altitude_m,
                            base_alt,
                            temp_c,
                            pressure_hpa,
                            station.density_ratio(),
                            humidity,
                        );
                        // The lapse at the station's own vapour mole fraction, held aloft
                        // (the virtual-temperature lapse; dry at 0 %).
                        let (temp_k, pressure_pa, density_again) =
                            local_temp_pressure_density_with_gravity(
                                altitude_m,
                                base_alt,
                                temp_c,
                                pressure_hpa,
                                station.density_ratio(),
                                governing_vapor_mole_fraction(temp_c, pressure_hpa, humidity),
                                Gravity::EARTH,
                            );
                        assert_eq!(density.to_bits(), density_again.to_bits());
                        for downrange_m in [0.0, 99.0, 500.0] {
                            let air = air_at(&atmosphere, None, downrange_m, altitude_m);
                            assert_eq!(
                                local_bits(air),
                                [
                                    density.to_bits(),
                                    sound.to_bits(),
                                    temp_k.to_bits(),
                                    pressure_pa.to_bits()
                                ],
                                "{base_alt} m {temp_c} C {pressure_hpa} hPa {humidity}% \
                                 +{offset} m"
                            );
                        }
                    }
                }
            }
        }
    }
}

#[test]
fn air_at_with_gravity_matches_the_legacy_gravity_aware_lookup() {
    let moon = Gravity::new(1.62).unwrap();
    let zero = Gravity::new(0.0).unwrap();
    let station = StationAir::new(300.0, 20.0, 950.0, rh(62.5)).unwrap();
    let atmosphere = KernelAtmosphere::Station(station);
    for gravity in [Gravity::EARTH, moon, zero] {
        for offset in ALT_OFFSETS_M {
            let altitude_m = 300.0 + offset;
            let (density, sound) = get_local_atmosphere_humid_with_gravity(
                altitude_m,
                300.0,
                20.0,
                950.0,
                station.density_ratio(),
                62.5,
                gravity,
            );
            let air = air_at_with_gravity(&atmosphere, None, 0.0, altitude_m, gravity);
            assert_eq!(
                [
                    air.density_kg_m3.to_bits(),
                    air.speed_of_sound_mps.to_bits()
                ],
                [density.to_bits(), sound.to_bits()],
                "g {} +{offset} m",
                gravity.magnitude()
            );
        }
    }
    // The public entry is the Earth-gravity case.
    for offset in ALT_OFFSETS_M {
        assert_eq!(
            local_bits(air_at(&atmosphere, None, 10.0, 300.0 + offset)),
            local_bits(air_at_with_gravity(
                &atmosphere,
                None,
                10.0,
                300.0 + offset,
                Gravity::EARTH
            ))
        );
    }
}

/// 0.49.0's `TrajectorySolver::calculate_acceleration` air block, verbatim but for `self`:
/// the zone (or station) base, then `get_local_atmosphere_humid_with_gravity`.
#[allow(clippy::too_many_arguments)]
fn trajectory_solver_air_0_49(
    atmo_sock: Option<&AtmoSock>,
    position_x: f64,
    local_alt: f64,
    station_altitude: f64,
    base_temp_c: f64,
    base_press_hpa: f64,
    station_ratio: f64,
    station_humidity: f64,
    gravity: Gravity,
) -> (f64, f64) {
    let (drag_base_temp_c, drag_base_press_hpa, drag_base_ratio, drag_humidity_percent) =
        if let Some(sock) = atmo_sock {
            let (zone_temp_c, zone_press_hpa, zone_humidity) = sock.atmo_for_range(position_x);
            let zone_base_ratio =
                calculate_air_density_cimp(zone_temp_c, zone_press_hpa, zone_humidity) / 1.225;
            (zone_temp_c, zone_press_hpa, zone_base_ratio, zone_humidity)
        } else {
            (base_temp_c, base_press_hpa, station_ratio, station_humidity)
        };
    get_local_atmosphere_humid_with_gravity(
        local_alt,
        station_altitude,
        drag_base_temp_c,
        drag_base_press_hpa,
        drag_base_ratio,
        drag_humidity_percent,
        gravity,
    )
}

#[test]
fn air_at_reproduces_the_0_49_trajectory_solver_air_with_and_without_zones() {
    // Zone RH differs from the station's, the zones cross the downrange grid, and one zone
    // carries an out-of-range RH that the internal guards still clamp until AtmoSock::validate
    // exists.
    let zones = AtmoSock::new(vec![
        (20.0, 1000.0, 20.0, 100.0),
        (30.0, 990.0, 80.0, 450.0),
        (5.0, 1020.0, 120.0, 800.0),
    ]);
    let moon = Gravity::new(1.62).unwrap();
    for base_alt in [0.0, 1_500.0] {
        for temp_c in [-10.0, 15.0, 35.0] {
            for pressure_hpa in [850.0, 1013.25] {
                for humidity in [0.0, 50.0, 100.0] {
                    let station = StationAir::new(base_alt, temp_c, pressure_hpa, rh(humidity))
                        .expect("possible air");
                    let atmosphere = KernelAtmosphere::Station(station);
                    for sock in [None, Some(&zones)] {
                        for gravity in [Gravity::EARTH, moon] {
                            for downrange_m in [0.0, 99.0, 100.0, 500.0, 2_000.0] {
                                for offset in [0.0, 50.0, 1_000.0, -200.0] {
                                    let altitude_m = base_alt + offset;
                                    let (density, sound) = trajectory_solver_air_0_49(
                                        sock,
                                        downrange_m,
                                        altitude_m,
                                        base_alt,
                                        temp_c,
                                        pressure_hpa,
                                        station.density_ratio(),
                                        humidity,
                                        gravity,
                                    );
                                    let air = air_at_with_gravity(
                                        &atmosphere,
                                        sock,
                                        downrange_m,
                                        altitude_m,
                                        gravity,
                                    );
                                    assert_eq!(
                                        [
                                            air.density_kg_m3.to_bits(),
                                            air.speed_of_sound_mps.to_bits()
                                        ],
                                        [density.to_bits(), sound.to_bits()],
                                        "{base_alt} m {temp_c} C {pressure_hpa} hPa \
                                         {humidity}% zones={} x={downrange_m} +{offset} m",
                                        sock.is_some()
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

#[test]
fn air_at_zone_base_is_the_cipm_ratio_at_the_zone_to_the_bit() {
    // A dense sweep of single-zone socks, so a last-bit change in the zone density ratio (a
    // reordered division, a different standard-density constant) cannot hide behind a lucky
    // handful of zones. Compared against the 0.49.0 solver block, which divides the CIPM
    // density by 1.225.
    let station = StationAir::new(0.0, 15.0, 1013.25, RelativeHumidity::DEFAULT).unwrap();
    let atmosphere = KernelAtmosphere::Station(station);
    let mut checked = 0usize;
    for t_step in 0..=10 {
        let zone_temp_c = -10.0 + 5.0 * f64::from(t_step);
        for p_step in 0..=8 {
            let zone_press_hpa = 850.0 + 25.0 * f64::from(p_step);
            for h_step in 0..=8 {
                let zone_humidity = 12.5 * f64::from(h_step);
                let zones = AtmoSock::new(vec![(zone_temp_c, zone_press_hpa, zone_humidity, 1e9)]);
                for offset in [0.0, 120.0, -60.0] {
                    let (density, sound) = trajectory_solver_air_0_49(
                        Some(&zones),
                        250.0,
                        offset,
                        0.0,
                        15.0,
                        1013.25,
                        station.density_ratio(),
                        50.0,
                        Gravity::EARTH,
                    );
                    let air = air_at(&atmosphere, Some(&zones), 250.0, offset);
                    assert_eq!(
                        [
                            air.density_kg_m3.to_bits(),
                            air.speed_of_sound_mps.to_bits()
                        ],
                        [density.to_bits(), sound.to_bits()],
                        "zone {zone_temp_c} C {zone_press_hpa} hPa {zone_humidity}% +{offset} m"
                    );
                    checked += 1;
                }
            }
        }
    }
    assert_eq!(checked, 11 * 9 * 9 * 3);
}

#[test]
fn air_at_selects_the_zone_base_by_downrange_and_lapses_from_the_station_altitude() {
    let station = StationAir::new(500.0, 10.0, 950.0, rh(30.0)).unwrap();
    let atmosphere = KernelAtmosphere::Station(station);
    let zones = AtmoSock::new(vec![(25.0, 940.0, 70.0, 300.0), (0.0, 960.0, 5.0, 900.0)]);
    for (downrange_m, (zt, zp, zh)) in [
        (0.0, (25.0, 940.0, 70.0)),
        (299.0, (25.0, 940.0, 70.0)),
        (300.0, (0.0, 960.0, 5.0)),
        (5_000.0, (0.0, 960.0, 5.0)),
    ] {
        let ratio = calculate_air_density_cimp(zt, zp, zh) / 1.225;
        for altitude_m in [500.0, 800.0, 300.0] {
            let (density, sound) = get_local_atmosphere_humid(altitude_m, 500.0, zt, zp, ratio, zh);
            let air = air_at(&atmosphere, Some(&zones), downrange_m, altitude_m);
            assert_eq!(
                [
                    air.density_kg_m3.to_bits(),
                    air.speed_of_sound_mps.to_bits()
                ],
                [density.to_bits(), sound.to_bits()],
                "x={downrange_m} alt={altitude_m}"
            );
        }
    }
}

#[test]
fn an_empty_zone_set_means_the_station() {
    let empty = AtmoSock::new(Vec::new());
    for humidity in [0.0, 50.0, 100.0] {
        let station = StationAir::new(1_500.0, 25.0, 830.0, rh(humidity)).unwrap();
        let atmosphere = KernelAtmosphere::Station(station);
        for downrange_m in [0.0, 500.0] {
            for altitude_m in [1_500.0, 2_500.0, 1_300.0] {
                assert_eq!(
                    local_bits(air_at(&atmosphere, Some(&empty), downrange_m, altitude_m)),
                    local_bits(air_at(&atmosphere, None, downrange_m, altitude_m)),
                    "{humidity}% x={downrange_m} alt={altitude_m}"
                );
            }
        }
    }
}

#[test]
fn air_at_flies_the_station_humidity_into_the_sound_speed() {
    // Density is held by the station ratio; the RH reaches the sound speed through the one
    // vapour mole fraction, so a humid station is faster than a dry one at the same state.
    let dry =
        StationAir::with_density_ratio(0.0, 30.0, 1013.25, 1.0, RelativeHumidity::DRY).unwrap();
    let humid = StationAir::with_density_ratio(0.0, 30.0, 1013.25, 1.0, rh(100.0)).unwrap();
    let dry_air = air_at(&KernelAtmosphere::Station(dry), None, 0.0, 0.0);
    let humid_air = air_at(&KernelAtmosphere::Station(humid), None, 0.0, 0.0);
    assert_eq!(
        dry_air.density_kg_m3.to_bits(),
        humid_air.density_kg_m3.to_bits()
    );
    assert!(humid_air.speed_of_sound_mps > dry_air.speed_of_sound_mps * 1.004);
    assert_eq!(
        dry_air.speed_of_sound_mps.to_bits(),
        (GAMMA * R_AIR * dry_air.temperature_k).sqrt().to_bits()
    );
    // At the station altitude the local sound speed is the station's own.
    assert_eq!(
        humid_air.speed_of_sound_mps.to_bits(),
        humid.speed_of_sound_mps().to_bits()
    );
}

#[test]
fn atmo_sock_validate_accepts_valid_and_empty_zone_sets() {
    assert!(AtmoSock::new(Vec::new()).validate().is_ok());
    assert!(AtmoSock::new(vec![
        (15.0, 1013.25, 0.0, 300.0),
        (-5.0, 900.0, 100.0, 1_000.0),
        (35.0, 1013.25, 50.0, f64::INFINITY),
    ])
    .validate()
    .is_ok());
}

#[test]
fn atmo_sock_validate_refuses_each_bad_zone_field_by_name_without_clamping() {
    use AtmosphereErrorReason as Reason;
    let cases: [(AtmoSegment, &str, Reason); 12] = [
        (
            (15.0, 1013.25, 150.0, 300.0),
            "humidity_percent",
            Reason::HumidityPercentOutOfRange,
        ),
        (
            (15.0, 1013.25, 105.000_000_1, 300.0),
            "humidity_percent",
            Reason::HumidityPercentOutOfRange,
        ),
        (
            (15.0, 1013.25, -5.000_000_1, 300.0),
            "humidity_percent",
            Reason::HumidityPercentOutOfRange,
        ),
        (
            (15.0, 1013.25, f64::NAN, 300.0),
            "humidity_percent",
            Reason::NotFinite,
        ),
        (
            (-273.15, 1013.25, 50.0, 300.0),
            "temperature_c",
            Reason::AtOrBelowAbsoluteZero,
        ),
        (
            (f64::NAN, 1013.25, 50.0, 300.0),
            "temperature_c",
            Reason::NotFinite,
        ),
        (
            (15.0, 0.0, 50.0, 300.0),
            "pressure_hpa",
            Reason::NonPositivePressure,
        ),
        (
            (15.0, f64::INFINITY, 50.0, 300.0),
            "pressure_hpa",
            Reason::NotFinite,
        ),
        // The density-altitude service's impossible air: saturated at 100 C and 1013.25 hPa.
        (
            (100.0, 1013.25, 100.0, 300.0),
            "humidity_percent",
            Reason::ImpossibleAir,
        ),
        (
            (15.0, 1013.25, 50.0, 0.0),
            "until_distance_m",
            Reason::NonPositiveZoneThreshold,
        ),
        (
            (15.0, 1013.25, 50.0, -5.0),
            "until_distance_m",
            Reason::NonPositiveZoneThreshold,
        ),
        (
            (15.0, 1013.25, 50.0, f64::NAN),
            "until_distance_m",
            Reason::NonPositiveZoneThreshold,
        ),
    ];
    for (zone, field, reason) in cases {
        let error = AtmoSock::new(vec![zone]).validate().unwrap_err();
        assert_eq!(
            error.field(),
            format!("atmo_sock.zones[0].{field}"),
            "{zone:?}"
        );
        assert_eq!(error.reason(), reason, "{zone:?}");
        assert!(
            error.value().to_bits() == zone.0.to_bits()
                || error.value().to_bits() == zone.1.to_bits()
                || error.value().to_bits() == zone.2.to_bits()
                || error.value().to_bits() == zone.3.to_bits(),
            "the refused value is reported unchanged: {error}"
        );
    }
    // The index is the zone's place in threshold order: a valid near zone comes first.
    let error = AtmoSock::new(vec![
        (15.0, 1013.25, 150.0, 900.0),
        (15.0, 1013.25, 50.0, 300.0),
    ])
    .validate()
    .unwrap_err();
    assert_eq!(error.field(), "atmo_sock.zones[1].humidity_percent");
    assert_eq!(
        error.to_string(),
        "atmo_sock.zones[1].humidity_percent = 150: relative humidity must be a percent from 0 \
         to 100 (a sensor reading from -5 to 105 is read as 0 or 100)"
    );
}

#[test]
fn an_empty_atmo_sock_lookup_falls_back_at_the_one_default_humidity() {
    // air_at never asks an empty sock (it means the station); the raw lookup's own fallback is
    // the engine's default humidity, not a 0 % of its own.
    let (_, _, humidity) = AtmoSock::new(Vec::new()).atmo_for_range(250.0);
    assert_eq!(humidity.to_bits(), DEFAULT_HUMIDITY_PERCENT.to_bits());
}

#[test]
#[allow(deprecated)] // the dry legacy lookup is what is under test
fn the_deprecated_dry_lookup_is_the_shared_composition_at_zero_humidity() {
    // One dry constant: get_local_atmosphere is get_local_atmosphere_humid at 0 %, to the bit,
    // over heights below, at and far above the base, so a dry path can only differ from a humid
    // one by its humidity.
    for (base_alt, base_t, base_p, ratio) in [
        (0.0, 15.0, 1013.25, 1.0),
        (1_500.0, 25.0, 830.0, 0.79),
        (500.0, -10.0, 950.0, 1.05),
    ] {
        for offset in ALT_OFFSETS_M {
            let altitude = base_alt + offset;
            let dry = get_local_atmosphere(altitude, base_alt, base_t, base_p, ratio);
            let humid0 = get_local_atmosphere_humid(altitude, base_alt, base_t, base_p, ratio, 0.0);
            assert_eq!(
                [dry.0.to_bits(), dry.1.to_bits()],
                [humid0.0.to_bits(), humid0.1.to_bits()],
                "{base_alt} m {base_t} C {base_p} hPa, query {altitude} m"
            );
        }
    }
}

#[test]
fn a_terminal_humidity_flag_parses_through_the_one_validator() {
    // The WASM terminal's parser for every humidity flag: a percent in [0, 100], a
    // sensor reading within 5 points of it read as the end, anything farther out refused
    // (never clamped), with the flag named in the error.
    for (raw, percent) in [("0", 0.0), ("100", 100.0), ("37.5", 37.5), (" 50 ", 50.0)] {
        assert_eq!(
            parse_humidity_percent_arg("--humidity", raw)
                .unwrap()
                .to_bits(),
            f64::to_bits(percent),
            "{raw}"
        );
    }
    // -0.0 is dry air with positive-zero bits, as everywhere else.
    assert_eq!(
        parse_humidity_percent_arg("--humidity", "-0")
            .unwrap()
            .to_bits(),
        0.0f64.to_bits()
    );
    // The refusal states the band it enforces, not a bare 0-100 a 103 contradicts.
    assert_eq!(
        parse_humidity_percent_arg("--humidity", "150").unwrap_err(),
        "--humidity = 150: relative humidity must be a percent from 0 to 100 (a sensor reading \
         from -5 to 105 is read as 0 or 100)"
    );
    assert_eq!(
        parse_humidity_percent_arg("--zero-humidity", "-5.0000001").unwrap_err(),
        "--zero-humidity = -5.0000001: relative humidity must be a percent from 0 to 100 (a \
         sensor reading from -5 to 105 is read as 0 or 100)"
    );
    // A sensor reading within 5 points of the range is the end it reads as, to the bit.
    for (raw, end) in [
        ("100.0001", 100.0),
        ("101.7", 100.0),
        ("105", 100.0),
        ("-0.0001", 0.0),
        ("-2", 0.0),
        ("-5", 0.0),
    ] {
        assert_eq!(
            parse_humidity_percent_arg("--humidity", raw)
                .unwrap_or_else(|e| panic!("{raw}: {e}"))
                .to_bits(),
            f64::to_bits(end),
            "{raw}"
        );
    }
    const BAND: &str =
        "percent from 0 to 100 (a sensor reading from -5 to 105 is read as 0 or 100)";
    for raw in ["105.0000001", "-5.0000001", "1e3"] {
        let error = parse_humidity_percent_arg("--humidity", raw).unwrap_err();
        assert!(
            error.starts_with("--humidity = ")
                && error.ends_with(&format!("relative humidity must be a {BAND}")),
            "{raw}: {error}"
        );
    }
    // A NaN or an infinity is told what it had to be, not a bare "must be finite".
    for raw in ["nan", "inf", "-inf"] {
        let error = parse_humidity_percent_arg("--humidity", raw).unwrap_err();
        assert!(
            error.ends_with(&format!("relative humidity must be a finite {BAND}")),
            "{raw}: {error}"
        );
    }
    let error = parse_humidity_percent_arg("--humidity", "fifty").unwrap_err();
    assert_eq!(
        error,
        format!("--humidity = fifty: not a number; relative humidity is a {BAND}")
    );
}

// =================================================================================================
// Which reading impossible air is blamed on
// =================================================================================================

/// Where a station pressure came from, from what a presence-aware resolution is handed: a
/// supplied reading, a sea-level default kept at a station within a metre of sea level, and
/// a non-default value under the legacy rule are readings; a default the rule replaces with the
/// standard pressure at the altitude is the standard's; a QNH is a QNH when it is itself a
/// troposphere pressure and a reading (hPa sent as pascals) when not.
#[test]
fn a_station_pressure_origin_names_the_reading_that_set_it() {
    use StationPressureOrigin::{Qnh, Reading, Standard};
    let absolute = PressureReferenceMode::Absolute;
    let qnh = PressureReferenceMode::Qnh;
    let both = StationAtmospherePresence::BOTH;
    let neither = StationAtmospherePresence::NEITHER;
    for (pressure_hpa, altitude_m, mode, presence, origin) in [
        (1013.25, 3000.0, absolute, both, Reading),
        (1013.25, 3000.0, absolute, neither, Standard),
        (1013.25, 0.5, absolute, neither, Reading),
        (850.0, 3000.0, absolute, neither, Reading),
        (1013.25, 20_000.0, qnh, both, Qnh),
        (1013.25, 20_000.0, qnh, neither, Qnh),
        (10.1325, 1500.0, qnh, both, Reading),
    ] {
        assert_eq!(
            StationPressureOrigin::resolving(pressure_hpa, altitude_m, mode, presence),
            origin,
            "{pressure_hpa} hPa at {altitude_m} m, {mode:?}, {presence:?}"
        );
    }
    assert_eq!(StationPressureOrigin::of(None, absolute), Standard);
    assert_eq!(StationPressureOrigin::of(None, qnh), Standard);
}

/// The one blame order for air that cannot exist, the order `atmosphere.density_altitude`'s
/// checks run in: the temperature outside 173.15-373.15 K (in kelvin, to the bit, edges
/// inside), then the reading that took the pressure out of the troposphere, then the humidity.
#[test]
fn impossible_air_is_blamed_on_one_reading_in_one_order() {
    let blamed = |temperature_c: f64, pressure_hpa: f64, origin: StationPressureOrigin| {
        let humidity = RelativeHumidity::try_percent(100.0).unwrap();
        let error = StationAir::new(0.0, temperature_c, pressure_hpa, humidity)
            .expect_err("this air cannot exist");
        let spelling = AirSpelling {
            humidity: AirField::named("h"),
            temperature: AirField::named("t"),
            pressure: AirField::named("p"),
            altitude: AirField::named("a"),
            humidity_sent: 100.0,
            temperature_text: format!("{temperature_c} °C"),
            temperature_unit: crate::atmosphere::TemperatureUnit::Celsius,
            temperature_origin: crate::atmosphere::StationTemperatureOrigin::Reading,
            pressure_text: format!("{pressure_hpa} hPa"),
            pressure_origin: origin,
            qnh_text: None,
            altitude_unit: AltitudeUnit::Meters,
        };
        let refusal = error.refusal_in(&spelling, 0.0, temperature_c, pressure_hpa);
        (refusal.path().to_string(), refusal.message().to_string())
    };
    let reading = StationPressureOrigin::Reading;
    // 100 °C is 373.15 K exactly: inside, so saturated air under 1000 hPa is the humidity's.
    assert_eq!(blamed(100.0, 1000.0, reading).0, "h");
    // One float above it in kelvin (the edge is compared in kelvin, where a float is coarser
    // than in °C), and far above, the temperature's.
    let above = f64::from_bits(373.15_f64.to_bits() + 1) - 273.15;
    assert_eq!(blamed(above, 1000.0, reading).0, "t");
    assert_eq!(blamed(107.0, 1013.25, reading).0, "t");
    assert_eq!(blamed(400.0, 1013.25, reading).0, "t");
    // Below -100 °C only a near-vacuum is impossible: still the temperature's first.
    assert_eq!(blamed(-103.15, 1e-6, reading).0, "t");
    // In range, above the troposphere: the pressure's when it was a reading...
    let (path, message) = blamed(40.0, 10.0, reading);
    assert_eq!(path, "p");
    assert!(
        message.contains("; p = 10 hPa is a pressure altitude of "),
        "{message}"
    );
    assert!(
        message.ends_with(": check the pressure's unit"),
        "{message}"
    );
    // ...and the altitude's when it was the standard pressure there or a QNH reduced there.
    let (path, message) = blamed(40.0, 10.0, StationPressureOrigin::Standard);
    assert_eq!(path, "a");
    assert!(
        message.contains("; no p was given, and the standard pressure at the station altitude"),
        "{message}"
    );
    assert!(
        message.ends_with(": check the altitude's unit"),
        "{message}"
    );
    let (path, message) = blamed(40.0, 10.0, StationPressureOrigin::Qnh);
    assert_eq!(path, "a");
    assert!(
        message.contains("; p is a QNH, and reduced to the station altitude"),
        "{message}"
    );
    assert!(
        message.ends_with(": check the altitude's unit"),
        "{message}"
    );
}