···11+// Test WeakMap functionality
22+console.log('=== WeakMap Tests ===');
33+44+const wm = new WeakMap();
55+66+// Test with object keys
77+const key1 = { id: 1 };
88+const key2 = { id: 2 };
99+const key3 = { id: 3 };
1010+1111+// Test set and get
1212+wm.set(key1, 'value1');
1313+wm.set(key2, { data: 42 });
1414+wm.set(key3, true);
1515+1616+console.log('Get key1:', wm.get(key1)); // Should be 'value1'
1717+console.log('Get key2:', wm.get(key2)); // Should be { data: 42 }
1818+console.log('Get key3:', wm.get(key3)); // Should be true
1919+2020+// Test has()
2121+console.log('Has key1:', wm.has(key1)); // Should be true
2222+console.log('Has key2:', wm.has(key2)); // Should be true
2323+const key4 = { id: 4 };
2424+console.log('Has key4 (not added):', wm.has(key4)); // Should be false
2525+2626+// Test delete()
2727+console.log('Delete key2:', wm.delete(key2)); // Should be true
2828+console.log('Has key2 after delete:', wm.has(key2)); // Should be false
2929+console.log('Get key2 after delete:', wm.get(key2)); // Should be undefined
3030+3131+// Test that key1 and key3 are still there
3232+console.log('Has key1 after delete key2:', wm.has(key1)); // Should be true
3333+console.log('Has key3 after delete key2:', wm.has(key3)); // Should be true
3434+3535+// Test overwrite
3636+wm.set(key1, 'newvalue1');
3737+console.log('Get key1 after overwrite:', wm.get(key1)); // Should be 'newvalue1'
3838+3939+// Test that primitive keys throw errors
4040+try {
4141+ wm.set('string', 'value');
4242+ console.log('ERROR: Should have thrown for string key');
4343+} catch (e) {
4444+ console.log('Correctly threw error for string key:', e.message);
4545+}
4646+4747+try {
4848+ wm.set(123, 'value');
4949+ console.log('ERROR: Should have thrown for number key');
5050+} catch (e) {
5151+ console.log('Correctly threw error for number key:', e.message);
5252+}
5353+5454+try {
5555+ wm.set(null, 'value');
5656+ console.log('ERROR: Should have thrown for null key');
5757+} catch (e) {
5858+ console.log('Correctly threw error for null key:', e.message);
5959+}
6060+6161+// Test get/has/delete with non-object keys (should return undefined/false)
6262+console.log('Get with string key:', wm.get('string')); // Should be undefined
6363+console.log('Has with number key:', wm.has(123)); // Should be false
6464+console.log('Delete with boolean key:', wm.delete(true)); // Should be false
6565+6666+// Test multiple WeakMaps
6767+const wm2 = new WeakMap();
6868+wm2.set(key1, 'different value');
6969+console.log('Get key1 from wm:', wm.get(key1)); // Should be 'newvalue1'
7070+console.log('Get key1 from wm2:', wm2.get(key1)); // Should be 'different value'
7171+7272+console.log('WeakMap tests completed!');
+85
tests/test_weakset.js
···11+// Test WeakSet functionality
22+console.log('=== WeakSet Tests ===');
33+44+const ws = new WeakSet();
55+66+// Test with object values
77+const obj1 = { id: 1 };
88+const obj2 = { id: 2 };
99+const obj3 = { id: 3 };
1010+1111+// Test add and has
1212+ws.add(obj1);
1313+ws.add(obj2);
1414+ws.add(obj3);
1515+1616+console.log('Has obj1:', ws.has(obj1)); // Should be true
1717+console.log('Has obj2:', ws.has(obj2)); // Should be true
1818+console.log('Has obj3:', ws.has(obj3)); // Should be true
1919+2020+const obj4 = { id: 4 };
2121+console.log('Has obj4 (not added):', ws.has(obj4)); // Should be false
2222+2323+// Test delete()
2424+console.log('Delete obj2:', ws.delete(obj2)); // Should be true
2525+console.log('Has obj2 after delete:', ws.has(obj2)); // Should be false
2626+2727+// Test that obj1 and obj3 are still there
2828+console.log('Has obj1 after delete obj2:', ws.has(obj1)); // Should be true
2929+console.log('Has obj3 after delete obj2:', ws.has(obj3)); // Should be true
3030+3131+// Test adding the same object twice (should not throw)
3232+ws.add(obj1);
3333+console.log('Has obj1 after re-adding:', ws.has(obj1)); // Should still be true
3434+3535+// Test that primitive values throw errors
3636+try {
3737+ ws.add('string');
3838+ console.log('ERROR: Should have thrown for string value');
3939+} catch (e) {
4040+ console.log('Correctly threw error for string value:', e.message);
4141+}
4242+4343+try {
4444+ ws.add(123);
4545+ console.log('ERROR: Should have thrown for number value');
4646+} catch (e) {
4747+ console.log('Correctly threw error for number value:', e.message);
4848+}
4949+5050+try {
5151+ ws.add(null);
5252+ console.log('ERROR: Should have thrown for null value');
5353+} catch (e) {
5454+ console.log('Correctly threw error for null value:', e.message);
5555+}
5656+5757+try {
5858+ ws.add(undefined);
5959+ console.log('ERROR: Should have thrown for undefined value');
6060+} catch (e) {
6161+ console.log('Correctly threw error for undefined value:', e.message);
6262+}
6363+6464+// Test has/delete with non-object values (should return false)
6565+console.log('Has with string:', ws.has('string')); // Should be false
6666+console.log('Has with number:', ws.has(123)); // Should be false
6767+console.log('Delete with boolean:', ws.delete(true)); // Should be false
6868+6969+// Test multiple WeakSets with the same object
7070+const ws2 = new WeakSet();
7171+ws2.add(obj1);
7272+console.log('Has obj1 in ws:', ws.has(obj1)); // Should be true
7373+console.log('Has obj1 in ws2:', ws2.has(obj1)); // Should be true
7474+7575+// Delete from ws should not affect ws2
7676+ws.delete(obj1);
7777+console.log('Has obj1 in ws after delete:', ws.has(obj1)); // Should be false
7878+console.log('Has obj1 in ws2 after delete from ws:', ws2.has(obj1)); // Should be true
7979+8080+// Test chaining
8181+ws.add(obj1).add(obj2);
8282+console.log('Has obj1 after chaining:', ws.has(obj1)); // Should be true
8383+console.log('Has obj2 after chaining:', ws.has(obj2)); // Should be true
8484+8585+console.log('WeakSet tests completed!');