aboutsummaryrefslogtreecommitdiff
path: root/ui/src/frontend/gridline_helper_unittest.ts
blob: 3b6dcacada357a4e16fc48602bbd17c687a39beb (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
309
310
311
312
313
314
// Copyright (C) 2018 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 {TimeSpan} from '../common/time';

import {getStepSize, Tick, TickGenerator, TickType} from './gridline_helper';
import {TimeScale} from './time_scale';

const pattern1 = '|....:....';
const pattern2 = '|.:.';
const pattern5 = '|....';
const timeScale = new TimeScale(new TimeSpan(0, 1), [1, 2]);

test('gridline helper to have sensible step sizes', () => {
  expect(getStepSize(10, 14)).toEqual([1, pattern1]);
  expect(getStepSize(30, 14)).toEqual([5, pattern5]);
  expect(getStepSize(60, 14)).toEqual([5, pattern5]);
  expect(getStepSize(100, 14)).toEqual([10, pattern1]);

  expect(getStepSize(10, 21)).toEqual([0.5, pattern5]);
  expect(getStepSize(30, 21)).toEqual([2, pattern2]);
  expect(getStepSize(60, 21)).toEqual([5, pattern5]);
  expect(getStepSize(100, 21)).toEqual([5, pattern5]);

  expect(getStepSize(10, 3)).toEqual([5, pattern5]);
  expect(getStepSize(30, 3)).toEqual([10, pattern1]);
  expect(getStepSize(60, 3)).toEqual([20, pattern2]);
  expect(getStepSize(100, 3)).toEqual([50, pattern5]);

  expect(getStepSize(800, 4)).toEqual([200, pattern2]);
});

test('gridline helper to scale to very small and very large values', () => {
  expect(getStepSize(.01, 14)).toEqual([.001, pattern1]);
  expect(getStepSize(10000, 14)).toEqual([1000, pattern1]);
});

test('gridline helper to always return a reasonable number of steps', () => {
  for (let i = 1; i <= 1000; i++) {
    const [stepSize, _] = getStepSize(i, 14);
    expect(Math.round(i / stepSize)).toBeGreaterThanOrEqual(6);
    expect(Math.round(i / stepSize)).toBeLessThanOrEqual(14);
  }
});

describe('TickGenerator with range 0.0-1.0 and room for 2 labels', () => {
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(0.0, 1.0);
    const timeScale = new TimeScale(timeSpan, [0, 200]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should produce major ticks at 0.5s and minor ticks at 0.1s starting at 0',
     () => {
       const expected = [
         {type: TickType.MAJOR, time: 0.0},
         {type: TickType.MINOR, time: 0.1},
         {type: TickType.MINOR, time: 0.2},
         {type: TickType.MINOR, time: 0.3},
         {type: TickType.MINOR, time: 0.4},
         {type: TickType.MAJOR, time: 0.5},
         {type: TickType.MINOR, time: 0.6},
         {type: TickType.MINOR, time: 0.7},
         {type: TickType.MINOR, time: 0.8},
         {type: TickType.MINOR, time: 0.9},
       ];
       const actual = Array.from(tickGen!);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 1 decimal place for labels', () => {
    expect(tickGen!.digits).toEqual(1);
  });
});

describe('TickGenerator with range 0.3-1.3 and room for 2 labels', () => {
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(0.3, 1.3);
    const timeScale = new TimeScale(timeSpan, [0, 200]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should produce major ticks at 0.5s and minor ticks at 0.1s starting at 0',
     () => {
       const expected = [
         {type: TickType.MINOR, time: 0.3},
         {type: TickType.MINOR, time: 0.4},
         {type: TickType.MAJOR, time: 0.5},
         {type: TickType.MINOR, time: 0.6},
         {type: TickType.MINOR, time: 0.7},
         {type: TickType.MINOR, time: 0.8},
         {type: TickType.MINOR, time: 0.9},
         {type: TickType.MAJOR, time: 1.0},
         {type: TickType.MINOR, time: 1.1},
         {type: TickType.MINOR, time: 1.2},
       ];
       const actual = Array.from(tickGen!);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 1 decimal place for labels', () => {
    expect(tickGen!.digits).toEqual(1);
  });
});

describe('TickGenerator with range 0.0-0.2 and room for 1 label', () => {
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(0.0, 0.2);
    const timeScale = new TimeScale(timeSpan, [0, 100]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should produce major ticks at 0.2s and minor ticks at 0.1s starting at 0',
     () => {
       const expected = [
         {type: TickType.MAJOR, time: 0.0},
         {type: TickType.MINOR, time: 0.05},
         {type: TickType.MEDIUM, time: 0.1},
         {type: TickType.MINOR, time: 0.15},
       ];
       const actual = Array.from(tickGen!);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 1 decimal place for labels', () => {
    expect(tickGen!.digits).toEqual(1);
  });
});

describe('TickGenerator with range 0.0-0.1 and room for 1 label', () => {
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(0.0, 0.1);
    const timeScale = new TimeScale(timeSpan, [0, 100]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should produce major ticks at 0.1s & minor ticks at 0.02s starting at 0',
     () => {
       const expected = [
         {type: TickType.MAJOR, time: 0.0},
         {type: TickType.MINOR, time: 0.01},
         {type: TickType.MINOR, time: 0.02},
         {type: TickType.MINOR, time: 0.03},
         {type: TickType.MINOR, time: 0.04},
         {type: TickType.MEDIUM, time: 0.05},
         {type: TickType.MINOR, time: 0.06},
         {type: TickType.MINOR, time: 0.07},
         {type: TickType.MINOR, time: 0.08},
         {type: TickType.MINOR, time: 0.09},
       ];
       const actual = Array.from(tickGen!);
       expect(tickGen!.digits).toEqual(1);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 1 decimal place for labels', () => {
    expect(tickGen!.digits).toEqual(1);
  });
});

describe('TickGenerator with a very small timespan', () => {
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(0.0, 1e-9);
    const timeScale = new TimeScale(timeSpan, [0, 100]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should generate minor ticks at 2e-10s and one major tick at the start',
     () => {
       const expected = [
         {type: TickType.MAJOR, time: 0.0},
         {type: TickType.MINOR, time: 1e-10},
         {type: TickType.MINOR, time: 2e-10},
         {type: TickType.MINOR, time: 3e-10},
         {type: TickType.MINOR, time: 4e-10},
         {type: TickType.MEDIUM, time: 5e-10},
         {type: TickType.MINOR, time: 6e-10},
         {type: TickType.MINOR, time: 7e-10},
         {type: TickType.MINOR, time: 8e-10},
         {type: TickType.MINOR, time: 9e-10},
       ];
       const actual = Array.from(tickGen!);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 9 decimal places for labels', () => {
    expect(tickGen!.digits).toEqual(9);
  });
});

describe('TickGenerator with a very large timespan', () => {
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(0.0, 1e9);
    const timeScale = new TimeScale(timeSpan, [0, 100]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should generate minor ticks at 2e8 and one major tick at the start',
     () => {
       const expected = [
         {type: TickType.MAJOR, time: 0.0},
         {type: TickType.MINOR, time: 1e8},
         {type: TickType.MINOR, time: 2e8},
         {type: TickType.MINOR, time: 3e8},
         {type: TickType.MINOR, time: 4e8},
         {type: TickType.MEDIUM, time: 5e8},
         {type: TickType.MINOR, time: 6e8},
         {type: TickType.MINOR, time: 7e8},
         {type: TickType.MINOR, time: 8e8},
         {type: TickType.MINOR, time: 9e8},
       ];
       const actual = Array.from(tickGen!);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 0 decimal places for labels', () => {
    expect(tickGen!.digits).toEqual(0);
  });
});

describe('TickGenerator where the timespan has a dynamic range of 1e12', () => {
  // This is the equivalent of zooming in to the nanosecond level, 1000 seconds
  // into a trace Note: this is about the limit of what this generator can
  // handle.
  let tickGen: TickGenerator|undefined = undefined;
  beforeAll(() => {
    const timeSpan = new TimeSpan(1000, 1000.000000001);
    const timeScale = new TimeScale(timeSpan, [0, 100]);
    tickGen = new TickGenerator(timeScale, {minLabelPx: 100});
  });
  it('should generate minor ticks at 1e-10s and one major tick at the start',
     () => {
       const expected = [
         {type: TickType.MAJOR, time: 1000.0000000000},
         {type: TickType.MINOR, time: 1000.0000000001},
         {type: TickType.MINOR, time: 1000.0000000002},
         {type: TickType.MINOR, time: 1000.0000000003},
         {type: TickType.MINOR, time: 1000.0000000004},
         {type: TickType.MEDIUM, time: 1000.0000000005},
         {type: TickType.MINOR, time: 1000.0000000006},
         {type: TickType.MINOR, time: 1000.0000000007},
         {type: TickType.MINOR, time: 1000.0000000008},
         {type: TickType.MINOR, time: 1000.0000000009},
       ];
       const actual = Array.from(tickGen!);
       expectTicksEqual(actual, expected);
     });
  it('should tell us to use 9 decimal places for labels', () => {
    expect(tickGen!.digits).toEqual(9);
  });
});

describe(
    'TickGenerator where the timespan has a ridiculously huge dynamic range',
    () => {
      // We don't expect this to work, just wanna make sure it doesn't crash or
      // get stuck
      it('should not crash or get stuck in an infinite loop', () => {
        const timeSpan = new TimeSpan(1000, 1000.000000000001);
        const timeScale = new TimeScale(timeSpan, [0, 100]);
        new TickGenerator(timeScale);
      });
    });

describe(
    'TickGenerator where the timespan has a ridiculously huge dynamic range',
    () => {
      // We don't expect this to work, just wanna make sure it doesn't crash or
      // get stuck
      it('should not crash or get stuck in an infinite loop', () => {
        const timeSpan = new TimeSpan(1000, 1000.000000000001);
        const timeScale = new TimeScale(timeSpan, [0, 100]);
        new TickGenerator(timeScale);
      });
    });

test('TickGenerator constructed with a 0 width throws an error', () => {
  expect(() => {
    const timeScale = new TimeScale(new TimeSpan(0.0, 1.0), [0, 0]);
    new TickGenerator(timeScale);
  }).toThrow(Error);
});

test(
    'TickGenerator constructed with desiredPxPerStep of 0 throws an error',
    () => {
      expect(() => {
        new TickGenerator(timeScale, {minLabelPx: 0});
      }).toThrow(Error);
    });

test('TickGenerator constructed with a 0 duration throws an error', () => {
  expect(() => {
    const timeScale = new TimeScale(new TimeSpan(0.0, 0.0), [0, 1]);
    new TickGenerator(timeScale);
  }).toThrow(Error);
});

function expectTicksEqual(actual: Tick[], expected: any[]) {
  // TODO(stevegolton) We could write a custom matcher for this; this approach
  // produces cryptic error messages.
  expect(actual.length).toEqual(expected.length);
  for (let i = 0; i < actual.length; ++i) {
    const ex = expected[i];
    const ac = actual[i];
    expect(ac.type).toEqual(ex.type);
    expect(ac.time).toBeCloseTo(ex.time, 9);
  }
}