Path: blob/main/src/vs/base/test/common/numbers.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';7import { isPointWithinTriangle, randomInt } from '../../common/numbers.js';89suite('isPointWithinTriangle', () => {10ensureNoDisposablesAreLeakedInTestSuite();1112test('should return true if the point is within the triangle', () => {13const result = isPointWithinTriangle(0.25, 0.25, 0, 0, 1, 0, 0, 1);14assert.ok(result);15});1617test('should return false if the point is outside the triangle', () => {18const result = isPointWithinTriangle(2, 2, 0, 0, 1, 0, 0, 1);19assert.ok(!result);20});2122test('should return true if the point is on the edge of the triangle', () => {23const result = isPointWithinTriangle(0.5, 0, 0, 0, 1, 0, 0, 1);24assert.ok(result);25});26});2728suite('randomInt', () => {29ensureNoDisposablesAreLeakedInTestSuite();3031/**32* Test helper that allows to run a test on the `randomInt()`33* utility with specified `max` and `min` values.34*/35const testRandomIntUtil = (max: number, min: number | undefined, testName: string) => {36suite(testName, () => {37let i = 0;38while (++i < 5) {39test(`should generate random boolean attempt#${i}`, async () => {40let iterations = 100;41while (iterations-- > 0) {42const int = randomInt(max, min);4344assert(45int <= max,46`Expected ${int} to be less than or equal to ${max}.`47);48assert(49int >= (min ?? 0),50`Expected ${int} to be greater than or equal to ${min ?? 0}.`,51);52}53});54}5556test('should include min and max', async () => {57let iterations = 125;58const results = [];59while (iterations-- > 0) {60results.push(randomInt(max, min));61}6263assert(64results.includes(max),65`Expected ${results} to include ${max}.`,66);67assert(68results.includes(min ?? 0),69`Expected ${results} to include ${min ?? 0}.`,70);71});72});73};7475suite('positive numbers', () => {76testRandomIntUtil(4, 2, 'max: 4, min: 2');77testRandomIntUtil(4, 0, 'max: 4, min: 0');78testRandomIntUtil(4, undefined, 'max: 4, min: undefined');79testRandomIntUtil(1, 0, 'max: 0, min: 0');80});8182suite('negative numbers', () => {83testRandomIntUtil(-2, -5, 'max: -2, min: -5');84testRandomIntUtil(0, -5, 'max: 0, min: -5');85testRandomIntUtil(0, -1, 'max: 0, min: -1');86});8788suite('split numbers', () => {89testRandomIntUtil(3, -1, 'max: 3, min: -1');90testRandomIntUtil(2, -2, 'max: 2, min: -2');91testRandomIntUtil(1, -3, 'max: 2, min: -2');92});9394suite('errors', () => {95test('should throw if "min" is == "max" #1', () => {96assert.throws(() => {97randomInt(200, 200);98}, `"max"(200) param should be greater than "min"(200)."`);99});100101test('should throw if "min" is == "max" #2', () => {102assert.throws(() => {103randomInt(2, 2);104}, `"max"(2) param should be greater than "min"(2)."`);105});106107test('should throw if "min" is == "max" #3', () => {108assert.throws(() => {109randomInt(0);110}, `"max"(0) param should be greater than "min"(0)."`);111});112113test('should throw if "min" is > "max" #1', () => {114assert.throws(() => {115randomInt(2, 3);116}, `"max"(2) param should be greater than "min"(3)."`);117});118119test('should throw if "min" is > "max" #2', () => {120assert.throws(() => {121randomInt(999, 2000);122}, `"max"(999) param should be greater than "min"(2000)."`);123});124125test('should throw if "min" is > "max" #3', () => {126assert.throws(() => {127randomInt(0, 1);128}, `"max"(0) param should be greater than "min"(1)."`);129});130131test('should throw if "min" is > "max" #4', () => {132assert.throws(() => {133randomInt(-5, 2);134}, `"max"(-5) param should be greater than "min"(2)."`);135});136137test('should throw if "min" is > "max" #5', () => {138assert.throws(() => {139randomInt(-4, 0);140}, `"max"(-4) param should be greater than "min"(0)."`);141});142143test('should throw if "min" is > "max" #6', () => {144assert.throws(() => {145randomInt(-4);146}, `"max"(-4) param should be greater than "min"(0)."`);147});148149test('should throw if "max" is `NaN`', () => {150assert.throws(() => {151randomInt(NaN);152}, `"max" param is not a number."`);153});154155test('should throw if "min" is `NaN`', () => {156assert.throws(() => {157randomInt(4, NaN);158}, `"min" param is not a number."`);159});160161suite('infinite arguments', () => {162test('should throw if "max" is infinite [Infinity]', () => {163assert.throws(() => {164randomInt(Infinity);165}, `"max" param is not finite."`);166});167168test('should throw if "max" is infinite [-Infinity]', () => {169assert.throws(() => {170randomInt(-Infinity);171}, `"max" param is not finite."`);172});173174test('should throw if "max" is infinite [+Infinity]', () => {175assert.throws(() => {176randomInt(+Infinity);177}, `"max" param is not finite."`);178});179180test('should throw if "min" is infinite [Infinity]', () => {181assert.throws(() => {182randomInt(Infinity, Infinity);183}, `"max" param is not finite."`);184});185186test('should throw if "min" is infinite [-Infinity]', () => {187assert.throws(() => {188randomInt(Infinity, -Infinity);189}, `"max" param is not finite."`);190});191192test('should throw if "min" is infinite [+Infinity]', () => {193assert.throws(() => {194randomInt(Infinity, +Infinity);195}, `"max" param is not finite."`);196});197});198});199});200201202