blob: 1a882b50776749e9de4b26cb665440b86eb99b29 [file] [log] [blame]
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Duration, Time, Timecode, TimeSpan} from '../base/time';
const t = Time.fromRaw;
test('Duration.format', () => {
expect(Duration.format(0n)).toEqual('0s');
expect(Duration.format(3_000_000_000n)).toEqual('3s');
expect(Duration.format(60_000_000_000n)).toEqual('1m');
expect(Duration.format(63_000_000_000n)).toEqual('1m 3s');
expect(Duration.format(63_200_000_000n)).toEqual('1m 3s 200ms');
expect(Duration.format(63_222_100_000n)).toEqual('1m 3s 222ms 100us');
expect(Duration.format(63_222_111_100n)).toEqual('1m 3s 222ms 111us 100ns');
expect(Duration.format(222_111_100n)).toEqual('222ms 111us 100ns');
expect(Duration.format(1_000n)).toEqual('1us');
expect(Duration.format(3_000n)).toEqual('3us');
expect(Duration.format(1_000_001_000n)).toEqual('1s 1us');
expect(Duration.format(200_000_000_030n)).toEqual('3m 20s 30ns');
expect(Duration.format(3_600_000_000_000n)).toEqual('1h');
expect(Duration.format(3_600_000_000_001n)).toEqual('1h 1ns');
expect(Duration.format(86_400_000_000_000n)).toEqual('24h');
expect(Duration.format(86_400_000_000_001n)).toEqual('24h 1ns');
expect(Duration.format(31_536_000_000_000_000n)).toEqual('8,760h');
expect(Duration.format(31_536_000_000_000_001n)).toEqual('8,760h 1ns');
});
test('Duration.humanise', () => {
expect(Duration.humanise(0n)).toEqual('0s');
expect(Duration.humanise(123n)).toEqual('123ns');
expect(Duration.humanise(1_234n)).toEqual('1.2us');
expect(Duration.humanise(12_345n)).toEqual('12.3us');
expect(Duration.humanise(3_000_000_000n)).toEqual('3s');
expect(Duration.humanise(60_000_000_000n)).toEqual('60s');
expect(Duration.humanise(63_000_000_000n)).toEqual('63s');
expect(Duration.humanise(63_200_000_000n)).toEqual('63.2s');
expect(Duration.humanise(63_222_100_000n)).toEqual('63.2s');
expect(Duration.humanise(63_222_111_100n)).toEqual('63.2s');
expect(Duration.humanise(222_111_100n)).toEqual('222.1ms');
expect(Duration.humanise(1_000n)).toEqual('1us');
expect(Duration.humanise(3_000n)).toEqual('3us');
expect(Duration.humanise(1_000_001_000n)).toEqual('1s');
expect(Duration.humanise(200_000_000_030n)).toEqual('200s');
expect(Duration.humanise(3_600_000_000_000n)).toEqual('3600s');
expect(Duration.humanise(86_400_000_000_000n)).toEqual('86400s');
expect(Duration.humanise(31_536_000_000_000_000n)).toEqual('31536000s');
});
test('Duration.fromMillis', () => {
expect(Duration.fromMillis(123.456789)).toEqual(123456789n);
expect(Duration.fromMillis(123.4567895)).toEqual(123456789n);
expect(Duration.fromMillis(0.0000001)).toEqual(0n);
});
test('timecode', () => {
expect(new Timecode(t(0n)).toString(' ')).toEqual('00:00:00.000 000 000');
expect(new Timecode(t(123n)).toString(' ')).toEqual('00:00:00.000 000 123');
expect(new Timecode(t(60_000_000_000n)).toString(' ')).toEqual(
'00:01:00.000 000 000',
);
expect(new Timecode(t(12_345_678_910n)).toString(' ')).toEqual(
'00:00:12.345 678 910',
);
expect(new Timecode(t(86_400_000_000_000n)).toString(' ')).toEqual(
'1d00:00:00.000 000 000',
);
expect(new Timecode(t(31_536_000_000_000_000n)).toString(' ')).toEqual(
'365d00:00:00.000 000 000',
);
expect(new Timecode(t(-123n)).toString(' ')).toEqual('-00:00:00.000 000 123');
});
function mkSpan(start: bigint, end: bigint) {
return new TimeSpan(t(start), t(end));
}
describe('TimeSpan', () => {
it('throws when start is later than end', () => {
expect(() => mkSpan(1n, 0n)).toThrow();
});
it('can calc duration', () => {
expect(mkSpan(10n, 20n).duration).toBe(10n);
});
it('can calc midpoint', () => {
expect(mkSpan(10n, 20n).midpoint).toBe(15n);
expect(mkSpan(10n, 19n).midpoint).toBe(14n);
expect(mkSpan(10n, 10n).midpoint).toBe(10n);
});
it('can be compared', () => {
const x = mkSpan(10n, 20n);
expect(x.equals(mkSpan(10n, 20n))).toBeTruthy();
expect(x.equals(mkSpan(11n, 20n))).toBeFalsy();
expect(x.equals(mkSpan(10n, 19n))).toBeFalsy();
});
it('checks containment', () => {
const x = mkSpan(10n, 20n);
expect(x.contains(t(9n))).toBeFalsy();
expect(x.contains(t(10n))).toBeTruthy();
expect(x.contains(t(15n))).toBeTruthy();
expect(x.contains(t(20n))).toBeFalsy();
expect(x.contains(t(21n))).toBeFalsy();
});
it('checks containment of another span', () => {
const x = mkSpan(10n, 20n);
expect(x.containsSpan(t(12n), t(18n))).toBeTruthy();
expect(x.containsSpan(t(5n), t(25n))).toBeFalsy();
expect(x.containsSpan(t(5n), t(15n))).toBeFalsy();
expect(x.containsSpan(t(15n), t(25n))).toBeFalsy();
expect(x.containsSpan(t(0n), t(10n))).toBeFalsy();
expect(x.containsSpan(t(20n), t(30n))).toBeFalsy();
});
it('checks overlap', () => {
const x = mkSpan(10n, 20n);
expect(x.overlaps(t(0n), t(10n))).toBeFalsy();
expect(x.overlaps(t(5n), t(15n))).toBeTruthy();
expect(x.overlaps(t(12n), t(18n))).toBeTruthy();
expect(x.overlaps(t(15n), t(25n))).toBeTruthy();
expect(x.overlaps(t(20n), t(30n))).toBeFalsy();
expect(x.overlaps(t(5n), t(25n))).toBeTruthy();
});
it('can add', () => {
const x = mkSpan(10n, 20n);
expect(x.translate(5n)).toEqual(mkSpan(15n, 25n));
});
it('can pad', () => {
const x = mkSpan(10n, 20n);
expect(x.pad(5n)).toEqual(mkSpan(5n, 25n));
});
});