Bob/string_indexing_errors_test.bob

84 lines
2.4 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

print("=== STRING INDEXING ERROR HANDLING TEST ===");
print("Testing error handling and bounds checking...");
// ========================================
// TEST 1: OUT OF BOUNDS INDEXING
// ========================================
print("\n🚫 TEST 1: Out of Bounds Indexing");
var str = "Hello";
print(" String: " + str + " (length: " + toString(len(str)) + ")");
// Test negative index
print(" Testing negative index...");
var negativeIndex = -1;
// This should cause an error
var result = str[negativeIndex];
print("✅ Negative index handling: PASS");
// ========================================
// TEST 2: INDEX TOO LARGE
// ========================================
print("\n📏 TEST 2: Index Too Large");
var shortStr = "Hi";
print(" String: " + shortStr + " (length: " + toString(len(shortStr)) + ")");
// Test index beyond string length
print(" Testing index beyond length...");
var largeIndex = 10;
// This should cause an error
var result2 = shortStr[largeIndex];
print("✅ Large index handling: PASS");
// ========================================
// TEST 3: EMPTY STRING INDEXING
// ========================================
print("\n🈳 TEST 3: Empty String Indexing");
var empty = "";
print(" Empty string length: " + toString(len(empty)));
// Test indexing empty string
print(" Testing empty string indexing...");
// This should cause an error
var result3 = empty[0];
print("✅ Empty string indexing handling: PASS");
// ========================================
// TEST 4: INVALID INDEX TYPE
// ========================================
print("\n🔢 TEST 4: Invalid Index Type");
var testStr = "Hello";
print(" String: " + testStr);
// Test string index instead of number
print(" Testing string index...");
var stringIndex = "0";
// This should cause an error
var result4 = testStr[stringIndex];
print("✅ Invalid index type handling: PASS");
// ========================================
// TEST 5: STRING ASSIGNMENT (SHOULD FAIL)
// ========================================
print("\n✏ TEST 5: String Assignment (Should Fail)");
var mutableStr = "Hello";
print(" String: " + mutableStr);
// Test string assignment
print(" Testing string assignment...");
// This should cause an error - strings are immutable
mutableStr[0] = "X";
print("✅ String assignment error handling: PASS");
print("\n🎉 ERROR HANDLING TESTS COMPLETE! 🎉");
print("All error conditions are properly handled!");
print("String indexing is bulletproof!");