diff options
Diffstat (limited to 'awk/rawk/tests')
37 files changed, 392 insertions, 2559 deletions
diff --git a/awk/rawk/tests/README.md b/awk/rawk/tests/README.md deleted file mode 100644 index e33a781..0000000 --- a/awk/rawk/tests/README.md +++ /dev/null @@ -1,74 +0,0 @@ -# rawk Test Suite - -This directory contains the comprehensive test suite for the rawk language, organized by category. - -## Directory Structure - -### `core/` - Core Language Features -Tests for fundamental language features like function definitions, calls, recursion, and edge cases. - -### `real_world/` - Real-World Examples -Practical examples that demonstrate rawk's utility for common data processing tasks. - -### `stdlib/` - Standard Library Tests -Tests for the built-in standard library functions. - -### `data/` - Test Data Files -Sample data files used by the real-world examples. - -## Running Tests - -### Run All Core Tests -```bash -# Run the comprehensive test suite -awk -f ../rawk.awk core/test_suite.rawk | awk -f - - -# Run individual core tests -awk -f ../rawk.awk core/test_basic.rawk | awk -f - -awk -f ../rawk.awk core/test_multiline.rawk | awk -f - -awk -f ../rawk.awk core/test_recursive.rawk | awk -f - -``` - -### Run Real-World Examples -```bash -# System monitoring -awk -f ../rawk.awk real_world/test_system_monitor.rawk | awk -f - data/test_data.txt - -# Log parsing -awk -f ../rawk.awk real_world/test_log_parser.rawk | awk -f - data/test_logs.txt - -# CSV processing -awk -f ../rawk.awk real_world/test_csv_processor.rawk | awk -f - data/test_employees.csv -``` - -### Run Standard Library Tests -```bash -awk -f ../rawk.awk stdlib/test_stdlib_simple.rawk | awk -f - -``` - -## Test Categories - -### Core Language Tests -- **test_suite.rawk**: Comprehensive test suite with 15+ test cases -- **test_basic.rawk**: Basic function definitions and calls -- **test_multiline.rawk**: Multi-line function definitions -- **test_edge_cases.rawk**: Edge cases and error conditions -- **test_recursive.rawk**: Recursive function support -- **test_array_fix.rawk**: Array handling and utilities -- **test_failure.rawk**: Demonstrates failing assertions - -### Real-World Examples -- **test_system_monitor.rawk**: System monitoring (df, ps, ls output) -- **test_log_parser.rawk**: Log parsing (Apache, syslog) -- **test_csv_processor.rawk**: CSV data processing with validation -- **test_data_processing.rawk**: General data processing scenarios -- **test_mixed.rawk**: Mixed awk and rawk code - -### Standard Library Tests -- **test_stdlib_simple.rawk**: Tests for built-in functions - -### Test Data -- **test_data.txt**: Simulated system command outputs -- **test_logs.txt**: Sample Apache and syslog entries -- **test_employees.csv**: Sample employee data -- **test_input.txt**: Simple input data for mixed tests \ No newline at end of file diff --git a/awk/rawk/tests/core/README.md b/awk/rawk/tests/core/README.md deleted file mode 100644 index 21ae650..0000000 --- a/awk/rawk/tests/core/README.md +++ /dev/null @@ -1,108 +0,0 @@ -# Core Language Tests - -This directory contains tests for the fundamental features of the rawk language. - -## Test Files - -### `test_suite.rawk` - Comprehensive Test Suite -The main test suite that covers all core language features: -- Basic function definitions and calls -- Multi-line functions -- Nested function calls -- Function calls within function bodies -- Edge cases and error conditions -- Boolean assertions -- Array operations -- Conditional expressions -- Complex expressions - -**Run with:** -```bash -awk -f ../../rawk.awk test_suite.rawk | awk -f - -``` - -### `test_basic.rawk` - Basic Functions -Tests basic single-line function definitions and calls: -- Addition, multiplication, string concatenation -- Function call replacement with internal names - -**Run with:** -```bash -awk -f ../../rawk.awk test_basic.rawk | awk -f - -``` - -### `test_multiline.rawk` - Multi-line Functions -Tests multi-line function definitions: -- Complex function bodies with multiple statements -- Return statements -- Array processing within functions - -**Run with:** -```bash -awk -f ../../rawk.awk test_multiline.rawk | awk -f - -``` - -### `test_edge_cases.rawk` - Edge Cases -Tests edge cases and error conditions: -- Functions with no arguments -- Functions with many arguments -- Complex expressions -- String operations -- Conditional expressions -- Array access - -**Run with:** -```bash -awk -f ../../rawk.awk test_edge_cases.rawk | awk -f - -``` - -### `test_recursive.rawk` - Recursive Functions -Tests recursive function support: -- Factorial function -- Fibonacci function -- Countdown function -- Self-referential function calls - -**Run with:** -```bash -awk -f ../../rawk.awk test_recursive.rawk | awk -f - -``` - -### `test_array_fix.rawk` - Array Handling -Tests array operations and utilities: -- Basic array operations -- Standard library array functions -- Associative arrays -- Array statistics - -**Run with:** -```bash -awk -f ../../rawk.awk test_array_fix.rawk | awk -f - -``` - -### `test_failure.rawk` - Assertion Failures -Demonstrates the assertion system: -- Shows how failing tests are reported -- Tests error message formatting -- Validates test framework functionality - -**Run with:** -```bash -awk -f ../../rawk.awk test_failure.rawk | awk -f - 2>&1 -``` - -## Expected Results - -All tests should pass with clear output showing: -- ✓ Test results with descriptions -- 🎉 Success messages -- Proper error reporting for failures - -The comprehensive test suite should show: -``` -=== Test Summary === -Total tests: 15 -Passed: 15 -Failed: 0 -🎉 All tests passed! -``` \ No newline at end of file diff --git a/awk/rawk/tests/core/test_array_fix.rawk b/awk/rawk/tests/core/test_array_fix.rawk deleted file mode 100644 index e488762..0000000 --- a/awk/rawk/tests/core/test_array_fix.rawk +++ /dev/null @@ -1,50 +0,0 @@ -# Test to isolate array handling issues -$test_array_func = (arr) -> { - return "Array has " length(arr) " elements" -}; - -BEGIN { - print "=== Testing Array Handling ===" - - # Test basic array operations - data[1] = 10 - data[2] = 20 - data[3] = 30 - - # Test our custom function - result = test_array_func(data) - expect_equal(result, "Array has 3 elements", "test_array_func should return correct count") - print "✓ " result - - # Test keys function - key_count = keys(data) - expect_equal(key_count, 3, "keys() should return count of 3") - get_keys(data, key_array) - expect_true(key_array[1] == 1 || key_array[1] == 2 || key_array[1] == 3, "First key should be 1, 2, or 3") - expect_true(key_array[2] == 1 || key_array[2] == 2 || key_array[2] == 3, "Second key should be 1, 2, or 3") - expect_true(key_array[3] == 1 || key_array[3] == 2 || key_array[3] == 3, "Third key should be 1, 2, or 3") - print "✓ keys() function works correctly" - - # Test values function - value_count = values(data) - expect_equal(value_count, 3, "values() should return count of 3") - get_values(data, value_array) - expect_true(value_array[1] == 10 || value_array[1] == 20 || value_array[1] == 30, "First value should be 10, 20, or 30") - expect_true(value_array[2] == 10 || value_array[2] == 20 || value_array[2] == 30, "Second value should be 10, 20, or 30") - expect_true(value_array[3] == 10 || value_array[3] == 20 || value_array[3] == 30, "Third value should be 10, 20, or 30") - print "✓ values() function works correctly" - - # Test associative array - info["name"] = "rawk" - info["type"] = "language" - info["target"] = "awk" - - info_key_count = keys(info) - info_value_count = values(info) - - expect_equal(info_key_count, 3, "keys() should work with associative arrays") - expect_equal(info_value_count, 3, "values() should work with associative arrays") - print "✓ Associative array operations work correctly" - - print "🎉 All array handling tests passed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_basic.rawk b/awk/rawk/tests/core/test_basic.rawk deleted file mode 100644 index d92091a..0000000 --- a/awk/rawk/tests/core/test_basic.rawk +++ /dev/null @@ -1,26 +0,0 @@ -# Basic rawk function definitions -$add = (x, y) -> x + y; -$multiply = (a, b) -> a * b; -$greet = (name) -> "Hello, " name; - -# Test the functions -BEGIN { - print "Testing basic functions:" - - # Test add function - result = add(5, 3) - expect_equal(result, 8, "add(5, 3) should return 8") - print "✓ add(5, 3) = " result - - # Test multiply function - result = multiply(4, 7) - expect_equal(result, 28, "multiply(4, 7) should return 28") - print "✓ multiply(4, 7) = " result - - # Test greet function - result = greet("World") - expect_equal(result, "Hello, World", "greet(\"World\") should return 'Hello, World'") - print "✓ greet(\"World\") = " result - - print "🎉 All basic function tests passed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_basic_functions.rawk b/awk/rawk/tests/core/test_basic_functions.rawk deleted file mode 100644 index 4c354ab..0000000 --- a/awk/rawk/tests/core/test_basic_functions.rawk +++ /dev/null @@ -1,171 +0,0 @@ -# Test suite for rawk basic functionality -# This demonstrates functions using standard awk flow control - -BEGIN { - print "=== rawk Basic Functionality Test Suite ===" - print "" - - # Test counters - total_tests = 0 - passed_tests = 0 - failed_tests = 0 - - # Helper function to run tests - $run_test = (name, actual, expected) -> { - total_tests++ - if (actual == expected) { - passed_tests++ - print "✓ " name - } else { - failed_tests++ - print "❌ " name " (expected '" expected "', got '" actual "')" - } - } - - # Basic function for number classification using if/else - $classify_number = (value) -> { - if (value == 0) { - return "zero" - } else if (value > 0) { - return "positive" - } else { - return "negative" - } - } - - # Basic function for string classification - $classify_string = (str) -> { - if (str == "") { - return "empty" - } else if (is_alpha(str)) { - return "alphabetic" - } else if (is_numeric(str)) { - return "numeric" - } else { - return "other" - } - } - - # Basic function for type checking - $classify_type = (value) -> { - if (is_number(value)) { - return "number" - } else if (is_empty(value)) { - return "empty" - } else { - return "string" - } - } - - # Basic function for validation - $validate_input = (value) -> { - if (value == "") { - return "empty input" - } else if (is_number(value) && is_in_range(value, 1, 100)) { - return "valid number in range" - } else { - return "invalid input" - } - } - - # Recursive Fibonacci function using if/else - $fibonacci = (n) -> { - if (n == 0) { - return 0 - } else if (n == 1) { - return 1 - } else { - return fibonacci(n - 1) + fibonacci(n - 2) - } - } - - # Recursive factorial function using if/else - $factorial = (n) -> { - if (n == 0) { - return 1 - } else if (n == 1) { - return 1 - } else { - return n * factorial(n - 1) - } - } - - # Single-line functions - $add = (a, b) -> a + b - $multiply = (a, b) -> a * b - $square = (x) -> x * x - $is_even = (n) -> n % 2 == 0 - $is_odd = (n) -> n % 2 == 1 - $max = (a, b) -> a > b ? a : b - $min = (a, b) -> a < b ? a : b - $abs = (x) -> x < 0 ? -x : x - - # Test number classification - print "=== Number Classification Tests ===" - run_test("classify 0", classify_number(0), "zero") - run_test("classify positive", classify_number(42), "positive") - run_test("classify negative", classify_number(-5), "negative") - print "" - - # Test string classification - print "=== String Classification Tests ===" - run_test("classify empty string", classify_string(""), "empty") - run_test("classify alphabetic", classify_string("hello"), "alphabetic") - run_test("classify numeric", classify_string("123"), "numeric") - run_test("classify other", classify_string("hello123"), "other") - print "" - - # Test type checking - print "=== Type Checking Tests ===" - run_test("classify number type", classify_type(42), "number") - run_test("classify string type", classify_type("hello"), "string") - run_test("classify empty type", classify_type(""), "empty") - print "" - - # Test validation - print "=== Validation Tests ===" - run_test("validate empty", validate_input(""), "empty input") - run_test("validate valid number", validate_input(50), "valid number in range") - run_test("validate invalid number", validate_input(150), "invalid input") - print "" - - # Test recursive functions - print "=== Recursive Function Tests ===" - run_test("fibonacci(0)", fibonacci(0), 0) - run_test("fibonacci(1)", fibonacci(1), 1) - run_test("fibonacci(5)", fibonacci(5), 5) - run_test("fibonacci(10)", fibonacci(10), 55) - print "" - - run_test("factorial(0)", factorial(0), 1) - run_test("factorial(1)", factorial(1), 1) - run_test("factorial(5)", factorial(5), 120) - run_test("factorial(6)", factorial(6), 720) - print "" - - # Test single-line functions - print "=== Single-Line Function Tests ===" - run_test("add(2, 3)", add(2, 3), 5) - run_test("multiply(4, 5)", multiply(4, 5), 20) - run_test("square(6)", square(6), 36) - run_test("is_even(4)", is_even(4), 1) - run_test("is_even(5)", is_even(5), 0) - run_test("is_odd(3)", is_odd(3), 1) - run_test("is_odd(4)", is_odd(4), 0) - run_test("max(10, 20)", max(10, 20), 20) - run_test("min(10, 20)", min(10, 20), 10) - run_test("abs(-5)", abs(-5), 5) - run_test("abs(5)", abs(5), 5) - print "" - - # Test summary - print "=== Test Summary ===" - print "Total tests: " total_tests - print "Passed: " passed_tests - print "Failed: " failed_tests - print "Success rate: " (passed_tests / total_tests * 100) "%" - - if (failed_tests > 0) { - exit 1 - } -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_edge_cases.rawk b/awk/rawk/tests/core/test_edge_cases.rawk deleted file mode 100644 index 8196acd..0000000 --- a/awk/rawk/tests/core/test_edge_cases.rawk +++ /dev/null @@ -1,59 +0,0 @@ -# Test edge cases and error conditions -$no_args = () -> "no arguments"; -$single_arg = (x) -> x; -$many_args = (a, b, c, d, e) -> a + b + c + d + e; -$empty_body = (x) -> ; -$complex_expr = (x, y) -> (x * y) + (x / y) - (x % y); - -# Test functions with different argument patterns -$string_concat = (str1, str2) -> str1 " " str2; -$array_access = (arr, idx) -> arr[idx]; -$conditional = (x) -> x > 0 ? "positive" : "negative"; - -# Test the edge cases -BEGIN { - print "=== Testing Edge Cases ===" - - # Test no arguments - result = no_args() - expect_equal(result, "no arguments", "no_args() should return 'no arguments'") - print "✓ no_args() = " result - - # Test single argument - result = single_arg(42) - expect_equal(result, 42, "single_arg(42) should return 42") - print "✓ single_arg(42) = " result - - # Test many arguments - result = many_args(1,2,3,4,5) - expect_equal(result, 15, "many_args(1,2,3,4,5) should return 15") - print "✓ many_args(1,2,3,4,5) = " result - - # Test complex expressions - result = complex_expr(10, 3) - expect_true(result > 32.3 && result < 32.4, "complex_expr(10, 3) should be approximately 32.3333") - print "✓ complex_expr(10, 3) = " result - - # Test string concatenation - result = string_concat("Hello", "World") - expect_equal(result, "Hello World", "string_concat(\"Hello\", \"World\") should return 'Hello World'") - print "✓ string_concat(\"Hello\", \"World\") = " result - - # Test conditional - result = conditional(5) - expect_equal(result, "positive", "conditional(5) should return 'positive'") - print "✓ conditional(5) = " result - - result = conditional(-3) - expect_equal(result, "negative", "conditional(-3) should return 'negative'") - print "✓ conditional(-3) = " result - - # Test array access - test_arr[1] = "first" - test_arr[2] = "second" - result = array_access(test_arr, 2) - expect_equal(result, "second", "array_access(test_arr, 2) should return 'second'") - print "✓ array_access(test_arr, 2) = " result - - print "🎉 All edge case tests passed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_failure.rawk b/awk/rawk/tests/core/test_failure.rawk deleted file mode 100644 index adeafa5..0000000 --- a/awk/rawk/tests/core/test_failure.rawk +++ /dev/null @@ -1,16 +0,0 @@ -# Test that demonstrates failing assertions -$add = (x, y) -> x + y; - -BEGIN { - print "Testing assertion failures (this should fail):" - - # This should pass - result = add(2, 3) - expect_equal(result, 5, "add(2, 3) should return 5") - print "✓ This assertion should pass" - - # This should fail - result = add(2, 3) - expect_equal(result, 10, "add(2, 3) should return 10 (this will fail)") - print "This line should not be reached" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_multiline.rawk b/awk/rawk/tests/core/test_multiline.rawk deleted file mode 100644 index 95a889f..0000000 --- a/awk/rawk/tests/core/test_multiline.rawk +++ /dev/null @@ -1,43 +0,0 @@ -# Multi-line rawk function definitions -$calculate_area = (width, height) -> { - area = width * height - return area -}; - -$format_message = (name, age) -> { - message = "Name: " name ", Age: " age - return message -}; - -$process_array = (arr) -> { - sum = 0 - for (i in arr) { - sum += arr[i] - } - return sum -}; - -# Test the multi-line functions -BEGIN { - print "Testing multi-line functions:" - - # Test calculate_area function - result = calculate_area(5, 3) - expect_equal(result, 15, "calculate_area(5, 3) should return 15") - print "✓ calculate_area(5, 3) = " result - - # Test format_message function - result = format_message("Alice", 30) - expect_equal(result, "Name: Alice, Age: 30", "format_message(\"Alice\", 30) should return 'Name: Alice, Age: 30'") - print "✓ format_message(\"Alice\", 30) = " result - - # Test with array - test_array[1] = 10 - test_array[2] = 20 - test_array[3] = 30 - result = process_array(test_array) - expect_equal(result, 60, "process_array([10,20,30]) should return 60") - print "✓ process_array([10,20,30]) = " result - - print "🎉 All multi-line function tests passed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_new_predicates.rawk b/awk/rawk/tests/core/test_new_predicates.rawk deleted file mode 100644 index d5c14c9..0000000 --- a/awk/rawk/tests/core/test_new_predicates.rawk +++ /dev/null @@ -1,44 +0,0 @@ -# Test new predicate functions: is_uuid and is_ipv6 - -BEGIN { - print "=== Testing New Predicate Functions ===" - - # Test is_uuid function - print "" - print "--- Testing is_uuid ---" - - # Valid UUIDs - expect_true(is_uuid("550e8400-e29b-41d4-a716-446655440000"), "Valid UUID should return true") - expect_true(is_uuid("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), "Valid UUID should return true") - expect_true(is_uuid("6ba7b811-9dad-11d1-80b4-00c04fd430c8"), "Valid UUID should return true") - - # Invalid UUIDs - expect_false(is_uuid(""), "Empty string should return false") - expect_false(is_uuid("not-a-uuid"), "Invalid format should return false") - expect_false(is_uuid("550e8400-e29b-41d4-a716-44665544000"), "Too short should return false") - expect_false(is_uuid("550e8400-e29b-41d4-a716-4466554400000"), "Too long should return false") - expect_false(is_uuid("550e8400e29b41d4a716446655440000"), "Missing hyphens should return false") - expect_false(is_uuid("550e8400-e29b-41d4-a716-44665544000g"), "Invalid hex should return false") - - # Test is_ipv6 function - print "" - print "--- Testing is_ipv6 ---" - - # Valid IPv6 addresses - expect_true(is_ipv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), "Valid IPv6 should return true") - expect_true(is_ipv6("2001:db8:85a3::8a2e:370:7334"), "Valid IPv6 with :: should return true") - expect_true(is_ipv6("::1"), "Localhost IPv6 should return true") - expect_true(is_ipv6("fe80::1ff:fe23:4567:890a"), "Valid IPv6 should return true") - expect_true(is_ipv6("2001:0db8:0000:0000:0000:0000:0000:0001"), "Valid IPv6 should return true") - - # Invalid IPv6 addresses - expect_false(is_ipv6(""), "Empty string should return false") - expect_false(is_ipv6("192.168.1.1"), "IPv4 should return false") - expect_false(is_ipv6("not-an-ip"), "Invalid format should return false") - expect_false(is_ipv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334:extra"), "Too many segments should return false") - expect_false(is_ipv6("2001:0db8:85a3:0000:0000:8a2e:0370"), "Too few segments should return false") - expect_false(is_ipv6("2001:0db8:85a3:0000:0000:8a2e:0370:733g"), "Invalid hex should return false") - - print "" - print "🎉 All new predicate function tests passed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_recursive.rawk b/awk/rawk/tests/core/test_recursive.rawk deleted file mode 100644 index 4e89a4d..0000000 --- a/awk/rawk/tests/core/test_recursive.rawk +++ /dev/null @@ -1,53 +0,0 @@ -# Test recursive functions -$factorial = (n) -> { - if (n <= 1) { - return 1 - } else { - return n * factorial(n - 1) - } -}; - -$fibonacci = (n) -> { - if (n <= 1) { - return n - } else { - return fibonacci(n - 1) + fibonacci(n - 2) - } -}; - -$countdown = (n) -> { - if (n <= 0) { - return "Done!" - } else { - return n " " countdown(n - 1) - } -}; - -BEGIN { - print "=== Testing Recursive Functions ===" - - # Test factorial - result = factorial(5) - expect_equal(result, 120, "factorial(5) should return 120") - print "✓ factorial(5) = " result - - result = factorial(3) - expect_equal(result, 6, "factorial(3) should return 6") - print "✓ factorial(3) = " result - - # Test fibonacci - result = fibonacci(6) - expect_equal(result, 8, "fibonacci(6) should return 8") - print "✓ fibonacci(6) = " result - - result = fibonacci(4) - expect_equal(result, 3, "fibonacci(4) should return 3") - print "✓ fibonacci(4) = " result - - # Test countdown - result = countdown(3) - expect_equal(result, "3 2 1 Done!", "countdown(3) should return '3 2 1 Done!'") - print "✓ countdown(3) = " result - - print "🎉 All recursive function tests passed!" -} \ No newline at end of file diff --git a/awk/rawk/tests/core/test_suite.rawk b/awk/rawk/tests/core/test_suite.rawk deleted file mode 100644 index fd069aa..0000000 --- a/awk/rawk/tests/core/test_suite.rawk +++ /dev/null @@ -1,145 +0,0 @@ -# rawk Test Suite -# This file tests all major features of the rawk language using assertions - -# Basic function definitions for testing -$add = (x, y) -> x + y; -$multiply = (a, b) -> a * b; -$greet = (name) -> "Hello, " name; -$square = (x) -> x * x; -$double = (x) -> x * 2; - -# Multi-line function for testing -$calculate_area = (width, height) -> { - area = width * height - return area -}; - -# Function that calls other functions -$complex_calc = (x, y) -> { - doubled = double(x) - squared = square(y) - result = add(doubled, squared) - return result -}; - -# Test runner -BEGIN { - print "=== rawk Test Suite ===" - test_count = 0 - passed_count = 0 - - # Test 1: Basic single-line functions - test_count++ - result = add(5, 3) - expect_equal(result, 8, "add(5, 3) should return 8") - passed_count++ - print "✓ Test " test_count ": Basic addition" - - test_count++ - result = multiply(4, 7) - expect_equal(result, 28, "multiply(4, 7) should return 28") - passed_count++ - print "✓ Test " test_count ": Basic multiplication" - - test_count++ - result = greet("World") - expect_equal(result, "Hello, World", "greet(\"World\") should return 'Hello, World'") - passed_count++ - print "✓ Test " test_count ": String concatenation" - - # Test 2: Multi-line functions - test_count++ - result = calculate_area(5, 3) - expect_equal(result, 15, "calculate_area(5, 3) should return 15") - passed_count++ - print "✓ Test " test_count ": Multi-line function" - - # Test 3: Nested function calls - test_count++ - result = double(square(3)) - expect_equal(result, 18, "double(square(3)) should return 18") - passed_count++ - print "✓ Test " test_count ": Nested function calls" - - test_count++ - result = square(double(3)) - expect_equal(result, 36, "square(double(3)) should return 36") - passed_count++ - print "✓ Test " test_count ": Different nested function order" - - # Test 4: Function calls within function bodies - test_count++ - result = complex_calc(3, 4) - expect_equal(result, 22, "complex_calc(3, 4) should return 22") - passed_count++ - print "✓ Test " test_count ": Function calls within function bodies" - - # Test 5: Edge cases - test_count++ - result = add(0, 0) - expect_equal(result, 0, "add(0, 0) should return 0") - passed_count++ - print "✓ Test " test_count ": Zero values" - - test_count++ - result = multiply(-2, 3) - expect_equal(result, -6, "multiply(-2, 3) should return -6") - passed_count++ - print "✓ Test " test_count ": Negative numbers" - - # Test 6: String operations - test_count++ - result = greet("") - expect_equal(result, "Hello, ", "greet(\"\") should return 'Hello, '") - passed_count++ - print "✓ Test " test_count ": Empty string" - - # Test 7: Boolean assertions - test_count++ - expect_true(add(2, 2) == 4, "2 + 2 should equal 4") - passed_count++ - print "✓ Test " test_count ": Boolean true assertion" - - test_count++ - expect_false(add(2, 2) == 5, "2 + 2 should not equal 5") - passed_count++ - print "✓ Test " test_count ": Boolean false assertion" - - # Test 8: Array operations (basic) - test_count++ - data[1] = 10 - data[2] = 20 - data[3] = 30 - expect_equal(data[1], 10, "data[1] should be 10") - expect_equal(data[2], 20, "data[2] should be 20") - expect_equal(data[3], 30, "data[3] should be 30") - passed_count++ - print "✓ Test " test_count ": Basic array operations" - - # Test 9: Conditional expressions - test_count++ - result = 5 > 3 ? "greater" : "less" - expect_equal(result, "greater", "5 > 3 should be 'greater'") - passed_count++ - print "✓ Test " test_count ": Conditional expressions" - - # Test 10: Complex expressions - test_count++ - result = (2 + 3) * 4 - expect_equal(result, 20, "(2 + 3) * 4 should be 20") - passed_count++ - print "✓ Test " test_count ": Complex expressions" - - # Summary - print "\n=== Test Summary ===" - print "Total tests: " test_count - print "Passed: " passed_count - print "Failed: " (test_count - passed_count) - - if (passed_count == test_count) { - print "🎉 All tests passed!" - } else { - print "❌ Some tests failed!" - exit 1 - } -} \ No newline at end of file diff --git a/awk/rawk/tests/data/README.md b/awk/rawk/tests/data/README.md deleted file mode 100644 index cb8f23b..0000000 --- a/awk/rawk/tests/data/README.md +++ /dev/null @@ -1,139 +0,0 @@ -# Test Data Files - -This directory contains sample data files used by the real-world examples. - -## Data Files - -### `test_data.txt` - System Command Outputs -Simulated output from common system commands: - -**df output:** -``` -Filesystem 1K-blocks Used Available Use% Mounted on -/dev/sda1 1048576 524288 524288 50 / -/dev/sdb1 2097152 1887436 209716 90 /home -/dev/sdc1 524288 104857 419431 20 /var -/dev/sdd1 1048576 943718 104858 90 /tmp -``` - -**ps output:** -``` -PID USER %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND -1234 user1 15.2 2.1 1234567 12345 pts/0 S 10:30 0:15 chrome -5678 user2 0.5 8.3 2345678 23456 pts/1 S 09:15 1:30 firefox -9012 user1 2.1 1.5 3456789 34567 pts/2 S 11:45 0:05 bash -3456 user3 25.7 1.2 4567890 45678 pts/3 R 12:00 0:30 stress -7890 user2 0.1 12.5 5678901 56789 pts/4 S 08:30 2:15 docker -``` - -**ls -l output:** -``` -total 1234 --rw-r--r-- 1 user1 group1 1024 Jan 15 10:30 file1.txt -drwxr-xr-x 2 user2 group2 4096 Jan 15 11:45 directory1 --rwxr-xr-x 1 user1 group1 2048 Jan 15 12:00 executable.sh --rw-r--r-- 1 user3 group1 512 Jan 15 12:15 config.json --rw-r--r-- 1 user1 group2 3072 Jan 15 12:30 large_file.dat -``` - -**Used by:** `../real_world/test_system_monitor.rawk` - -### `test_logs.txt` - Log Entries -Sample log entries in common formats: - -**Apache log entries:** -``` -192.168.1.100 - - [15/Jan/2024:10:30:15 +0000] "GET /index.html HTTP/1.1" 200 1024 -192.168.1.101 - - [15/Jan/2024:10:30:16 +0000] "GET /style.css HTTP/1.1" 200 512 -192.168.1.102 - - [15/Jan/2024:10:30:17 +0000] "POST /login HTTP/1.1" 302 0 -192.168.1.103 - - [15/Jan/2024:10:30:18 +0000] "GET /image.jpg HTTP/1.1" 200 2048 -192.168.1.104 - - [15/Jan/2024:10:30:19 +0000] "GET /nonexistent.html HTTP/1.1" 404 0 -192.168.1.105 - - [15/Jan/2024:10:30:20 +0000] "GET /script.js HTTP/1.1" 200 768 -192.168.1.106 - - [15/Jan/2024:10:30:21 +0000] "POST /submit HTTP/1.1" 500 0 -``` - -**Syslog entries:** -``` -Jan 15 10:30:15 server1 sshd: Accepted password for user1 from 192.168.1.100 -Jan 15 10:30:16 server1 kernel: ERROR: Out of memory -Jan 15 10:30:17 server1 apache2: WARNING: Server reached MaxClients -Jan 15 10:30:18 server1 cron: INFO: Daily backup completed -Jan 15 10:30:19 server1 sshd: ERROR: Failed password for user2 from 192.168.1.101 -Jan 15 10:30:20 server1 systemd: INFO: Started network service -``` - -**Used by:** `../real_world/test_log_parser.rawk` - -### `test_employees.csv` - Employee Data -Sample CSV file with employee information: - -``` -Name,Email,Age,Salary,Department -John Smith,john.smith@company.com,32,65000,Engineering -Jane Doe,jane.doe@company.com,28,72000,Marketing -Bob Johnson,bob.johnson@company.com,45,85000,Sales -Alice Brown,alice.brown@company.com,22,55000,Engineering -Charlie Wilson,charlie.wilson@company.com,38,78000,Finance -Diana Davis,diana.davis@company.com,29,68000,Marketing -Eve Miller,eve.miller@company.com,52,92000,Management -Frank Garcia,frank.garcia@company.com,25,60000,Engineering -Grace Lee,grace.lee@company.com,41,82000,Sales -Henry Taylor,henry.taylor@company.com,35,75000,Finance -Ivy Chen,ivy.chen@company.com,27,67000,Engineering -Jack Anderson,jack.anderson@company.com,48,88000,Management -``` - -**Features:** -- 12 employees across 4 departments -- Mix of valid email addresses -- Age range from 22 to 52 -- Salary range from $55,000 to $92,000 -- Various data quality scenarios - -**Used by:** `../real_world/test_csv_processor.rawk` - -### `test_input.txt` - Simple Input Data -Simple text input for basic processing: - -``` -Hello -This is a short line -This is a much longer line that should be detected -``` - -**Used by:** `../real_world/test_mixed.rawk` - -## Data Characteristics - -### System Data (`test_data.txt`) -- **Disk usage**: Mix of normal (20-50%) and critical (90%) usage -- **Process data**: Various CPU and memory usage patterns -- **File data**: Mix of files, directories, and executables - -### Log Data (`test_logs.txt`) -- **Apache logs**: Mix of successful (200), redirect (302), and error (404, 500) responses -- **Syslog entries**: Mix of INFO, WARNING, and ERROR messages -- **Realistic patterns**: Common log entry formats and content - -### Employee Data (`test_employees.csv`) -- **Valid data**: All emails are properly formatted -- **Age distribution**: Spread across different age groups -- **Salary variation**: Realistic salary ranges by department -- **Department balance**: Multiple employees per department - -## Usage - -These data files are designed to test various scenarios: - -1. **Normal operation**: Most data represents typical, valid cases -2. **Edge cases**: Some data includes boundary conditions (90% disk usage, high CPU processes) -3. **Error conditions**: Log files include error responses and system issues -4. **Data validation**: CSV includes various data types for validation testing - -## Customization - -You can modify these files to test different scenarios: -- Add more system data for different monitoring scenarios -- Include different log formats for additional parsing tests -- Modify CSV data to test different validation rules -- Create new data files for specific use cases \ No newline at end of file diff --git a/awk/rawk/tests/data/test_data.txt b/awk/rawk/tests/data/test_data.txt deleted file mode 100644 index 7559aea..0000000 --- a/awk/rawk/tests/data/test_data.txt +++ /dev/null @@ -1,22 +0,0 @@ -# Simulated df output -Filesystem 1K-blocks Used Available Use% Mounted on -/dev/sda1 1048576 524288 524288 50 / -/dev/sdb1 2097152 1887436 209716 90 /home -/dev/sdc1 524288 104857 419431 20 /var -/dev/sdd1 1048576 943718 104858 90 /tmp - -# Simulated ps output -PID USER %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND -1234 user1 15.2 2.1 1234567 12345 pts/0 S 10:30 0:15 chrome -5678 user2 0.5 8.3 2345678 23456 pts/1 S 09:15 1:30 firefox -9012 user1 2.1 1.5 3456789 34567 pts/2 S 11:45 0:05 bash -3456 user3 25.7 1.2 4567890 45678 pts/3 R 12:00 0:30 stress -7890 user2 0.1 12.5 5678901 56789 pts/4 S 08:30 2:15 docker - -# Simulated ls -l output -total 1234 --rw-r--r-- 1 user1 group1 1024 Jan 15 10:30 file1.txt -drwxr-xr-x 2 user2 group2 4096 Jan 15 11:45 directory1 --rwxr-xr-x 1 user1 group1 2048 Jan 15 12:00 executable.sh --rw-r--r-- 1 user3 group1 512 Jan 15 12:15 config.json --rw-r--r-- 1 user1 group2 3072 Jan 15 12:30 large_file.dat \ No newline at end of file diff --git a/awk/rawk/tests/data/test_employees.csv b/awk/rawk/tests/data/test_employees.csv deleted file mode 100644 index 040d2f1..0000000 --- a/awk/rawk/tests/data/test_employees.csv +++ /dev/null @@ -1,13 +0,0 @@ -Name,Email,Age,Salary,Department -John Smith,john.smith@company.com,32,65000,Engineering -Jane Doe,jane.doe@company.com,28,72000,Marketing -Bob Johnson,bob.johnson@company.com,45,85000,Sales -Alice Brown,alice.brown@company.com,22,55000,Engineering -Charlie Wilson,charlie.wilson@company.com,38,78000,Finance -Diana Davis,diana.davis@company.com,29,68000,Marketing -Eve Miller,eve.miller@company.com,52,92000,Management -Frank Garcia,frank.garcia@company.com,25,60000,Engineering -Grace Lee,grace.lee@company.com,41,82000,Sales -Henry Taylor,henry.taylor@company.com,35,75000,Finance -Ivy Chen,ivy.chen@company.com,27,67000,Engineering -Jack Anderson,jack.anderson@company.com,48,88000,Management \ No newline at end of file diff --git a/awk/rawk/tests/data/test_input.txt b/awk/rawk/tests/data/test_input.txt deleted file mode 100644 index 2c0a73c..0000000 --- a/awk/rawk/tests/data/test_input.txt +++ /dev/null @@ -1,3 +0,0 @@ -Hello -This is a short line -This is a much longer line that should be detected \ No newline at end of file diff --git a/awk/rawk/tests/data/test_logs.txt b/awk/rawk/tests/data/test_logs.txt deleted file mode 100644 index 7fb0e19..0000000 --- a/awk/rawk/tests/data/test_logs.txt +++ /dev/null @@ -1,16 +0,0 @@ -# Sample Apache log entries -192.168.1.100 - - [15/Jan/2024:10:30:15 +0000] "GET /index.html HTTP/1.1" 200 1024 -192.168.1.101 - - [15/Jan/2024:10:30:16 +0000] "GET /style.css HTTP/1.1" 200 512 -192.168.1.102 - - [15/Jan/2024:10:30:17 +0000] "POST /login HTTP/1.1" 302 0 -192.168.1.103 - - [15/Jan/2024:10:30:18 +0000] "GET /image.jpg HTTP/1.1" 200 2048 -192.168.1.104 - - [15/Jan/2024:10:30:19 +0000] "GET /nonexistent.html HTTP/1.1" 404 0 -192.168.1.105 - - [15/Jan/2024:10:30:20 +0000] "GET /script.js HTTP/1.1" 200 768 -192.168.1.106 - - [15/Jan/2024:10:30:21 +0000] "POST /submit HTTP/1.1" 500 0 - -# Sample syslog entries -Jan 15 10:30:15 server1 sshd: Accepted password for user1 from 192.168.1.100 -Jan 15 10:30:16 server1 kernel: ERROR: Out of memory -Jan 15 10:30:17 server1 apache2: WARNING: Server reached MaxClients -Jan 15 10:30:18 server1 cron: INFO: Daily backup completed -Jan 15 10:30:19 server1 sshd: ERROR: Failed password for user2 from 192.168.1.101 -Jan 15 10:30:20 server1 systemd: INFO: Started network service \ No newline at end of file diff --git a/awk/rawk/tests/real_world/README.md b/awk/rawk/tests/real_world/README.md deleted file mode 100644 index c4ba349..0000000 --- a/awk/rawk/tests/real_world/README.md +++ /dev/null @@ -1,130 +0,0 @@ -# Real-World Examples - -This directory contains practical examples that demonstrate rawk's utility for common data processing tasks. - -## Test Files - -### `test_system_monitor.rawk` - System Monitoring -Processes output from common system commands: -- **df**: Disk usage monitoring with warnings -- **ps**: Process resource analysis -- **ls -l**: File categorization and statistics - -**Features:** -- Disk usage alerts (WARNING/CRITICAL thresholds) -- Process resource monitoring (CPU/MEM usage) -- File type categorization (DIRECTORY/EXECUTABLE/LARGE/SMALL) -- Statistical summaries - -**Run with:** -```bash -awk -f ../../rawk.awk test_system_monitor.rawk | awk -f - ../data/test_data.txt -``` - -**Sample Output:** -``` -DISK: WARNING: /dev/sdb1 (/home) is 90% full -PROCESS: HIGH CPU: stress (PID: 3456, 25.7% CPU) -FILE: EXECUTABLE: executable.sh (2048 bytes) -``` - -### `test_log_parser.rawk` - Log Parsing -Processes common log formats: -- **Apache logs**: Web server access logs -- **Syslog**: System log entries - -**Features:** -- HTTP status code categorization (SUCCESS/ERROR/REDIRECT) -- Log level detection (INFO/WARNING/ERROR) -- Request type classification -- Error rate calculation - -**Run with:** -```bash -awk -f ../../rawk.awk test_log_parser.rawk | awk -f - ../data/test_logs.txt -``` - -**Sample Output:** -``` -APACHE: ERROR: 404 - GET /nonexistent.html from 192.168.1.104 -SYSLOG: ERROR: kernel - ERROR: Out of memory -``` - -### `test_csv_processor.rawk` - CSV Data Processing -Processes CSV files with validation: -- **Email validation**: Basic email format checking -- **Age categorization**: Group employees by age -- **Salary statistics**: Calculate averages and ranges -- **Department analysis**: Employee distribution - -**Features:** -- Data validation and categorization -- Statistical analysis -- Report generation -- Error detection - -**Run with:** -```bash -awk -f ../../rawk.awk test_csv_processor.rawk | awk -f - ../data/test_employees.csv -``` - -**Sample Output:** -``` -EMPLOYEE: John Smith (ADULT, Engineering) - VALID email, $65000 -Average salary: $73916.7 -Email validity rate: 100% -``` - -### `test_data_processing.rawk` - General Data Processing -General data processing scenarios: -- Array filtering and manipulation -- Data aggregation -- Formatting and reporting - -**Run with:** -```bash -awk -f ../../rawk.awk test_data_processing.rawk | awk -f - -``` - -### `test_mixed.rawk` - Mixed awk/rawk Code -Demonstrates mixing rawk functions with regular awk code: -- Line-by-line processing -- Integration with awk patterns -- Combined functionality - -**Run with:** -```bash -awk -f ../../rawk.awk test_mixed.rawk | awk -f - ../data/test_input.txt -``` - -## Use Cases - -These examples demonstrate rawk's practical applications: - -### System Administration -- Monitor disk usage and alert on thresholds -- Track process resource consumption -- Analyze file system contents - -### Web Server Management -- Parse and analyze web server logs -- Monitor error rates and traffic patterns -- Identify problematic requests - -### Data Analysis -- Process CSV files with validation -- Generate business intelligence reports -- Analyze employee or customer data - -### Log Analysis -- Parse various log formats -- Identify system issues -- Generate operational reports - -## Data Files - -The examples use sample data files in the `../data/` directory: -- `test_data.txt`: Simulated system command outputs -- `test_logs.txt`: Sample Apache and syslog entries -- `test_employees.csv`: Sample employee data -- `test_input.txt`: Simple input data for mixed tests \ No newline at end of file diff --git a/awk/rawk/tests/real_world/test_csv_processor.rawk b/awk/rawk/tests/real_world/test_csv_processor.rawk deleted file mode 100644 index 5aa14b5..0000000 --- a/awk/rawk/tests/real_world/test_csv_processor.rawk +++ /dev/null @@ -1,143 +0,0 @@ -# CSV data processing with rawk -# This demonstrates processing CSV files with headers - -# Function to validate email format -$is_valid_email = (email) -> { - # Simple email validation: contains @ and . after @ - at_pos = index(email, "@") - if (at_pos == 0) return 0 - - # Check if there's a dot after the @ symbol - dot_pos = index(substr(email, at_pos + 1), ".") - return dot_pos > 0 -}; - -# Function to categorize age groups -$categorize_age = (age) -> { - if (age < 18) { - return "MINOR" - } else if (age < 30) { - return "YOUNG_ADULT" - } else if (age < 50) { - return "ADULT" - } else if (age < 65) { - return "MIDDLE_AGED" - } else { - return "SENIOR" - } -}; - -# Function to calculate salary statistics -$calculate_salary_stats = (data, result, i, total, count, max, min) -> { - total = 0 - count = 0 - max = 0 - min = 0 - first = 1 - - for (i in data) { - total += data[i] - count++ - if (first || data[i] > max) { - max = data[i] - } - if (first || data[i] < min) { - min = data[i] - } - first = 0 - } - - result["total"] = total - result["count"] = count - result["average"] = count > 0 ? total / count : 0 - result["max"] = max - result["min"] = min - - return count -}; - -# Function to format employee record -$format_employee = (name, email, age, salary, department) -> { - age_group = categorize_age(age) - email_status = is_valid_email(email) ? "VALID" : "INVALID" - - return name " (" age_group ", " department ") - " email_status " email, $" salary -}; - -BEGIN { - FS = "," # Set field separator to comma - print "=== CSV Data Processor ===" - print "" - header_processed = 0 -} - -# Skip header line -NR == 1 { - print "Processing CSV with columns: " $0 - print "" - next -} - -# Process data rows -{ - if (NF >= 5) { - name = $1 - email = $2 - age = $3 - salary = $4 - department = $5 - - result = format_employee(name, email, age, salary, department) - print "EMPLOYEE: " result - - # Store for statistics - employee_count++ - ages[employee_count] = age - salaries[employee_count] = salary - departments[employee_count] = department - age_groups[employee_count] = categorize_age(age) - - # Track department counts - dept_count[department]++ - - # Track age group counts - age_group_count[categorize_age(age)]++ - - # Track email validity - if (is_valid_email(email)) { - valid_emails++ - } else { - invalid_emails++ - } - } -} - -END { - print "" - print "=== Employee Statistics ===" - - if (employee_count > 0) { - calculate_salary_stats(salaries, salary_stats) - print "Total employees: " employee_count - print "Average salary: $" salary_stats["average"] - print "Salary range: $" salary_stats["min"] " - $" salary_stats["max"] - print "Valid emails: " valid_emails - print "Invalid emails: " invalid_emails - print "Email validity rate: " (valid_emails / employee_count * 100) "%" - } - - print "" - print "=== Department Distribution ===" - for (dept in dept_count) { - print dept ": " dept_count[dept] " employees" - } - - print "" - print "=== Age Group Distribution ===" - for (group in age_group_count) { - print group ": " age_group_count[group] " employees" - } - - print "" - print "=== Report Complete ===" -} \ No newline at end of file diff --git a/awk/rawk/tests/real_world/test_data_processing.rawk b/awk/rawk/tests/real_world/test_data_processing.rawk deleted file mode 100644 index dba1a0b..0000000 --- a/awk/rawk/tests/real_world/test_data_processing.rawk +++ /dev/null @@ -1,75 +0,0 @@ -# Test data processing scenarios -$filter_positive = (arr, result, i, count) -> { - count = 0 - for (i in arr) { - if (arr[i] > 0) { - result[++count] = arr[i] - } - } - return result -}; - -$sum_array = (arr, sum, i) -> { - sum = 0 - for (i in arr) { - sum += arr[i] - } - return sum -}; - -$average_array = (arr, sum, count, i) -> { - sum = 0 - count = 0 - for (i in arr) { - sum += arr[i] - count++ - } - return count > 0 ? sum / count : 0 -}; - -$find_max = (arr, max, i, first) -> { - first = 1 - for (i in arr) { - if (first || arr[i] > max) { - max = arr[i] - first = 0 - } - } - return max -}; - -$format_data = (name, age, city) -> { - return "Name: " name ", Age: " age ", City: " city -}; - -# Test data processing -BEGIN { - print "=== Testing Data Processing ===" - - # Test array operations - data[1] = 10 - data[2] = -5 - data[3] = 20 - data[4] = -3 - data[5] = 15 - - print "Original data:", data[1], data[2], data[3], data[4], data[5] - - # Test filtering - positive_nums = filter_positive(data) - print "Positive numbers:", positive_nums[1], positive_nums[2], positive_nums[3] - - # Test sum and average - total = sum_array(data) - avg = average_array(data) - print "Sum:", total - print "Average:", avg - - # Test finding maximum - max_val = find_max(data) - print "Maximum:", max_val - - # Test data formatting - formatted = format_data("Alice", 30, "New York") - print "Formatted:", formatted -} \ No newline at end of file diff --git a/awk/rawk/tests/real_world/test_log_parser.rawk b/awk/rawk/tests/real_world/test_log_parser.rawk deleted file mode 100644 index 1abdbaf..0000000 --- a/awk/rawk/tests/real_world/test_log_parser.rawk +++ /dev/null @@ -1,139 +0,0 @@ -# Log parsing with rawk -# This demonstrates processing common log formats like Apache, syslog, etc. - -# Function to parse Apache log entries -$parse_apache_log = (ip, date, method, url, status, bytes, referer, user_agent) -> { - if (status >= 400) { - return "ERROR: " status " - " method " " url " from " ip - } else if (status >= 300) { - return "REDIRECT: " status " - " method " " url " from " ip - } else { - return "SUCCESS: " status " - " method " " url " (" bytes " bytes)" - } -}; - -# Function to parse syslog entries -$parse_syslog = (timestamp, host, program, message) -> { - if (index(message, "error") > 0 || index(message, "ERROR") > 0) { - return "ERROR: " program " - " message - } else if (index(message, "warning") > 0 || index(message, "WARNING") > 0) { - return "WARNING: " program " - " message - } else { - return "INFO: " program " - " message - } -}; - -# Function to categorize requests -$categorize_request = (method, url, status) -> { - if (method == "GET" && index(url, ".jpg") > 0) { - return "IMAGE_REQUEST" - } else if (method == "POST") { - return "FORM_SUBMISSION" - } else if (method == "GET" && index(url, ".css") > 0) { - return "STYLESHEET" - } else if (method == "GET" && index(url, ".js") > 0) { - return "JAVASCRIPT" - } else { - return "PAGE_REQUEST" - } -}; - -# Function to calculate request statistics -$calculate_request_stats = (data, result, i, total, count, errors, redirects) -> { - total = 0 - count = 0 - errors = 0 - redirects = 0 - - for (i in data) { - total++ - if (data[i] >= 400) { - errors++ - } else if (data[i] >= 300) { - redirects++ - } - } - - result["total"] = total - result["errors"] = errors - result["redirects"] = redirects - result["success_rate"] = total > 0 ? ((total - errors - redirects) / total) * 100 : 0 - - return total -}; - -BEGIN { - print "=== Log Parser Report ===" - print "" -} - -# Process Apache log entries (simplified format) -/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ { - ip = $1 - date = $4 " " $5 - method = $6 - url = $7 - status = $9 - bytes = $10 - - result = parse_apache_log(ip, date, method, url, status, bytes, "", "") - print "APACHE: " result - - # Store for statistics - request_count++ - status_codes[request_count] = status - request_types[request_count] = categorize_request(method, url, status) -} - -# Process syslog entries -/^[A-Z][a-z]{2} [0-9]+ [0-9:]+/ { - timestamp = $1 " " $2 " " $3 - host = $4 - program = substr($5, 1, length($5) - 1) # Remove trailing colon - message = substr($0, index($0, $6)) - - result = parse_syslog(timestamp, host, program, message) - print "SYSLOG: " result - - # Store for statistics - log_count++ - log_programs[log_count] = program -} - -END { - print "" - print "=== Request Statistics ===" - - if (request_count > 0) { - calculate_request_stats(status_codes, request_stats) - print "Total requests: " request_stats["total"] - print "Error rate: " request_stats["errors"] " (" (request_stats["errors"] / request_stats["total"] * 100) "%)" - print "Success rate: " request_stats["success_rate"] "%" - print "Redirects: " request_stats["redirects"] - } - - print "" - print "=== Request Types ===" - for (i = 1; i <= request_count; i++) { - type = request_types[i] - type_count[type]++ - } - - for (type in type_count) { - print type ": " type_count[type] " requests" - } - - print "" - print "=== Log Sources ===" - for (i = 1; i <= log_count; i++) { - program = log_programs[i] - program_count[program]++ - } - - for (program in program_count) { - print program ": " program_count[program] " entries" - } - - print "" - print "=== Report Complete ===" -} \ No newline at end of file diff --git a/awk/rawk/tests/real_world/test_mixed.rawk b/awk/rawk/tests/real_world/test_mixed.rawk deleted file mode 100644 index 50cb6bb..0000000 --- a/awk/rawk/tests/real_world/test_mixed.rawk +++ /dev/null @@ -1,27 +0,0 @@ -# Mixed rawk and awk code -$increment = (x) -> x + 1; -$format_line = (line_num, text) -> "Line " line_num ": " text; - -# Regular awk code mixed in -BEGIN { - print "=== Mixed rawk and awk test ===" -} - -# Process each input line -{ - # Use rawk functions - incremented_line = increment(NR) - formatted = format_line(NR, $0) - - # Regular awk processing - if (length($0) > 10) { - print formatted " (long line)" - } else { - print formatted " (short line)" - } -} - -END { - print "=== End of processing ===" - print "Total lines processed:", NR -} \ No newline at end of file diff --git a/awk/rawk/tests/real_world/test_system_monitor.rawk b/awk/rawk/tests/real_world/test_system_monitor.rawk deleted file mode 100644 index 1e1ef1a..0000000 --- a/awk/rawk/tests/real_world/test_system_monitor.rawk +++ /dev/null @@ -1,157 +0,0 @@ -# System monitoring with rawk -# This demonstrates processing real command outputs like df, ps, ls - -# Function to analyze disk usage -$analyze_disk = (filesystem, size, used, avail, percent, mount) -> { - if (percent > 90) { - return "CRITICAL: " filesystem " (" mount ") is " percent "% full!" - } else if (percent > 80) { - return "WARNING: " filesystem " (" mount ") is " percent "% full" - } else if (percent > 60) { - return "NOTICE: " filesystem " (" mount ") is " percent "% full" - } else { - return "OK: " filesystem " (" mount ") has " avail " blocks free" - } -}; - -# Function to analyze process resource usage -$analyze_process = (pid, user, cpu, mem, command) -> { - if (cpu > 20) { - return "HIGH CPU: " command " (PID: " pid ", " cpu "% CPU)" - } else if (mem > 10) { - return "HIGH MEM: " command " (PID: " pid ", " mem "% MEM)" - } else { - return "NORMAL: " command " (PID: " pid ")" - } -}; - -# Function to categorize files -$categorize_file = (permissions, size, name) -> { - if (substr(permissions, 1, 1) == "d") { - return "DIRECTORY: " name " (" size " bytes)" - } else if (substr(permissions, 4, 1) == "x") { - return "EXECUTABLE: " name " (" size " bytes)" - } else if (size > 1000) { - return "LARGE FILE: " name " (" size " bytes)" - } else { - return "SMALL FILE: " name " (" size " bytes)" - } -}; - -# Function to calculate statistics -$calculate_stats = (data, result, i, total, count, max, min) -> { - total = 0 - count = 0 - max = 0 - min = 0 - first = 1 - - for (i in data) { - total += data[i] - count++ - if (first || data[i] > max) { - max = data[i] - } - if (first || data[i] < min) { - min = data[i] - } - first = 0 - } - - result["total"] = total - result["count"] = count - result["average"] = count > 0 ? total / count : 0 - result["max"] = max - result["min"] = min - - return count -}; - -BEGIN { - print "=== System Monitor Report ===" - print "" -} - -# Process df output (disk usage) -/^\/dev\// { - filesystem = $1 - size = $2 - used = $3 - avail = $4 - percent = $5 - mount = $6 - - result = analyze_disk(filesystem, size, used, avail, percent, mount) - print "DISK: " result - - # Store for statistics - disk_count++ - disk_usage[disk_count] = percent -} - -# Process ps output (process information) -/^[0-9]+\t/ { - pid = $1 - user = $2 - cpu = $3 - mem = $4 - command = $11 - - result = analyze_process(pid, user, cpu, mem, command) - print "PROCESS: " result - - # Store for statistics - process_count++ - cpu_usage[process_count] = cpu - mem_usage[process_count] = mem -} - -# Process ls output (file information) -/^[d-][rwx-]{9}\t/ { - permissions = $1 - size = $5 - name = $9 - - result = categorize_file(permissions, size, name) - print "FILE: " result - - # Store for statistics - file_count++ - file_sizes[file_count] = size -} - -END { - print "" - print "=== Summary Statistics ===" - - # Disk usage statistics - if (disk_count > 0) { - calculate_stats(disk_usage, disk_stats) - print "Disk Usage:" - print " Average: " disk_stats["average"] "%" - print " Maximum: " disk_stats["max"] "%" - print " Minimum: " disk_stats["min"] "%" - } - - # CPU usage statistics - if (process_count > 0) { - calculate_stats(cpu_usage, cpu_stats) - print "CPU Usage:" - print " Average: " cpu_stats["average"] "%" - print " Maximum: " cpu_stats["max"] "%" - print " Total processes: " process_count - } - - # File size statistics - if (file_count > 0) { - calculate_stats(file_sizes, file_stats) - print "File Sizes:" - print " Total size: " file_stats["total"] " bytes" - print " Average size: " file_stats["average"] " bytes" - print " Largest file: " file_stats["max"] " bytes" - print " Total files: " file_count - } - - print "" - print "=== Report Complete ===" -} \ No newline at end of file diff --git a/awk/rawk/tests/simple_stdlib_test.rawk b/awk/rawk/tests/simple_stdlib_test.rawk new file mode 100644 index 0000000..0a726df --- /dev/null +++ b/awk/rawk/tests/simple_stdlib_test.rawk @@ -0,0 +1,24 @@ +BEGIN { + print "=== Simple Standard Library Tests ===" +} + +RAWK { + $test_function = (value) -> { + return is_number(value) && is_positive(value); + }; +} + +{ + # Test basic type checking + expect_true(is_number(42), "42 should be a number"); + expect_true(is_string("hello"), "hello should be a string"); + expect_false(is_number("abc"), "abc should not be a number"); + + # Test the custom function + expect_true(test_function(5), "5 should pass our test"); + expect_false(test_function(-3), "-3 should fail our test"); + expect_false(test_function("text"), "text should fail our test"); + + print "All simple standard library tests passed!"; + exit 0; +} \ No newline at end of file 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_enhanced_utilities.rawk b/awk/rawk/tests/stdlib/test_enhanced_utilities.rawk deleted file mode 100644 index eacc3f7..0000000 --- a/awk/rawk/tests/stdlib/test_enhanced_utilities.rawk +++ /dev/null @@ -1,192 +0,0 @@ -$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" -} \ No newline at end of file diff --git a/awk/rawk/tests/stdlib/test_enhanced_utilities_simple.rawk b/awk/rawk/tests/stdlib/test_enhanced_utilities_simple.rawk deleted file mode 100644 index 09c5988..0000000 --- a/awk/rawk/tests/stdlib/test_enhanced_utilities_simple.rawk +++ /dev/null @@ -1,174 +0,0 @@ -$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" -} \ No newline at end of file diff --git a/awk/rawk/tests/stdlib/test_functional.rawk b/awk/rawk/tests/stdlib/test_functional.rawk deleted file mode 100644 index b2d7e43..0000000 --- a/awk/rawk/tests/stdlib/test_functional.rawk +++ /dev/null @@ -1,108 +0,0 @@ -$double = (x) -> x * 2; -$add = (x, y) -> x + y; -$square = (x) -> x * x; -$add_one = (x) -> x + 1; -$multiply = (x, y) -> x * y; - -BEGIN { - print "=== Functional Programming Test Suite ===" - print "" - - # Test 1: Basic dispatch_call - print "Test 1: Function Dispatch" - expect_equal(dispatch_call("double", 5), 10, "dispatch_call('double', 5) should be 10") - expect_equal(dispatch_call("add", 3, 4), 7, "dispatch_call('add', 3, 4) should be 7") - expect_equal(dispatch_call("square", 4), 16, "dispatch_call('square', 4) should be 16") - print "✓ Function dispatch working correctly" - print "" - - # Test 2: Map function - print "Test 2: Map Function" - numbers[1] = 1 - numbers[2] = 2 - numbers[3] = 3 - numbers[4] = 4 - numbers[5] = 5 - - doubled_count = map("double", numbers, doubled) - expect_equal(doubled_count, 5, "doubled array should have 5 elements") - expect_equal(doubled[1], 2, "doubled[1] should be 2") - expect_equal(doubled[2], 4, "doubled[2] should be 4") - expect_equal(doubled[3], 6, "doubled[3] should be 6") - expect_equal(doubled[4], 8, "doubled[4] should be 8") - expect_equal(doubled[5], 10, "doubled[5] should be 10") - print "✓ Map function working correctly" - print "" - - # Test 3: Reduce function - print "Test 3: Reduce Function" - sum = reduce("add", numbers) - expect_equal(sum, 15, "sum of [1,2,3,4,5] should be 15") - - product = reduce("multiply", numbers) - expect_equal(product, 120, "product of [1,2,3,4,5] should be 120") - print "✓ Reduce function working correctly" - print "" - - # Test 4: Pipe function (single function) - print "Test 4: Pipe Function (Single)" - result = pipe(5, "double") - expect_equal(result, 10, "pipe(5, 'double') should be 10") - result = pipe(3, "square") - expect_equal(result, 9, "pipe(3, 'square') should be 9") - print "✓ Pipe function working correctly" - print "" - - # Test 5: Pipe_multi function (multiple functions) - print "Test 5: Pipe Function (Multiple)" - func_names[1] = "double" - func_names[2] = "add_one" - - result = pipe_multi(5, func_names) - expect_equal(result, 11, "pipe_multi(5, ['double', 'add_one']) should be 11") - - func_names[1] = "square" - func_names[2] = "double" - result = pipe_multi(3, func_names) - expect_equal(result, 18, "pipe_multi(3, ['square', 'double']) should be 18") - print "✓ Pipe_multi function working correctly" - print "" - - # Test 6: Complex functional composition - print "Test 6: Complex Functional Composition" - # Create array of squares - squared_count = map("square", numbers, squared) - expect_equal(squared_count, 5, "squared array should have 5 elements") - expect_equal(squared[1], 1, "squared[1] should be 1") - expect_equal(squared[2], 4, "squared[2] should be 4") - expect_equal(squared[3], 9, "squared[3] should be 9") - - # Sum of squares - sum_of_squares = reduce("add", squared) - expect_equal(sum_of_squares, 55, "sum of squares [1,4,9,16,25] should be 55") - print "✓ Complex functional composition working correctly" - print "" - - # Test 7: Error handling - print "Test 7: Error Handling" - # Test non-existent function - result = dispatch_call("nonexistent", 1) - expect_equal(result, "", "dispatch_call should return empty for non-existent function") - print "✓ Error handling working correctly" - print "" - - print "=== Functional Programming Test Summary ===" - print "Total tests: 7" - print "Passed: 7" - print "Failed: 0" - print "🎉 All functional programming tests passed!" - print "" - print "Features verified:" - print "✓ Function dispatch with switch statements" - print "✓ map() - Apply function to array elements" - print "✓ reduce() - Reduce array with function" - print "✓ pipe() - Single function pipeline" - print "✓ pipe_multi() - Multiple function pipeline" - print "✓ Error handling for non-existent functions" - print "✓ Complex functional composition" -} \ 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 60cc4d7..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"), 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!" - } -} \ 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 56010ff..0000000 --- a/awk/rawk/tests/stdlib/test_stdlib_simple.rawk +++ /dev/null @@ -1,30 +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_count = keys(data) - value_count = values(data) - get_keys(data, key_array) - get_values(data, value_array) - print "keys(data) =", key_array[1], key_array[2], key_array[3] - print "values(data) =", value_array[1], value_array[2], value_array[3] - print "key count =", key_count, "value count =", value_count - - # Test nested function calls - print "double(square(3)) =", double(square(3)) - print "square(double(3)) =", square(double(3)) -} \ No newline at end of file diff --git a/awk/rawk/tests/test_basic.rawk b/awk/rawk/tests/test_basic.rawk new file mode 100644 index 0000000..bb3470c --- /dev/null +++ b/awk/rawk/tests/test_basic.rawk @@ -0,0 +1,41 @@ +BEGIN { + print "=== Basic Block-Based rawk Tests ===" +} + +RAWK { + $add = (x, y) -> { + return x + y; + }; + + $multiply = (a, b) -> { + return a * b; + }; + + $greet = (name) -> { + return "Hello, " name "!"; + }; + + $is_positive_num = (num) -> { + return num > 0; + }; +} + +{ + # Test basic arithmetic + result1 = add(5, 3); + expect_equal(result1, 8, "add(5, 3) should return 8"); + + result2 = multiply(4, 7); + expect_equal(result2, 28, "multiply(4, 7) should return 28"); + + # Test string functions + greeting = greet("World"); + expect_equal(greeting, "Hello, World!", "greet('World') should return 'Hello, World!'"); + + # Test boolean functions + expect_true(is_positive_num(10), "is_positive_num(10) should return true"); + expect_false(is_positive_num(-5), "is_positive_num(-5) should return false"); + + print "All basic tests passed!"; + exit 0; +} \ No newline at end of file diff --git a/awk/rawk/tests/test_errors.rawk b/awk/rawk/tests/test_errors.rawk new file mode 100644 index 0000000..2376822 --- /dev/null +++ b/awk/rawk/tests/test_errors.rawk @@ -0,0 +1,12 @@ +# This test file should fail compilation because it is missing a RAWK block +BEGIN { + print "This should fail because there's no RAWK block" +} + +$invalid_function = (x) -> { + return x * 2; +}; + +{ + print "This should not compile" +} \ No newline at end of file diff --git a/awk/rawk/tests/test_functional.rawk b/awk/rawk/tests/test_functional.rawk new file mode 100644 index 0000000..41020a3 --- /dev/null +++ b/awk/rawk/tests/test_functional.rawk @@ -0,0 +1,117 @@ +BEGIN { + print "=== Functional Programming Tests ===" +} + +RAWK { + $double = (x) -> { + return x * 2; + }; + + $add = (x, y) -> { + return x + y; + }; + + $is_even = (x) -> { + return x % 2 == 0; + }; + + $is_positive = (x) -> { + return x > 0; + }; + + $square = (x) -> { + return x * x; + }; + + $split_words = (text, result) -> { + split(text, result, " "); + return length(result); + }; +} + +{ + # Create test data + numbers[1] = 1; + numbers[2] = 2; + numbers[3] = 3; + numbers[4] = 4; + numbers[5] = 5; + + mixed[1] = -2; + mixed[2] = 0; + mixed[3] = 3; + mixed[4] = -5; + mixed[5] = 10; + + texts[1] = "hello world"; + texts[2] = "functional programming"; + texts[3] = "awk is rad"; + + # Test map function + doubled_count = map("double", numbers, doubled); + expect_equal(doubled_count, 5, "map should return correct count"); + expect_equal(doubled[1], 2, "First element should be doubled"); + expect_equal(doubled[5], 10, "Last element should be doubled"); + + # Test reduce function + sum = reduce("add", numbers); + expect_equal(sum, 15, "Sum of 1+2+3+4+5 should be 15"); + + # Test filter function + positive_count = filter("is_positive", mixed, positive_numbers); + expect_equal(positive_count, 2, "Should find 2 positive numbers"); + expect_equal(positive_numbers[1], 3, "First positive should be 3"); + expect_equal(positive_numbers[2], 10, "Second positive should be 10"); + + # Test find function + first_even = find("is_even", numbers); + expect_equal(first_even, 2, "First even number should be 2"); + + # Test findIndex function + first_positive_index = findIndex("is_positive", mixed); + expect_equal(first_positive_index, 3, "First positive should be at index 3"); + + # Test take function + first_three_count = take(3, numbers, first_three); + expect_equal(first_three_count, 3, "Should take 3 elements"); + expect_equal(first_three[1], 1, "First element should be 1"); + expect_equal(first_three[3], 3, "Third element should be 3"); + + # Test drop function + remaining_count = drop(2, numbers, remaining); + expect_equal(remaining_count, 3, "Should drop 2 elements"); + expect_equal(remaining[1], 3, "First remaining should be 3"); + expect_equal(remaining[3], 5, "Last remaining should be 5"); + + # Test flatMap function + all_words_count = flatMap("split_words", texts, all_words); + expect_equal(all_words_count, 7, "Should have 7 words total"); + + # Test pipe function + result = pipe(5, "square"); + expect_equal(result, 25, "5 squared should be 25"); + + # Test pipe_multi function + func_names[1] = "double"; + func_names[2] = "square"; + result = pipe_multi(3, func_names); + expect_equal(result, 36, "3 doubled then squared should be 36"); + + # Test array utilities + key_count = keys(numbers); + expect_equal(key_count, 5, "Should have 5 keys"); + + value_count = values(numbers); + expect_equal(value_count, 5, "Should have 5 values"); + + get_keys(numbers, keys_array); + expect_equal(keys_array[1], 1, "First key should be 1"); + expect_equal(keys_array[5], 5, "Last key should be 5"); + + get_values(numbers, values_array); + expect_equal(values_array[1], 1, "First value should be 1"); + expect_equal(values_array[5], 5, "Last value should be 5"); + + print "All functional programming tests passed!"; + exit 0; +} \ No newline at end of file diff --git a/awk/rawk/tests/test_runner.sh b/awk/rawk/tests/test_runner.sh new file mode 100755 index 0000000..d0b316d --- /dev/null +++ b/awk/rawk/tests/test_runner.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +echo "a rawking test runner" +echo "==================================" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Test counter +PASSED=0 +FAILED=0 +TOTAL=0 + +# Function to run a test +run_test() { + local test_file="$1" + local test_name="$2" + + echo -n "Testing $test_name... " + + # Step 1: Compile + awk -f ../rawk.awk "$test_file" > temp_output.awk + + # Step 2: Run with input + output=$(echo "test input" | awk -f temp_output.awk 2>&1) + exit_code=$? + + if [ $exit_code -eq 0 ]; then + echo -e "${GREEN}✓ PASS${NC}" + ((PASSED++)) + else + echo -e "${RED}✗ FAIL${NC}" + echo " Output: $output" + ((FAILED++)) + fi + + ((TOTAL++)) + rm -f temp_output.awk +} + +# Function to run an error test (should fail) +run_error_test() { + local test_file="$1" + local test_name="$2" + + echo -n "Testing $test_name (should fail)... " + + output=$(awk -f ../rawk.awk "$test_file" 2>&1) + exit_code=$? + + if [ $exit_code -ne 0 ]; then + echo -e "${GREEN}✓ PASS (correctly failed)${NC}" + ((PASSED++)) + else + echo -e "${RED}✗ FAIL (should have failed)${NC}" + echo " Output: $output" + ((FAILED++)) + fi + + ((TOTAL++)) +} + +# Run all tests +echo "" +echo "Running basic functionality tests..." +run_test "test_basic.rawk" "Basic Functionality" + +echo "" +echo "Running simple standard library tests..." +run_test "simple_stdlib_test.rawk" "Simple Standard Library" + +echo "" +echo "Running full standard library tests..." +run_test "test_stdlib.rawk" "Full Standard Library" + +echo "" +echo "Running functional programming tests..." +run_test "test_functional.rawk" "Functional Programming" + +echo "" +echo "Running error handling tests..." +run_error_test "test_errors.rawk" "Error Handling" + +# Summary +echo "" +echo "==================================" +echo "Test Summary:" +echo " Total tests: $TOTAL" +echo -e " ${GREEN}Passed: $PASSED${NC}" +echo -e " ${RED}Failed: $FAILED${NC}" + +if [ $FAILED -eq 0 ]; then + echo -e "\n${GREEN}All tests passed!${NC}" + exit 0 +else + echo -e "\n${RED}Some tests failed!${NC}" + exit 1 +fi \ No newline at end of file diff --git a/awk/rawk/tests/test_smart_stdlib.rawk b/awk/rawk/tests/test_smart_stdlib.rawk new file mode 100644 index 0000000..5c3d9fe --- /dev/null +++ b/awk/rawk/tests/test_smart_stdlib.rawk @@ -0,0 +1,28 @@ +BEGIN { + print "=== Smart Standard Library Test ===" + print "This test uses only a few standard library functions" + print "to demonstrate smart inclusion" +} + +RAWK { + $validate_email = (email) -> { + return is_email(email); + }; + + $check_number = (num) -> { + return is_number(num); + }; +} + +{ + # Only use is_email and is_number from standard library + expect_true(validate_email("test@example.com"), "Valid email should pass"); + expect_false(validate_email("invalid"), "Invalid email should fail"); + + expect_true(check_number(42), "Number should pass"); + expect_false(check_number("abc"), "String should fail"); + + print "Smart standard library test passed!"; + print "Only is_email and is_number should be included in output"; + exit 0; +} \ No newline at end of file diff --git a/awk/rawk/tests/test_stdlib.rawk b/awk/rawk/tests/test_stdlib.rawk new file mode 100644 index 0000000..480e707 --- /dev/null +++ b/awk/rawk/tests/test_stdlib.rawk @@ -0,0 +1,70 @@ +BEGIN { + print "=== Standard Library Tests ===" +} + +RAWK { + $validate_email = (email) -> { + return is_email(email); + }; + + $validate_url = (url) -> { + return is_url(url); + }; + + $validate_number = (num) -> { + return is_number(num) && is_positive(num); + }; + + $process_data = (data) -> { + if (is_csv(data)) { + return "CSV data detected"; + } else if (is_hex(data)) { + return "Hex data detected"; + } else { + return "Unknown format"; + } + }; +} + +{ + # Test email validation + expect_true(validate_email("user@example.com"), "Valid email should pass"); + expect_false(validate_email("invalid-email"), "Invalid email should fail"); + + # Test URL validation + expect_true(validate_url("https://example.com"), "Valid URL should pass"); + expect_false(validate_url("not-a-url"), "Invalid URL should fail"); + + # Test number validation + expect_true(validate_number(42), "Positive number should pass"); + expect_false(validate_number(-5), "Negative number should fail"); + expect_false(validate_number("abc"), "Non-number should fail"); + + # Test data format detection + expect_equal(process_data("name,age,city"), "CSV data detected", "CSV detection should work"); + expect_equal(process_data("FF00AA"), "Hex data detected", "Hex detection should work"); + expect_equal(process_data("plain text"), "Unknown format", "Unknown format should be detected"); + + # Test HTTP predicates + expect_true(http_is_redirect(301), "301 should be a redirect"); + expect_true(http_is_client_error(404), "404 should be a client error"); + expect_true(http_is_server_error(500), "500 should be a server error"); + expect_true(http_is_get("GET"), "GET should be a GET method"); + expect_true(http_is_post("POST"), "POST should be a POST method"); + + # Test string predicates + expect_true(is_alpha("Hello"), "Alphabetic string should pass"); + expect_true(is_numeric("12345"), "Numeric string should pass"); + expect_true(is_alphanumeric("Hello123"), "Alphanumeric string should pass"); + expect_true(is_uppercase("HELLO"), "Uppercase string should pass"); + expect_true(is_lowercase("hello"), "Lowercase string should pass"); + + # Test numeric predicates + expect_true(is_even(2), "2 should be even"); + expect_true(is_odd(3), "3 should be odd"); + expect_true(is_prime(7), "7 should be prime"); + expect_false(is_prime(4), "4 should not be prime"); + + print "All standard library tests passed!"; + exit 0; +} \ No newline at end of file |