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
| const { DynamicArray } = require('./index.js');
const expect = require('chai').expect;
describe('DynamicArray', () => {
describe('push', () => {
it('should add a value to the end of the array', () => {
let arr = new DynamicArray();
arr.push(3, 4);
const actual = arr;
const expected = {
length: 2,
data: {0: 3, 1: 4},
capacity: 10
};
expect(actual).to.deep.equal(expected);
});
it('should return the new length of the array', () => {
let arr = new DynamicArray();
const actualLength = arr.push(3, 4);
const expectedLength = 2;
expect(actualLength).to.equal(expectedLength);
})
it('should double the capacity if the length meets or exceeds capacity', () => {
let arr = new DynamicArray(2);
arr.push(2, 3);
const actual = arr.capacity;
const expected = 4
expect(actual).to.equal(expected)
})
})
describe('pop', () => {
describe('should remove the last element from the list and return it', () => {
it('should work for integers', () => {
let arr = new DynamicArray();
arr.push(1,2,3,4,5);
const actual = arr.pop();
const expected = 5;
expect(actual).to.equal(expected)
})
it('should work for strings', () => {
let arr = new DynamicArray();
arr.push('One', 'for', 'the', 'money');
const actual = arr.pop();
const expected = 'money';
expect(actual).to.equal(expected)
})
it('should work for objects', () => {
let arr = new DynamicArray();
arr.push({doe: 'a deer'}, {ray: 'a drop of golden sun'}, {me: 'a name I call myself'}, { fa: 'a long, long way to run'});
const actual = arr.pop();
const expected = { fa: 'a long, long way to run'}
expect(actual).to.deep.equal(expected)
})
})
})
describe('shift', () => {
describe('should remove the first element of the list and return it', () => {
it('should work for integers', () => {
let arr = new DynamicArray();
arr.push(1,2,3,3,4);
const actual = arr.shift();
const expected = 1;
expect(actual).to.equal(expected)
})
it('should work for strings', () => {
let arr = new DynamicArray();
arr.push('One', 'is', 'the', 'loneliest', 'number');
const actual = arr.shift();
const expected = 'One';
expect(actual).to.equal(expected)
})
it('should work for objects', () => {
let arr = new DynamicArray();
arr.push({love: 'love me do'}, {you: 'know I love you'});
const actual = arr.shift();
const expected = { love: 'love me do' };
expect(actual).to.deep.equal(expected)
})
})
})
describe('unshift', () => {
it('should add values to the start of the array', () => {
let arr = new DynamicArray();
arr.push(8,9,10);
arr.unshift(1,2);
const actual = arr;
const expected = {
length: 5,
capacity: 10,
data: {
0: 1,
1: 2,
2: 8,
3: 9,
4: 10,
}
};
expect(actual).to.deep.equal(expected);
})
it('should return the new length of the array', () => {
let arr = new DynamicArray();
arr.push(3);
const actual = arr.unshift(7,8,9);
const expected = 4;
expect(actual).to.equal(expected)
})
it('should double the capacity if the length meets or exceeds capacity', () => {
let arr = new DynamicArray();
arr.unshift(1,2,3,4,5,6,7,8,9,10,11);
const actual = arr.capacity;
const expected = 20;
expect(actual).to.equal(actual)
})
})
})
|