$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); $double = (x) -> x * 2; BEGIN { print "=== Enhanced Utilities Test Suite (Simplified) ===" 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") # Check that all expected negative numbers are present (order may vary) has_neg1 = 0 has_neg5 = 0 has_neg3 = 0 for (i = 1; i <= negative_count; i++) { if (negative_numbers[i] == -1) has_neg1 = 1 if (negative_numbers[i] == -5) has_neg5 = 1 if (negative_numbers[i] == -3) has_neg3 = 1 } expect_true(has_neg1, "Should contain -1") expect_true(has_neg5, "Should contain -5") expect_true(has_neg3, "Should contain -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 (order may vary) first_negative = find("is_negative", numbers) expect_true(first_negative == -1 || first_negative == -5 || first_negative == -3, "First negative should be one of the negative numbers") print "✓ Find first negative working" print "" # Test 3: FindIndex function print "Test 3: FindIndex Function" # Find index of first positive number (order may vary) first_positive_index = findIndex("is_positive", numbers) expect_true(first_positive_index >= 1 && first_positive_index <= 7, "First positive should be at a valid index") print "✓ FindIndex first positive working" # Find index of first even number (order may vary) first_even_index = findIndex("is_even", numbers) expect_true(first_even_index >= 1 && first_even_index <= 7, "First even should be at a valid index") print "✓ FindIndex first even working" # Find index of first negative number (order may vary) first_negative_index = findIndex("is_negative", numbers) expect_true(first_negative_index >= 1 && first_negative_index <= 7, "First negative should be at a valid index") print "✓ FindIndex first negative 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") # Check that both valid emails are present (order may vary) has_user = 0 has_another = 0 for (i = 1; i <= valid_emails_count; i++) { if (valid_emails[i] == "user@example.com") has_user = 1 if (valid_emails[i] == "another@domain.org") has_another = 1 } expect_true(has_user, "Should contain user@example.com") expect_true(has_another, "Should contain another@domain.org") print "✓ Email filtering 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)" print "✓ Edge cases (empty arrays, single elements)" print "✓ Integration with existing functional programming features" }