Add overflow tests for sample count to duration conversion methods

These methods were updated to use the new overflow-resistant
`scaleLargeValue` method in 885ddb167e.

PiperOrigin-RevId: 565059609
This commit is contained in:
ibaker 2023-09-13 08:35:46 -07:00 committed by Copybara-Service
parent d111976125
commit f8b23da7af

View file

@ -850,6 +850,26 @@ public class UtilTest {
}
}
@Test
public void durationToSampleCount_doesntOverflowWithLargeDuration() {
// Choose a durationUs & sampleRate that will overflow a signed 64-bit integer if they are
// multiplied together, but not if the durationUs is converted to seconds first.
long sampleCount =
Util.durationUsToSampleCount(
/* durationUs= */ Long.MAX_VALUE / 100_000, /* sampleRate= */ 192_000);
assertThat(sampleCount).isEqualTo(17708874310762L);
}
@Test
public void sampleCountToDuration_doesntOverflowWithLargeDuration() {
// Choose a sampleCount that will overflow a signed 64-bit integer if it is multiplied directly
// by C.MICROS_PER_SECOND, but not if it is divided by sampleRate first.
long durationUs =
Util.sampleCountToDurationUs(
/* sampleCount= */ Long.MAX_VALUE / 100_000, /* sampleRate= */ 192_000);
assertThat(durationUs).isEqualTo(480383960252848L);
}
@Test
public void parseXsDuration_returnsParsedDurationInMillis() {
assertThat(parseXsDuration("PT150.279S")).isEqualTo(150279L);