1// Copyright (C) 2018 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 {Time, TimeSpan} from '../base/time'; 16import {getPattern, generateTicks, TickType} from './gridline_helper'; 17 18test('gridline helper to have sensible step sizes', () => { 19 expect(getPattern(1n)).toEqual([1n, '|']); 20 expect(getPattern(2n)).toEqual([2n, '|:']); 21 expect(getPattern(3n)).toEqual([5n, '|....']); 22 expect(getPattern(4n)).toEqual([5n, '|....']); 23 expect(getPattern(5n)).toEqual([5n, '|....']); 24 expect(getPattern(7n)).toEqual([10n, '|....:....']); 25 26 expect(getPattern(10n)).toEqual([10n, '|....:....']); 27 expect(getPattern(20n)).toEqual([20n, '|.:.']); 28 expect(getPattern(50n)).toEqual([50n, '|....']); 29 30 expect(getPattern(100n)).toEqual([100n, '|....:....']); 31}); 32 33describe('generateTicks', () => { 34 it('can generate ticks with span starting at origin', () => { 35 const tickGen = generateTicks( 36 new TimeSpan(Time.fromRaw(0n), Time.fromRaw(10n)), 37 1, 38 ); 39 const expected = [ 40 {type: TickType.MAJOR, time: 0n}, 41 {type: TickType.MINOR, time: 1n}, 42 {type: TickType.MINOR, time: 2n}, 43 {type: TickType.MINOR, time: 3n}, 44 {type: TickType.MINOR, time: 4n}, 45 {type: TickType.MEDIUM, time: 5n}, 46 {type: TickType.MINOR, time: 6n}, 47 {type: TickType.MINOR, time: 7n}, 48 {type: TickType.MINOR, time: 8n}, 49 {type: TickType.MINOR, time: 9n}, 50 ]; 51 const actual = Array.from(tickGen!); 52 expect(actual).toStrictEqual(expected); 53 }); 54 55 it('can generate ticks when span has an offset', () => { 56 const tickGen = generateTicks( 57 new TimeSpan(Time.fromRaw(10n), Time.fromRaw(20n)), 58 1, 59 ); 60 const expected = [ 61 {type: TickType.MAJOR, time: 10n}, 62 {type: TickType.MINOR, time: 11n}, 63 {type: TickType.MINOR, time: 12n}, 64 {type: TickType.MINOR, time: 13n}, 65 {type: TickType.MINOR, time: 14n}, 66 {type: TickType.MEDIUM, time: 15n}, 67 {type: TickType.MINOR, time: 16n}, 68 {type: TickType.MINOR, time: 17n}, 69 {type: TickType.MINOR, time: 18n}, 70 {type: TickType.MINOR, time: 19n}, 71 ]; 72 const actual = Array.from(tickGen!); 73 expect(actual).toStrictEqual(expected); 74 }); 75 76 it('can generate ticks when span is large', () => { 77 const tickGen = generateTicks( 78 new TimeSpan(Time.fromRaw(1000000000n), Time.fromRaw(2000000000n)), 79 1, 80 ); 81 const expected = [ 82 {type: TickType.MAJOR, time: 1000000000n}, 83 {type: TickType.MINOR, time: 1100000000n}, 84 {type: TickType.MINOR, time: 1200000000n}, 85 {type: TickType.MINOR, time: 1300000000n}, 86 {type: TickType.MINOR, time: 1400000000n}, 87 {type: TickType.MEDIUM, time: 1500000000n}, 88 {type: TickType.MINOR, time: 1600000000n}, 89 {type: TickType.MINOR, time: 1700000000n}, 90 {type: TickType.MINOR, time: 1800000000n}, 91 {type: TickType.MINOR, time: 1900000000n}, 92 ]; 93 const actual = Array.from(tickGen!); 94 expect(actual).toStrictEqual(expected); 95 }); 96 97 it('throws an error when timespan duration is 0', () => { 98 expect(() => { 99 Array.from(generateTicks(TimeSpan.ZERO, 1)); 100 }).toThrow(Error); 101 }); 102 103 it('throws an error when max ticks is 0', () => { 104 const nonZeroTimeSpan = new TimeSpan(Time.fromRaw(0n), Time.fromRaw(1n)); 105 expect(() => { 106 Array.from(generateTicks(nonZeroTimeSpan, 0)); 107 }).toThrow(Error); 108 }); 109}); 110