oxedyne/fe2o3/fe2o3_datime/tests/hebrew_calendar_integration.rs
5.9 KiB, 4 runs
created by r1870400018:8648, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 2 | //! Anthropic Claude |
| 3 | |
| 4 | use oxedyne_fe2o3_datime::{ |
| 5 | calendar::{CalendarDate, hebrew::{HebrewCalendar, HebrewMonth}}, |
| 6 | time::{CalClock, CalClockZone}, |
| 7 | }; |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | |
| 10 | #[test] |
| 11 | fn test_hebrew_calendar_with_calendar_date() { |
| 12 | let zone = CalClockZone::utc(); |
| 13 | let hebrew_cal = HebrewCalendar::new(); |
| 14 | |
| 15 | // Create a known date: Rosh Hashanah 5784 (September 16, 2023) |
| 16 | let date = CalendarDate::new(2023, 9, 16, zone).unwrap(); |
| 17 | |
| 18 | // Test calendar properties |
| 19 | assert_eq!(hebrew_cal.calendar_name(), "Hebrew"); |
| 20 | assert!(hebrew_cal.is_leap_year_for_date(&date)); // 5784 is a leap year |
| 21 | assert_eq!(hebrew_cal.months_in_year_for_date(&date), 13); |
| 22 | |
| 23 | // Test formatting |
| 24 | let formatted = hebrew_cal.format_calendar_date(&date); |
| 25 | assert!(formatted.contains("Tishrei")); |
| 26 | assert!(formatted.contains("5784")); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn test_hebrew_calendar_holidays() { |
| 31 | // Test major Jewish holidays in 2024 (Hebrew year 5784) |
| 32 | let test_cases = vec![ |
| 33 | // (Gregorian date, Hebrew date expected, Holiday name) |
| 34 | ((2023, 9, 16), (5784, 1, 1), "Rosh Hashanah"), |
| 35 | ((2023, 9, 25), (5784, 1, 10), "Yom Kippur"), |
| 36 | ((2023, 9, 30), (5784, 1, 15), "Sukkot"), |
| 37 | ((2023, 12, 8), (5784, 3, 25), "Chanukah (First Night)"), |
| 38 | ((2024, 3, 24), (5784, 7, 14), "Purim"), |
| 39 | ((2024, 4, 23), (5784, 8, 15), "Passover (First Day)"), |
| 40 | ((2024, 6, 12), (5784, 10, 6), "Shavuot"), |
| 41 | ]; |
| 42 | |
| 43 | for ((g_year, g_month, g_day), (h_year, h_month, h_day), holiday) in test_cases { |
| 44 | let (calc_year, calc_month, calc_day) = |
| 45 | HebrewCalendar::from_gregorian(g_year, g_month, g_day).unwrap(); |
| 46 | |
| 47 | assert_eq!(calc_year, h_year, "Year mismatch for {}", holiday); |
| 48 | assert_eq!(calc_month, h_month, "Month mismatch for {}", holiday); |
| 49 | assert_eq!(calc_day, h_day, "Day mismatch for {}", holiday); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn test_hebrew_calendar_with_calclock() { |
| 55 | let zone = CalClockZone::utc(); |
| 56 | |
| 57 | // Create a CalClock for Rosh Hashanah 5784 |
| 58 | let calclock = CalClock::new(2023, 9, 16, 10, 0, 0, 0, zone).unwrap(); |
| 59 | let date = calclock.date(); |
| 60 | |
| 61 | let hebrew_cal = HebrewCalendar::new(); |
| 62 | |
| 63 | // Verify the date converts correctly |
| 64 | let (h_year, h_month, h_day) = HebrewCalendar::from_gregorian( |
| 65 | date.year(), |
| 66 | date.month_of_year().of(), |
| 67 | date.day() |
| 68 | ).unwrap(); |
| 69 | |
| 70 | assert_eq!(h_year, 5784); |
| 71 | assert_eq!(h_month, 1); // Tishrei |
| 72 | assert_eq!(h_day, 1); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn test_hebrew_leap_year_months() { |
| 77 | // Test Adar I and Adar II in a leap year |
| 78 | let leap_year = 5784; |
| 79 | |
| 80 | // Verify 5784 is a leap year |
| 81 | assert!(HebrewCalendar::is_hebrew_leap_year(leap_year)); |
| 82 | assert_eq!(HebrewCalendar::months_in_hebrew_year(leap_year), 13); |
| 83 | |
| 84 | // Test Adar I (month 6 in leap year) |
| 85 | assert_eq!(HebrewCalendar::days_in_hebrew_month(leap_year, 6).unwrap(), 30); |
| 86 | |
| 87 | // Test Adar II (month 7 in leap year) |
| 88 | assert_eq!(HebrewCalendar::days_in_hebrew_month(leap_year, 7).unwrap(), 29); |
| 89 | |
| 90 | // Test that we can convert dates in both Adar months |
| 91 | let (g_year1, g_month1, g_day1) = HebrewCalendar::to_gregorian(leap_year, 6, 15).unwrap(); |
| 92 | let (g_year2, g_month2, g_day2) = HebrewCalendar::to_gregorian(leap_year, 7, 15).unwrap(); |
| 93 | |
| 94 | // Verify they convert to different Gregorian dates |
| 95 | assert!(g_year1 == g_year2); |
| 96 | assert!(g_month1 < g_month2 || (g_month1 == g_month2 && g_day1 < g_day2)); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn test_hebrew_year_lengths() { |
| 101 | // Hebrew years can be deficient (353/383), regular (354/384), or abundant (355/385) days |
| 102 | let years_to_test = vec![5780, 5781, 5782, 5783, 5784, 5785]; |
| 103 | |
| 104 | for year in years_to_test { |
| 105 | let days = HebrewCalendar::days_in_hebrew_year(year); |
| 106 | let is_leap = HebrewCalendar::is_hebrew_leap_year(year); |
| 107 | |
| 108 | if is_leap { |
| 109 | // Leap years: 383, 384, or 385 days |
| 110 | assert!(days >= 383 && days <= 385, |
| 111 | "Leap year {} has {} days, expected 383-385", year, days); |
| 112 | } else { |
| 113 | // Regular years: 353, 354, or 355 days |
| 114 | assert!(days >= 353 && days <= 355, |
| 115 | "Regular year {} has {} days, expected 353-355", year, days); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn test_hebrew_month_enum() { |
| 122 | // Test month number conversions |
| 123 | assert_eq!(HebrewMonth::Tishrei.number(false), 1); |
| 124 | assert_eq!(HebrewMonth::Adar.number(false), 6); |
| 125 | assert_eq!(HebrewMonth::Adar.number(true), 7); // Adar II in leap year |
| 126 | assert_eq!(HebrewMonth::AdarI.number(true), 6); |
| 127 | assert_eq!(HebrewMonth::Nisan.number(false), 7); |
| 128 | assert_eq!(HebrewMonth::Nisan.number(true), 8); |
| 129 | assert_eq!(HebrewMonth::Elul.number(false), 12); |
| 130 | assert_eq!(HebrewMonth::Elul.number(true), 13); |
| 131 | |
| 132 | // Test invalid case |
| 133 | assert_eq!(HebrewMonth::AdarI.number(false), 0); // AdarI doesn't exist in non-leap years |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn test_hebrew_date_edge_cases() { |
| 138 | // Test very early date |
| 139 | let (h_year, h_month, h_day) = HebrewCalendar::from_gregorian(1, 1, 1).unwrap(); |
| 140 | assert!(h_year > 0); |
| 141 | |
| 142 | // Test date validation |
| 143 | assert!(HebrewCalendar::validate_hebrew_date(5784, 1, 30).is_ok()); // Valid |
| 144 | assert!(HebrewCalendar::validate_hebrew_date(5784, 1, 31).is_err()); // Invalid - Tishrei has 30 days |
| 145 | assert!(HebrewCalendar::validate_hebrew_date(5783, 13, 1).is_err()); // Invalid - non-leap year |
| 146 | assert!(HebrewCalendar::validate_hebrew_date(10000, 1, 1).is_err()); // Invalid - year too large |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn test_hebrew_calendar_date_arithmetic() { |
| 151 | let zone = CalClockZone::utc(); |
| 152 | |
| 153 | // Start with a known date |
| 154 | let date = CalendarDate::new(2023, 9, 16, zone).unwrap(); // 1 Tishrei 5784 |
| 155 | |
| 156 | // Add 29 days to get to 30 Tishrei |
| 157 | let new_date = date.add_days(29).unwrap(); |
| 158 | let (h_year, h_month, h_day) = HebrewCalendar::from_gregorian( |
| 159 | new_date.year(), |
| 160 | new_date.month_of_year().of(), |
| 161 | new_date.day() |
| 162 | ).unwrap(); |
| 163 | |
| 164 | assert_eq!(h_year, 5784); |
| 165 | assert_eq!(h_month, 1); // Still Tishrei |
| 166 | assert_eq!(h_day, 30); |
| 167 | |
| 168 | // Add one more day to get to 1 Cheshvan |
| 169 | let next_month = new_date.add_days(1).unwrap(); |
| 170 | let (h_year2, h_month2, h_day2) = HebrewCalendar::from_gregorian( |
| 171 | next_month.year(), |
| 172 | next_month.month_of_year().of(), |
| 173 | next_month.day() |
| 174 | ).unwrap(); |
| 175 | |
| 176 | assert_eq!(h_year2, 5784); |
| 177 | assert_eq!(h_month2, 2); // Cheshvan |
| 178 | assert_eq!(h_day2, 1); |
| 179 | } |