-
Notifications
You must be signed in to change notification settings - Fork 612
Expand file tree
/
Copy pathforEach.spec.ts
More file actions
143 lines (110 loc) · 3.15 KB
/
Copy pathforEach.spec.ts
File metadata and controls
143 lines (110 loc) · 3.15 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
import { describe, expect, it } from 'vitest';
import { forEach } from './forEach';
describe('forEach', () => {
it('should execute callback for each element in the Set', () => {
const set = new Set([1, 2, 3]);
const result: number[] = [];
forEach(set, value => {
result.push(value);
});
expect(result).toEqual([1, 2, 3]);
});
it('should pass the value twice and the set to the callback', () => {
const set = new Set(['a', 'b', 'c']);
const results: Array<{ value1: string; hasValue: boolean }> = [];
forEach(set, (value, originalSet) => {
results.push({
value1: value,
hasValue: originalSet.has(value),
});
});
expect(results).toEqual([
{ value1: 'a', hasValue: true },
{ value1: 'b', hasValue: true },
{ value1: 'c', hasValue: true },
]);
});
it('should work with an empty Set', () => {
const set = new Set<number>();
let callCount = 0;
forEach(set, () => {
callCount++;
});
expect(callCount).toBe(0);
});
it('should work with a single-element Set', () => {
const set = new Set([42]);
const result: number[] = [];
forEach(set, value => {
result.push(value);
});
expect(result).toEqual([42]);
});
it('should iterate in insertion order', () => {
const set = new Set<string>();
set.add('first');
set.add('second');
set.add('third');
const values: string[] = [];
forEach(set, value => {
values.push(value);
});
expect(values).toEqual(['first', 'second', 'third']);
});
it('should work with various value types', () => {
const obj = { id: 1 };
const arr = [1, 2, 3];
const set = new Set(['hello', 42, true, obj, arr]);
const values: any[] = [];
forEach(set, value => {
values.push(value);
});
expect(values).toEqual(['hello', 42, true, obj, arr]);
});
it('should work with object elements', () => {
const set = new Set([
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
]);
const names: string[] = [];
forEach(set, value => {
names.push(value.name);
});
expect(names).toEqual(['Alice', 'Bob', 'Charlie']);
});
it('should allow side effects in the callback', () => {
const set = new Set([1, 2, 3, 4, 5]);
let sum = 0;
forEach(set, value => {
sum += value;
});
expect(sum).toBe(15);
});
it('should work with boolean values', () => {
const set = new Set([true, false]);
const result: boolean[] = [];
forEach(set, value => {
result.push(value);
});
expect(result).toEqual([true, false]);
});
it('should work with symbol values', () => {
const sym1 = Symbol('sym1');
const sym2 = Symbol('sym2');
const set = new Set([sym1, sym2]);
const symbols: symbol[] = [];
forEach(set, value => {
symbols.push(value);
});
expect(symbols).toEqual([sym1, sym2]);
});
it('should handle large Sets', () => {
const set = new Set(Array.from({ length: 1000 }, (_, i) => i));
let count = 0;
forEach(set, () => {
count++;
});
expect(count).toBe(1000);
});
});