|
| 1 | +use date_component::date_component::*; |
| 2 | +use chrono::prelude::*; |
| 3 | + |
| 4 | +#[test] |
| 5 | +fn test_month_length_edge_cases() { |
| 6 | + // Test from January 31 to February 28 (non-leap year) |
| 7 | + let from = Utc.with_ymd_and_hms(2023, 1, 31, 0, 0, 0).unwrap(); |
| 8 | + let to = Utc.with_ymd_and_hms(2023, 2, 28, 0, 0, 0).unwrap(); |
| 9 | + let diff = calculate(&from, &to); |
| 10 | + |
| 11 | + // This should not crash and should give reasonable results |
| 12 | + println!("Jan 31 to Feb 28, 2023: {:?}", diff); |
| 13 | + assert!(diff.month >= 0); |
| 14 | + assert!(diff.day >= 0); |
| 15 | + assert!(!diff.invert); |
| 16 | +} |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn test_month_length_edge_cases_leap_year() { |
| 20 | + // Test from January 31 to February 29 (leap year) |
| 21 | + let from = Utc.with_ymd_and_hms(2024, 1, 31, 0, 0, 0).unwrap(); |
| 22 | + let to = Utc.with_ymd_and_hms(2024, 2, 29, 0, 0, 0).unwrap(); |
| 23 | + let diff = calculate(&from, &to); |
| 24 | + |
| 25 | + println!("Jan 31 to Feb 29, 2024: {:?}", diff); |
| 26 | + assert!(diff.month >= 0); |
| 27 | + assert!(diff.day >= 0); |
| 28 | + assert!(!diff.invert); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn test_negative_duration_edge_case() { |
| 33 | + // Test a case that might cause issues with negative duration |
| 34 | + let from = Utc.with_ymd_and_hms(2023, 12, 31, 23, 59, 59).unwrap(); |
| 35 | + let to = Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 0).unwrap(); |
| 36 | + let diff = calculate(&from, &to); |
| 37 | + |
| 38 | + println!("Dec 31 23:59:59 to Jan 1 00:00:00: {:?}", diff); |
| 39 | + assert!(diff.invert); // Should be inverted since going backwards |
| 40 | + assert!(diff.year >= 0 || diff.month >= 0 || diff.day >= 0); // Should have some positive time difference |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_complex_date_arithmetic() { |
| 45 | + // Test a complex case that might reveal arithmetic bugs |
| 46 | + let from = Utc.with_ymd_and_hms(2020, 2, 29, 12, 30, 45).unwrap(); // Leap day |
| 47 | + let to = Utc.with_ymd_and_hms(2021, 3, 31, 14, 45, 30).unwrap(); |
| 48 | + let diff = calculate(&from, &to); |
| 49 | + |
| 50 | + println!("Complex leap year calculation: {:?}", diff); |
| 51 | + |
| 52 | + // Basic sanity checks |
| 53 | + assert!(diff.year >= 0); |
| 54 | + assert!(diff.month >= 0); |
| 55 | + assert!(diff.day >= 0); |
| 56 | + assert!(diff.hour >= 0); |
| 57 | + assert!(diff.minute >= 0); |
| 58 | + assert!(diff.second >= 0); |
| 59 | + assert!(!diff.invert); |
| 60 | + |
| 61 | + // The total should be consistent with the components |
| 62 | + let total_seconds = diff.interval_seconds; |
| 63 | + assert!(total_seconds > 0); |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn test_dst_boundary_arithmetic() { |
| 68 | + // Test around DST boundaries to check for arithmetic consistency |
| 69 | + use chrono_tz::America::Los_Angeles; |
| 70 | + |
| 71 | + // Spring forward: 2023-03-12 02:00 becomes 03:00 |
| 72 | + let before_spring = Los_Angeles.with_ymd_and_hms(2023, 3, 12, 1, 30, 0).unwrap(); |
| 73 | + let after_spring = Los_Angeles.with_ymd_and_hms(2023, 3, 12, 3, 30, 0).unwrap(); |
| 74 | + let diff = calculate(&before_spring, &after_spring); |
| 75 | + |
| 76 | + println!("DST spring forward: {:?}", diff); |
| 77 | + |
| 78 | + // Should handle DST transition gracefully |
| 79 | + assert!(diff.interval_hours > 0); |
| 80 | + assert!(!diff.invert); |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +fn test_zero_difference() { |
| 85 | + // Test when from and to are exactly the same |
| 86 | + let dt = Utc.with_ymd_and_hms(2023, 6, 15, 12, 30, 45).unwrap(); |
| 87 | + let diff = calculate(&dt, &dt); |
| 88 | + |
| 89 | + assert_eq!(diff.year, 0); |
| 90 | + assert_eq!(diff.month, 0); |
| 91 | + assert_eq!(diff.day, 0); |
| 92 | + assert_eq!(diff.hour, 0); |
| 93 | + assert_eq!(diff.minute, 0); |
| 94 | + assert_eq!(diff.second, 0); |
| 95 | + assert_eq!(diff.interval_seconds, 0); |
| 96 | + assert!(!diff.invert); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn test_very_small_differences() { |
| 101 | + // Test with very small time differences |
| 102 | + let from = Utc.with_ymd_and_hms(2023, 6, 15, 12, 30, 45).unwrap(); |
| 103 | + let to = Utc.with_ymd_and_hms(2023, 6, 15, 12, 30, 46).unwrap(); // 1 second difference |
| 104 | + let diff = calculate(&from, &to); |
| 105 | + |
| 106 | + assert_eq!(diff.year, 0); |
| 107 | + assert_eq!(diff.month, 0); |
| 108 | + assert_eq!(diff.day, 0); |
| 109 | + assert_eq!(diff.hour, 0); |
| 110 | + assert_eq!(diff.minute, 0); |
| 111 | + assert_eq!(diff.second, 1); |
| 112 | + assert_eq!(diff.interval_seconds, 1); |
| 113 | + assert!(!diff.invert); |
| 114 | +} |
| 115 | + |
| 116 | +#[test] |
| 117 | +fn test_year_boundary_consistency() { |
| 118 | + // Test consistency across year boundaries |
| 119 | + let from = Utc.with_ymd_and_hms(2022, 12, 31, 23, 59, 59).unwrap(); |
| 120 | + let to = Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 1).unwrap(); |
| 121 | + let diff = calculate(&from, &to); |
| 122 | + |
| 123 | + println!("Year boundary: {:?}", diff); |
| 124 | + |
| 125 | + // Should be 2 seconds total |
| 126 | + assert_eq!(diff.interval_seconds, 2); |
| 127 | + assert_eq!(diff.second, 2); |
| 128 | + |
| 129 | + // FIXED: These should now be correct after the consistency check fix |
| 130 | + assert_eq!(diff.day, 0); |
| 131 | + assert_eq!(diff.modulo_days, 0); |
| 132 | + |
| 133 | + assert!(!diff.invert); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +fn test_bug_small_time_differences() { |
| 138 | + // Test that small time differences don't incorrectly calculate days |
| 139 | + let from = Utc.with_ymd_and_hms(2023, 1, 1, 23, 59, 59).unwrap(); |
| 140 | + let to = Utc.with_ymd_and_hms(2023, 1, 2, 0, 0, 1).unwrap(); |
| 141 | + let diff = calculate(&from, &to); |
| 142 | + |
| 143 | + println!("Small time diff across day boundary: {:?}", diff); |
| 144 | + |
| 145 | + // This is a 2 second difference, but crosses midnight |
| 146 | + assert_eq!(diff.interval_seconds, 2); |
| 147 | + |
| 148 | + // FIXED: Should now correctly calculate 0 days for small time differences |
| 149 | + assert_eq!(diff.day, 0); |
| 150 | +} |
0 commit comments