about summary refs log tree commit diff stats
path: root/awk/rawk/scratch/tests_old/stdlib/test_functional.rawk
blob: b2d7e4343b59ada95bd1d271be4adc042428f2de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
$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"
}