2 * Copyright 2015-present Greg Hurrell. All rights reserved.
3 * Licensed under the terms of the MIT license.
8 import add from '../add';
10 describe('add()', () => {
11 it('adds empty numbers to 0', () => {
12 expect(add('', '', 2)).toBe('0');
13 expect(add('', '', 8)).toBe('0');
14 expect(add('', '', 10)).toBe('0');
15 expect(add('', '', 16)).toBe('0');
17 // Whitespace is considered "empty" as well.
18 expect(add(' ', ' ', 2)).toBe('0');
19 expect(add(' ', ' ', 8)).toBe('0');
20 expect(add(' ', ' ', 10)).toBe('0');
21 expect(add(' ', ' ', 16)).toBe('0');
24 it('adds an empty number to a non-empty number', () => {
25 expect(add('', '101', 2)).toBe('101');
26 expect(add('', '755', 8)).toBe('755');
27 expect(add('', '321', 10)).toBe('321');
28 expect(add('', 'ff4', 16)).toBe('ff4');
31 it('adds a non-empty number to an empty number', () => {
32 expect(add('101', '', 2)).toBe('101');
33 expect(add('755', '', 8)).toBe('755');
34 expect(add('321', '', 10)).toBe('321');
35 expect(add('ff4', '', 16)).toBe('ff4');
38 it('adds binary numbers with overflow', () => {
39 // Equal number of digits.
40 expect(add('101', '110', 2)).toBe('1011');
42 // More digits in first operand.
43 expect(add('1001', '101', 2)).toBe('1110');
45 // More digits in second operand.
46 expect(add('11', '1011', 2)).toBe('1110');
49 it('adds decimal numbers with overflow', () => {
50 // Equal number of digits.
51 expect(add('481', '110', 10)).toBe('591');
53 // More digits in first operand.
54 expect(add('1024', '85', 10)).toBe('1109');
56 // More digits in second operand.
57 expect(add('12', '4999', 10)).toBe('5011');
60 it('adds hexadecimal numbers with overflow', () => {
61 // Equal number of digits.
62 expect(add('200', 'f39', 16)).toBe('1139');
64 // More digits in first operand.
65 expect(add('102a', '805', 16)).toBe('182f');
67 // More digits in second operand.
68 expect(add('a2', '4f02', 16)).toBe('4fa4');
71 it('adds hexadecimal numbers case-insensitively', () => {
72 expect(add('feed', 'FACE', 16)).toBe('1f9bb');
75 it('ignores leading and trailing whitespace', () => {
76 expect(add(' 429 ', ' 120 ', 10)).toBe('549');
79 it('ignores an optional 0 prefix for octal numbers', () => {
80 expect(add('0755', '002', 8)).toBe('757');
83 it('ignores an optional 0x prefix for hexadecimal numbers', () => {
84 expect(add('0x100', 'ff', 16)).toBe('1ff');
85 expect(add('feff', '0Xff', 16)).toBe('fffe');
88 it('adds arbitrary precision numbers', () => {
90 'dff026dc84b91cb70e984582a9ce6515e2100d55',
91 'cf4fee757d570bd5f3d390004312c40867a512d1',
93 )).toBe('1af4015520210288d026bd582ece1291e49b52026');
97 it('complains if given a digit that is out-of-range for the base', () => {
98 expect(() => add('10', '20', 2))
99 .toThrow('Invalid digit `2` for base `2`');
100 expect(() => add('558', '444', 8))
101 .toThrow('Invalid digit `8` for base `8`');
102 expect(() => add('404', '3*', 8))
103 .toThrow('Invalid digit `*` for base `10`');
104 expect(() => add('feedface', 'groundbeef', 16))
105 .toThrow('Invalid digit `g` for base `16`');