Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { describe, it, expect } from 'vitest'
import { add, multiply, subtract, divide, quicksort } from './index.js'

describe('add function', () => {
it('should add two positive numbers correctly', () => {
expect(add(2, 3)).toBe(5)
})

it('should add negative numbers correctly', () => {
expect(add(-2, -3)).toBe(-5)
})

it('should add positive and negative numbers correctly', () => {
expect(add(5, -3)).toBe(2)
})

it('should handle zero correctly', () => {
expect(add(0, 5)).toBe(5)
expect(add(5, 0)).toBe(5)
expect(add(0, 0)).toBe(0)
})

it('should handle decimal numbers correctly', () => {
expect(add(1.5, 2.5)).toBe(4)
expect(add(0.1, 0.2)).toBeCloseTo(0.3)
})
})

describe('multiply function', () => {
it('should multiply two positive numbers correctly', () => {
expect(multiply(3, 4)).toBe(12)
})

it('should multiply negative numbers correctly', () => {
expect(multiply(-2, 3)).toBe(-6)
expect(multiply(-2, -3)).toBe(6)
})

it('should handle zero correctly', () => {
expect(multiply(5, 0)).toBe(0)
expect(multiply(0, 5)).toBe(0)
})
})

describe('subtract function', () => {
it('should subtract two positive numbers correctly', () => {
expect(subtract(10, 3)).toBe(7)
})

it('should handle negative numbers correctly', () => {
expect(subtract(5, -3)).toBe(8)
expect(subtract(-5, 3)).toBe(-8)
})

it('should handle zero correctly', () => {
expect(subtract(5, 0)).toBe(5)
expect(subtract(0, 5)).toBe(-5)
})
})

describe('divide function', () => {
it('should divide two positive numbers correctly', () => {
expect(divide(10, 2)).toBe(5)
})

it('should handle negative numbers correctly', () => {
expect(divide(-10, 2)).toBe(-5)
expect(divide(10, -2)).toBe(-5)
})

it('should handle decimal results correctly', () => {
expect(divide(10, 3)).toBeCloseTo(3.333, 2)
expect(divide(7, 2)).toBe(3.5)
})
})

describe('quicksort function', () => {
it('should sort an unsorted array of positive numbers', () => {
expect(quicksort([3, 1, 4, 1, 5, 9, 2, 6])).toEqual([1, 1, 2, 3, 4, 5, 6, 9])
})

it('should sort an array with negative numbers', () => {
expect(quicksort([3, -1, 4, -5, 2, 0])).toEqual([-5, -1, 0, 2, 3, 4])
})

it('should handle an already sorted array', () => {
expect(quicksort([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5])
})

it('should handle a reverse sorted array', () => {
expect(quicksort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5])
})

it('should handle an empty array', () => {
expect(quicksort([])).toEqual([])
})

it('should handle a single element array', () => {
expect(quicksort([42])).toEqual([42])
})

it('should handle an array with duplicate values', () => {
expect(quicksort([5, 2, 8, 2, 9, 1, 5, 5])).toEqual([1, 2, 2, 5, 5, 5, 8, 9])
})

it('should handle an array with all same values', () => {
expect(quicksort([7, 7, 7, 7])).toEqual([7, 7, 7, 7])
})

it('should handle decimal numbers', () => {
expect(quicksort([3.5, 1.2, 4.8, 2.1])).toEqual([1.2, 2.1, 3.5, 4.8])
})
})
28 changes: 28 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export function add(a: number, b: number) {
return a + b;
}

export function multiply(a: number, b: number) {
return a * b;
}

export function subtract(a: number, b: number) {
return a - b;
}

export function divide(a: number, b: number) {
return a / b;
}

export function quicksort(arr: number[]): number[] {
if (arr.length <= 1) {
return arr;
}

const pivot = arr[Math.floor(arr.length / 2)];
const left = arr.filter(x => x < pivot);
const middle = arr.filter(x => x === pivot);
const right = arr.filter(x => x > pivot);

return [...quicksort(left), ...middle, ...quicksort(right)];
}
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "zed-quicksort",
"type": "module",
"private": true,
"scripts": {
"test": "vitest run",
"test:watch": "vitest --watch"
},
"devDependencies": {
"@types/node": "^22.10.5",
"typescript": "^5.7.2",
"vitest": "^3.2.4"
}
}
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"lib": ["ES2022"],
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"noUncheckedIndexedAccess": true,
"noEmit": true
},
"include": ["*.ts"],
"exclude": ["node_modules"]
}
8 changes: 8 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
environment: 'node',
},
})