# Test suite for rawk predicate functions # This demonstrates all the new type checking and validation functions BEGIN { print "=== rawk Predicate Functions Test Suite ===" print "" # Test counters total_tests = 0 passed_tests = 0 failed_tests = 0 # Helper function to run tests $run_test = (name, condition, expected) -> { total_tests++ if (condition == expected) { passed_tests++ print "✓ " name } else { failed_tests++ print "❌ " name " (expected " expected ", got " condition ")" } } # Helper function to print section headers $print_section = (title) -> { print "" print "--- " title " ---" } # Test basic type checking print_section("Basic Type Checking") run_test("is_number(42)", is_number(42), 1) run_test("is_number(0)", is_number(0), 1) run_test("is_number(-3.14)", is_number(-3.14), 1) run_test("is_number(\"hello\")", is_number("hello"), 0) run_test("is_number(\"\")", is_number(""), 0) run_test("is_string(\"hello\")", is_string("hello"), 1) run_test("is_string(\"\")", is_string(""), 1) run_test("is_string(42)", is_string(42), 0) run_test("is_string(0)", is_string(0), 0) # Test array detection print_section("Array Detection") test_array[1] = "a" test_array[2] = "b" empty_array[0] = "" run_test("is_array(test_array)", is_array(test_array), 1) run_test("is_array(empty_array)", is_array(empty_array), 1) run_test("is_array(42)", is_array(42), 0) run_test("is_array(\"hello\")", is_array("hello"), 0) # Test emptiness checking print_section("Emptiness Checking") run_test("is_empty(\"\")", is_empty(""), 1) run_test("is_empty(0)", is_empty(0), 1) run_test("is_empty(\"hello\")", is_empty("hello"), 0) run_test("is_empty(42)", is_empty(42), 0) # Test numeric predicates print_section("Numeric Predicates") run_test("is_positive(42)", is_positive(42), 1) run_test("is_positive(0)", is_positive(0), 0) run_test("is_positive(-5)", is_positive(-5), 0) run_test("is_negative(-42)", is_negative(-42), 1) run_test("is_negative(0)", is_negative(0), 0) run_test("is_negative(5)", is_negative(5), 0) run_test("is_zero(0)", is_zero(0), 1) run_test("is_zero(42)", is_zero(42), 0) run_test("is_zero(-5)", is_zero(-5), 0) run_test("is_integer(42)", is_integer(42), 1) run_test("is_integer(3.14)", is_integer(3.14), 0) run_test("is_integer(0)", is_integer(0), 1) run_test("is_float(3.14)", is_float(3.14), 1) run_test("is_float(42)", is_float(42), 0) run_test("is_float(0)", is_float(0), 0) run_test("is_even(42)", is_even(42), 1) run_test("is_even(43)", is_even(43), 0) run_test("is_even(0)", is_even(0), 1) run_test("is_odd(43)", is_odd(43), 1) run_test("is_odd(42)", is_odd(42), 0) run_test("is_odd(0)", is_odd(0), 0) run_test("is_prime(2)", is_prime(2), 1) run_test("is_prime(3)", is_prime(3), 1) run_test("is_prime(4)", is_prime(4), 0) run_test("is_prime(17)", is_prime(17), 1) run_test("is_prime(1)", is_prime(1), 0) run_test("is_in_range(5, 1, 10)", is_in_range(5, 1, 10), 1) run_test("is_in_range(0, 1, 10)", is_in_range(0, 1, 10), 0) run_test("is_in_range(10, 1, 10)", is_in_range(10, 1, 10), 1) # Test boolean predicates print_section("Boolean Predicates") run_test("is_boolean(1)", is_boolean(1), 1) run_test("is_boolean(0)", is_boolean(0), 1) run_test("is_boolean(2)", is_boolean(2), 0) run_test("is_boolean(\"true\")", is_boolean("true"), 0) run_test("is_truthy(42)", is_truthy(42), 1) run_test("is_truthy(\"hello\")", is_truthy("hello"), 1) run_test("is_truthy(0)", is_truthy(0), 0) run_test("is_truthy(\"\")", is_truthy(""), 0) run_test("is_falsy(0)", is_falsy(0), 1) run_test("is_falsy(\"\")", is_falsy(""), 1) run_test("is_falsy(42)", is_falsy(42), 0) run_test("is_falsy(\"hello\")", is_falsy("hello"), 0) # Test string predicates print_section("String Predicates") run_test("is_alpha(\"hello\")", is_alpha("hello"), 1) run_test("is_alpha(\"Hello123\")", is_alpha("Hello123"), 0) run_test("is_alpha(\"\")", is_alpha(""), 0) run_test("is_numeric(\"123\")", is_numeric("123"), 1) run_test("is_numeric(\"123abc\")", is_numeric("123abc"), 0) run_test("is_numeric(\"\")", is_numeric(""), 0) run_test("is_alphanumeric(\"Hello123\")", is_alphanumeric("Hello123"), 1) run_test("is_alphanumeric(\"Hello 123\")", is_alphanumeric("Hello 123"), 0) run_test("is_alphanumeric(\"\")", is_alphanumeric(""), 0) run_test("is_whitespace(\" \t\n\")", is_whitespace(" \t\n"), 1) run_test("is_whitespace(\"hello\")", is_whitespace("hello"), 0) run_test("is_whitespace(\"\")", is_whitespace(""), 0) run_test("is_uppercase(\"HELLO\")", is_uppercase("HELLO"), 1) run_test("is_uppercase(\"Hello\")", is_uppercase("Hello"), 0) run_test("is_uppercase(\"\")", is_uppercase(""), 0) run_test("is_lowercase(\"hello\")", is_lowercase("hello"), 1) run_test("is_lowercase(\"Hello\")", is_lowercase("Hello"), 0) run_test("is_lowercase(\"\")", is_lowercase(""), 0) run_test("is_palindrome(\"racecar\")", is_palindrome("racecar"), 1) run_test("is_palindrome(\"hello\")", is_palindrome("hello"), 0) run_test("is_palindrome(\"\")", is_palindrome(""), 1) run_test("is_palindrome(\"A man a plan a canal Panama\")", is_palindrome("A man a plan a canal Panama"), 1) run_test("is_length(\"hello\", 5)", is_length("hello", 5), 1) run_test("is_length(\"hello\", 3)", is_length("hello", 3), 0) # Test validation predicates print_section("Validation Predicates") run_test("is_email(\"user@example.com\")", is_email("user@example.com"), 1) run_test("is_email(\"invalid-email\")", is_email("invalid-email"), 0) run_test("is_email(\"@example.com\")", is_email("@example.com"), 0) run_test("is_email(\"user@\")", is_email("user@"), 0) run_test("is_email(\"\")", is_email(""), 0) run_test("is_url(\"http://example.com\")", is_url("http://example.com"), 1) run_test("is_url(\"https://example.com\")", is_url("https://example.com"), 1) run_test("is_url(\"ftp://example.com\")", is_url("ftp://example.com"), 1) run_test("is_url(\"example.com\")", is_url("example.com"), 0) run_test("is_ipv4(\"192.168.1.1\")", is_ipv4("192.168.1.1"), 1) run_test("is_ipv4(\"256.1.2.3\")", is_ipv4("256.1.2.3"), 0) run_test("is_ipv4(\"192.168.1\")", is_ipv4("192.168.1"), 0) run_test("is_ipv4(\"192.168.1.1.1\")", is_ipv4("192.168.1.1.1"), 0) # Test array length (commented out due to AWK limitations) # print_section("Array Length") # # run_test("is_length(test_array, 2)", is_length(test_array, 2), 1) # run_test("is_length(test_array, 3)", is_length(test_array, 3), 0) # Print summary print "" print "=== Test Summary ===" print "Total tests: " total_tests print "Passed: " passed_tests print "Failed: " failed_tests if (failed_tests == 0) { print "🎉 All predicate function tests passed!" } else { print "❌ Some tests failed!" } }