about summary refs log tree commit diff stats
path: root/awk/rawk/tests/test_functional.rawk
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/tests/test_functional.rawk')
-rw-r--r--awk/rawk/tests/test_functional.rawk117
1 files changed, 117 insertions, 0 deletions
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