| <!doctype html> |
| <html> |
| <head> |
| <script src="../../http/tests/inspector/resources/inspector-test.js"></script> |
| <script> |
| function test() |
| { |
| // This is the floating point precision error value from: `0.3 - 0.2 - 0.1` |
| const superSmallValue = -2.7755575615628914e-17; |
| |
| let suite = InspectorTest.createSyncSuite("NumberUtilities"); |
| |
| suite.addTestCase({ |
| name: "Number.constrain", |
| test() { |
| InspectorTest.expectEqual(Number.constrain(0, -1, 1), 0, "constrain of a value between min and max does not change"); |
| InspectorTest.expectEqual(Number.constrain(0.5, -1, 1), 0.5, "constrain of a value between min and max does not change"); |
| InspectorTest.expectEqual(Number.constrain(1, -1, 1), 1, "constrain of a value between min and max does not change"); |
| InspectorTest.expectEqual(Number.constrain(-1, -1, 1), -1, "constrain of a value between min and max does not change"); |
| |
| InspectorTest.expectEqual(Number.constrain(-1.01, -1, 1), -1, "constrain of a value below min becomes min"); |
| InspectorTest.expectEqual(Number.constrain(-2, -1, 1), -1, "constrain of a value below min becomes min"); |
| InspectorTest.expectEqual(Number.constrain(-Infinity, -1, 1), -1, "constrain of a value below min becomes min"); |
| |
| InspectorTest.expectEqual(Number.constrain(1.01, -1, 1), 1, "constrain of a value above max becomes max"); |
| InspectorTest.expectEqual(Number.constrain(2, -1, 1), 1, "constrain of a value above max becomes max"); |
| InspectorTest.expectEqual(Number.constrain(Infinity, -1, 1), 1, "constrain of a value above max becomes max"); |
| |
| InspectorTest.expectEqual(Number.constrain(NaN, -1, 1), -1, "constrain of NaN becomes min"); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Number.secondsToString", |
| test() { |
| // Normal resolution. |
| InspectorTest.expectEqual(Number.secondsToString(0, false), "0ms", "normal resolution of 0ms should be ms with no decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(0.000123456, false), "0.12ms", "normal resolution of sub 1ms should be ms"); |
| InspectorTest.expectEqual(Number.secondsToString(0.00123456, false), "1.23ms", "normal resolution of sub 10ms should be ms"); |
| InspectorTest.expectEqual(Number.secondsToString(0.0123456, false), "12.3ms", "normal resolution of sub 100ms should be ms"); |
| InspectorTest.expectEqual(Number.secondsToString(0.123456, false), "123ms", "normal resolution of greater than 100ms but sub 1s should be ms"); |
| InspectorTest.expectEqual(Number.secondsToString(1.123456, false), "1.12s", "normal resolution of greater than 1s but sub 1min should be seconds"); |
| InspectorTest.expectEqual(Number.secondsToString(30.123456, false), "30.12s", "normal resolution of greater than 1s but sub 1min should be seconds"); |
| InspectorTest.expectEqual(Number.secondsToString(60.123456, false), "1.0min", "normal resolution of greater than 1min but sub 1hr should be minutes"); |
| InspectorTest.expectEqual(Number.secondsToString(100.123456, false), "1.7min", "normal resolution of greater than 1min but sub 1hr should be minutes"); |
| InspectorTest.expectEqual(Number.secondsToString(12345, false), "3.4hrs", "normal resolution of greater than 1hr but sub 1 day should be hrs"); |
| InspectorTest.expectEqual(Number.secondsToString(123456, false), "1.4 days", "normal resolution of greater than 1 day should be days"); |
| InspectorTest.expectEqual(Number.secondsToString(1234567, false), "14.3 days", "normal resolution of greater than 1 day should be days"); |
| InspectorTest.expectEqual(Number.secondsToString(superSmallValue, false), "0ms", "normal resolution of super small value should be ms with no decimals"); |
| |
| // High resolution. |
| InspectorTest.expectEqual(Number.secondsToString(0, true), "0ms", "high resolution of 0ms should be ms with no decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(0.000123456, true), "0.123ms", "high resolution of sub 1ms should be ms with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(0.00123456, true), "1.235ms", "high resolution of sub 10ms should be ms with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(0.0123456, true), "12.35ms", "high resolution of sub 100ms should be ms with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(0.123456, true), "123.5ms", "high resolution of greater than 100ms but sub 1s should be ms with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(1.123456, true), "1.12s", "high resolution of greater than 1s should be seconds with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(30.123456, true), "30.12s", "high resolution of greater than 1s should be seconds with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(60.123456, true), "60.12s", "high resolution of greater than 1s should be seconds with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(100.123456, true), "100.12s", "high resolution greater than 1s should be seconds with decimals"); |
| InspectorTest.expectEqual(Number.secondsToString(superSmallValue, true), "0ms", "high resolution of super small value should be ms with no decimals"); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Number.bytesToString", |
| test() { |
| const kb = 1024; |
| const mb = kb * 1024; |
| |
| // Normal resolution. |
| InspectorTest.expectEqual(Number.bytesToString(123, false), "123 B", "normal resolution of sub 1k should be bytes"); |
| InspectorTest.expectEqual(Number.bytesToString(1.23 * kb, false), "1.23 KB", "normal resolution of sub 10k should be kilobytes"); |
| InspectorTest.expectEqual(Number.bytesToString(10.123 * kb, false), "10.1 KB", "normal resolution of sub 10k should be kilobytes"); |
| InspectorTest.expectEqual(Number.bytesToString(1.123 * mb, false), "1.12 MB", "normal resolution of sub 10mb should be megabytes"); |
| InspectorTest.expectEqual(Number.bytesToString(10.123 * mb, false), "10.1 MB", "normal resolution of greater than 10mb should be megabytes"); |
| |
| // High resolution. |
| InspectorTest.expectEqual(Number.bytesToString(123, true), "123 B", "high resolution of sub 1k should be bytes"); |
| InspectorTest.expectEqual(Number.bytesToString(1.23 * kb, true), "1.23 KB", "high resolution of sub 10k should be kilobytes"); |
| InspectorTest.expectEqual(Number.bytesToString(10.123 * kb, true), "10.12 KB", "high resolution of sub 10k should be kilobytes"); |
| InspectorTest.expectEqual(Number.bytesToString(1.123 * mb, true), "1.12 MB", "high resolution of sub 10mb should be megabytes"); |
| InspectorTest.expectEqual(Number.bytesToString(10.123 * mb, true), "10.12 MB", "high resolution of greater than 10mb should be megabytes"); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Number.percentageString", |
| test() { |
| InspectorTest.expectEqual(Number.percentageString(1 / 3), "33.3%", "precision should default to 1 if unspecified"); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Number.zeroPad", |
| test() { |
| InspectorTest.expectEqual(Number.zeroPad(1, 0), "1", "padding of 0 should add no zeros"); |
| InspectorTest.expectEqual(Number.zeroPad(1, 1), "1", "padding of 1 with one digit should add no zeros"); |
| InspectorTest.expectEqual(Number.zeroPad(1, 2), "01", "padding of 2 with one digit should add one zero"); |
| |
| InspectorTest.expectEqual(Number.zeroPad(10, 1), "10", "padding of 1 with two digits should add no zeros"); |
| InspectorTest.expectEqual(Number.zeroPad(10, 2), "10", "padding of 2 with two digits should add no zeros"); |
| InspectorTest.expectEqual(Number.zeroPad(10, 3), "010", "padding of 3 with two digits should add one zero"); |
| InspectorTest.expectEqual(Number.zeroPad(10, 4), "0010", "padding of 4 with two digits should add two zeros"); |
| |
| InspectorTest.expectEqual(Number.zeroPad(1.1, 2), "1.1", "padding of 2 with one decimal should add no zeros"); |
| InspectorTest.expectEqual(Number.zeroPad(1.1, 3), "1.1", "padding of 3 with one decimal should add no zeros"); |
| InspectorTest.expectEqual(Number.zeroPad(1.1, 4), "01.1", "padding of 4 with one decimal should add one zero"); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Number.countDigits", |
| test() { |
| InspectorTest.expectEqual(Number.countDigits(0), 1, "0 should have 1 digit"); |
| InspectorTest.expectEqual(Number.countDigits(-0), 1, "-0 should have 1 digit"); |
| for (let i = 1; i < 10; ++i) { |
| let digits = i + 1; |
| let num = 10 ** i; |
| InspectorTest.expectEqual(Number.countDigits(num), digits, `${num} should have ${digits} digits`); |
| InspectorTest.expectEqual(Number.countDigits(-num), digits, `${-num} should have ${digits} digits`); |
| } |
| |
| return true; |
| } |
| }); |
| |
| suite.runTestCasesAndFinish(); |
| } |
| </script> |
| </head> |
| <body onLoad="runTest()"> |
| </body> |
| </html> |