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 17test('Duration.format', () => { 18 expect(Duration.format(0n)).toEqual('0s'); 19 expect(Duration.format(3_000_000_000n)).toEqual('3s'); 20 expect(Duration.format(60_000_000_000n)).toEqual('1m'); 21 expect(Duration.format(63_000_000_000n)).toEqual('1m 3s'); 22 expect(Duration.format(63_200_000_000n)).toEqual('1m 3s 200ms'); 23 expect(Duration.format(63_222_100_000n)).toEqual('1m 3s 222ms 100us'); 24 expect(Duration.format(63_222_111_100n)).toEqual('1m 3s 222ms 111us 100ns'); 25 expect(Duration.format(222_111_100n)).toEqual('222ms 111us 100ns'); 26 expect(Duration.format(1_000n)).toEqual('1us'); 27 expect(Duration.format(3_000n)).toEqual('3us'); 28 expect(Duration.format(1_000_001_000n)).toEqual('1s 1us'); 29 expect(Duration.format(200_000_000_030n)).toEqual('3m 20s 30ns'); 30 expect(Duration.format(3_600_000_000_000n)).toEqual('1h'); 31 expect(Duration.format(3_600_000_000_001n)).toEqual('1h 1ns'); 32 expect(Duration.format(86_400_000_000_000n)).toEqual('24h'); 33 expect(Duration.format(86_400_000_000_001n)).toEqual('24h 1ns'); 34 expect(Duration.format(31_536_000_000_000_000n)).toEqual('8,760h'); 35 expect(Duration.format(31_536_000_000_000_001n)).toEqual('8,760h 1ns'); 36}); 37 38test('Duration.humanise', () => { 39 expect(Duration.humanise(0n)).toEqual('0s'); 40 expect(Duration.humanise(123n)).toEqual('123ns'); 41 expect(Duration.humanise(1_234n)).toEqual('1.2us'); 42 expect(Duration.humanise(12_345n)).toEqual('12.3us'); 43 expect(Duration.humanise(3_000_000_000n)).toEqual('3s'); 44 expect(Duration.humanise(60_000_000_000n)).toEqual('60s'); 45 expect(Duration.humanise(63_000_000_000n)).toEqual('63s'); 46 expect(Duration.humanise(63_200_000_000n)).toEqual('63.2s'); 47 expect(Duration.humanise(63_222_100_000n)).toEqual('63.2s'); 48 expect(Duration.humanise(63_222_111_100n)).toEqual('63.2s'); 49 expect(Duration.humanise(222_111_100n)).toEqual('222.1ms'); 50 expect(Duration.humanise(1_000n)).toEqual('1us'); 51 expect(Duration.humanise(3_000n)).toEqual('3us'); 52 expect(Duration.humanise(1_000_001_000n)).toEqual('1s'); 53 expect(Duration.humanise(200_000_000_030n)).toEqual('200s'); 54 expect(Duration.humanise(3_600_000_000_000n)).toEqual('3600s'); 55 expect(Duration.humanise(86_400_000_000_000n)).toEqual('86400s'); 56 expect(Duration.humanise(31_536_000_000_000_000n)).toEqual('31536000s'); 57}); 58 59test('Duration.fromMillis', () => { 60 expect(Duration.fromMillis(123.456789)).toEqual(123456789n); 61 expect(Duration.fromMillis(123.4567895)).toEqual(123456789n); 62 expect(Duration.fromMillis(0.0000001)).toEqual(0n); 63}); 64 65test('timecode', () => { 66 expect(new Timecode(Time.fromRaw(0n)).toString(' ')).toEqual( 67 '00:00:00.000 000 000', 68 ); 69 expect(new Timecode(Time.fromRaw(123n)).toString(' ')).toEqual( 70 '00:00:00.000 000 123', 71 ); 72 expect(new Timecode(Time.fromRaw(60_000_000_000n)).toString(' ')).toEqual( 73 '00:01:00.000 000 000', 74 ); 75 expect(new Timecode(Time.fromRaw(12_345_678_910n)).toString(' ')).toEqual( 76 '00:00:12.345 678 910', 77 ); 78 expect(new Timecode(Time.fromRaw(86_400_000_000_000n)).toString(' ')).toEqual( 79 '1d00:00:00.000 000 000', 80 ); 81 expect( 82 new Timecode(Time.fromRaw(31_536_000_000_000_000n)).toString(' '), 83 ).toEqual('365d00:00:00.000 000 000'); 84 expect(new Timecode(Time.fromRaw(-123n)).toString(' ')).toEqual( 85 '-00:00:00.000 000 123', 86 ); 87}); 88 89function mkSpan(start: bigint, end: bigint) { 90 return new TimeSpan(Time.fromRaw(start), Time.fromRaw(end)); 91} 92 93describe('TimeSpan', () => { 94 it('throws when start is later than end', () => { 95 expect(() => mkSpan(1n, 0n)).toThrow(); 96 }); 97 98 it('can calc duration', () => { 99 expect(mkSpan(10n, 20n).duration).toBe(10n); 100 }); 101 102 it('can calc midpoint', () => { 103 expect(mkSpan(10n, 20n).midpoint).toBe(15n); 104 expect(mkSpan(10n, 19n).midpoint).toBe(14n); 105 expect(mkSpan(10n, 10n).midpoint).toBe(10n); 106 }); 107 108 it('can be compared', () => { 109 const x = mkSpan(10n, 20n); 110 expect(x.equals(mkSpan(10n, 20n))).toBeTruthy(); 111 expect(x.equals(mkSpan(11n, 20n))).toBeFalsy(); 112 expect(x.equals(mkSpan(10n, 19n))).toBeFalsy(); 113 }); 114 115 it('checks containment', () => { 116 const x = mkSpan(10n, 20n); 117 118 expect(x.contains(Time.fromRaw(9n))).toBeFalsy(); 119 expect(x.contains(Time.fromRaw(10n))).toBeTruthy(); 120 expect(x.contains(Time.fromRaw(15n))).toBeTruthy(); 121 expect(x.contains(Time.fromRaw(20n))).toBeFalsy(); 122 expect(x.contains(Time.fromRaw(21n))).toBeFalsy(); 123 124 expect(x.contains(mkSpan(12n, 18n))).toBeTruthy(); 125 expect(x.contains(mkSpan(5n, 25n))).toBeFalsy(); 126 expect(x.contains(mkSpan(5n, 15n))).toBeFalsy(); 127 expect(x.contains(mkSpan(15n, 25n))).toBeFalsy(); 128 expect(x.contains(mkSpan(0n, 10n))).toBeFalsy(); 129 expect(x.contains(mkSpan(20n, 30n))).toBeFalsy(); 130 }); 131 132 it('checks intersection with span', () => { 133 const x = mkSpan(10n, 20n); 134 135 expect(x.intersectsInterval(mkSpan(0n, 10n))).toBeFalsy(); 136 expect(x.intersectsInterval(mkSpan(5n, 15n))).toBeTruthy(); 137 expect(x.intersectsInterval(mkSpan(12n, 18n))).toBeTruthy(); 138 expect(x.intersectsInterval(mkSpan(15n, 25n))).toBeTruthy(); 139 expect(x.intersectsInterval(mkSpan(20n, 30n))).toBeFalsy(); 140 expect(x.intersectsInterval(mkSpan(5n, 25n))).toBeTruthy(); 141 }); 142 143 it('checks intersection', () => { 144 const x = mkSpan(10n, 20n); 145 146 expect(x.intersects(Time.fromRaw(0n), Time.fromRaw(10n))).toBeFalsy(); 147 expect(x.intersects(Time.fromRaw(5n), Time.fromRaw(15n))).toBeTruthy(); 148 expect(x.intersects(Time.fromRaw(12n), Time.fromRaw(18n))).toBeTruthy(); 149 expect(x.intersects(Time.fromRaw(15n), Time.fromRaw(25n))).toBeTruthy(); 150 expect(x.intersects(Time.fromRaw(20n), Time.fromRaw(30n))).toBeFalsy(); 151 expect(x.intersects(Time.fromRaw(5n), Time.fromRaw(25n))).toBeTruthy(); 152 }); 153 154 it('can add', () => { 155 const x = mkSpan(10n, 20n); 156 expect(x.add(5n)).toEqual(mkSpan(15n, 25n)); 157 }); 158 159 it('can pad', () => { 160 const x = mkSpan(10n, 20n); 161 expect(x.pad(5n)).toEqual(mkSpan(5n, 25n)); 162 }); 163}); 164