add
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { chapters, getAllFiles, getChapterById } from '../data/chapters.js'
|
||||
|
||||
describe('chapters data', () => {
|
||||
it('should have 6 chapters', () => {
|
||||
expect(chapters.length).toBe(6)
|
||||
})
|
||||
|
||||
it('each chapter should have required fields', () => {
|
||||
for (const ch of chapters) {
|
||||
expect(ch.id).toBeTruthy()
|
||||
expect(ch.title).toBeTruthy()
|
||||
expect(ch.subtitle).toBeTruthy()
|
||||
expect(ch.description).toBeTruthy()
|
||||
expect(ch.icon).toBeTruthy()
|
||||
expect(Array.isArray(ch.topics)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
it('chapter ids should be ch1 through ch6', () => {
|
||||
const ids = chapters.map(ch => ch.id)
|
||||
expect(ids).toEqual(['ch1', 'ch2', 'ch3', 'ch4', 'ch5', 'ch6'])
|
||||
})
|
||||
|
||||
it('getChapterById should find correct chapter', () => {
|
||||
const ch3 = getChapterById('ch3')
|
||||
expect(ch3.title).toContain('动态规划')
|
||||
expect(getChapterById('nonexistent')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('each chapter should have at least one topic', () => {
|
||||
for (const ch of chapters) {
|
||||
expect(ch.topics.length).toBeGreaterThan(0)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAllFiles', () => {
|
||||
it('should return all files flattened', () => {
|
||||
const files = getAllFiles()
|
||||
expect(files.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('each file should have path, name, label', () => {
|
||||
const files = getAllFiles()
|
||||
for (const f of files) {
|
||||
expect(f.path).toBeTruthy()
|
||||
expect(f.name).toBeTruthy()
|
||||
expect(f.label).toBeTruthy()
|
||||
expect(f.chapterId).toBeTruthy()
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,118 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { generateMergeSortSteps, generateQuickSortSteps, generateRandomArray } from '../utils/sortAnimations.js'
|
||||
|
||||
describe('generateMergeSortSteps', () => {
|
||||
it('should produce steps for a small array', () => {
|
||||
const arr = [3, 1, 2]
|
||||
const steps = generateMergeSortSteps(arr)
|
||||
expect(steps.length).toBeGreaterThan(0)
|
||||
// 最后一步应标记全部已排序
|
||||
const last = steps[steps.length - 1]
|
||||
expect(last.highlights.sorted).toEqual([0, 1, 2])
|
||||
expect(last.description).toContain('完成')
|
||||
})
|
||||
|
||||
it('should sort the array correctly', () => {
|
||||
const arr = [5, 3, 8, 1, 9, 2]
|
||||
const steps = generateMergeSortSteps(arr)
|
||||
const last = steps[steps.length - 1]
|
||||
// 检查最终数组有序
|
||||
const sorted = [...last.array].sort((a, b) => a - b)
|
||||
expect(last.array).toEqual(sorted)
|
||||
})
|
||||
|
||||
it('should handle single element array', () => {
|
||||
const steps = generateMergeSortSteps([42])
|
||||
expect(steps.length).toBeGreaterThan(0)
|
||||
expect(steps[steps.length - 1].highlights.sorted).toEqual([0])
|
||||
})
|
||||
|
||||
it('should handle empty array', () => {
|
||||
const steps = generateMergeSortSteps([])
|
||||
expect(steps.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('should have description for every step', () => {
|
||||
const steps = generateMergeSortSteps([4, 2, 7, 1])
|
||||
for (const step of steps) {
|
||||
expect(step.description).toBeTruthy()
|
||||
expect(typeof step.description).toBe('string')
|
||||
}
|
||||
})
|
||||
|
||||
it('should record array state at each step', () => {
|
||||
const arr = [4, 2, 7, 1]
|
||||
const steps = generateMergeSortSteps(arr)
|
||||
for (const step of steps) {
|
||||
expect(Array.isArray(step.array)).toBe(true)
|
||||
expect(step.array.length).toBe(arr.length)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('generateQuickSortSteps', () => {
|
||||
it('should produce steps for a small array', () => {
|
||||
const arr = [3, 1, 2]
|
||||
const steps = generateQuickSortSteps(arr)
|
||||
expect(steps.length).toBeGreaterThan(0)
|
||||
const last = steps[steps.length - 1]
|
||||
expect(last.highlights.sorted).toEqual([0, 1, 2])
|
||||
expect(last.description).toContain('完成')
|
||||
})
|
||||
|
||||
it('should sort the array correctly', () => {
|
||||
const arr = [5, 3, 8, 1, 9, 2]
|
||||
const steps = generateQuickSortSteps(arr)
|
||||
const last = steps[steps.length - 1]
|
||||
const sorted = [...last.array].sort((a, b) => a - b)
|
||||
expect(last.array).toEqual(sorted)
|
||||
})
|
||||
|
||||
it('should handle single element array', () => {
|
||||
const steps = generateQuickSortSteps([42])
|
||||
expect(steps.length).toBeGreaterThan(0)
|
||||
expect(steps[steps.length - 1].highlights.sorted).toEqual([0])
|
||||
})
|
||||
|
||||
it('should handle already sorted array', () => {
|
||||
const arr = [1, 2, 3, 4, 5]
|
||||
const steps = generateQuickSortSteps(arr)
|
||||
const last = steps[steps.length - 1]
|
||||
expect(last.array).toEqual([1, 2, 3, 4, 5])
|
||||
})
|
||||
|
||||
it('should handle reverse sorted array', () => {
|
||||
const arr = [5, 4, 3, 2, 1]
|
||||
const steps = generateQuickSortSteps(arr)
|
||||
const last = steps[steps.length - 1]
|
||||
expect(last.array).toEqual([1, 2, 3, 4, 5])
|
||||
})
|
||||
|
||||
it('should have description for every step', () => {
|
||||
const steps = generateQuickSortSteps([4, 2, 7, 1])
|
||||
for (const step of steps) {
|
||||
expect(step.description).toBeTruthy()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('generateRandomArray', () => {
|
||||
it('should generate array of correct size', () => {
|
||||
const arr = generateRandomArray(10)
|
||||
expect(arr.length).toBe(10)
|
||||
})
|
||||
|
||||
it('should generate positive numbers', () => {
|
||||
const arr = generateRandomArray(20)
|
||||
for (const v of arr) {
|
||||
expect(v).toBeGreaterThanOrEqual(1)
|
||||
}
|
||||
})
|
||||
|
||||
it('should respect maxValue', () => {
|
||||
const arr = generateRandomArray(50, 10)
|
||||
for (const v of arr) {
|
||||
expect(v).toBeLessThanOrEqual(10)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user