Skip to content

Commit 44297c8

Browse files
committed
✅ test: update tests
1 parent 5521403 commit 44297c8

File tree

22 files changed

+272
-176
lines changed

22 files changed

+272
-176
lines changed

Diff for: .vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"plusplus",
2222
"postpublish",
2323
"prebuild",
24-
"totient"
24+
"totient",
25+
"varepsilon"
2526
],
2627
"files.associations": {
2728
".prettierrc": "yaml"

Diff for: _fixtures/leetcode/word-break-ii/data.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"input": ["catsanddog", ["cat", "cats", "and", "sand", "dog"]],
4+
"answer": ["cat sand dog", "cats and dog"]
5+
},
6+
{
7+
"input": ["pineapplepenapple", ["apple", "pen", "applepen", "pine", "pineapple"]],
8+
"answer": ["pine apple pen apple", "pine applepen apple", "pineapple pen apple"]
9+
},
10+
{
11+
"input": ["catsandog", ["cats", "dog", "sand", "and", "cat"]],
12+
"answer": []
13+
}
14+
]

Diff for: _fixtures/leetcode/word-search-ii/data.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[
2+
{
3+
"input": [
4+
[
5+
["o", "a", "a", "n"],
6+
["e", "t", "a", "e"],
7+
["i", "h", "k", "r"],
8+
["i", "f", "l", "v"]
9+
],
10+
["oath", "pea", "eat", "rain"]
11+
],
12+
"answer": ["oath", "eat"]
13+
},
14+
{
15+
"input": [
16+
[
17+
["a", "b"],
18+
["c", "d"]
19+
],
20+
["abcb"]
21+
],
22+
"answer": []
23+
},
24+
{
25+
"input": [
26+
[
27+
["d", "c", "e", "b", "d", "e", "d", "a"],
28+
["c", "a", "e", "a", "d", "d", "e", "e"],
29+
["a", "c", "e", "d", "b", "c", "c", "b"],
30+
["c", "b", "a", "a", "a", "e", "e", "e"],
31+
["a", "e", "d", "e", "b", "d", "d", "e"],
32+
["a", "a", "d", "c", "e", "a", "d", "e"],
33+
["b", "d", "e", "b", "b", "b", "c", "e"],
34+
["d", "a", "e", "e", "b", "e", "b", "d"],
35+
["b", "b", "c", "a", "b", "b", "b", "a"],
36+
["a", "c", "b", "a", "c", "a", "d", "d"]
37+
],
38+
[
39+
"ab",
40+
"bddbebcba",
41+
"ededa",
42+
"daebeda",
43+
"edecaeabc",
44+
"cbeedad",
45+
"bcaaecb",
46+
"c",
47+
"eb",
48+
"aadbdbacee",
49+
"dcaaba"
50+
]
51+
],
52+
"answer": ["ab", "ededa", "daebeda", "c", "eb"]
53+
}
54+
]

Diff for: packages/binary-index-tree/__test__/binary-saerch-tree.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ describe('tree1-mod', function () {
160160

161161
for (let q = 0; q < 1000; ++q) {
162162
const x = Math.max(1, Math.ceil(Math.random() * MAX_N))
163-
const value = Math.round(Math.random() * (MOD - 1))
163+
const value = Math.round(Math.random() * MOD * 2) - MOD
164164

165-
A[x - 1] += value
165+
A[x - 1] = (((A[x - 1] + value) % MOD) + MOD) % MOD
166166
bit.add(x, value)
167167
expect(bit.query(x)).toBe(getSum(x))
168168
}

Diff for: packages/calculate/__test__/calculate.spec.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1-
import { bigintCalculate, calculate, decimalCalculate } from '../src'
1+
import {
2+
bigintCalculate,
3+
calculate,
4+
createCalculate,
5+
decimalCalculate,
6+
integerOperations,
7+
} from '../src'
8+
9+
describe('basic', function () {
10+
test('custom', function () {
11+
const f = createCalculate(integerOperations)
12+
expect(f('1+2')).toEqual(3)
13+
expect(() => f('1+2.2')).toThrow(/Unrecognized symbol/)
14+
})
15+
})
216

317
describe('calculate', function () {
418
const data = [
19+
{
20+
input: '-0',
21+
answer: 0,
22+
},
23+
{
24+
input: '+1.3',
25+
answer: 1.3,
26+
},
27+
{
28+
input: '0.2',
29+
answer: 0.2,
30+
},
531
{
632
input: '-2+1',
733
answer: -1,
@@ -72,6 +98,11 @@ describe('calculate', function () {
7298
}
7399
})
74100
}
101+
102+
test('exceptional', function () {
103+
expect(() => decimalCalculate('$0.2')).toThrow(/Not a valid arithmetic expression/)
104+
expect(() => decimalCalculate('1.1$0.2')).toThrow(/Not a valid arithmetic expression/)
105+
})
75106
})
76107

77108
describe('integer calculate', function () {

Diff for: packages/calculate/src/calculate.ts

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function createCalculate<T extends number | bigint>(
5151
}
5252

5353
// Syntax error.
54+
/* istanbul ignore next */
5455
if (id > 0) {
5556
throw new SyntaxError('Not a valid arithmetic expression.')
5657
}
@@ -63,6 +64,7 @@ export function createCalculate<T extends number | bigint>(
6364
const tokens: ReadonlyArray<number> = sddTable[ssdId]
6465
const syn0: T = tokens.length > 0 ? execute(tokens[0], ZERO, ZERO) : ZERO
6566

67+
/* istanbul ignore next */
6668
switch (ssdId) {
6769
// 0: A --> BD
6870
case 0:
@@ -129,6 +131,7 @@ export function createCalculate<T extends number | bigint>(
129131

130132
// Here is not reachable.
131133
default:
134+
/* istanbul ignore next */
132135
throw new Error('Shit codes!')
133136
}
134137
}

Diff for: packages/circular-queue/__test__/circular-queue.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,10 @@ describe('createCircularQueue', function () {
156156
expect(queue.size()).toBe(2)
157157
expect(queue.size()).toBe(2)
158158
})
159+
160+
test('destroy', function () {
161+
const queue = createCircularQueue()
162+
queue.init(10)
163+
queue.destroy()
164+
})
159165
})

Diff for: packages/circular-queue/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface ICircularQueue<T> {
1616
/**
1717
* Free memory.
1818
*/
19-
free(): void
19+
destroy(): void
2020
/**
2121
* Get the front element of the queue.
2222
*/
@@ -78,7 +78,7 @@ export function createCircularQueue<T>(): ICircularQueue<T> {
7878

7979
return {
8080
init,
81-
free,
81+
destroy,
8282
front,
8383
end,
8484
dequeue,
@@ -99,7 +99,7 @@ export function createCircularQueue<T>(): ICircularQueue<T> {
9999
_endIndex = 0
100100
}
101101

102-
function free(): void {
102+
function destroy(): void {
103103
_MAX_SIZE = 0
104104
_size = 0
105105
_queue.length = 0

Diff for: packages/dlx/__test__/dlx.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createDLX } from '@algorithm.ts/dlx'
12
import multipleSudoku3x3 from './fixtures/sudoku9x9/multiple.json'
23
import uniqueSudoku3x3 from './fixtures/sudoku9x9/unique.json'
34
import { solveSudoku } from './sudoku3x3'
@@ -23,6 +24,26 @@ describe('dlx', function () {
2324
expect(checkSudoku(solution, 3)).toBe(true)
2425
}
2526
})
27+
28+
test('no solution', function () {
29+
const solution: number[][] = new Array(9)
30+
for (let r = 0; r < 9; ++r) solution[r] = new Array(9)
31+
32+
for (const { puzzle, solution: _solution } of uniqueSudoku3x3) {
33+
puzzle[0][0] = 2
34+
puzzle[0][1] = 2
35+
expect(solveSudoku(puzzle, solution)).toBe(false)
36+
}
37+
})
38+
})
39+
40+
test('destroy', function () {
41+
const dlx = createDLX(10)
42+
43+
dlx.init(10)
44+
dlx.destroy()
45+
46+
expect(dlx.solve()).toEqual(null)
2647
})
2748
})
2849

Diff for: packages/dlx/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function createDLX(MAX_N: number): IDancingLinkX {
5858
const selectedRowNos: number[] = new Array(MAX_N) // list of row numbers of selected rows
5959
let countOfSelectedRows: number // the number of selected rows
6060

61-
const count: number[] = new Array(MAX_N) // lhe number of nodes of a column in the dancing-link
61+
const count: number[] = new Array(MAX_N) // the number of nodes of a column in the dancing-link
6262
const row: number[] = new Array(MAX_N) // the row number of a node in the dancing-link
6363
const col: number[] = new Array(MAX_N) // the column number of a node in the dancing-link
6464
const L: number[] = new Array(MAX_N) // left pointer of cross-link list
@@ -91,6 +91,7 @@ export function createDLX(MAX_N: number): IDancingLinkX {
9191
* @public
9292
*/
9393
function destroy(): void {
94+
sz = 0
9495
selectedRowNos.length = 0
9596
count.length = 0
9697
row.length = 0
@@ -138,6 +139,7 @@ export function createDLX(MAX_N: number): IDancingLinkX {
138139
* @public
139140
*/
140141
function solve(): number[] | null {
142+
if (sz === 0) return null
141143
if (!algorithmX(0)) return null
142144
return selectedRowNos.slice(0, countOfSelectedRows)
143145
}

Diff for: packages/knuth-shuffle/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function knuthShuffle<T = unknown>(
2828
*/
2929
export function randomInt(n: number): number {
3030
const x = (Math.random() * n) >> 0
31+
/* istanbul ignore next */
3132
return x === n ? n - 1 : x
3233
}
3334

Diff for: packages/roman/__test__/int2roman.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ describe('int2roman', function () {
99
expect(results).toMatchSnapshot('1..3999')
1010
})
1111

12+
test('custom', function () {
13+
expect(int2roman(2137, 'ABCDEF')).toEqual('BBDFFF')
14+
})
15+
1216
test('exception', function () {
1317
expect(() => int2roman(0)).toThrow(/Out of range/)
1418
expect(() => int2roman(4000)).toThrow(/Out of range/)

Diff for: packages/roman/__test__/roman2int.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ describe('roman2int', function () {
88
expect(v).toEqual(n)
99
}
1010
})
11+
12+
test('exception', function () {
13+
expect(() => roman2int('ABCD')).toThrow(/Invalid roman number/)
14+
})
1115
})

Diff for: packages/roman/src/roman2int.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export function roman2int(
3232
const N = roman.length
3333
let value = 0
3434
for (let i = 0; i < N; ++i) {
35-
const u: number = romanCodeMap[roman[i]] ?? 0
35+
const u: number | undefined = romanCodeMap[roman[i]]
36+
if (u === undefined) {
37+
throw new TypeError(`Invalid roman number: (${roman})`)
38+
}
39+
3640
if (i + 1 < N && u < romanCodeMap[roman[i + 1]]) value -= u
3741
else value += u
3842
}

Diff for: packages/sudoku/src/creator.ts

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export class SudokuCreator {
105105
const c = p & 0xffff
106106
radicalPuzzle[r][c] = -1
107107
}
108+
109+
/* istanbul ignore next */
108110
throw new Error('[createSolution] This is impossible!')
109111
}
110112

Diff for: packages/sudoku/src/util.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export function copySudokuBoard(
5757
* @param SUDOKU_SIZE_SQRT
5858
* @returns
5959
*/
60+
/* istanbul ignore next */
6061
export function checkSudokuSolution(solution: ISudokuBoard, SUDOKU_SIZE_SQRT: number): boolean {
6162
const SUDOKU_SIZE: number = SUDOKU_SIZE_SQRT * SUDOKU_SIZE_SQRT
6263
const nums: number[] = new Array(SUDOKU_SIZE)

Diff for: packages/trie/__test__/leetcode.word-break-ii.spec.ts

-69
This file was deleted.

0 commit comments

Comments
 (0)