all-your-base.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { convert } from './all-your-base'
  2. xit = it
  3. describe('Converter', () => {
  4. it('single bit one to decimal', () => {
  5. expect(convert([1], 2, 10)).toEqual([1])
  6. })
  7. xit('binary to single decimal', () => {
  8. expect(convert([1, 0, 1], 2, 10)).toEqual([5])
  9. })
  10. xit('single decimal to binary', () => {
  11. expect(convert([5], 10, 2)).toEqual([1, 0, 1])
  12. })
  13. xit('binary to multiple decimal', () => {
  14. expect(convert([1, 0, 1, 0, 1, 0], 2, 10)).toEqual([4, 2])
  15. })
  16. xit('decimal to binary', () => {
  17. expect(convert([4, 2], 10, 2)).toEqual([1, 0, 1, 0, 1, 0])
  18. })
  19. xit('trinary to hexadecimal', () => {
  20. expect(convert([1, 1, 2, 0], 3, 16)).toEqual([2, 10])
  21. })
  22. xit('hexadecimal to trinary', () => {
  23. expect(convert([2, 10], 16, 3)).toEqual([1, 1, 2, 0])
  24. })
  25. xit('15-bit integer', () => {
  26. expect(convert([3, 46, 60], 97, 73)).toEqual([6, 10, 45])
  27. })
  28. xit('empty list', () => {
  29. expect(() => {
  30. convert([], 2, 10)
  31. }).toThrowError('Input has wrong format')
  32. })
  33. xit('single zero', () => {
  34. expect(convert([0], 10, 2)).toEqual([0])
  35. })
  36. xit('multiple zeros', () => {
  37. expect(() => {
  38. convert([0, 0, 0], 10, 2)
  39. }).toThrowError('Input has wrong format')
  40. })
  41. xit('leading zeros', () => {
  42. expect(() => {
  43. convert([0, 6, 0], 7, 10)
  44. }).toThrowError('Input has wrong format')
  45. })
  46. xit('negative digit', () => {
  47. expect(() => {
  48. convert([1, -1, 1, 0, 1, 0], 2, 10)
  49. }).toThrowError('Input has wrong format')
  50. })
  51. xit('invalid positive digit', () => {
  52. expect(() => {
  53. convert([1, 2, 1, 0, 1, 0], 2, 10)
  54. }).toThrowError('Input has wrong format')
  55. })
  56. xit('first base is one', () => {
  57. expect(() => {
  58. convert([], 1, 10)
  59. }).toThrowError('Wrong input base')
  60. })
  61. xit('second base is one', () => {
  62. expect(() => {
  63. convert([1, 0, 1, 0, 1, 0], 2, 1)
  64. }).toThrowError('Wrong output base')
  65. })
  66. xit('first base is zero', () => {
  67. expect(() => {
  68. convert([], 0, 10)
  69. }).toThrowError('Wrong input base')
  70. })
  71. xit('second base is zero', () => {
  72. expect(() => {
  73. convert([7], 10, 0)
  74. }).toThrowError('Wrong output base')
  75. })
  76. xit('first base is negative', () => {
  77. expect(() => {
  78. convert([1], -2, 10)
  79. }).toThrowError('Wrong input base')
  80. })
  81. xit('second base is negative', () => {
  82. expect(() => {
  83. convert([1], 2, -7)
  84. }).toThrowError('Wrong output base')
  85. })
  86. xit('both bases are negative', () => {
  87. expect(() => {
  88. convert([1], -2, -7)
  89. }).toThrowError('Wrong input base')
  90. })
  91. xit('wrong output_base base not integer', () => {
  92. expect(() => {
  93. convert([0], 3, 2.5)
  94. }).toThrowError('Wrong output base')
  95. })
  96. })