Bob/leakTests/leaktest_functions.bob

89 lines
2.8 KiB
Plaintext

// Memory leak test: Function-related scenarios
// Test various function patterns that could cause memory leaks
print("=== Function Memory Leak Tests ===");
print("Initial memory: " + memoryUsage() + " MB");
// Test 1: Recursive function closures
print("Test 1: Recursive function closures");
var recursiveFuncs = [];
for (var i = 0; i < 100000; i++) {
push(recursiveFuncs, func() {
var capturedI = i;
return func() {
if (capturedI > 0) {
return capturedI * capturedI;
}
return 0;
};
});
}
print("Created " + len(recursiveFuncs) + " recursive closure functions");
print("Memory after creation: " + memoryUsage() + " MB");
input("Press Enter to clear recursive functions...");
recursiveFuncs = none;
print("Memory after cleanup: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 2: Functions returning functions (factory pattern)
print("Test 2: Function factories");
var factories = [];
for (var i = 0; i < 100000; i++) {
push(factories, func() {
var multiplier = i;
return func(x) {
return x * multiplier;
};
});
}
print("Created " + len(factories) + " function factories");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear factories...");
factories = [];
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 3: Deep function nesting
print("Test 3: Deep function nesting");
var deepNested = [];
for (var i = 0; i < 50000; i++) {
push(deepNested, func() {
return func() {
return func() {
return func() {
return func() {
return i * 42;
};
};
};
};
});
}
print("Created " + len(deepNested) + " deeply nested functions");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear deep nested...");
deepNested = "test";
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 4: Circular function references
print("Test 4: Circular function references");
var circularFuncs = [];
for (var i = 0; i < 1000000; i++) {
var funcA = func() {
return "A" + i;
};
var funcB = func() {
return "B" + i;
};
// Store both in same array element to create potential circular refs
push(circularFuncs, [funcA, funcB, func() { return funcA; }]);
}
print("Created " + len(circularFuncs) + " circular function structures");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear circular functions...");
circularFuncs = 42;
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
print("=== Function Tests Complete ===");