-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal_reference.rs
More file actions
2754 lines (2520 loc) · 109 KB
/
Copy pathexternal_reference.rs
File metadata and controls
2754 lines (2520 loc) · 109 KB
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
//! External-reference regression tests.
//!
//! Each test pins one formula-solving function in `celestial-core` to a
//! known published value (or a verified astronomical event). The goal is
//! to catch order-of-magnitude bugs (transcription errors, sign flips,
//! formula inversions) before they reach a release.
//!
//! Reference sources:
//! - Meeus, "Astronomical Algorithms" 2nd ed. (chapter and example noted
//! per test).
//! - SIDC / SILSO (solar cycle observations).
//! - NASA Horizons / JPL ephemerides (planet positions cross-checked).
//! - Verified astrology software output (World of Wisdom, AstroDienst —
//! noted where a third-party software's published chart is the source).
//!
//! Tolerances are deliberately generous (1–10° for planet positions,
//! 5 arcmin for house cusps, full-second for ΔT). The tests are
//! ORDER-OF-MAGNITUDE sanity checks, not micro-precision pins. Specific
//! precision pins live in `integration_tests.rs`.
use celestial_core::body::{Body, CalcFlags, Calendar, HouseSystem, SiderealMode};
use celestial_core::Degrees;
use celestial_core::JulianDay;
use celestial_core::Latitude;
use celestial_core::Longitude;
use celestial_core::{
almuten, annual_profection, antiscion, ayanamsa_ut, azalt, best_time_method, calc, calc_ut,
calendar_round, coord_transform, day_of_week, decan_ruler, deltat, egyptian_terms_ruler,
firdaria, fixstar_mag, four_pillars, full_dignity, haab, hindu_festivals, is_day_chart,
iso_week, julday, long_to_nakshatra, long_to_navamsa, long_to_rasi, lunar_return_jd,
maya_long_count, mean_sidereal_time_deg, midpoint_deg, next_first_quarter, next_new_moon,
nutation, panchanga, same_sect, secondary_progressions, set_sid_mode, sidereal_time_deg,
sol_eclipse_when_glob, solar_arc_directions, solar_return_jd, solcross_ut, time_equ,
tonalpohualli, triplicity_rulers, true_obliquity, tzolkin, vietnamese_month_start_jd,
vimshottari_dasha, yallop_q, Dignity,
};
#[cfg(feature = "calendar-traditions")]
use celestial_core::{
christian_feasts, coptic_to_jd, days_in_hebrew_year, easter_gregorian, easter_jd,
esbats_for_year, fasli_nowruz_jd, hebrew_new_year_jd, hijri_from_jd, hijri_month_days,
is_coptic_leap_year, jd_to_coptic, jewish_holidays, losar_jd, naw_ruz_jd, nowruz_jd,
sabbats_for_year, tibetan_year_name, vesak_jd,
};
const FLG: CalcFlags = CalcFlags::BUILTIN;
/// Macro: assert two longitudes are within `tol_deg` degrees, modulo 360°.
macro_rules! assert_lon_within {
($got:expr, $expected:expr, $tol:expr, $label:expr) => {
let got = $got;
let expected: f64 = $expected;
let diff = ((got - expected + 540.0) % 360.0 - 180.0).abs();
assert!(
diff < $tol,
"{}: got {:.4}°, expected {:.4}° (diff {:.4}°, tol {}°)",
$label,
got,
expected,
diff,
$tol,
);
};
}
// ─── VSOP87 + ELP2000: planets at multiple dates ─────────────────────────────
/// Princess Diana, 1961-07-01 18:45 UT (well-published nativity chart).
/// All seven classical planets match independently published values within
/// the engine's current accuracy band.
#[test]
fn diana_chart_planet_positions() {
let jd = julday(1961, 7, 1, 18.75, Calendar::Gregorian);
let cases: &[(Body, f64, f64)] = &[
// body expected_lon_deg tol_deg
(Body::SUN, 99.67, 0.05),
(Body::MOON, 325.03, 0.05),
(Body::MERCURY, 93.17, 0.1),
(Body::VENUS, 54.40, 0.05),
(Body::MARS, 151.67, 0.1),
(Body::JUPITER, 305.10, 0.5),
(Body::SATURN, 297.80, 0.5), // tightened after VSOP87 L0[2] fix
(Body::URANUS, 145.04, 2.0), // 25°02' Leo per published Diana chart
(Body::NEPTUNE, 218.62, 2.0), // 8°37' Scorpio
];
for &(body, expected, tol) in cases {
let pos = calc_ut(JulianDay::new(jd), body, FLG).unwrap();
assert_lon_within!(pos.lon, expected, tol, format!("Diana {body:?}"));
}
}
/// Geraldo Netto chart, 1986-05-30 09:00 UT (PDF reference from World of
/// Wisdom astrology software, supplied by the user). Cross-checks inner-
/// planet VSOP87 against a second independent chart.
#[test]
fn netto_chart_inner_planets() {
let jd = julday(1986, 5, 30, 9.0, Calendar::Gregorian);
let cases: &[(Body, f64, f64)] = &[
(Body::SUN, 68.667, 0.05),
(Body::MOON, 336.65, 0.1),
(Body::MERCURY, 77.533, 0.05),
(Body::VENUS, 100.533, 0.1),
(Body::MARS, 292.550, 0.1),
];
for &(body, expected, tol) in cases {
let pos = calc_ut(JulianDay::new(jd), body, FLG).unwrap();
assert_lon_within!(pos.lon, expected, tol, format!("Netto {body:?}"));
}
}
/// Sun at J2000.0 noon TT — Meeus AA chapter 25 worked example.
/// Apparent geocentric ecliptic longitude is ≈ 280.4°.
#[test]
fn sun_at_j2000_meeus() {
let pos = calc(JulianDay::new(2_451_545.0), Body::SUN, FLG).unwrap();
assert_lon_within!(pos.lon, 280.4, 0.5, "Sun J2000 TT");
}
/// Chiron geocentric ecliptic longitude across 4 well-separated dates.
/// Tolerance 5° (loose) because the engine uses a simple Kepler
/// propagation of fixed orbital elements; it does not model the
/// gravitational perturbations from Saturn/Uranus that significantly
/// affect Chiron's orbit. Tighten when those perturbations are
/// implemented.
///
/// Two bugs fixed in tandem before this test was added:
/// 1. `calc_chiron` was using heliocentric values as if geocentric
/// (10–40° error at every chart date — needed a vector
/// subtraction from Earth's position).
/// 2. Mean anomaly at J2000 was 48.5° instead of ~27° (Chiron's
/// perihelion was 1996-02-14; at J2000 that's 1383 d post-
/// perihelion ≈ 27° mean anomaly).
#[test]
fn chiron_multi_date_consistency() {
let cases: &[(i32, u32, u32, f64, f64, f64)] = &[
// (year, month, day, hour_ut, expected_lon_deg, tol_deg)
(2000, 1, 1, 12.0, 253.60, 5.0), // J2000: 13°36' Sgr
(1961, 7, 1, 18.75, 336.00, 2.0), // Diana: ~6° Pis
(2024, 1, 1, 0.0, 16.00, 5.0), // 2024: ~16° Ari
];
for &(y, m, d, h, expected, tol) in cases {
let jd = julday(y, m as i32, d as i32, h, Calendar::Gregorian);
let pos = calc_ut(JulianDay::new(jd), Body::CHIRON, FLG).unwrap();
let diff = ((pos.lon - expected + 540.0) % 360.0 - 180.0).abs();
assert!(
diff < tol,
"Chiron {y}-{m:02}-{d:02}: got {:.4}°, expected {expected:.4}° (diff {:.4}°, tol {tol}°)",
pos.lon,
diff,
);
}
}
/// Saturn longitude across 5 well-separated dates spanning 75 years.
/// Cross-checks the VSOP87D Saturn L series against published ephemerides
/// at multiple phases of Saturn's 29.5-year orbit. Currently within 0.6°
/// at every date after the L0 coefficient fixes.
///
/// Catches regressions in Saturn-specific coefficients (the L0[2] /
/// freq 426.6 amplitude that was 30× too large was discovered through
/// exactly this kind of multi-date comparison).
#[test]
fn saturn_multi_date_consistency() {
let cases: &[(i32, u32, u32, f64, f64, f64)] = &[
// (year, month, day, hour_ut, expected_lon_deg, tol_deg)
(1961, 7, 1, 18.75, 297.80, 1.0), // Princess Diana (Capricorn)
(1986, 5, 30, 9.0, 246.23, 1.0), // Geraldo Netto PDF (Sagittarius)
(2000, 1, 1, 12.0, 40.42, 1.0), // J2000.0 (Taurus)
(2024, 1, 1, 0.0, 333.55, 1.0), // 2024 (Pisces)
];
for &(y, m, d, h, expected, tol) in cases {
let jd = julday(y, m as i32, d as i32, h, Calendar::Gregorian);
let pos = calc_ut(JulianDay::new(jd), Body::SATURN, FLG).unwrap();
let diff = ((pos.lon - expected + 540.0) % 360.0 - 180.0).abs();
assert!(
diff < tol,
"Saturn {y}-{m:02}-{d:02}: got {:.4}°, expected {expected:.4}° (diff {:.4}°, tol {tol}°)",
pos.lon,
diff,
);
}
}
/// Saturn moves on average 12.2° per year (360° / 29.46y). Daily
/// motion is ~0.033°/d direct, slowing to retrograde at ~−0.08°/d at
/// opposition. Pin physical bounds at a representative date.
#[test]
fn saturn_physical_motion_bounds() {
// Mid-2024: Saturn is in retrograde mid-year (apparent stationary
// at June 2024). Daily motion in absolute value should be < 0.15°.
let jd = julday(2024, 7, 1, 0.0, Calendar::Gregorian);
let pos = calc_ut(JulianDay::new(jd), Body::SATURN, FLG).unwrap();
assert!(
pos.speed_lon.abs() < 0.15,
"Saturn daily motion {:.4}°/d outside physical bounds (±0.15°/d)",
pos.speed_lon,
);
// Saturn never gets closer than ~8 AU or farther than ~11 AU from Earth.
assert!(
(7.5..=11.5).contains(&pos.dist),
"Saturn distance {:.4} AU outside physical bounds (7.5..11.5 AU)",
pos.dist,
);
}
// ─── Refraction ─────────────────────────────────────────────────────────────
/// Bennett refraction at horizon (altitude = 0°) under standard
/// atmosphere (1010 mb, 10°C) is about 34'10" = 0.5694°.
/// Tolerance 0.1° covers small differences between Bennett truncations.
#[test]
fn refraction_at_horizon() {
let r = celestial_core::refrac(0.0, 1010.0, 10.0, 0);
// `direction == 0` returns altitude + r_corrected (true altitude).
// Convert back to refraction angle: r_corrected = result - altitude = result - 0.
assert!(
(0.4..=0.7).contains(&r),
"Refraction at horizon = {r:.4}°, expected ~0.569° (34'10\")",
);
}
/// Refraction must decrease monotonically with altitude in the 0°-90° band.
#[test]
fn refraction_monotonic_with_altitude() {
let mut prev = f64::INFINITY;
for alt_deg in [0.5_f64, 5.0, 10.0, 20.0, 45.0, 80.0] {
let r = celestial_core::refrac(alt_deg, 1010.0, 10.0, 0) - alt_deg;
assert!(
r < prev,
"Refraction at {alt_deg}° = {r:.4}° not less than previous {prev:.4}°",
);
prev = r;
}
}
// ─── ΔT ──────────────────────────────────────────────────────────────────────
/// ΔT well-known reference values, in seconds:
/// 1900-01-01: -2.79
/// J2000.0: +63.83
/// 2024-01-01: ~+69
#[test]
fn delta_t_reference_values() {
let cases: &[(f64, f64, f64)] = &[
// (JD, expected ΔT seconds, tolerance seconds)
(2_415_021.0, -2.79, 2.0), // 1900-01-01
(2_451_545.0, 63.83, 1.0), // J2000.0
(2_460_311.0, 69.18, 5.0), // 2024-01-01 (rough — extrapolated)
];
for &(jd, expected, tol) in cases {
let dt_days = deltat(JulianDay::new(jd));
let dt_sec = dt_days * 86_400.0;
assert!(
(dt_sec - expected).abs() < tol,
"ΔT at JD {jd}: got {dt_sec:.2}s, expected {expected:.2}s",
);
}
}
// ─── Solar crossings (root finder) ──────────────────────────────────────────
/// Vernal equinox 2024: 2024-03-20 03:06:36 UT = JD 2460389.629…
/// (NASA Horizons published value.)
#[test]
fn vernal_equinox_2024() {
let jd_start = julday(2024, 1, 1, 0.0, Calendar::Gregorian);
let jd =
solcross_ut(Longitude::new(0.0), JulianDay::new(jd_start), FLG).expect("solcross found");
let expected = 2_460_389.629_5;
assert!(
(jd - expected).abs() < 0.01,
"vernal equinox 2024: got {jd:.4}, expected {expected:.4}",
);
}
// ─── Calendars ──────────────────────────────────────────────────────────────
/// Easter Sunday dates from US Naval Observatory / multiple ecclesiastical
/// computi — these are universally agreed historical / liturgical values.
#[cfg(feature = "calendar-traditions")]
#[test]
fn easter_gregorian_published_dates() {
let cases: &[(i32, i32, u8, u8)] = &[
// (year, expected_year, month, day)
(2024, 2024, 3, 31),
(2025, 2025, 4, 20),
(2000, 2000, 4, 23),
(1981, 1981, 4, 19),
(1900, 1900, 4, 15),
];
for &(yr, ey, em, ed) in cases {
let (y, m, d) = easter_gregorian(yr);
assert_eq!(
(y, m, d),
(ey, em, ed),
"Easter {yr}: got {y}-{m:02}-{d:02}, expected {ey}-{em:02}-{ed:02}",
);
}
}
/// Hijri date conversion at known epochs (1 Muharram 1 AH = JD 1948439.5;
/// 1 Muharram 1444 = 2022-07-30 ≈ JD 2459790.5).
#[cfg(feature = "calendar-traditions")]
#[test]
fn hijri_known_dates() {
// 2000-01-01 00:00 UT = JD 2451544.5; should be in Ramadan 1420 (year 1420).
let (y, _m, _d) = hijri_from_jd(JulianDay::new(julday(2000, 1, 1, 0.0, Calendar::Gregorian)));
assert!(
(1419..=1421).contains(&y),
"Hijri year at 2000-01-01 = {y}, expected ≈ 1420",
);
// 2022-07-30 ≈ start of Hijri year 1444.
let (y2, m2, _d2) = hijri_from_jd(JulianDay::new(julday(
2022,
7,
30,
0.0,
Calendar::Gregorian,
)));
assert!(
(1443..=1444).contains(&y2),
"Hijri year at 2022-07-30 = {y2}, expected ≈ 1444",
);
assert!(m2 == 1 || m2 == 12, "Hijri month at 2022-07-30 = {m2}");
}
// ─── ISO 8601 week numbering ─────────────────────────────────────────────────
/// ISO 8601 known anchors:
/// 2012-12-31 → ISO week 1 of 2013 (Monday)
/// 2020-12-31 → ISO week 53 of 2020
/// 2024-01-01 → ISO week 1 of 2024 (Monday)
#[test]
fn iso_week_published_anchors() {
let cases: &[(i32, i32, i32, i32, u32)] = &[
// (greg_y, greg_m, greg_d, iso_y, iso_w)
(2012, 12, 31, 2013, 1),
(2024, 1, 1, 2024, 1),
(2020, 12, 31, 2020, 53),
];
for &(gy, gm, gd, iy_exp, iw_exp) in cases {
let jd = julday(gy, gm, gd, 0.0, Calendar::Gregorian);
let (iy, iw) = iso_week(JulianDay::new(jd));
assert_eq!(
(iy, iw),
(iy_exp, iw_exp),
"ISO week {gy}-{gm:02}-{gd:02}: got ({iy}, {iw}), expected ({iy_exp}, {iw_exp})",
);
}
}
// ─── Maya Long Count ────────────────────────────────────────────────────────
/// 13.0.0.0.0 (end of 13th baktun) — different correlations place this
/// on 2012-12-21, -22, or -23 depending on the day-noon convention.
/// celestial's GMT correlation lands the rollover on 2012-12-22.
#[test]
fn maya_long_count_baktun_13() {
let jd = julday(2012, 12, 22, 0.0, Calendar::Gregorian);
let (b, k, t, u, kin) = maya_long_count(JulianDay::new(jd));
assert_eq!(
(b, k, t, u, kin),
(13, 0, 0, 0, 0),
"2012-12-22 Long Count: got {b}.{k}.{t}.{u}.{kin}",
);
// Sanity: the previous day must be the last kin of baktun 12.
let prev = maya_long_count(JulianDay::new(jd - 1.0));
assert_eq!(prev, (12, 19, 19, 17, 19));
}
// ─── Mesoamerican Tonalpohualli ─────────────────────────────────────────────
/// Tonalpohualli is a 260-day permutation cycle. Output sanity: trecena
/// 1..=13, sign index 0..=19. Per the published GMT correlation, the day
/// JD 584283 (the Long-Count epoch) corresponds to "4 Ahau" (4 in trecena,
/// sign Ahau = index 19).
#[test]
fn tonalpohualli_ranges_and_epoch() {
let (trecena, sign_idx, _, _) = tonalpohualli(JulianDay::new(584_283.0));
assert!(
(1..=13).contains(&trecena) && sign_idx < 20,
"out-of-range at GMT epoch: trecena={trecena}, sign_idx={sign_idx}",
);
// Two arbitrary modern dates: each must return valid ranges.
for &jd in &[2_451_545.0_f64, 2_460_000.0] {
let (t, s, _, _) = tonalpohualli(JulianDay::new(jd));
assert!(
(1..=13).contains(&t) && s < 20,
"Tonalpohualli out of range at JD {jd}: ({t}, {s})",
);
}
}
// ─── Hellenistic dignity ────────────────────────────────────────────────────
/// Classical dignity assignments (Ptolemy / Hellenistic tradition):
/// Sun in Leo (≈ 130°) → Domicile
/// Moon in Cancer (≈ 100°) → Domicile
/// Sun in Aquarius (≈ 310°) → Detriment
/// Saturn in Libra (≈ 190°) → Exaltation
/// Sun in Aries 19° (≈ 19°) → Exaltation
#[test]
fn hellenistic_dignity_canonical_cases() {
let cases: &[(Body, f64, bool, Dignity)] = &[
(Body::SUN, 130.0, true, Dignity::Domicile),
(Body::MOON, 100.0, false, Dignity::Domicile),
(Body::SUN, 310.0, true, Dignity::Detriment),
(Body::SUN, 19.0, true, Dignity::Exaltation),
];
for &(body, lon, is_day, expected) in cases {
let (dig, _score) = full_dignity(body, Longitude::new(lon), is_day);
assert_eq!(
dig, expected,
"Dignity({body:?}, {lon}°, day={is_day}) = {dig:?}, expected {expected:?}",
);
}
}
// ─── Yallop crescent visibility ─────────────────────────────────────────────
/// Yallop's empirical class boundaries (Yallop 1998, NAO TN 69):
/// q ≥ +0.216 → 'A' (easily visible)
/// q ∈ [−0.014, +0.216) → 'B' (visible under perfect conditions)
/// q ∈ [−0.160, −0.014) → 'C'
/// ...
/// q < −0.293 → 'F' (not visible)
#[test]
fn yallop_q_class_boundaries() {
// Large ARCV / ARCL gives easy visibility → A.
let (_q, c) = yallop_q(15.0, 20.0, 15.0);
assert_eq!(c, 'A', "wide separation should be class A, got {c}");
// Near-conjunction (very small ARCV) → never visible → F (or D/E for
// intermediate). Just assert it's not A:
let (_q, c2) = yallop_q(1.0, 2.0, 15.0);
assert_ne!(c2, 'A', "near-conjunction should not be class A");
}
// ─── Hindu Panchanga ────────────────────────────────────────────────────────
/// Panchanga returns five elements: tithi (1..=30), paksha, vara (0..=6),
/// nakshatra (0..=26), yoga and karana. Order-of-magnitude sanity over a
/// few representative JDs — internal consistency only, not against a
/// specific reference.
#[test]
fn panchanga_field_ranges() {
for &jd in &[2_451_545.0_f64, 2_460_000.0, 2_446_580.875] {
let p = panchanga(JulianDay::new(jd));
assert!(
(1..=30).contains(&p.tithi),
"tithi {} out of range at jd {jd}",
p.tithi
);
assert!(p.vara <= 6, "vara {} out of range at jd {jd}", p.vara);
assert!(
p.nakshatra < 27,
"nakshatra {} out of range at jd {jd}",
p.nakshatra
);
}
}
// ─── GMST (Greenwich Mean Sidereal Time) ────────────────────────────────────
/// GMST at J2000.0 noon UT = 18h 41m 50.5479s.
/// In degrees: 280.46061837°.
/// (Meeus, AA 2nd ed., chapter 12 worked example.)
#[test]
fn gmst_at_j2000_noon_ut() {
let gmst = mean_sidereal_time_deg(JulianDay::new(2_451_545.0));
let expected = 280.46061837;
let diff = ((gmst - expected + 540.0) % 360.0 - 180.0).abs();
assert!(
diff < 0.0001,
"GMST at J2000 noon UT = {gmst:.6}°, expected {expected:.6}° (diff {diff:.6}°)",
);
}
/// Apparent sidereal time differs from mean by the equation of the
/// equinoxes (Δψ · cos ε). At J2000.0 Δψ ≈ −13.85" and cos ε ≈ 0.917,
/// so |GAST − GMST| ≈ 12.7". Both must be in [0, 360°).
#[test]
fn apparent_sidereal_time_near_mean() {
let jd = 2_451_545.0;
let gmst = mean_sidereal_time_deg(JulianDay::new(jd));
let gast = sidereal_time_deg(JulianDay::new(jd));
let eqeq = ((gast - gmst + 540.0) % 360.0 - 180.0).abs();
assert!(
eqeq < 0.01, // < 36"
"Equation of equinoxes |GAST - GMST| = {eqeq:.6}° at J2000 — should be ~12.7\"",
);
assert!((0.0..360.0).contains(&gmst));
assert!((0.0..360.0).contains(&gast));
}
// ─── IAU nutation ───────────────────────────────────────────────────────────
/// Meeus AA 2nd ed., chapter 22 worked example: 1987-Apr-10 0h UT
/// (= JDE 2446895.5). Reference values:
/// Δψ = -3.788" (nutation in longitude)
/// Δε = +9.443" (nutation in obliquity)
/// Tolerance 1" — accommodates the slight numerical differences
/// between IAU 1980 (Meeus reference) and IAU 2000B (engine's series).
#[test]
fn nutation_meeus_1987_april() {
let jde = 2_446_895.5;
let (dpsi_deg, deps_deg) = nutation(JulianDay::new(jde));
let dpsi_arcsec = dpsi_deg * 3600.0;
let deps_arcsec = deps_deg * 3600.0;
assert!(
(dpsi_arcsec - (-3.788)).abs() < 1.0,
"Δψ at 1987-04-10 0h UT = {dpsi_arcsec:.4}\", expected ≈ -3.788\"",
);
assert!(
(deps_arcsec - 9.443).abs() < 1.0,
"Δε at 1987-04-10 0h UT = {deps_arcsec:.4}\", expected ≈ +9.443\"",
);
}
// ─── Obliquity ──────────────────────────────────────────────────────────────
/// Mean obliquity of the ecliptic at J2000.0 = 23°26'21.406" = 23.43928889°.
/// IAU 2006 published value (Capitaine et al. 2003).
/// celestial uses the IAU 2006 polynomial — this test pins it to the
/// canonical 1 mas precision.
#[test]
fn mean_obliquity_at_j2000_iau_2006() {
let eps = celestial_core::mean_obliquity(JulianDay::new(2_451_545.0));
assert!(
(eps - 23.43928889).abs() < 0.0001, // < 0.36"
"Mean obliquity at J2000 (IAU 2006) = {eps:.7}°, expected ≈ 23.4392889°",
);
}
/// True obliquity = mean + Δε. At J2000 Δε ≈ −5.85" per IAU 2000B
/// (small lunisolar nutation correction). So true ε ≈ 23.43766°.
#[test]
fn true_obliquity_at_j2000() {
let eps = true_obliquity(JulianDay::new(2_451_545.0));
let mean = celestial_core::mean_obliquity(JulianDay::new(2_451_545.0));
let delta_eps_arcsec = (eps - mean) * 3600.0;
assert!(
delta_eps_arcsec.abs() < 15.0, // |Δε| ≤ 15" anywhere in the cycle
"Δε at J2000 = {delta_eps_arcsec:.4}\", expected |Δε| ≤ 15\"",
);
assert!(
(eps - 23.4393).abs() < 0.01,
"True obliquity at J2000 = {eps:.6}°, near mean 23.4393°",
);
}
// ─── Lahiri ayanamsa ────────────────────────────────────────────────────────
/// Lahiri ayanamsa at J2000.0 = 23°51'11" = 23.853°.
/// (Lahiri official Indian / Government of India convention.)
#[test]
fn lahiri_ayanamsa_at_j2000() {
set_sid_mode(SiderealMode::LAHIRI, 0.0, 0.0);
let ay = ayanamsa_ut(JulianDay::new(2_451_545.0));
assert!(
(ay - 23.853).abs() < 0.05,
"Lahiri ayanamsa at J2000 = {ay:.4}°, expected ≈ 23.853°",
);
}
/// Krishnamurti (KP) ayanamsa at J2000 = 23°47'07" = 23.7853°.
/// Per K.S. Krishnamurti, "Krishnamurti Paddhati" foundational tables.
#[test]
fn krishnamurti_ayanamsa_at_j2000() {
set_sid_mode(SiderealMode::KRISHNAMURTI, 0.0, 0.0);
let ay = ayanamsa_ut(JulianDay::new(2_451_545.0));
assert!(
(ay - 23.785).abs() < 0.05,
"Krishnamurti ayanamsa at J2000 = {ay:.4}°, expected ≈ 23.785°",
);
}
// ─── Vedic / Jyotish ────────────────────────────────────────────────────────
/// Nakshatra boundaries: each spans 13°20' = 13.333°. So:
/// λ ∈ [0°, 13.333°) → 0 (Ashwini)
/// λ ∈ [13.333°, 26.667°) → 1 (Bharani)
/// λ ∈ [26.667°, 40.000°) → 2 (Krittika)
/// ...
#[test]
fn nakshatra_boundary_anchors() {
let cases: &[(f64, i32)] = &[
(0.0, 0),
(13.0, 0),
(13.34, 1),
(26.5, 1),
(26.67, 2),
(40.0, 3),
(359.0, 26),
];
for &(lon, expected) in cases {
let (nak, _pada) = long_to_nakshatra(Longitude::new(lon));
assert_eq!(
nak, expected,
"Nakshatra at {lon}° = {nak}, expected {expected}",
);
}
}
/// Navamsa is 1/9 of a sign (3°20' = 3.333°). Sign + navamsa-within-sign
/// determines the navamsa rasi (0..=11). Sanity-pin the first three
/// navamsa boundaries inside Aries:
/// λ ∈ [0°, 3.333°) → Aries (0)
/// λ ∈ [3.333°, 6.667°) → Taurus (1)
/// λ ∈ [6.667°,10.000°) → Gemini (2)
#[test]
fn navamsa_within_aries() {
let cases: &[(f64, i32)] = &[(0.0, 0), (3.0, 0), (3.5, 1), (6.5, 1), (7.0, 2), (10.0, 3)];
for &(lon, expected) in cases {
let nav = long_to_navamsa(Longitude::new(lon));
assert_eq!(
nav, expected,
"Navamsa at {lon}° = {nav}, expected {expected}"
);
}
}
/// Vimshottari dasha: nine planetary lords with total cycle of 120 years.
/// Ketu 7 · Venus 20 · Sun 6 · Moon 10 · Mars 7 · Rahu 18 ·
/// Jupiter 16 · Saturn 19 · Mercury 17 = 120
#[test]
fn vimshottari_total_period_is_120_years() {
// Use a Moon longitude near Ashwini start (Ketu's nakshatra) so we
// get a full 120-year cycle.
let dashas = vimshottari_dasha(JulianDay::new(2_451_545.0), Longitude::new(0.0), 120.0);
let total: f64 = dashas.iter().take(9).map(|d| d.years).sum();
assert!(
(total - 120.0).abs() < 0.01,
"Vimshottari first 9 mahadashas sum to {total:.4}y, expected 120.0y",
);
}
// ─── Solar return ───────────────────────────────────────────────────────────
/// `solar_return_jd(natal, year, flags)` finds the next Sun-return JD
/// at or after Jan 1 of `year`. Two consecutive SRs are separated by
/// approximately one tropical year (365.24 days).
#[test]
fn consecutive_solar_returns_separated_by_one_year() {
let jd_natal = julday(1985, 7, 14, 12.0, Calendar::Gregorian);
let sr_2024 = solar_return_jd(JulianDay::new(jd_natal), 2024, CalcFlags::BUILTIN).unwrap();
let sr_2025 = solar_return_jd(JulianDay::new(jd_natal), 2025, CalcFlags::BUILTIN).unwrap();
let dt = sr_2025 - sr_2024;
assert!(
(dt - 365.24).abs() < 1.0,
"Consecutive SRs: dt = {dt:.4} days, expected ≈ 365.24",
);
}
// ─── Annual profection ──────────────────────────────────────────────────────
/// Profected house = ((age - 1) mod 12) + 1 starting from ASC (h1).
/// Age 0 → h1 (ASC). Age 12 → h1 again. Age 35 → h12.
#[test]
fn annual_profection_house_cycle() {
let cusps = [0.0; 13]; // placeholder; the fn only uses age to count houses
assert_eq!(annual_profection(&cusps, 0).0, 1);
assert_eq!(annual_profection(&cusps, 12).0, 1);
assert_eq!(annual_profection(&cusps, 1).0, 2);
assert_eq!(annual_profection(&cusps, 11).0, 12);
assert_eq!(annual_profection(&cusps, 35).0, 12);
}
// ─── Easter JD round-trip ───────────────────────────────────────────────────
/// `easter_jd(year)` returns the Julian Day of Easter Sunday at 0h UT.
/// For 2024 (March 31): JD = julday(2024, 3, 31, 0.0, Gregorian).
#[cfg(feature = "calendar-traditions")]
#[test]
fn easter_jd_round_trips_with_julday() {
let jd_easter = easter_jd(2024);
let expected = julday(2024, 3, 31, 0.0, Calendar::Gregorian);
assert!(
(jd_easter - expected).abs() < 0.5,
"easter_jd(2024) = {jd_easter}, expected ≈ {expected} (2024-03-31)",
);
}
// ─── Nowruz ─────────────────────────────────────────────────────────────────
/// Nowruz (Persian / Bahá'í New Year) is the moment of the vernal equinox.
/// 2024 vernal equinox = 2024-03-20 03:06 UT ≈ JD 2460389.63.
#[cfg(feature = "calendar-traditions")]
#[test]
fn nowruz_2024_at_vernal_equinox() {
let jd = nowruz_jd(2024);
let expected = julday(2024, 3, 20, 3.1, Calendar::Gregorian); // ≈ 03:06 UT
assert!(
(jd - expected).abs() < 1.0,
"nowruz_jd(2024) = {jd:.4}, expected ≈ {expected:.4} (2024-03-20 ~03:06 UT)",
);
}
// ─── Sabbats (Celtic Wheel of the Year) ─────────────────────────────────────
/// Yule (winter solstice in the Northern hemisphere): 2024-12-21.
/// Sabbat positions are at sun longitudes: Yule 270°, Imbolc 315°, Ostara 0°,
/// Beltane 45°, Litha 90°, Lughnasadh 135°, Mabon 180°, Samhain 225°.
#[cfg(feature = "calendar-traditions")]
#[test]
fn sabbats_2024_solstices_and_equinoxes() {
let sabbats = sabbats_for_year(2024).unwrap();
assert_eq!(
sabbats.len(),
8,
"expected 8 sabbats per year, got {}",
sabbats.len()
);
// All sabbats fall in 2024.
for s in &sabbats {
let d = celestial_core::revjul(JulianDay::new(s.jd), Calendar::Gregorian);
assert_eq!(
d.year, 2024,
"sabbat {:?} JD {:.4} not in 2024",
s.kind, s.jd
);
}
}
/// `esbats_for_year` returns ~12 named full moons per year (13 in some
/// years). Each must fall inside the year.
#[cfg(feature = "calendar-traditions")]
#[test]
fn esbats_2024_count_and_year_bounds() {
let esbats = esbats_for_year(2024).unwrap();
assert!(
(12..=13).contains(&esbats.len()),
"expected 12-13 esbats in 2024, got {}",
esbats.len(),
);
for e in &esbats {
let d = celestial_core::revjul(JulianDay::new(e.jd), Calendar::Gregorian);
assert_eq!(
d.year, 2024,
"esbat {:?} JD {:.4} not in 2024",
e.name, e.jd
);
}
}
// ─── Moon phase root finder ─────────────────────────────────────────────────
/// `next_new_moon(jd)` finds the next new moon strictly after `jd`.
/// Consecutive new moons are separated by one synodic month (29.530588 d).
#[test]
fn consecutive_new_moons_match_synodic_month() {
let jd0 = 2_451_545.0;
let nm1 = next_new_moon(JulianDay::new(jd0)).unwrap();
let nm2 = next_new_moon(JulianDay::new(nm1 + 0.5)).unwrap();
let dt = nm2 - nm1;
assert!(
(dt - 29.530_588).abs() < 0.5,
"Consecutive new moons {} → {}: dt = {dt:.4}d, expected ≈ 29.530588d",
nm1,
nm2,
);
}
// ─── Chinese Ba Zi ──────────────────────────────────────────────────────────
/// Four-pillars internal consistency: each pillar must return valid
/// stem (0..=9) and branch (0..=11) indices, and the year/month/day
/// pillars must be deterministic given a fixed (jd, hour, sun_lon).
#[test]
fn four_pillars_field_ranges_and_determinism() {
let jd = julday(1986, 5, 30, 9.0, Calendar::Gregorian);
let sun_lon = 68.667; // PDF reference
let pillars_a = four_pillars(JulianDay::new(jd), 9.0, Longitude::new(sun_lon));
let pillars_b = four_pillars(JulianDay::new(jd), 9.0, Longitude::new(sun_lon));
for (a, b) in pillars_a.iter().zip(pillars_b.iter()) {
assert!(a.stem < 10, "stem {} out of range", a.stem);
assert!(a.branch < 12, "branch {} out of range", a.branch);
assert_eq!(a.stem, b.stem, "non-deterministic stem");
assert_eq!(a.branch, b.branch, "non-deterministic branch");
}
}
// ─── Hebrew calendar ────────────────────────────────────────────────────────
/// 1 Tishrei (Rosh Hashanah) of Hebrew year 5785 corresponds to
/// 2024-10-03 in the Gregorian calendar (sunset 2024-10-02 by
/// Hebrew convention; the calendar-day JD is the daytime portion).
/// Per Hebcal / Maharil tables.
#[cfg(feature = "calendar-traditions")]
#[test]
fn hebrew_new_year_5785() {
let jd = hebrew_new_year_jd(5785);
let expected = julday(2024, 10, 3, 0.0, Calendar::Gregorian) as i64;
assert!(
(jd - expected).abs() < 2,
"Hebrew NY 5785 JD = {jd}, expected ≈ {expected} (2024-10-03 ± 1 d)",
);
}
/// 5783 = 2022-09-26. 5784 = 2023-09-16. Sanity check ordering.
#[cfg(feature = "calendar-traditions")]
#[test]
fn hebrew_new_year_ordering() {
let jd_5783 = hebrew_new_year_jd(5783);
let jd_5784 = hebrew_new_year_jd(5784);
let jd_5785 = hebrew_new_year_jd(5785);
assert!(
jd_5783 < jd_5784 && jd_5784 < jd_5785,
"Hebrew NY must be ordered"
);
// Hebrew year length: 353, 354, 355, 383, 384, or 385 days.
let d1 = jd_5784 - jd_5783;
let d2 = jd_5785 - jd_5784;
assert!(
(353..=385).contains(&d1),
"Hebrew year length 5783→5784 = {d1} days, expected 353-385",
);
assert!(
(353..=385).contains(&d2),
"Hebrew year length 5784→5785 = {d2} days, expected 353-385",
);
}
// ─── Tibetan Losar ──────────────────────────────────────────────────────────
/// Losar (Tibetan New Year) 2024 = 2024-02-10 (Year of the Wood Dragon).
/// Per the Phugpa system tables published by Tibet House.
#[cfg(feature = "calendar-traditions")]
#[test]
fn tibetan_losar_2024() {
let jd = losar_jd(2024).expect("losar found");
let expected = julday(2024, 2, 10, 0.0, Calendar::Gregorian);
assert!(
(jd - expected).abs() < 2.0,
"Losar 2024 = {jd:.4}, expected ≈ {expected:.4} (2024-02-10 ± 1 d)",
);
}
// ─── Zoroastrian Fasli Nowruz ───────────────────────────────────────────────
/// Fasli Nowruz 2024 ≈ vernal equinox 2024 = 2024-03-20 03:06 UT.
/// (Fasli is locked to the astronomical equinox per 1906 reform.)
#[cfg(feature = "calendar-traditions")]
#[test]
fn fasli_nowruz_2024_matches_equinox() {
let jd = fasli_nowruz_jd(2024).expect("fasli nowruz");
let nowruz = nowruz_jd(2024);
assert!(
(jd - nowruz).abs() < 1.5,
"Fasli Nowruz vs astronomical Nowruz: {jd} vs {nowruz}",
);
}
// ─── Coptic calendar ────────────────────────────────────────────────────────
/// Coptic Thout 1 of year 1740 AM = 2023-09-11 Gregorian. (Coptic year
/// is 8 months ahead of Ethiopic for the same AM year, and runs from
/// Aug-Sep to Aug-Sep Gregorian.) JD ≈ 2460199.5.
#[cfg(feature = "calendar-traditions")]
#[test]
fn coptic_to_jd_round_trip() {
let jd = coptic_to_jd(1740, 1, 1);
let (y, m, d) = jd_to_coptic(JulianDay::new(jd));
assert_eq!(
(y, m, d),
(1740, 1, 1),
"Coptic round-trip failed: ({y}, {m}, {d}) ≠ (1740, 1, 1)",
);
// Sanity: 1740 Thout 1 lands in early September 2023 Gregorian.
let d_greg = celestial_core::revjul(JulianDay::new(jd), Calendar::Gregorian);
assert!(
d_greg.year == 2023 && d_greg.month == 9 && (10..=12).contains(&(d_greg.day as i32)),
"Coptic 1740-01-01 should be ~2023-09-11, got {}-{}-{}",
d_greg.year,
d_greg.month,
d_greg.day,
);
}
// ─── Moon-phase root finder ─────────────────────────────────────────────────
/// Known new moon: 2024-01-11 11:57 UT. Searching from 2024-01-01
/// must converge to within an hour of this published time.
#[test]
fn new_moon_2024_january() {
let jd_start = julday(2024, 1, 1, 0.0, Calendar::Gregorian);
let nm = next_new_moon(JulianDay::new(jd_start)).unwrap();
let expected = julday(2024, 1, 11, 11.95, Calendar::Gregorian);
assert!(
(nm - expected).abs() < 0.05, // < 72 min
"Jan 2024 new moon: {nm:.4}, expected ≈ {expected:.4}",
);
}
// ─── Solar position pins ────────────────────────────────────────────────────
/// At the 2024 vernal equinox (2024-03-20 03:06 UT) the Sun's
/// geocentric ecliptic longitude is, by definition, ≈ 0° (within the
/// solar oblateness corrections).
#[test]
fn sun_at_vernal_equinox_is_zero_lon() {
let jd = julday(2024, 3, 20, 3.1, Calendar::Gregorian);
let pos = calc_ut(JulianDay::new(jd), Body::SUN, FLG).unwrap();
let diff = ((pos.lon + 540.0) % 360.0 - 180.0).abs();
assert!(
diff < 0.1,
"Sun at vernal equinox 2024: lon = {:.4}°, expected ≈ 0°",
pos.lon,
);
}
/// At the 2024 winter solstice (~2024-12-21 09:21 UT) the Sun is at
/// 270° (= 0° Capricorn).
#[test]
fn sun_at_winter_solstice_is_270_lon() {
let jd = julday(2024, 12, 21, 9.35, Calendar::Gregorian);
let pos = calc_ut(JulianDay::new(jd), Body::SUN, FLG).unwrap();
let diff = ((pos.lon - 270.0 + 540.0) % 360.0 - 180.0).abs();
assert!(
diff < 0.1,
"Sun at winter solstice 2024: lon = {:.4}°, expected ≈ 270°",
pos.lon,
);
}
// ─── House systems (other than Placidus) ────────────────────────────────────
/// Every quadrant-based house system must satisfy:
/// - h1 ≈ ASC, h10 ≈ MC
/// - h4 = h10 + 180° (IC = MC + 180°)
/// - h7 = h1 + 180° (DSC = ASC + 180°)
/// - all cusps in [0°, 360°)
/// - cusps monotonically increasing (modulo 360°)
#[test]
fn quadrant_house_systems_invariants() {
let jd = julday(1986, 5, 30, 9.0, Calendar::Gregorian);
let lat = -23.5333;
let lon = -46.6333;
// Quadrant systems (h1 = ASC, h10 = MC). Excludes Morinus (M) and
// Meridian/Axial (X) which derive ALL cusps from ARMC equally and
// don't preserve ASC/MC at h1/h10.
let systems: &[u8] = b"PKORCB";
for &sys in systems {
let h = celestial_core::houses(
JulianDay::new(jd),
Latitude::new(lat),
Longitude::new(lon),
HouseSystem(sys),
)
.unwrap();
let asc = h.ascmc[0];
let mc = h.ascmc[1];
for i in 1..=12 {
assert!(
(0.0..360.0).contains(&h.cusps[i]),
"{} h{i} = {} out of [0,360)",
sys as char,
h.cusps[i],
);
}
let diff_asc = ((h.cusps[1] - asc + 540.0) % 360.0 - 180.0).abs();
let diff_mc = ((h.cusps[10] - mc + 540.0) % 360.0 - 180.0).abs();
assert!(
diff_asc < 0.001,
"{} h1 != ASC: diff {diff_asc}",
sys as char
);
assert!(diff_mc < 0.001, "{} h10 != MC: diff {diff_mc}", sys as char);
let diff_ic = ((h.cusps[4] - (mc + 180.0) + 540.0) % 360.0 - 180.0).abs();
let diff_dsc = ((h.cusps[7] - (asc + 180.0) + 540.0) % 360.0 - 180.0).abs();
assert!(diff_ic < 0.001, "{} h4 != IC: diff {diff_ic}", sys as char);
assert!(
diff_dsc < 0.001,
"{} h7 != DSC: diff {diff_dsc}",
sys as char
);
}
}
/// Whole Sign (W): h1 is at 0° of ASC's sign. Each subsequent cusp is
/// 30° later. h10 = MC's longitude irrelevant — h10 is just sign-10
/// from h1.
#[test]
fn whole_sign_houses_30_apart() {
let jd = julday(1986, 5, 30, 9.0, Calendar::Gregorian);
let h = celestial_core::houses(
JulianDay::new(jd),
Latitude::new(-23.5333),
Longitude::new(-46.6333),
HouseSystem(b'W'),
)
.unwrap();
let h1 = h.cusps[1];
assert!(
(h1 % 30.0).abs() < 0.001 || (h1 % 30.0 - 30.0).abs() < 0.001,
"Whole-Sign h1 not on sign boundary: {h1}",
);
for i in 1..12 {