-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall-your-base.spec.js
135 lines (110 loc) · 3.25 KB
/
all-your-base.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { convert } from './all-your-base';
describe('Converter', () => {
test('single bit one to decimal', () => {
expect(convert([1], 2, 10)).toEqual([1]);
});
test('binary to single decimal', () => {
expect(convert([1, 0, 1], 2, 10)).toEqual([5]);
});
test('single decimal to binary', () => {
expect(convert([5], 10, 2)).toEqual([1, 0, 1]);
});
test('binary to multiple decimal', () => {
expect(convert([1, 0, 1, 0, 1, 0], 2, 10)).toEqual([4, 2]);
});
test('decimal to binary', () => {
expect(convert([4, 2], 10, 2)).toEqual([1, 0, 1, 0, 1, 0]);
});
test('trinary to hexadecimal', () => {
expect(convert([1, 1, 2, 0], 3, 16)).toEqual([2, 10]);
});
test('hexadecimal to trinary', () => {
expect(convert([2, 10], 16, 3)).toEqual([1, 1, 2, 0]);
});
test('15-bit integer', () => {
expect(convert([3, 46, 60], 97, 73)).toEqual([6, 10, 45]);
});
test('empty list', () => {
expect(() => {
convert([], 2, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('single zero', () => {
expect(convert([0], 10, 2)).toEqual([0]);
});
test('multiple zeros', () => {
expect(() => {
convert([0, 0, 0], 10, 2);
}).toThrow(new Error('Input has wrong format'));
});
test('leading zeros', () => {
expect(() => {
convert([0, 6, 0], 7, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('negative digit', () => {
expect(() => {
convert([1, -1, 1, 0, 1, 0], 2, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('invalid positive digit', () => {
expect(() => {
convert([1, 2, 1, 0, 1, 0], 2, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('first base is one', () => {
expect(() => {
convert([], 1, 10);
}).toThrow(new Error('Wrong input base'));
});
test('second base is one', () => {
expect(() => {
convert([1, 0, 1, 0, 1, 0], 2, 1);
}).toThrow(new Error('Wrong output base'));
});
test('first base is zero', () => {
expect(() => {
convert([], 0, 10);
}).toThrow(new Error('Wrong input base'));
});
test('second base is zero', () => {
expect(() => {
convert([7], 10, 0);
}).toThrow(new Error('Wrong output base'));
});
test('first base is negative', () => {
expect(() => {
convert([1], -2, 10);
}).toThrow(new Error('Wrong input base'));
});
test('second base is negative', () => {
expect(() => {
convert([1], 2, -7);
}).toThrow(new Error('Wrong output base'));
});
test('both bases are negative', () => {
expect(() => {
convert([1], -2, -7);
}).toThrow(new Error('Wrong input base'));
});
test('missing input base throws an error', () => {
expect(() => {
convert([0]);
}).toThrow(new Error('Wrong input base'));
});
test('wrong input_base base not integer', () => {
expect(() => {
convert([0], 2.5);
}).toThrow(new Error('Wrong input base'));
});
test('missing output base throws an error', () => {
expect(() => {
convert([0], 2);
}).toThrow(new Error('Wrong output base'));
});
test('wrong output_base base not integer', () => {
expect(() => {
convert([0], 3, 2.5);
}).toThrow(new Error('Wrong output base'));
});
});