Skip to content

Commit 3623e42

Browse files
authored
tests: add HexToDecimal.test.js (#1662)
1 parent e2b9754 commit 3623e42

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: Conversions/HexToDecimal.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function hexToInt(hexNum) {
2+
if (!/^[0-9A-F]+$/.test(hexNum)) {
3+
throw new Error('Invalid hex string.')
4+
}
25
const numArr = hexNum.split('') // converts number to array
36
return numArr.map((item, index) => {
47
switch (item) {
@@ -29,4 +32,4 @@ function hexToDecimal(hexNum) {
2932
}, 0)
3033
}
3134

32-
export { hexToInt, hexToDecimal }
35+
export { hexToDecimal }

Diff for: Conversions/test/HexToDecimal.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { hexToDecimal } from '../HexToDecimal'
2+
3+
describe('Testing HexToDecimal', () => {
4+
it.each([
5+
['0', 0],
6+
['1', 1],
7+
['A', 10],
8+
['B', 11],
9+
['C', 12],
10+
['D', 13],
11+
['E', 14],
12+
['F', 15],
13+
['10', 16],
14+
['859', 2137],
15+
['4D2', 1234],
16+
['81323ABD92', 554893491602]
17+
])('check with %s', (hexStr, expected) => {
18+
expect(hexToDecimal(hexStr)).toBe(expected)
19+
})
20+
21+
it.each(['a', '-1', 'G', ''])('throws for %s', (hexStr) => {
22+
expect(() => hexToDecimal(hexStr)).toThrowError()
23+
})
24+
})

0 commit comments

Comments
 (0)