about summary refs log tree commit diff stats
path: root/awk/rawk/tests
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/tests')
-rw-r--r--awk/rawk/tests/simple_stdlib_test.rawk24
-rw-r--r--awk/rawk/tests/test_basic.rawk41
-rw-r--r--awk/rawk/tests/test_errors.rawk12
-rw-r--r--awk/rawk/tests/test_functional.rawk117
-rwxr-xr-xawk/rawk/tests/test_runner.sh100
-rw-r--r--awk/rawk/tests/test_smart_stdlib.rawk28
-rw-r--r--awk/rawk/tests/test_stdlib.rawk70
7 files changed, 392 insertions, 0 deletions
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/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