Skip to content

Commit 3623e42

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

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Conversions/HexToDecimal.js

Lines changed: 4 additions & 1 deletion
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 }

Conversions/test/HexToDecimal.test.js

Lines changed: 24 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy