1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {Duration, Time, Timecode, TimeSpan} from '../base/time'; 16 17const t = Time.fromRaw; 18 19test('Duration.format', () => { 20 expect(Duration.format(0n)).toEqual('0s'); 21 expect(Duration.format(3_000_000_000n)).toEqual('3s'); 22 expect(Duration.format(60_000_000_000n)).toEqual('1m'); 23 expect(Duration.format(63_000_000_000n)).toEqual('1m 3s'); 24 expect(Duration.format(63_200_000_000n)).toEqual('1m 3s 200ms'); 25 expect(Duration.format(63_222_100_000n)).toEqual('1m 3s 222ms 100us'); 26 expect(Duration.format(63_222_111_100n)).toEqual('1m 3s 222ms 111us 100ns'); 27 expect(Duration.format(222_111_100n)).toEqual('222ms 111us 100ns'); 28 expect(Duration.format(1_000n)).toEqual('1us'); 29 expect(Duration.format(3_000n)).toEqual('3us'); 30 expect(Duration.format(1_000_001_000n)).toEqual('1s 1us'); 31 expect(Duration.format(200_000_000_030n)).toEqual('3m 20s 30ns'); 32 expect(Duration.format(3_600_000_000_000n)).toEqual('1h'); 33 expect(Duration.format(3_600_000_000_001n)).toEqual('1h 1ns'); 34 expect(Duration.format(86_400_000_000_000n)).toEqual('24h'); 35 expect(Duration.format(86_400_000_000_001n)).toEqual('24h 1ns'); 36 expect(Duration.format(31_536_000_000_000_000n)).toEqual('8,760h'); 37 expect(Duration.format(31_536_000_000_000_001n)).toEqual('8,760h 1ns'); 38}); 39 40test('Duration.humanise', () => { 41 expect(Duration.humanise(0n)).toEqual('0s'); 42 expect(Duration.humanise(123n)).toEqual('123ns'); 43 expect(Duration.humanise(1_234n)).toEqual('1.2us'); 44 expect(Duration.humanise(12_345n)).toEqual('12.3us'); 45 expect(Duration.humanise(3_000_000_000n)).toEqual('3s'); 46 expect(Duration.humanise(60_000_000_000n)).toEqual('60s'); 47 expect(Duration.humanise(63_000_000_000n)).toEqual('63s'); 48 expect(Duration.humanise(63_200_000_000n)).toEqual('63.2s'); 49 expect(Duration.humanise(63_222_100_000n)).toEqual('63.2s'); 50 expect(Duration.humanise(63_222_111_100n)).toEqual('63.2s'); 51 expect(Duration.humanise(222_111_100n)).toEqual('222.1ms'); 52 expect(Duration.humanise(1_000n)).toEqual('1us'); 53 expect(Duration.humanise(3_000n)).toEqual('3us'); 54 expect(Duration.humanise(1_000_001_000n)).toEqual('1s'); 55 expect(Duration.humanise(200_000_000_030n)).toEqual('200s'); 56 expect(Duration.humanise(3_600_000_000_000n)).toEqual('3600s'); 57 expect(Duration.humanise(86_400_000_000_000n)).toEqual('86400s'); 58 expect(Duration.humanise(31_536_000_000_000_000n)).toEqual('31536000s'); 59}); 60 61test('Duration.fromMillis', () => { 62 expect(Duration.fromMillis(123.456789)).toEqual(123456789n); 63 expect(Duration.fromMillis(123.4567895)).toEqual(123456789n); 64 expect(Duration.fromMillis(0.0000001)).toEqual(0n); 65}); 66 67test('timecode', () => { 68 expect(new Timecode(t(0n)).toString(' ')).toEqual('00:00:00.000 000 000'); 69 expect(new Timecode(t(123n)).toString(' ')).toEqual('00:00:00.000 000 123'); 70 expect(new Timecode(t(60_000_000_000n)).toString(' ')).toEqual( 71 '00:01:00.000 000 000', 72 ); 73 expect(new Timecode(t(12_345_678_910n)).toString(' ')).toEqual( 74 '00:00:12.345 678 910', 75 ); 76 expect(new Timecode(t(86_400_000_000_000n)).toString(' ')).toEqual( 77 '1d00:00:00.000 000 000', 78 ); 79 expect(new Timecode(t(31_536_000_000_000_000n)).toString(' ')).toEqual( 80 '365d00:00:00.000 000 000', 81 ); 82 expect(new Timecode(t(-123n)).toString(' ')).toEqual('-00:00:00.000 000 123'); 83}); 84 85function mkSpan(start: bigint, end: bigint) { 86 return new TimeSpan(t(start), t(end)); 87} 88 89describe('TimeSpan', () => { 90 it('throws when start is later than end', () => { 91 expect(() => mkSpan(1n, 0n)).toThrow(); 92 }); 93 94 it('can calc duration', () => { 95 expect(mkSpan(10n, 20n).duration).toBe(10n); 96 }); 97 98 it('can calc midpoint', () => { 99 expect(mkSpan(10n, 20n).midpoint).toBe(15n); 100 expect(mkSpan(10n, 19n).midpoint).toBe(14n); 101 expect(mkSpan(10n, 10n).midpoint).toBe(10n); 102 }); 103 104 it('can be compared', () => { 105 const x = mkSpan(10n, 20n); 106 expect(x.equals(mkSpan(10n, 20n))).toBeTruthy(); 107 expect(x.equals(mkSpan(11n, 20n))).toBeFalsy(); 108 expect(x.equals(mkSpan(10n, 19n))).toBeFalsy(); 109 }); 110 111 it('checks containment', () => { 112 const x = mkSpan(10n, 20n); 113 114 expect(x.contains(t(9n))).toBeFalsy(); 115 expect(x.contains(t(10n))).toBeTruthy(); 116 expect(x.contains(t(15n))).toBeTruthy(); 117 expect(x.contains(t(20n))).toBeFalsy(); 118 expect(x.contains(t(21n))).toBeFalsy(); 119 }); 120 121 it('checks containment of another span', () => { 122 const x = mkSpan(10n, 20n); 123 124 expect(x.containsSpan(t(12n), t(18n))).toBeTruthy(); 125 expect(x.containsSpan(t(5n), t(25n))).toBeFalsy(); 126 expect(x.containsSpan(t(5n), t(15n))).toBeFalsy(); 127 expect(x.containsSpan(t(15n), t(25n))).toBeFalsy(); 128 expect(x.containsSpan(t(0n), t(10n))).toBeFalsy(); 129 expect(x.containsSpan(t(20n), t(30n))).toBeFalsy(); 130 }); 131 132 it('checks overlap', () => { 133 const x = mkSpan(10n, 20n); 134 135 expect(x.overlaps(t(0n), t(10n))).toBeFalsy(); 136 expect(x.overlaps(t(5n), t(15n))).toBeTruthy(); 137 expect(x.overlaps(t(12n), t(18n))).toBeTruthy(); 138 expect(x.overlaps(t(15n), t(25n))).toBeTruthy(); 139 expect(x.overlaps(t(20n), t(30n))).toBeFalsy(); 140 expect(x.overlaps(t(5n), t(25n))).toBeTruthy(); 141 }); 142 143 it('can add', () => { 144 const x = mkSpan(10n, 20n); 145 expect(x.translate(5n)).toEqual(mkSpan(15n, 25n)); 146 }); 147 148 it('can pad', () => { 149 const x = mkSpan(10n, 20n); 150 expect(x.pad(5n)).toEqual(mkSpan(5n, 25n)); 151 }); 152}); 153