diff options
Diffstat (limited to 'awk/rawk/tests/stdlib')
-rw-r--r-- | awk/rawk/tests/stdlib/README.md | 89 | ||||
-rw-r--r-- | awk/rawk/tests/stdlib/example_predicates_simple.rawk | 56 | ||||
-rw-r--r-- | awk/rawk/tests/stdlib/test_predicates.rawk | 196 | ||||
-rw-r--r-- | awk/rawk/tests/stdlib/test_predicates_simple.rawk | 61 | ||||
-rw-r--r-- | awk/rawk/tests/stdlib/test_stdlib_simple.rawk | 27 |
5 files changed, 0 insertions, 429 deletions
diff --git a/awk/rawk/tests/stdlib/README.md b/awk/rawk/tests/stdlib/README.md deleted file mode 100644 index 1b7b028..0000000 --- a/awk/rawk/tests/stdlib/README.md +++ /dev/null @@ -1,89 +0,0 @@ -# Standard Library Tests - -This directory contains tests for the built-in standard library functions. - -## Test Files - -### `test_stdlib_simple.rawk` - Standard Library Functions -Tests the built-in standard library functions: -- **Array utilities**: `keys()`, `values()`, `get_keys()`, `get_values()` -- **Testing functions**: `assert()`, `expect_equal()`, `expect_true()`, `expect_false()` -- **Functional programming**: `map()`, `reduce()`, `pipe()` (limited support) - -**Features:** -- Direct function calls (these work reliably) -- Array operations with proper error handling -- Boolean assertions for testing -- Basic functional programming utilities - -**Run with:** -```bash -awk -f ../../rawk.awk test_stdlib_simple.rawk | awk -f - -``` - -**Sample Output:** -``` -✓ double(5) = 10 -✓ square(4) = 16 -✓ add(3, 7) = 10 -🎉 All basic function tests passed! -``` - -## Standard Library Functions - -### Array Utilities -- `keys(array)`: Returns count of keys in array -- `values(array)`: Returns count of values in array -- `get_keys(array, result)`: Populates result array with keys -- `get_values(array, result)`: Populates result array with values - -### Testing Functions -- `assert(condition, message)`: Asserts a condition is true -- `expect_equal(actual, expected, message)`: Asserts actual equals expected -- `expect_true(condition, message)`: Asserts condition is true -- `expect_false(condition, message)`: Asserts condition is false - -### Functional Programming (Limited Support) -- `map(func_name, array)`: Maps function over array -- `reduce(func_name, array, initial)`: Reduces array with function -- `pipe(value, func_names...)`: Pipes value through functions - -### Predicate Functions (25+ functions) -**Type Checking:** `is_number()`, `is_string()`, `is_array()`, `is_empty()` -**Numeric:** `is_positive()`, `is_negative()`, `is_zero()`, `is_integer()`, `is_float()`, `is_even()`, `is_odd()`, `is_prime()`, `is_in_range()` -**Boolean:** `is_boolean()`, `is_truthy()`, `is_falsy()` -**String:** `is_alpha()`, `is_numeric()`, `is_alphanumeric()`, `is_whitespace()`, `is_uppercase()`, `is_lowercase()`, `is_palindrome()`, `is_length()` -**Validation:** `is_email()`, `is_url()`, `is_ipv4()` - -## Limitations - -The standard library functions have some limitations due to awk's constraints: - -1. **Indirect Function Calls**: Standard awk doesn't support `@func` syntax, so some functional programming features are limited -2. **Array Returns**: Functions cannot return arrays directly (use pass-by-reference) -3. **String-based Dispatch**: The `map` and `reduce` functions work with string function names but have limited support - -## Usage Examples - -### Array Operations -```rawk -data["a"] = 1 -data["b"] = 2 -data["c"] = 3 - -key_count = keys(data) # Returns 3 -get_keys(data, key_array) # Populates key_array with keys -``` - -### Testing -```rawk -result = add(2, 3) -expect_equal(result, 5, "add(2, 3) should return 5") -expect_true(result > 0, "result should be positive") -``` - -### Functional Programming -```rawk -numbers[1] = 1; numbers[2] = 2; numbers[3] = 3 -doubled = map("double", numbers) # Limited support -``` \ No newline at end of file diff --git a/awk/rawk/tests/stdlib/example_predicates_simple.rawk b/awk/rawk/tests/stdlib/example_predicates_simple.rawk deleted file mode 100644 index 426f369..0000000 --- a/awk/rawk/tests/stdlib/example_predicates_simple.rawk +++ /dev/null @@ -1,56 +0,0 @@ -# Simple example: Using rawk predicate functions - -BEGIN { - print "=== rawk Predicate Functions Example ===" - print "" - - # Test various predicate functions - print "=== Type Checking ===" - print "is_number(42): " is_number(42) - print "is_string(\"hello\"): " is_string("hello") - print "is_empty(\"\"): " is_empty("") - print "is_empty(0): " is_empty(0) - - print "" - print "=== Numeric Predicates ===" - print "is_positive(42): " is_positive(42) - print "is_negative(-5): " is_negative(-5) - print "is_zero(0): " is_zero(0) - print "is_integer(42): " is_integer(42) - print "is_float(3.14): " is_float(3.14) - print "is_even(42): " is_even(42) - print "is_odd(43): " is_odd(43) - print "is_prime(17): " is_prime(17) - print "is_in_range(5, 1, 10): " is_in_range(5, 1, 10) - - print "" - print "=== String Predicates ===" - print "is_alpha(\"hello\"): " is_alpha("hello") - print "is_numeric(\"123\"): " is_numeric("123") - print "is_alphanumeric(\"Hello123\"): " is_alphanumeric("Hello123") - print "is_uppercase(\"HELLO\"): " is_uppercase("HELLO") - print "is_lowercase(\"hello\"): " is_lowercase("hello") - print "is_palindrome(\"racecar\"): " is_palindrome("racecar") - print "is_length(\"hello\", 5): " is_length("hello", 5) - - print "" - print "=== Validation Predicates ===" - print "is_email(\"user@example.com\"): " is_email("user@example.com") - print "is_email(\"invalid-email\"): " is_email("invalid-email") - print "is_url(\"http://example.com\"): " is_url("http://example.com") - print "is_url(\"example.com\"): " is_url("example.com") - print "is_ipv4(\"192.168.1.1\"): " is_ipv4("192.168.1.1") - print "is_ipv4(\"256.1.2.3\"): " is_ipv4("256.1.2.3") - - print "" - print "=== Boolean Predicates ===" - print "is_boolean(1): " is_boolean(1) - print "is_boolean(0): " is_boolean(0) - print "is_truthy(42): " is_truthy(42) - print "is_truthy(0): " is_truthy(0) - print "is_falsy(0): " is_falsy(0) - print "is_falsy(42): " is_falsy(42) - - print "" - print "🎉 Predicate functions example completed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/stdlib/test_predicates.rawk b/awk/rawk/tests/stdlib/test_predicates.rawk deleted file mode 100644 index a8e2b47..0000000 --- a/awk/rawk/tests/stdlib/test_predicates.rawk +++ /dev/null @@ -1,196 +0,0 @@ -# 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"), 0) - 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 - 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!" - } -} \ No newline at end of file diff --git a/awk/rawk/tests/stdlib/test_predicates_simple.rawk b/awk/rawk/tests/stdlib/test_predicates_simple.rawk deleted file mode 100644 index b5f6970..0000000 --- a/awk/rawk/tests/stdlib/test_predicates_simple.rawk +++ /dev/null @@ -1,61 +0,0 @@ -# Simple test for rawk predicate functions - -BEGIN { - print "=== Simple Predicate Functions Test ===" - print "" - - # Test basic type checking - print "is_number(42): " is_number(42) - print "is_number(\"hello\"): " is_number("hello") - print "is_string(\"hello\"): " is_string("hello") - print "is_string(42): " is_string(42) - print "is_empty(\"\"): " is_empty("") - print "is_empty(0): " is_empty(0) - print "is_empty(\"hello\"): " is_empty("hello") - - # Test numeric predicates - print "" - print "is_positive(42): " is_positive(42) - print "is_positive(-5): " is_positive(-5) - print "is_negative(-42): " is_negative(-42) - print "is_negative(5): " is_negative(5) - print "is_zero(0): " is_zero(0) - print "is_zero(42): " is_zero(42) - print "is_integer(42): " is_integer(42) - print "is_integer(3.14): " is_integer(3.14) - print "is_even(42): " is_even(42) - print "is_odd(43): " is_odd(43) - print "is_prime(17): " is_prime(17) - print "is_prime(4): " is_prime(4) - - # Test string predicates - print "" - print "is_alpha(\"hello\"): " is_alpha("hello") - print "is_alpha(\"Hello123\"): " is_alpha("Hello123") - print "is_numeric(\"123\"): " is_numeric("123") - print "is_numeric(\"123abc\"): " is_numeric("123abc") - print "is_uppercase(\"HELLO\"): " is_uppercase("HELLO") - print "is_lowercase(\"hello\"): " is_lowercase("hello") - print "is_palindrome(\"racecar\"): " is_palindrome("racecar") - print "is_palindrome(\"hello\"): " is_palindrome("hello") - - # Test validation predicates - print "" - print "is_email(\"user@example.com\"): " is_email("user@example.com") - print "is_email(\"invalid-email\"): " is_email("invalid-email") - print "is_url(\"http://example.com\"): " is_url("http://example.com") - print "is_url(\"example.com\"): " is_url("example.com") - print "is_ipv4(\"192.168.1.1\"): " is_ipv4("192.168.1.1") - print "is_ipv4(\"256.1.2.3\"): " is_ipv4("256.1.2.3") - - # Test string length - print "" - print "is_length(\"hello\", 5): " is_length("hello", 5) - print "is_length(\"hello\", 3): " is_length("hello", 3) - - print "" - print "🎉 Simple predicate function tests completed!" - print "" - print "Note: Array detection functions have limitations in standard awk" - print "and cannot be tested in this simple format." -} \ No newline at end of file diff --git a/awk/rawk/tests/stdlib/test_stdlib_simple.rawk b/awk/rawk/tests/stdlib/test_stdlib_simple.rawk deleted file mode 100644 index 247e8d6..0000000 --- a/awk/rawk/tests/stdlib/test_stdlib_simple.rawk +++ /dev/null @@ -1,27 +0,0 @@ -# Simple standard library test -$double = (x) -> x * 2; -$square = (x) -> x * x; -$add = (a, b) -> a + b; - -# Test the standard library with direct function calls -BEGIN { - print "=== Testing Standard Library (Simple) ===" - - # Test direct function calls (these work) - print "double(5) =", double(5) - print "square(4) =", square(4) - print "add(3, 7) =", add(3, 7) - - # Test keys and values functions (these work) - data["a"] = 1 - data["b"] = 2 - data["c"] = 3 - key_array = keys(data) - value_array = values(data) - print "keys(data) =", key_array[1], key_array[2], key_array[3] - print "values(data) =", value_array[1], value_array[2], value_array[3] - - # Test nested function calls - print "double(square(3)) =", double(square(3)) - print "square(double(3)) =", square(double(3)) -} \ No newline at end of file |