mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -1,22 +1,22 @@
|
||||
import { minimumEditDistance } from '../EditDistance'
|
||||
|
||||
test('minimumEditDistance(kitten, sitten) => 1', () => {
|
||||
const str1 = 'kitten'
|
||||
const str2 = 'sitten'
|
||||
const res = minimumEditDistance(str1, str2)
|
||||
expect(res).toEqual(1)
|
||||
})
|
||||
|
||||
test('minimumEditDistance(school, skull) => 4', () => {
|
||||
const str1 = 'school'
|
||||
const str2 = 'skull'
|
||||
const res = minimumEditDistance(str1, str2)
|
||||
expect(res).toEqual(4)
|
||||
})
|
||||
|
||||
test('minimumEditDistance(Algorithm, Algorithm) => 0', () => {
|
||||
const str1 = 'Algorithm'
|
||||
const str2 = 'Algorithm'
|
||||
const res = minimumEditDistance(str1, str2)
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
import { minimumEditDistance } from '../EditDistance'
|
||||
|
||||
test('minimumEditDistance(kitten, sitten) => 1', () => {
|
||||
const str1 = 'kitten'
|
||||
const str2 = 'sitten'
|
||||
const res = minimumEditDistance(str1, str2)
|
||||
expect(res).toEqual(1)
|
||||
})
|
||||
|
||||
test('minimumEditDistance(school, skull) => 4', () => {
|
||||
const str1 = 'school'
|
||||
const str2 = 'skull'
|
||||
const res = minimumEditDistance(str1, str2)
|
||||
expect(res).toEqual(4)
|
||||
})
|
||||
|
||||
test('minimumEditDistance(Algorithm, Algorithm) => 0', () => {
|
||||
const str1 = 'Algorithm'
|
||||
const str2 = 'Algorithm'
|
||||
const res = minimumEditDistance(str1, str2)
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
@ -26,7 +26,11 @@ describe('LongestCommonSubsequence', () => {
|
||||
})
|
||||
|
||||
it('expects to return the longest common subsequence, medium-length inputs', () => {
|
||||
expect(longestCommonSubsequence('bsbininm', 'jmjkbkjkv')).toEqual('b'.length)
|
||||
expect(longestCommonSubsequence('oxcpqrsvwf', 'shmtulqrypy')).toEqual('qr'.length)
|
||||
expect(longestCommonSubsequence('bsbininm', 'jmjkbkjkv')).toEqual(
|
||||
'b'.length
|
||||
)
|
||||
expect(longestCommonSubsequence('oxcpqrsvwf', 'shmtulqrypy')).toEqual(
|
||||
'qr'.length
|
||||
)
|
||||
})
|
||||
})
|
||||
|
@ -21,7 +21,7 @@ describe('MaxProductOfThree', () => {
|
||||
describe('MaxProductOfThree, random arrays of size 3 to 5', () => {
|
||||
// Slower function that operates in O(n^3), where n is the length of the input array.
|
||||
// Calculates all possible products of 3 numbers in the array and returns the largest
|
||||
function completeMaxThree (array) {
|
||||
function completeMaxThree(array) {
|
||||
let maximumProduct = null
|
||||
for (let i = 0; i < array.length - 2; i++) {
|
||||
for (let j = i + 1; j < array.length - 1; j++) {
|
||||
@ -47,7 +47,9 @@ describe('MaxProductOfThree, random arrays of size 3 to 5', () => {
|
||||
for (let i = 0; i < numberOfRandomTests; i++) {
|
||||
const arr = []
|
||||
// Randomize the length of the array in the current test
|
||||
const length = Math.floor(Math.random() * (maxLength - minLength) + minLength)
|
||||
const length = Math.floor(
|
||||
Math.random() * (maxLength - minLength) + minLength
|
||||
)
|
||||
|
||||
// Fill the array with random values in the specified range
|
||||
for (let j = 0; j < length + 1; j++) {
|
||||
@ -58,13 +60,19 @@ describe('MaxProductOfThree, random arrays of size 3 to 5', () => {
|
||||
const expectedProduct = completeMaxThree(arr)
|
||||
|
||||
// Set up the expectation
|
||||
it('Expect the array ' + arr.toString() + ' to return the maximum three product of ' + expectedProduct, () => {
|
||||
// Calculate the max three product using the function being tested
|
||||
const actualProduct = maxProductOfThree(arr)
|
||||
it(
|
||||
'Expect the array ' +
|
||||
arr.toString() +
|
||||
' to return the maximum three product of ' +
|
||||
expectedProduct,
|
||||
() => {
|
||||
// Calculate the max three product using the function being tested
|
||||
const actualProduct = maxProductOfThree(arr)
|
||||
|
||||
// Was unable to use expect().toBe(), since it sometimes compared 0 to -0, and that would not pass
|
||||
// At the same time, standardjs forbid me from checking for === -0 to convert to 0
|
||||
expect(actualProduct === expectedProduct).toBeTruthy()
|
||||
})
|
||||
// Was unable to use expect().toBe(), since it sometimes compared 0 to -0, and that would not pass
|
||||
// At the same time, standardjs forbid me from checking for === -0 to convert to 0
|
||||
expect(actualProduct === expectedProduct).toBeTruthy()
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
|
@ -18,6 +18,8 @@ describe('SieveOfEratosthenes', () => {
|
||||
})
|
||||
|
||||
it('Primes till 70', () => {
|
||||
expect(sieveOfEratosthenes(70)).toEqual([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67])
|
||||
expect(sieveOfEratosthenes(70)).toEqual([
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67
|
||||
])
|
||||
})
|
||||
})
|
||||
|
@ -3,13 +3,37 @@ import { uniquePaths2 } from '../UniquePaths2'
|
||||
describe('Unique Paths2', () => {
|
||||
// Should return number of ways, taken into account the obstacles
|
||||
test('There are obstacles in the way', () => {
|
||||
expect(uniquePaths2([[0, 0, 0], [0, 1, 0], [0, 0, 0]])).toEqual(2)
|
||||
expect(uniquePaths2([[0, 0, 0], [0, 1, 0], [0, 0, 0], [1, 0, 0]])).toEqual(3)
|
||||
expect(
|
||||
uniquePaths2([
|
||||
[0, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0, 0, 0]
|
||||
])
|
||||
).toEqual(2)
|
||||
expect(
|
||||
uniquePaths2([
|
||||
[0, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0, 0, 0],
|
||||
[1, 0, 0]
|
||||
])
|
||||
).toEqual(3)
|
||||
})
|
||||
// Should return number of all possible ways to reach right-bottom corner
|
||||
test('There are no obstacles in the way', () => {
|
||||
expect(uniquePaths2([[0, 0, 0], [0, 0, 0], [0, 0, 0]])).toEqual(6)
|
||||
expect(uniquePaths2([[0, 0, 0], [0, 0, 0]])).toEqual(3)
|
||||
expect(
|
||||
uniquePaths2([
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
])
|
||||
).toEqual(6)
|
||||
expect(
|
||||
uniquePaths2([
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
])
|
||||
).toEqual(3)
|
||||
})
|
||||
// Should throw an exception b/c input data has wrong type
|
||||
test('There are wrong type of input data', () => {
|
||||
|
@ -2,11 +2,36 @@ import { zeroOneKnapsack } from '../ZeroOneKnapsack'
|
||||
|
||||
describe('ZeroOneKnapsack', () => {
|
||||
it('zeroOneKnapsack when capacity is 4 and 5 items', () => {
|
||||
expect(zeroOneKnapsack([[1, 8], [2, 4], [3, 0], [2, 5], [2, 3]], 5, 4, [[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]])).toBe(13)
|
||||
expect(
|
||||
zeroOneKnapsack(
|
||||
[
|
||||
[1, 8],
|
||||
[2, 4],
|
||||
[3, 0],
|
||||
[2, 5],
|
||||
[2, 3]
|
||||
],
|
||||
5,
|
||||
4,
|
||||
[
|
||||
[-1, -1, -1, -1, -1],
|
||||
[-1, -1, -1, -1, -1],
|
||||
[-1, -1, -1, -1, -1],
|
||||
[-1, -1, -1, -1, -1],
|
||||
[-1, -1, -1, -1, -1],
|
||||
[-1, -1, -1, -1, -1]
|
||||
]
|
||||
)
|
||||
).toBe(13)
|
||||
})
|
||||
|
||||
it('zeroOneKnapsack when capacity is 1 and 1 items', () => {
|
||||
expect(zeroOneKnapsack([[1, 80]], 1, 1, [[-1, -1], [-1, -1]])).toBe(80)
|
||||
expect(
|
||||
zeroOneKnapsack([[1, 80]], 1, 1, [
|
||||
[-1, -1],
|
||||
[-1, -1]
|
||||
])
|
||||
).toBe(80)
|
||||
})
|
||||
|
||||
it('zeroOneKnapsack when capacity is 0 and 1 items', () => {
|
||||
|
Reference in New Issue
Block a user