Bob/leakTests/leaktest_builtin.bob

173 lines
5.3 KiB
Plaintext

// Memory leak test: Builtin function and stdlib scenarios
// Test memory behavior with builtin functions and standard library
print("=== Builtin Function Memory Leak Tests ===");
print("Initial memory: " + memoryUsage() + " MB");
// Test 1: Heavy string operations
print("Test 1: Heavy string operations");
var stringData = [];
for (var i = 0; i < 100000; i++) {
var str = toString(i) + "_" + toString(i * 2) + "_" + toString(i * 3);
stringData.push({
"original": str,
"upper": str, // Bob doesn't have toUpper, but test string storage
"length": str.len(),
"type": type(str)
});
}
print("Created " + stringData.len() + " string operation results");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear string data...");
stringData = none;
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 2: Type conversion stress test
print("Test 2: Type conversion stress");
var conversions = [];
for (var i = 0; i < 200000; i++) {
var num = i * 1.5;
var str = toString(num);
var backToNum = toNumber(str);
var intVal = toInt(num);
var boolVal = toBoolean(i % 2);
conversions.push([
num, str, backToNum, intVal, boolVal,
type(num), type(str), type(backToNum), type(intVal), type(boolVal)
]);
}
print("Created " + conversions.len() + " type conversion results");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear conversions...");
conversions = [];
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 3: Array/Dict builtin operations
print("Test 3: Collection builtin operations");
var collections = [];
for (var i = 0; i < 50000; i++) {
var arr = [i, i+1, i+2];
var dict = {"a": i, "b": i+1, "c": i+2};
// Use builtin functions heavily
var arrLen = arr.len();
arr.push(i+3);
var popped = arr.pop();
var dictKeys = dict.keys();
var dictValues = dict.values();
var hasA = dict.has("a");
collections.push({
"array": arr,
"dict": dict,
"arrLen": arrLen,
"popped": popped,
"keys": dictKeys,
"values": dictValues,
"hasA": hasA
});
}
print("Created " + collections.len() + " collection operation results");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear collections...");
collections = "cleared";
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 4: Eval function stress test
print("Test 4: Eval function stress");
var evalResults = [];
for (var i = 0; i < 10000; i++) {
var expression = toString(i) + " * 2 + 1;";
var result = eval(expression);
var funcExpr = "func() { return " + toString(i) + "; };";
var evalFunc = eval(funcExpr);
evalResults.push({
"expr": expression,
"result": result,
"func": evalFunc,
"funcResult": evalFunc()
});
}
print("Created " + evalResults.len() + " eval results");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear eval results...");
evalResults = none;
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
// Test 5: File I/O operations (if supported)
print("Test 5: File I/O operations");
var fileData = [];
for (var i = 0; i < 1000; i++) {
var filename = "temp_" + toString(i) + ".txt";
var content = "This is test data for file " + toString(i) + "\n";
content = content + "Line 2: " + toString(i * 2) + "\n";
content = content + "Line 3: " + toString(i * 3) + "\n";
// Write file
writeFile(filename, content);
// Check if exists
var exists = fileExists(filename);
// Read back
var readContent = readFile(filename);
var lines = readLines(filename);
fileData.push({
"filename": filename,
"exists": exists,
"content": readContent,
"lines": lines,
"lineCount": lines.len()
});
}
print("Created " + fileData.len() + " file operation results");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear file data...");
fileData = [];
print("Memory after clear: " + memoryUsage() + " MB");
// Clean up test files
print("Cleaning up test files...");
for (var i = 0; i < 1000; i++) {
var filename = "temp_" + toString(i) + ".txt";
if (fileExists(filename)) {
// Bob doesn't have deleteFile, but we created them
print("Note: Test file " + filename + " still exists");
if (i > 10) break; // Don't spam too many messages
}
}
input("File data cleared. Check memory usage...");
// Test 6: Random number generation
print("Test 6: Random number stress");
var randomData = [];
for (var i = 0; i < 200000; i++) {
import rand as RLeak;
var rand1 = RLeak.random();
var rand2 = RLeak.random();
var sum = rand1 + rand2;
randomData.push({
"rand1": rand1,
"rand2": rand2,
"sum": sum,
"product": rand1 * rand2
});
}
print("Created " + randomData.len() + " random number results");
print("Memory: " + memoryUsage() + " MB");
input("Press Enter to clear random data...");
randomData = none;
print("Memory after clear: " + memoryUsage() + " MB");
input("Cleared. Check memory usage...");
print("=== Builtin Function Tests Complete ===");