Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
tests: add HexToDecimal.test.js (TheAlgorithms#1662)
  • Loading branch information
vil02 authored May 25, 2024
commit 3623e4270f98d6dd5a1fe6a05cbec3c1a0657fbb
5 changes: 4 additions & 1 deletion Conversions/HexToDecimal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function hexToInt(hexNum) {
if (!/^[0-9A-F]+$/.test(hexNum)) {
throw new Error('Invalid hex string.')
}
const numArr = hexNum.split('') // converts number to array
return numArr.map((item, index) => {
switch (item) {
Expand Down Expand Up @@ -29,4 +32,4 @@ function hexToDecimal(hexNum) {
}, 0)
}

export { hexToInt, hexToDecimal }
export { hexToDecimal }
24 changes: 24 additions & 0 deletions Conversions/test/HexToDecimal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { hexToDecimal } from '../HexToDecimal'

describe('Testing HexToDecimal', () => {
it.each([
['0', 0],
['1', 1],
['A', 10],
['B', 11],
['C', 12],
['D', 13],
['E', 14],
['F', 15],
['10', 16],
['859', 2137],
['4D2', 1234],
['81323ABD92', 554893491602]
])('check with %s', (hexStr, expected) => {
expect(hexToDecimal(hexStr)).toBe(expected)
})

it.each(['a', '-1', 'G', ''])('throws for %s', (hexStr) => {
expect(() => hexToDecimal(hexStr)).toThrowError()
})
})