|
| 1 | +import { decExp } from '../DecimalExpansion' |
| 2 | + |
| 3 | +/** |
| 4 | + * Decimal |
| 5 | + */ |
| 6 | + |
| 7 | +describe('Finite Decimal Expansion', () => { |
| 8 | + it('1/2 = 0.5', () => { |
| 9 | + const [integer, decimals, cycleIndex] = decExp(1, 2) |
| 10 | + expect(integer).toBe(0) |
| 11 | + expect(decimals).toEqual([5]) |
| 12 | + expect(cycleIndex).toBeUndefined() |
| 13 | + }) |
| 14 | + |
| 15 | + it('1/5 = 0.2', () => { |
| 16 | + const [integer, decimals, cycleIndex] = decExp(1, 5) |
| 17 | + expect(integer).toBe(0) |
| 18 | + expect(decimals).toEqual([2]) |
| 19 | + expect(cycleIndex).toBeUndefined() |
| 20 | + }) |
| 21 | + |
| 22 | + it('1/8 = 0.125', () => { |
| 23 | + const [integer, decimals, cycleIndex] = decExp(1, 8) |
| 24 | + expect(integer).toBe(0) |
| 25 | + expect(decimals).toEqual([1, 2, 5]) |
| 26 | + expect(cycleIndex).toBeUndefined() |
| 27 | + }) |
| 28 | + |
| 29 | + it('255/40 = 6.375', () => { |
| 30 | + const [integer, decimals, cycleIndex] = decExp(255, 40) |
| 31 | + expect(integer).toBe(6) |
| 32 | + expect(decimals).toEqual([3, 7, 5]) |
| 33 | + expect(cycleIndex).toBeUndefined() |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +describe('Repeating Decimal Expansion', () => { |
| 38 | + it('1/3 = 0.(3)', () => { |
| 39 | + expect(decExp(1, 3)).toStrictEqual([0, [3], 0]) |
| 40 | + }) |
| 41 | + |
| 42 | + it('1/6 = 0.1(6)', () => { |
| 43 | + expect(decExp(1, 6)).toStrictEqual([0, [1, 6], 1]) |
| 44 | + }) |
| 45 | + |
| 46 | + it('1/7 = 0.(142857)', () => { |
| 47 | + expect(decExp(1, 7)).toStrictEqual([0, [1, 4, 2, 8, 5, 7], 0]) |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +/** |
| 52 | + * Binary |
| 53 | + */ |
| 54 | + |
| 55 | +describe('Finite Binary Expansion', () => { |
| 56 | + it('1/2 = 0.1₂', () => { |
| 57 | + const [integer, decimals, cycleIndex] = decExp(1, 2, 2) |
| 58 | + expect(integer).toBe(0) |
| 59 | + expect(decimals).toEqual([1]) |
| 60 | + expect(cycleIndex).toBeUndefined() |
| 61 | + }) |
| 62 | + |
| 63 | + it('1/8 = 0.001₂', () => { |
| 64 | + const [integer, decimals, cycleIndex] = decExp(1, 8, 2) |
| 65 | + expect(integer).toBe(0) |
| 66 | + expect(decimals).toEqual([0, 0, 1]) |
| 67 | + expect(cycleIndex).toBeUndefined() |
| 68 | + }) |
| 69 | + |
| 70 | + it('255/40 = 110.011₂', () => { |
| 71 | + const [integer, decimals, cycleIndex] = decExp(255, 40, 2) |
| 72 | + expect(integer).toBe(110) |
| 73 | + expect(decimals).toEqual([0, 1, 1]) |
| 74 | + expect(cycleIndex).toBeUndefined() |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe('Repeating Binary Expansion', () => { |
| 79 | + it('1/3 = 0.(01)₂', () => { |
| 80 | + expect(decExp(1, 3, 2)).toStrictEqual([0, [0, 1], 0]) |
| 81 | + }) |
| 82 | + |
| 83 | + it('1/5 = 0.(0011)₂', () => { |
| 84 | + expect(decExp(1, 5, 2)).toStrictEqual([0, [0, 0, 1, 1], 0]) |
| 85 | + }) |
| 86 | + |
| 87 | + it('1/6 = 0.0(01)₂', () => { |
| 88 | + expect(decExp(1, 6, 2)).toStrictEqual([0, [0, 0, 1], 1]) |
| 89 | + }) |
| 90 | + |
| 91 | + it('1/7 = 0.(001)₂', () => { |
| 92 | + expect(decExp(1, 7, 2)).toStrictEqual([0, [0, 0, 1], 0]) |
| 93 | + }) |
| 94 | +}) |
| 95 | + |
| 96 | +/** |
| 97 | + * Octal |
| 98 | + */ |
| 99 | + |
| 100 | +describe('Finite Octal Expansion', () => { |
| 101 | + it('1/2 = 0.4₈', () => { |
| 102 | + const [integer, decimals, cycleIndex] = decExp(1, 2, 8) |
| 103 | + expect(integer).toBe(0) |
| 104 | + expect(decimals).toEqual([4]) |
| 105 | + expect(cycleIndex).toBeUndefined() |
| 106 | + }) |
| 107 | + |
| 108 | + it('1/8 = 0.1₈', () => { |
| 109 | + const [integer, decimals, cycleIndex] = decExp(1, 8, 8) |
| 110 | + expect(integer).toBe(0) |
| 111 | + expect(decimals).toEqual([1]) |
| 112 | + expect(cycleIndex).toBeUndefined() |
| 113 | + }) |
| 114 | + |
| 115 | + it('255/40 = 6.3₈', () => { |
| 116 | + const [integer, decimals, cycleIndex] = decExp(255, 40, 8) |
| 117 | + expect(integer).toBe(6) |
| 118 | + expect(decimals).toEqual([3]) |
| 119 | + expect(cycleIndex).toBeUndefined() |
| 120 | + }) |
| 121 | +}) |
| 122 | + |
| 123 | +describe('Repeating Octal Expansion', () => { |
| 124 | + it('1/3 = 0.(25)₈', () => { |
| 125 | + expect(decExp(1, 3, 8)).toStrictEqual([0, [2, 5], 0]) |
| 126 | + }) |
| 127 | + |
| 128 | + it('1/5 = 0.(1463)₈', () => { |
| 129 | + expect(decExp(1, 5, 8)).toStrictEqual([0, [1, 4, 6, 3], 0]) |
| 130 | + }) |
| 131 | + |
| 132 | + it('1/6 = 0.1(25)₈', () => { |
| 133 | + expect(decExp(1, 6, 8)).toStrictEqual([0, [1, 2, 5], 1]) |
| 134 | + }) |
| 135 | + |
| 136 | + it('1/7 = 0.(1)₈', () => { |
| 137 | + expect(decExp(1, 7, 8)).toStrictEqual([0, [1], 0]) |
| 138 | + }) |
| 139 | +}) |
| 140 | + |
| 141 | +/** |
| 142 | + * Integers |
| 143 | + */ |
| 144 | + |
| 145 | +describe('Integers', () => { |
| 146 | + it('1/1 = 1', () => { |
| 147 | + const [integer, decimals, cycleIndex] = decExp(1, 1) |
| 148 | + expect(integer).toBe(1) |
| 149 | + expect(decimals).toStrictEqual([]) |
| 150 | + expect(cycleIndex).toBeUndefined() |
| 151 | + }) |
| 152 | + |
| 153 | + it('5/5 = 1', () => { |
| 154 | + const [integer, decimals, cycleIndex] = decExp(5, 5) |
| 155 | + expect(integer).toBe(1) |
| 156 | + expect(decimals).toStrictEqual([]) |
| 157 | + expect(cycleIndex).toBeUndefined() |
| 158 | + }) |
| 159 | + |
| 160 | + it('2/1 = 2', () => { |
| 161 | + const [integer, decimals, cycleIndex] = decExp(2, 1) |
| 162 | + expect(integer).toBe(2) |
| 163 | + expect(decimals).toStrictEqual([]) |
| 164 | + expect(cycleIndex).toBeUndefined() |
| 165 | + }) |
| 166 | + |
| 167 | + it('9/3 = 3', () => { |
| 168 | + const [integer, decimals, cycleIndex] = decExp(9, 3) |
| 169 | + expect(integer).toBe(3) |
| 170 | + expect(decimals).toStrictEqual([]) |
| 171 | + expect(cycleIndex).toBeUndefined() |
| 172 | + }) |
| 173 | +}) |
| 174 | + |
| 175 | +/** |
| 176 | + * Unsupported base |
| 177 | + */ |
| 178 | + |
| 179 | +describe('Throws on unsupported base', () => { |
| 180 | + it('negative base', () => { |
| 181 | + expect(() => decExp(1, 1, -2)).toThrow(RangeError) |
| 182 | + }) |
| 183 | + it('base 0', () => { |
| 184 | + expect(() => decExp(1, 1, 0)).toThrow(RangeError) |
| 185 | + }) |
| 186 | + it('base 1', () => { |
| 187 | + expect(() => decExp(1, 1, 1)).toThrow(RangeError) |
| 188 | + }) |
| 189 | + it('base 11', () => { |
| 190 | + expect(() => decExp(1, 1, 11)).toThrow(RangeError) |
| 191 | + }) |
| 192 | +}) |
0 commit comments