|
1 | 1 | use core::str::FromStr; |
2 | 2 |
|
3 | | -use crate::{options::ToStringRoundingOptions, parsers::Precision, partial::PartialDuration}; |
| 3 | +use crate::{options::{RoundingOptions, ToStringRoundingOptions, Unit}, parsers::Precision, partial::PartialDuration, provider::NeverProvider}; |
4 | 4 |
|
5 | 5 | use super::Duration; |
6 | 6 |
|
@@ -184,3 +184,53 @@ fn duration_from_str() { |
184 | 184 | assert_eq!(duration.microseconds(), 999); |
185 | 185 | assert_eq!(duration.nanoseconds(), 940); |
186 | 186 | } |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn duration_max() { |
| 190 | + let cases = [ |
| 191 | + (Duration::new(0, 0, 0, 104249991374, 7, 36, 31, 999, 999, 999).unwrap(), "max days", 9007199254740991.999999999), |
| 192 | + (Duration::new(0, 0, 0, 0, 2501999792983, 36, 31, 999, 999, 999).unwrap(), "max hours", 9007199254740991.999999999), |
| 193 | + (Duration::new(0, 0, 0, 0, 0, 150119987579016, 31, 999, 999, 999).unwrap(), "max minutes", 9007199254740991.999999999), |
| 194 | + (Duration::new(0, 0, 0, 0, 0, 0, 9007199254740991, 999, 999, 999).unwrap(), "max seconds", 9007199254740991.999999999), |
| 195 | + (Duration::new(0, 0, 0, -104249991374, -7, -36, -31, -999, -999, -999).unwrap(), "min days", -9007199254740991.999999999), |
| 196 | + (Duration::new(0, 0, 0, 0, -2501999792983, -36, -31, -999, -999, -999).unwrap(), "min hours", -9007199254740991.999999999), |
| 197 | + (Duration::new(0, 0, 0, 0, 0, -150119987579016, -31, -999, -999, -999).unwrap(), "min minutes", -9007199254740991.999999999), |
| 198 | + (Duration::new(0, 0, 0, 0, 0, 0, -9007199254740991, -999, -999, -999).unwrap(), "min seconds", -9007199254740991.999999999), |
| 199 | + ]; |
| 200 | + |
| 201 | + for (duration, description, result) in cases { |
| 202 | + assert_eq!(duration.total_with_provider(Unit::Second, None, &NeverProvider).unwrap().0, result, "{description}"); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +#[test] |
| 207 | +fn duration_round_negative() { |
| 208 | + let duration = Duration::new(0, 0, 0, 0, -60, 0, 0, 0, 0, 0).unwrap(); |
| 209 | + let result = duration.round_with_provider(RoundingOptions { |
| 210 | + smallest_unit: Some(Unit::Day), |
| 211 | + ..Default::default() |
| 212 | + }, None, &NeverProvider).unwrap(); |
| 213 | + assert_eq!(result.days(), -3); |
| 214 | + |
| 215 | +} |
| 216 | + |
| 217 | +/* |
| 218 | +TODO: Uncomment |
| 219 | +
|
| 220 | +The below test should fail, but currently doesn't. This has to do with weird |
| 221 | +floating point math in IsValidDuration Step 6-8 that defers to C++ std::remquo |
| 222 | +
|
| 223 | +Needs further clarification. |
| 224 | +
|
| 225 | +#[test] |
| 226 | +fn duration_round_out_of_range_norm_conversion() { |
| 227 | + const MAX_SAFE_INT: i64 = 9_007_199_254_740_991; |
| 228 | + let duration = Duration::new(0, 0, 0, 0, 0, 0, MAX_SAFE_INT, 0, 0, 999_999_999).unwrap(); |
| 229 | + let err = duration.round_with_provider( RoundingOptions { |
| 230 | + largest_unit: Some(Unit::Nanosecond), |
| 231 | + increment: Some(RoundingIncrement::ONE), |
| 232 | + ..Default::default() |
| 233 | + }, None, &NeverProvider); |
| 234 | + assert!(err.is_err()) |
| 235 | +} |
| 236 | +*/ |
0 commit comments