From cad85c76676cff7fe8c47ccb8d332809f7276e28 Mon Sep 17 00:00:00 2001 From: MAZE Date: Wed, 19 Jun 2024 14:18:47 +0430 Subject: [PATCH] test: write tests for random helper --- src/helpers/random.ts | 6 ++- src/helpers/tests/random.test.ts | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/helpers/tests/random.test.ts diff --git a/src/helpers/random.ts b/src/helpers/random.ts index d150965..0da3c34 100644 --- a/src/helpers/random.ts +++ b/src/helpers/random.ts @@ -28,7 +28,11 @@ export function randomInt(min: number, max: number): number { * @returns {T} A random element from the array. */ export function pick(array: Array): T { - const randomIndex = random(0, array.length); + if (array.length === 0) { + throw new Error("The array shouldn't be empty"); + } + + const randomIndex = randomInt(0, array.length); return array[randomIndex]; } diff --git a/src/helpers/tests/random.test.ts b/src/helpers/tests/random.test.ts new file mode 100644 index 0000000..ea87cd6 --- /dev/null +++ b/src/helpers/tests/random.test.ts @@ -0,0 +1,89 @@ +import { describe, it, expect } from 'vitest'; +import { random, randomInt, pick, pickMany, shuffle } from '../random'; + +describe('random function', () => { + it('should generate a number between min (inclusive) and max (exclusive)', () => { + const min = 1; + const max = 10; + for (let i = 0; i < 100; i++) { + const result = random(min, max); + expect(result).toBeGreaterThanOrEqual(min); + expect(result).toBeLessThan(max); + } + }); +}); + +describe('randomInt function', () => { + it('should generate an integer between min (inclusive) and max (exclusive)', () => { + const min = 1; + const max = 10; + for (let i = 0; i < 100; i++) { + const result = randomInt(min, max); + expect(result).toBeGreaterThanOrEqual(min); + expect(result).toBeLessThan(max); + expect(Number.isInteger(result)).toBe(true); + } + }); +}); + +describe('pick function', () => { + it('should pick a random element from the array', () => { + const array = [1, 2, 3, 4, 5]; + const result = pick(array); + expect(array).toContain(result); + }); + + it('should handle an array with one element', () => { + const array = [1]; + const result = pick(array); + expect(result).toBe(1); + }); + + it('should throw an error when picking from an empty array', () => { + const array: unknown[] = []; + expect(() => pick(array)).toThrow(); + }); +}); + +describe('pickMany function', () => { + it('should pick the specified number of random elements from the array', () => { + const array = [1, 2, 3, 4, 5]; + const count = 3; + const result = pickMany(array, count); + expect(result).toHaveLength(count); + result.forEach(element => { + expect(array).toContain(element); + }); + }); + + it('should handle picking more elements than in the array', () => { + const array = [1, 2, 3]; + const count = 5; + const result = pickMany(array, count); + expect(result).toHaveLength(array.length); + }); +}); + +describe('shuffle function', () => { + it('should shuffle the elements of the array', () => { + const array = [1, 2, 3, 4, 5]; + const result = shuffle(array); + expect(result).toHaveLength(array.length); + expect(result).not.toEqual(array); // It's possible for the arrays to be equal, but this is highly unlikely + array.forEach(element => { + expect(result).toContain(element); + }); + }); + + it('should handle an empty array', () => { + const array: unknown[] = []; + const result = shuffle(array); + expect(result).toHaveLength(0); + }); + + it('should handle an array with one element', () => { + const array = [1]; + const result = shuffle(array); + expect(result).toEqual(array); + }); +});