// 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); push(stringData, { "original": str, "upper": str, // Bob doesn't have toUpper, but test string storage "length": len(str), "type": type(str) }); } print("Created " + len(stringData) + " 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); push(conversions, [ num, str, backToNum, intVal, boolVal, type(num), type(str), type(backToNum), type(intVal), type(boolVal) ]); } print("Created " + len(conversions) + " 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 = len(arr); push(arr, i+3); var popped = pop(arr); var dictKeys = keys(dict); var dictValues = values(dict); var hasA = has(dict, "a"); push(collections, { "array": arr, "dict": dict, "arrLen": arrLen, "popped": popped, "keys": dictKeys, "values": dictValues, "hasA": hasA }); } print("Created " + len(collections) + " 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); push(evalResults, { "expr": expression, "result": result, "func": evalFunc, "funcResult": evalFunc() }); } print("Created " + len(evalResults) + " 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); push(fileData, { "filename": filename, "exists": exists, "content": readContent, "lines": lines, "lineCount": len(lines) }); } print("Created " + len(fileData) + " 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++) { var rand1 = random(); var rand2 = random(); var sum = rand1 + rand2; push(randomData, { "rand1": rand1, "rand2": rand2, "sum": sum, "product": rand1 * rand2 }); } print("Created " + len(randomData) + " 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 ===");