-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
261 lines (221 loc) · 7.11 KB
/
tests.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* eslint-disable */
var assert = chai.assert;
var expect = chai.expect;
describe('Superbalanced binary tree', function() {
const tree = new BinaryTreeNode(10);
tree.insertLeft(5);
tree.insertRight(7);
it('Can create a binary tree node with a value', function() {
expect(tree.value).to.equal(10);
});
it('Can can insert left and right values', function() {
expect(tree.left.value).to.equal(5);
expect(tree.right.value).to.equal(7);
});
it('Child nodes are instances of BinaryTreeNode', function() {
expect(tree.left instanceof BinaryTreeNode).to.be.true;
});
it('Can detect superbalanced tree', function() {
const superbalanced = checkSuperBalanced(tree);
expect(superbalanced).to.be.true;
});
it('Can detect an imbalanced tree', function() {
tree.insertLeft(6).insertLeft(8).insertLeft(10);
const notBalanced = checkSuperBalanced(tree);
expect(notBalanced).to.be.false;
});
});
describe('Sort array', function(){
var ints = [...Array(50).keys()];
ints = ints.concat(ints.map((int) => (int + 1) * 100));
it('Should detect an item present', function(){
expect(findInArray(500, ints)).to.be.true;
});
it('Should detect when an item is not present', function(){
expect(findInArray(501, ints)).to.be.false;
});
});
describe('Find rotation point', function(){
var words = [
'ptolemaic',
'retrograde',
'supplant',
'undulate',
'xenoepist',
'asymptote', // <-- rotates here!
'babka',
'banoffee',
'engender',
'karpatka',
'othellolagkage',
];
it('Should find the index of the first word alphabetically when array is odd', function(){
expect(words.length % 2 === 1).to.be.true;
expect(findRotationPoint(words)).to.equal(5);
});
it('Should find the index of the first word alphabetically when array is even', function(){
var words2 = words.slice(1);
expect(words2.length % 2 === 0).to.be.true;
expect(findRotationPoint(words2)).to.equal(4);
var words3 = ['pussilaminous', 'querulous', ...words2];
expect(findRotationPoint(words3)).to.equal(6);
});
it('Should detect if the first word searched is the proper choice', function(){
var words2 = words.slice();
expect(findRotationPoint(words2)).to.equal(5);
});
it('Should detect if item is first word in array', function(){
var words4 = [
'asymptote', // <-- rotates here!
'babka',
'banoffee',
'engender',
'karpatka',
'othellolagkage',
'ptolemaic',
'retrograde',
'supplant',
'undulate',
'xenoepist',
];
expect(findRotationPoint(words4, true)).to.equal(0);
});
it('Should detect if item is last word in array', function(){
var words4 = [
'babka',
'banoffee',
'engender',
'karpatka',
'othellolagkage',
'ptolemaic',
'retrograde',
'supplant',
'undulate',
'xenoepist',
'asymptote', // <-- rotates here!
];
expect(findRotationPoint(words4, true)).to.equal(10);
});
});
describe('Bit Manipulations', function(){
it('Should identify the bit value at a specified index', function(){
expect(getIthBit(8, 0)).to.equal(0);
expect(getIthBit(8, 1)).to.equal(0);
expect(getIthBit(8, 3)).to.equal(1);
});
it('Should set the bit value at a specified index', function(){
expect(setIthBit(8, 0)).to.equal(9);
expect(setIthBit(8, 2)).to.equal(12);
});
it('Should set the bit value at a specified index', function(){
expect(setIthBit(8, 0)).to.equal(9);
expect(setIthBit(8, 2)).to.equal(12);
});
it('Should clear the bit value at a specified index', function(){
expect(clearIthBit(8, 0)).to.equal(8);
expect(clearIthBit(8, 3)).to.equal(0);
});
it('Should clear the rightmost bit value', function(){
expect(clearRightmostBit(8)).to.equal(0);
expect(clearRightmostBit(9)).to.equal(8);
});
it('Should detect parity of bits (even/odd number of bits)', function(){
expect(getParity(15)).to.be.true;
expect(getParity(8)).to.be.false;
expect(getParity(9)).to.be.true;
});
it('Should detect Hamming distance of two bit sets', function(){
expect(getHammingDistance(4, 1)).to.equal(2);
expect(getHammingDistance(15, 1)).to.equal(3);
expect(getHammingDistance(8, 5)).to.equal(3);
});
});
describe('Linked Lists', function(){
it('Should delete a node', function(){
var a = new LinkedListNode('A');
var b = new LinkedListNode('B');
var c = new LinkedListNode('C');
a.next = b;
b.next = c;
deleteNode(b);
var result = new LinkedListNode('A');
result.next = c;
expect(a).to.eql(result);
});
it('Should detect a cycle', function(){
var a = new LinkedListNode('A');
var b = new LinkedListNode('B');
var c = new LinkedListNode('C');
var d = new LinkedListNode('D');
a.next = b;
b.next = a;
c.next = d;
expect(containsCycle(a)).to.be.true;
});
it('Should be able to reverse in place', function(){
var a = new LinkedListNode('A');
var b = new LinkedListNode('B');
var c = new LinkedListNode('C');
var d = new LinkedListNode('D');
a.next = b;
b.next = c;
c.next = d;
var revD = new LinkedListNode('D');
var revC = new LinkedListNode('C');
var revB = new LinkedListNode('B');
var revA = new LinkedListNode('A');
revD.next = revC;
revC.next = revB;
revB.next = revA;
expect(reverseLinkedList(a)).to.eql(revD);
});
it('Should be able to reverse without mutating', function(){
var a = new LinkedListNode('A');
var b = new LinkedListNode('B');
var c = new LinkedListNode('C');
var d = new LinkedListNode('D');
a.next = b;
b.next = c;
c.next = d;
var revD = new LinkedListNode('D');
var revC = new LinkedListNode('C');
var revB = new LinkedListNode('B');
var revA = new LinkedListNode('A');
revD.next = revC;
revC.next = revB;
revB.next = revA;
expect(reverseLinkedListOutOfPlace(a)).to.eql(revD);
var aCopy = new LinkedListNode('A');
var bCopy = new LinkedListNode('B');
var cCopy = new LinkedListNode('C');
var dCopy = new LinkedListNode('D');
aCopy.next = b;
bCopy.next = c;
cCopy.next = d;
expect(a).to.eql(aCopy);
});
it('Should find the kth item', function(){
var a = new LinkedListNode("Angel Food");
var b = new LinkedListNode("Bundt");
var c = new LinkedListNode("Cheese");
var d = new LinkedListNode("Devil's Food");
var e = new LinkedListNode("Eccles");
a.next = b;
b.next = c;
c.next = d;
d.next = e;
expect(kthToLastNode(2, a).value).to.eql("Devil's Food");
});
});
describe('String Manipulations', function(){
it('Should reverse a string in place', function(){
var str = 'You are the puppy of my dreams';
var result = 'smaerd ym fo yppup eht era uoY';
expect(reverseStringInPlaceLongFor(str)).to.equal(result);
});
it('Should reverse words in a sentence', function(){
var message = 'find you will pain only go you recordings security the into if';
var result = 'if into the security recordings you go only pain will you find';
expect(reverseWords(message)).to.equal(result);
});
});