Skip to content

Commit 4527b5d

Browse files
committed
Add test for full numerical precision during Duration::total
1 parent 2b07fde commit 4527b5d

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

src/builtins/core/duration/tests.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@ fn negative_fields_to_string() {
159159

160160
#[test]
161161
fn preserve_precision_loss() {
162-
const MAX_SAFE_INT: i64 = 9_007_199_254_740_991;
163162
let duration = Duration::from_partial_duration(PartialDuration {
164-
milliseconds: Some(MAX_SAFE_INT),
165-
microseconds: Some(MAX_SAFE_INT as i128),
163+
milliseconds: Some(MAX_SAFE_INTEGER),
164+
microseconds: Some(MAX_SAFE_INTEGER as i128),
166165
..Default::default()
167166
})
168167
.unwrap();
@@ -198,8 +197,6 @@ fn duration_from_str() {
198197

199198
#[test]
200199
fn duration_max_safe() {
201-
const MAX_SAFE_INTEGER: i64 = 9007199254740991;
202-
203200
// From test262 built-ins/Temporal/Duration/prototype/subtract/result-out-of-range-3.js
204201
assert!(Duration::new(0, 0, 0, 0, 0, 0, 0, 0, 9_007_199_254_740_991_926_258, 0).is_err());
205202

@@ -405,11 +402,11 @@ fn test_duration_compare() {
405402
}
406403
}
407404

408-
const MAX_SAFE_INT: i64 = 9_007_199_254_740_991;
405+
const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_991;
409406

410407
#[test]
411408
fn duration_round_out_of_range_norm_conversion() {
412-
let duration = Duration::new(0, 0, 0, 0, 0, 0, MAX_SAFE_INT, 0, 0, 999_999_999).unwrap();
409+
let duration = Duration::new(0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER, 0, 0, 999_999_999).unwrap();
413410
let err = duration.round_with_provider(
414411
RoundingOptions {
415412
largest_unit: Some(Unit::Nanosecond),
@@ -426,8 +423,8 @@ fn duration_round_out_of_range_norm_conversion() {
426423
#[cfg_attr(not(feature = "float64_representable_durations"), should_panic)]
427424
fn duration_float64_representable() {
428425
// built-ins/Temporal/Duration/prototype/add/float64-representable-integer
429-
let duration = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INT as i128, 0).unwrap();
430-
let duration2 = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INT as i128 - 1, 0).unwrap();
426+
let duration = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER as i128, 0).unwrap();
427+
let duration2 = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER as i128 - 1, 0).unwrap();
431428
let added = duration.add(&duration2).unwrap();
432429
assert_eq!(added.microseconds, 18014398509481980);
433430
assert_eq!(
@@ -441,3 +438,17 @@ fn duration_float64_representable() {
441438
"Should not internally use a more accurate representation when adding"
442439
);
443440
}
441+
442+
#[test]
443+
#[cfg(feature = "compiled_data")]
444+
fn total_full_numeric_precision() {
445+
// Tests that Duration::total operates without any loss of precision
446+
447+
// built-ins/Temporal/Duration/prototype/total/precision-exact-mathematical-values-6
448+
let d = Duration::new(0, 0, 0, 0, 816, 0, 0, 0, 0, 2_049_187_497_660).unwrap();
449+
assert_eq!(d.total(Unit::Hour, None).unwrap(), 816.56921874935);
450+
451+
// built-ins/Temporal/Duration/prototype/total/precision-exact-mathematical-values-7
452+
let d = Duration::new(0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER + 1, 1999, 0).unwrap();
453+
assert_eq!(d.total(Unit::Millisecond, None).unwrap(), 9007199254740994.);
454+
}

0 commit comments

Comments
 (0)