algorithm: add IntToBase algo and a test for it (#1258)

This commit is contained in:
Alex Popov
2022-10-31 19:49:14 +03:00
committed by GitHub
parent 7fb121508d
commit 35e1fe68d0
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,25 @@
import { intToBase } from '../intToBase'
describe('Int to Base', () => {
test('Conversion to the binary system', () => {
expect(intToBase(210, 2)).toEqual('11010010')
expect(intToBase(-210, 2)).toEqual('-11010010')
})
test('Conversion to the system with base 5', () => {
expect(intToBase(210, 5)).toEqual('1320')
expect(intToBase(-210, 5)).toEqual('-1320')
})
test('Conversion to the octal system', () => {
expect(intToBase(210, 8)).toEqual('322')
expect(intToBase(-210, 8)).toEqual('-322')
})
test('Output is 0', () => {
expect(intToBase(0, 8)).toEqual('0')
expect(intToBase(0, 8)).toEqual('0')
})
test('Throwing an exception', () => {
expect(() => intToBase('string', 2)).toThrow()
expect(() => intToBase(10, 'base')).toThrow()
expect(() => intToBase(true, false)).toThrow()
})
})