github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/util/format.spec.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 import { assert } from "chai"; 12 import { 13 DurationFitScale, 14 durationUnits, 15 BytesFitScale, 16 byteUnits, 17 } from "./format"; 18 19 describe("Format utils", () => { 20 describe("DurationFitScale", () => { 21 it("converts nanoseconds to provided units", () => { 22 // test zero values 23 assert.equal(DurationFitScale(durationUnits[0])(undefined), "0.00 ns"); 24 assert.equal(DurationFitScale(durationUnits[0])(0), "0.00 ns"); 25 // "ns", "µs", "ms", "s" 26 assert.equal(DurationFitScale(durationUnits[0])(32), "32.00 ns"); 27 assert.equal(DurationFitScale(durationUnits[1])(32120), "32.12 µs"); 28 assert.equal(DurationFitScale(durationUnits[2])(32122300), "32.12 ms"); 29 assert.equal(DurationFitScale(durationUnits[3])(32122343000), "32.12 s"); 30 }); 31 }); 32 33 describe("BytesFitScale", () => { 34 it("converts bytes to provided units", () => { 35 // test zero values 36 assert.equal(BytesFitScale(byteUnits[0])(undefined), "0.00 B"); 37 assert.equal(BytesFitScale(byteUnits[0])(0), "0.00 B"); 38 // "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" 39 assert.equal(BytesFitScale(byteUnits[0])(1), "1.00 B"); 40 assert.equal(BytesFitScale(byteUnits[1])(10240), "10.00 KiB"); 41 assert.equal(BytesFitScale(byteUnits[2])(12582912), "12.00 MiB"); 42 assert.equal(BytesFitScale(byteUnits[3])(12884901888), "12.00 GiB"); 43 assert.equal(BytesFitScale(byteUnits[4])(1.319414e13), "12.00 TiB"); 44 assert.equal(BytesFitScale(byteUnits[5])(1.3510799e16), "12.00 PiB"); 45 assert.equal(BytesFitScale(byteUnits[6])(1.3835058e19), "12.00 EiB"); 46 assert.equal(BytesFitScale(byteUnits[7])(1.4167099e22), "12.00 ZiB"); 47 assert.equal(BytesFitScale(byteUnits[8])(1.450711e25), "12.00 YiB"); 48 }); 49 }); 50 });