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 pathfixed-array.test.ts
More file actions
58 lines (50 loc) · 1.72 KB
/
fixed-array.test.ts
File metadata and controls
58 lines (50 loc) · 1.72 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
import { FixedArray } from ".";
test("accepts a size", () => {
const arr = new FixedArray(5);
expect(arr.size()).toBe(5);
});
test("holds elements", () => {
const arr = new FixedArray(2);
arr.set(0, "Hello");
arr.set(1, "World");
expect(arr.get(0)).toBe("Hello");
expect(arr.get(1)).toBe("World");
});
test("can create copies", () => {
const arr = new FixedArray(2);
arr.set(0, "Hello");
arr.set(1, "World");
const cp = arr.copy(3);
expect(cp.size()).toBe(3);
for (const el of arr)
expect(cp["_array"].includes(el));
});
test("doesn't accept initial size of less than one", () => {
expect(() => new FixedArray(0)).toThrowError(RangeError);
});
test("doesn't allow indexing out of bounds", () => {
const arr = new FixedArray(5);
expect(() => arr.get(-1)).toThrowError(ReferenceError);
expect(() => arr.get(99)).toThrowError(ReferenceError);
expect(() => arr.set(-1, 5)).toThrowError(ReferenceError);
expect(() => arr.set(99, 5)).toThrowError(ReferenceError);
});
test("doesn't allow creating a smaller copy", () => {
expect(() => (new FixedArray(5)).copy(4)).toThrowError(RangeError);
});
test("default value is set", () => {
const arr = new FixedArray(50, "test");
for (let i = 0; i < 50; i++)
expect(arr.get(i)).toBe("test");
});
test("changeWithFn method changes elements like dictated by the second argument", () => {
const arr = new FixedArray(50, 0);
for (let i = 0; i < 50; i++)
arr.changeWithFn(i, () => i);
for (let i = 0; i < 50; i++)
expect(arr.get(i)).toBe(i);
for (let i = 0; i < 50; i++)
arr.changeWithFn(i, x => x / 2);
for (let i = 0; i < 50; i++)
expect(arr.get(i)).toBe(i / 2);
});