aboutsummaryrefslogtreecommitdiff
path: root/ui/src/common/high_precision_time_unittest.ts
blob: c3a2033d94c83c804d9bd1272f2d7cb9ed310f95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (C) 2023 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 {
  HighPrecisionTime as HPTime,
  HighPrecisionTimeSpan as HPTimeSpan,
} from './high_precision_time';
import {TPTime} from './time';

// Quick 'n' dirty function to convert a string to a HPtime
// Used to make tests more readable
// E.g. '1.3' -> {base: 1, offset: 0.3}
// E.g. '-0.3' -> {base: -1, offset: 0.7}
function mkTime(time: string): HPTime {
  const array = time.split('.');
  if (array.length > 2) throw new Error(`Bad time format ${time}`);
  const [base, fractions] = array;
  const negative = time.startsWith('-');
  const numBase = BigInt(base);

  if (fractions) {
    const numFractions = Number(`0.${fractions}`);
    if (negative) {
      return new HPTime(numBase - 1n, 1.0 - numFractions);
    } else {
      return new HPTime(numBase, numFractions);
    }
  } else {
    return new HPTime(numBase);
  }
}

function mkSpan(t1: string, t2: string): HPTimeSpan {
  return new HPTimeSpan(mkTime(t1), mkTime(t2));
}

describe('Time', () => {
  it('should create a new Time object with the given base and offset', () => {
    const time = new HPTime(136n, 0.3);
    expect(time.base).toBe(136n);
    expect(time.offset).toBeCloseTo(0.3);
  });

  it('should normalize when offset is >= 1', () => {
    let time = new HPTime(1n, 2.3);
    expect(time.base).toBe(3n);
    expect(time.offset).toBeCloseTo(0.3);

    time = new HPTime(1n, 1);
    expect(time.base).toBe(2n);
    expect(time.offset).toBeCloseTo(0);
  });

  it('should normalize when offset is < 0', () => {
    const time = new HPTime(1n, -0.4);
    expect(time.base).toBe(0n);
    expect(time.offset).toBeCloseTo(0.6);
  });

  it('should store timestamps without losing precision', () => {
    let time = HPTime.fromTPTime(123n as TPTime);
    expect(time.toTPTime()).toBe(123n as TPTime);

    time = HPTime.fromTPTime(1152921504606846976n as TPTime);
    expect(time.toTPTime()).toBe(1152921504606846976n as TPTime);
  });

  it('should store and manipulate timestamps without losing precision', () => {
    let time = HPTime.fromTPTime(123n as TPTime);
    time = time.addTPTime(456n);
    expect(time.toTPTime()).toBe(579n);

    time = HPTime.fromTPTime(2315700508990407843n as TPTime);
    time = time.addTPTime(2315718101717517451n as TPTime);
    expect(time.toTPTime()).toBe(4631418610707925294n);
  });

  it('should add time', () => {
    const time1 = mkTime('1.3');
    const time2 = mkTime('3.1');
    const result = time1.add(time2);
    expect(result.base).toEqual(4n);
    expect(result.offset).toBeCloseTo(0.4);
  });

  it('should subtract time', () => {
    const time1 = mkTime('3.1');
    const time2 = mkTime('1.3');
    const result = time1.subtract(time2);
    expect(result.base).toEqual(1n);
    expect(result.offset).toBeCloseTo(0.8);
  });

  it('should add nanoseconds', () => {
    const time = mkTime('1.3');
    const result = time.addNanos(0.8);
    expect(result.base).toEqual(2n);
    expect(result.offset).toBeCloseTo(0.1);
  });

  it('should add seconds', () => {
    const time = mkTime('1.3');
    const result = time.addSeconds(0.008);
    expect(result.base).toEqual(8000001n);
    expect(result.offset).toBeCloseTo(0.3);
  });

  it('should perform gt comparisions', () => {
    const time = mkTime('1.2');
    expect(time.isGreaterThanOrEqual(mkTime('0.5'))).toBeTruthy();
    expect(time.isGreaterThanOrEqual(mkTime('1.1'))).toBeTruthy();
    expect(time.isGreaterThanOrEqual(mkTime('1.2'))).toBeTruthy();
    expect(time.isGreaterThanOrEqual(mkTime('1.5'))).toBeFalsy();
    expect(time.isGreaterThanOrEqual(mkTime('5.5'))).toBeFalsy();
  });

  it('should perform gte comparisions', () => {
    const time = mkTime('1.2');
    expect(time.isGreaterThan(mkTime('0.5'))).toBeTruthy();
    expect(time.isGreaterThan(mkTime('1.1'))).toBeTruthy();
    expect(time.isGreaterThan(mkTime('1.2'))).toBeFalsy();
    expect(time.isGreaterThan(mkTime('1.5'))).toBeFalsy();
    expect(time.isGreaterThan(mkTime('5.5'))).toBeFalsy();
  });

  it('should perform lt comparisions', () => {
    const time = mkTime('1.2');
    expect(time.isLessThan(mkTime('0.5'))).toBeFalsy();
    expect(time.isLessThan(mkTime('1.1'))).toBeFalsy();
    expect(time.isLessThan(mkTime('1.2'))).toBeFalsy();
    expect(time.isLessThan(mkTime('1.5'))).toBeTruthy();
    expect(time.isLessThan(mkTime('5.5'))).toBeTruthy();
  });

  it('should perform lte comparisions', () => {
    const time = mkTime('1.2');
    expect(time.isLessThanOrEqual(mkTime('0.5'))).toBeFalsy();
    expect(time.isLessThanOrEqual(mkTime('1.1'))).toBeFalsy();
    expect(time.isLessThanOrEqual(mkTime('1.2'))).toBeTruthy();
    expect(time.isLessThanOrEqual(mkTime('1.5'))).toBeTruthy();
    expect(time.isLessThanOrEqual(mkTime('5.5'))).toBeTruthy();
  });

  it('should detect equality', () => {
    const time = new HPTime(1n, 0.2);
    expect(time.equals(new HPTime(1n, 0.2))).toBeTruthy();
    expect(time.equals(new HPTime(0n, 1.2))).toBeTruthy();
    expect(time.equals(new HPTime(-100n, 101.2))).toBeTruthy();
    expect(time.equals(new HPTime(1n, 0.3))).toBeFalsy();
    expect(time.equals(new HPTime(2n, 0.2))).toBeFalsy();
  });

  it('should clamp a time to a range', () => {
    const time1 = mkTime('1.2');
    const time2 = mkTime('5.4');
    const time3 = mkTime('2.8');
    const lower = mkTime('2.3');
    const upper = mkTime('4.5');
    expect(time1.clamp(lower, upper)).toEqual(lower);
    expect(time2.clamp(lower, upper)).toEqual(upper);
    expect(time3.clamp(lower, upper)).toEqual(time3);
  });

  it('should convert to seconds', () => {
    expect(new HPTime(1n, .2).seconds).toBeCloseTo(0.0000000012);
    expect(new HPTime(1000000000n, .0).seconds).toBeCloseTo(1);
  });

  it('should convert to nanos', () => {
    expect(new HPTime(1n, .2).nanos).toBeCloseTo(1.2);
    expect(new HPTime(1000000000n, .0).nanos).toBeCloseTo(1e9);
  });

  it('should convert to timestamps', () => {
    expect(new HPTime(1n, .2).toTPTime('round')).toBe(1n);
    expect(new HPTime(1n, .5).toTPTime('round')).toBe(2n);
    expect(new HPTime(1n, .2).toTPTime('floor')).toBe(1n);
    expect(new HPTime(1n, .5).toTPTime('floor')).toBe(1n);
    expect(new HPTime(1n, .2).toTPTime('ceil')).toBe(2n);
    expect(new HPTime(1n, .5).toTPTime('ceil')).toBe(2n);
  });

  it('should divide', () => {
    let result = mkTime('1').divide(2);
    expect(result.base).toBe(0n);
    expect(result.offset).toBeCloseTo(0.5);

    result = mkTime('1.6').divide(2);
    expect(result.base).toBe(0n);
    expect(result.offset).toBeCloseTo(0.8);

    result = mkTime('-0.5').divide(2);
    expect(result.base).toBe(-1n);
    expect(result.offset).toBeCloseTo(0.75);

    result = mkTime('123.1').divide(123);
    expect(result.base).toBe(1n);
    expect(result.offset).toBeCloseTo(0.000813, 6);
  });

  it('should multiply', () => {
    let result = mkTime('1').multiply(2);
    expect(result.base).toBe(2n);
    expect(result.offset).toBeCloseTo(0);

    result = mkTime('1').multiply(2.5);
    expect(result.base).toBe(2n);
    expect(result.offset).toBeCloseTo(0.5);

    result = mkTime('-0.5').multiply(2);
    expect(result.base).toBe(-1n);
    expect(result.offset).toBeCloseTo(0.0);

    result = mkTime('123.1').multiply(25.5);
    expect(result.base).toBe(3139n);
    expect(result.offset).toBeCloseTo(0.05);
  });

  it('should convert to string', () => {
    expect(mkTime('1.3').toString()).toBe('1.3');
    expect(mkTime('12983423847.332533').toString()).toBe('12983423847.332533');
    expect(new HPTime(234n).toString()).toBe('234');
  });
});

describe('HighPrecisionTimeSpan', () => {
  it('can be constructed from HP time', () => {
    const span = new HPTimeSpan(mkTime('10'), mkTime('20'));
    expect(span.start).toEqual(mkTime('10'));
    expect(span.end).toEqual(mkTime('20'));
  });

  it('can be constructed from integer time', () => {
    const span = new HPTimeSpan(10n, 20n);
    expect(span.start).toEqual(mkTime('10'));
    expect(span.end).toEqual(mkTime('20'));
  });

  it('throws when start is later than end', () => {
    expect(() => new HPTimeSpan(mkTime('0.1'), mkTime('0'))).toThrow();
    expect(() => new HPTimeSpan(mkTime('1124.0001'), mkTime('1124'))).toThrow();
  });

  it('can calc duration', () => {
    let dur = mkSpan('10', '20').duration;
    expect(dur.base).toBe(10n);
    expect(dur.offset).toBeCloseTo(0);

    dur = mkSpan('10.123', '20.456').duration;
    expect(dur.base).toBe(10n);
    expect(dur.offset).toBeCloseTo(0.333);
  });

  it('can calc midpoint', () => {
    let mid = mkSpan('10', '20').midpoint;
    expect(mid.base).toBe(15n);
    expect(mid.offset).toBeCloseTo(0);

    mid = mkSpan('10.25', '16.75').midpoint;
    expect(mid.base).toBe(13n);
    expect(mid.offset).toBeCloseTo(0.5);
  });

  it('can be compared', () => {
    expect(mkSpan('0.1', '34.2').equals(mkSpan('0.1', '34.2'))).toBeTruthy();
    expect(mkSpan('0.1', '34.5').equals(mkSpan('0.1', '34.2'))).toBeFalsy();
    expect(mkSpan('0.9', '34.2').equals(mkSpan('0.1', '34.2'))).toBeFalsy();
  });

  it('checks if span contains another span', () => {
    const x = mkSpan('10', '20');

    expect(x.contains(mkTime('9'))).toBeFalsy();
    expect(x.contains(mkTime('10'))).toBeTruthy();
    expect(x.contains(mkTime('15'))).toBeTruthy();
    expect(x.contains(mkTime('20'))).toBeFalsy();
    expect(x.contains(mkTime('21'))).toBeFalsy();

    expect(x.contains(mkSpan('12', '18'))).toBeTruthy();
    expect(x.contains(mkSpan('5', '25'))).toBeFalsy();
    expect(x.contains(mkSpan('5', '15'))).toBeFalsy();
    expect(x.contains(mkSpan('15', '25'))).toBeFalsy();
    expect(x.contains(mkSpan('0', '10'))).toBeFalsy();
    expect(x.contains(mkSpan('20', '30'))).toBeFalsy();
  });

  it('checks if span intersects another span', () => {
    const x = mkSpan('10', '20');

    expect(x.intersects(mkSpan('0', '10'))).toBeFalsy();
    expect(x.intersects(mkSpan('5', '15'))).toBeTruthy();
    expect(x.intersects(mkSpan('12', '18'))).toBeTruthy();
    expect(x.intersects(mkSpan('15', '25'))).toBeTruthy();
    expect(x.intersects(mkSpan('20', '30'))).toBeFalsy();
    expect(x.intersects(mkSpan('5', '25'))).toBeTruthy();
  });
});