This repository was archived by the owner on Apr 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-list.test.ts
More file actions
192 lines (165 loc) · 6.16 KB
/
array-list.test.ts
File metadata and controls
192 lines (165 loc) · 6.16 KB
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
import { ArrayList } from ".";
test("uses default size", () => {
const list = new ArrayList();
expect(list["_array"].size()).toBe(256);
});
test("accepts a size", () => {
const list = new ArrayList(5);
expect(list["_array"].size()).toBe(5);
expect(list.size()).toBe(0);
});
test("can add elements when there's space", () => {
const list = new ArrayList<string>(5);
list.add("Hello World");
expect(list.get(0)).toBe("Hello World");
});
test("cannot get out of bounds", () => {
expect(() => (new ArrayList(1)).get(-1)).toThrowError(ReferenceError);
expect(() => (new ArrayList(1)).get(99)).toThrowError(ReferenceError);
});
test("cannot set out of bounds", () => {
expect(() => (new ArrayList(1)).set(-1, 1)).toThrowError(ReferenceError);
expect(() => (new ArrayList(1)).set(99, 1)).toThrowError(ReferenceError);
});
test("can set elements by index", () => {
const list = new ArrayList(4);
[1, 2, 3, 4].forEach(n => list.add(n));
list.set(0, 5);
expect(list.get(0)).toBe(5);
});
test("dynamic memory allocation works", () => {
const list = new ArrayList(1);
list.add("Testing");
expect(list.size()).toBe(1);
expect(list["_array"].size()).toBe(1);
list.add("Testing");
expect(list.size()).toBe(2);
expect(list["_array"].size()).toBe(2);
list.add("Testing");
expect(list.size()).toBe(3);
expect(list["_array"].size()).toBe(4);
[1, 2, 3, 4].forEach(() => list.add("Testing"));
expect(list.size()).toBe(7);
expect(list["_array"].size()).toBe(8);
});
test("head and tail getters work", () => {
const list = new ArrayList(5);
list.add("Hello");
list.add("World");
expect(list.getHead()).toEqual({ value: "Hello", index: 0 });
expect(list.getTail()).toEqual({ value: "World", index: 1 });
});
test("can't get head or tail from empty list", () => {
expect(() => (new ArrayList(1)).getHead()).toThrowError(ReferenceError);
expect(() => (new ArrayList(1)).getTail()).toThrowError(ReferenceError);
});
test("head and tail getters work when head and tail reference the same element", () => {
const list = new ArrayList(1);
list.add("Hello World");
expect(list.getHead()).toEqual(list.getTail());
});
test("can remove elements", () => {
const list = new ArrayList(4);
[1, 2, 3, 4].forEach(n => list.add(n));
list.remove(0);
expect(list.size()).toBe(3);
[0, 1, 2].forEach(i => expect(list.get(i)).toBe(i + 2));
});
test("can't remove out of bounds", () => {
const list = new ArrayList(4);
[1, 2, 3, 4].forEach(n => list.add(n));
expect(() => list.remove(-1)).toThrowError(ReferenceError);
expect(() => list.remove(99)).toThrowError(ReferenceError);
});
test("can't remove from empty array", () => {
const list = new ArrayList(1);
expect(() => list.remove(0)).toThrowError(ReferenceError);
});
test("can swap by index", () => {
const list = new ArrayList(4);
[1, 2, 3, 4].forEach(n => list.add(n));
list.swapByIndex(0, 3);
expect(list.get(0)).toBe(4);
expect(list.get(3)).toBe(1);
list.swapByIndex(1, 2);
expect(list.get(1)).toBe(3);
expect(list.get(2)).toBe(2);
});
test("can't swap out of bounds", () => {
const list = new ArrayList(2);
list.add(1);
list.add(2);
expect(() => list.swapByIndex(0, 99)).toThrowError(ReferenceError);
expect(() => list.swapByIndex(-1, 0)).toThrowError(ReferenceError);
});
test("can pop tail", () => {
const list = new ArrayList(4);
[1, 2, 3, 4].forEach(n => list.add(n));
const tail = list.popTail();
expect(tail).toBe(4);
expect(list.size()).toBe(3);
});
test("can't pop tail from empty list", () => {
expect(() => (new ArrayList(1)).popTail()).toThrowError(ReferenceError);
});
test("safe getter returns element at first parameter's index when in bounds", () => {
const list = new ArrayList(1);
list.add(1337);
expect(list.getSafe(0, "NO")).toBe(1337);
});
test("safe getter returns second parameter when out of bounds", () => {
const list = new ArrayList(1);
list.add(1337);
expect(list.getSafe(1, "NO")).toBe("NO");
});
test("safe setter doesn't throw when setting out of bounds", () => {
const list = new ArrayList(1);
list.add(1337);
expect(() => list.setSafe(5, 0)).not.toThrow();
});
test("safe setter sets the specified element when in bounds", () => {
const list = new ArrayList(1);
list.add(1337);
expect(list.setSafe(0, 0)).toBeUndefined();
expect(list.get(0)).toBe(0);
});
test("find returns the first that matches the predicate", () => {
const list = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 8].forEach(n => list.add(n));
expect(list.find(n => n % 3 === 0)).toEqual(3);
expect(list.find(n => n % 2 === 0)).toEqual(2);
});
test("find returns undefined if predicate doesn't match", () => {
const list = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 8].forEach(n => list.add(n));
expect(list.find(() => false)).toBeUndefined();
});
test("isEqual returns false when parameters are uneven length", () => {
const list1 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7].forEach(n => list1.add(n));
const list2 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 9].forEach(n => list2.add(n));
expect(list1.isEqual(list2)).toBe(false);
});
test("isEqual returns true when the parameter matches", () => {
const list1 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 8].forEach(n => list1.add(n));
const list2 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 8].forEach(n => list2.add(n));
expect(list1.isEqual(list2)).toBe(true);
});
test("isEqual returns false when the parameter doesn't match", () => {
const list1 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 8].forEach(n => list1.add(n));
const list2 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 9].forEach(n => list2.add(n));
expect(list1.isEqual(list2)).toBe(false);
});
test("array list copy has exactly the same elements", () => {
const list1 = new ArrayList<number>(8);
[1, 2, 3, 4, 5, 6, 7, 8].forEach(n => list1.add(n));
const list2 = list1.copy();
[1, 2, 3, 4, 5, 6, 7, 8].forEach(
(n, idx) => expect(list2.get(idx)).toEqual(n)
);
});