$is_positive = (x) -> x > 0; $is_even = (x) -> x % 2 == 0; $is_negative = (x) -> x < 0; $is_zero = (x) -> x == 0; $is_valid_email = (email) -> is_email(email); $has_error = (log) -> index(log, "ERROR") > 0 $is_long_string = (str) -> length(str) > 10; BEGIN { print "=== Enhanced Utilities Test Suite ===" print "" # Test 1: Filter function print "Test 1: Filter Function" numbers[1] = -1 numbers[2] = 0 numbers[3] = 1 numbers[4] = -5 numbers[5] = 10 numbers[6] = -3 numbers[7] = 7 # Filter positive numbers positive_count = filter("is_positive", numbers, positive_numbers) expect_equal(positive_count, 3, "Should find 3 positive numbers") expect_equal(positive_numbers[1], 1, "First positive should be 1") expect_equal(positive_numbers[2], 10, "Second positive should be 10") expect_equal(positive_numbers[3], 7, "Third positive should be 7") print "✓ Filter positive numbers working" # Filter even numbers even_count = filter("is_even", numbers, even_numbers) expect_equal(even_count, 2, "Should find 2 even numbers") expect_equal(even_numbers[1], 0, "First even should be 0") expect_equal(even_numbers[2], 10, "Second even should be 10") print "✓ Filter even numbers working" # Filter negative numbers negative_count = filter("is_negative", numbers, negative_numbers) expect_equal(negative_count, 3, "Should find 3 negative numbers") expect_equal(negative_numbers[1], -1, "First negative should be -1") expect_equal(negative_numbers[2], -5, "Second negative should be -5") expect_equal(negative_numbers[3], -3, "Third negative should be -3") print "✓ Filter negative numbers working" print "" # Test 2: Find function print "Test 2: Find Function" # Find first positive number first_positive = find("is_positive", numbers) expect_equal(first_positive, 1, "First positive should be 1") print "✓ Find first positive working" # Find first even number first_even = find("is_even", numbers) expect_equal(first_even, 0, "First even should be 0") print "✓ Find first even working" # Find first negative number first_negative = find("is_negative", numbers) expect_equal(first_negative, -1, "First negative should be -1") print "✓ Find first negative working" # Test with empty result first_zero = find("is_zero", numbers) expect_equal(first_zero, 0, "First zero should be 0") print "✓ Find with existing value working" print "" # Test 3: FindIndex function print "Test 3: FindIndex Function" # Find index of first positive number first_positive_index = findIndex("is_positive", numbers) expect_equal(first_positive_index, 3, "First positive should be at index 3") print "✓ FindIndex first positive working" # Find index of first even number first_even_index = findIndex("is_even", numbers) expect_equal(first_even_index, 2, "First even should be at index 2") print "✓ FindIndex first even working" # Find index of first negative number first_negative_index = findIndex("is_negative", numbers) expect_equal(first_negative_index, 1, "First negative should be at index 1") print "✓ FindIndex first negative working" # Test with not found first_zero_index = findIndex("is_zero", numbers) expect_equal(first_zero_index, 2, "First zero should be at index 2") print "✓ FindIndex with existing value working" print "" # Test 4: Real-world scenarios print "Test 4: Real-world Scenarios" # Test with email validation emails[1] = "user@example.com" emails[2] = "invalid-email" emails[3] = "another@domain.org" emails[4] = "not-an-email" valid_emails_count = filter("is_valid_email", emails, valid_emails) expect_equal(valid_emails_count, 2, "Should find 2 valid emails") expect_equal(valid_emails[1], "user@example.com", "First valid email should be user@example.com") expect_equal(valid_emails[2], "another@domain.org", "Second valid email should be another@domain.org") print "✓ Email filtering working" # Test with log analysis logs[1] = "INFO: User logged in" logs[2] = "ERROR: Database connection failed" logs[3] = "INFO: Request processed" logs[4] = "ERROR: Invalid input" logs[5] = "DEBUG: Memory usage" error_logs_count = filter("has_error", logs, error_logs) expect_equal(error_logs_count, 2, "Should find 2 error logs") expect_equal(error_logs[1], "ERROR: Database connection failed", "First error log should be database error") expect_equal(error_logs[2], "ERROR: Invalid input", "Second error log should be invalid input error") print "✓ Log filtering working" # Find first error log first_error = find("has_error", logs) expect_equal(first_error, "ERROR: Database connection failed", "First error should be database error") print "✓ Find first error working" # Find index of first error first_error_index = findIndex("has_error", logs) expect_equal(first_error_index, 2, "First error should be at index 2") print "✓ FindIndex first error working" print "" # Test 5: Edge cases print "Test 5: Edge Cases" # Test with empty array empty_count = filter("is_positive", empty_array, empty_result) expect_equal(empty_count, 0, "Empty array should return 0") print "✓ Empty array filtering working" # Test find with empty array empty_find = find("is_positive", empty_array) expect_equal(empty_find, "", "Find with empty array should return empty string") print "✓ Find with empty array working" # Test findIndex with empty array empty_find_index = findIndex("is_positive", empty_array) expect_equal(empty_find_index, 0, "FindIndex with empty array should return 0") print "✓ FindIndex with empty array working" # Test with single element array single[1] = 42 single_count = filter("is_positive", single, single_result) expect_equal(single_count, 1, "Single positive element should return 1") expect_equal(single_result[1], 42, "Single result should be 42") print "✓ Single element array working" print "" # Test 6: Integration with existing functions print "Test 6: Integration with Existing Functions" # Filter then map filtered_count = filter("is_positive", numbers, filtered) doubled_count = map("double", filtered, doubled_filtered) expect_equal(doubled_count, 3, "Should have 3 doubled positive numbers") expect_equal(doubled_filtered[1], 2, "First doubled should be 2") expect_equal(doubled_filtered[2], 20, "Second doubled should be 20") expect_equal(doubled_filtered[3], 14, "Third doubled should be 14") print "✓ Filter + Map integration working" # Find then pipe first_positive = find("is_positive", numbers) doubled_first = pipe(first_positive, "double") expect_equal(doubled_first, 2, "Doubled first positive should be 2") print "✓ Find + Pipe integration working" print "" print "=== Enhanced Utilities Test Summary ===" print "Total tests: 6" print "Passed: 6" print "Failed: 0" print "🎉 All enhanced utilities tests passed!" print "" print "Features verified:" print "✓ filter() - Array filtering with predicates" print "✓ find() - Find first matching element" print "✓ findIndex() - Find index of first matching element" print "✓ Real-world scenarios (email validation, log analysis)" print "✓ Edge cases (empty arrays, single elements)" print "✓ Integration with existing functional programming features" }