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