about summary refs log tree commit diff stats
path: root/awk/rawk/scratch/tests_old/stdlib/test_phase2_utilities.rawk
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/scratch/tests_old/stdlib/test_phase2_utilities.rawk')
-rw-r--r--awk/rawk/scratch/tests_old/stdlib/test_phase2_utilities.rawk209
1 files changed, 209 insertions, 0 deletions
diff --git a/awk/rawk/scratch/tests_old/stdlib/test_phase2_utilities.rawk b/awk/rawk/scratch/tests_old/stdlib/test_phase2_utilities.rawk
new file mode 100644
index 0000000..c99083a
--- /dev/null
+++ b/awk/rawk/scratch/tests_old/stdlib/test_phase2_utilities.rawk
@@ -0,0 +1,209 @@
+$split_words = (text, result) -> {
+    split(text, result, " ")
+    return length(result)
+};
+
+$double = (x) -> x * 2;
+$is_positive = (x) -> x > 0;
+$get_tags = (item, result) -> {
+    split(item, result, ",")
+    return length(result)
+};
+
+$create_range = (n, result) -> {
+    for (i = 1; i <= n; i++) {
+        result[i] = i
+    }
+    return n
+};
+
+BEGIN {
+    print "=== Phase 2 Utilities Test Suite ==="
+    print ""
+    
+    # Test 1: flatMap function
+    print "Test 1: flatMap Function"
+    
+    # Test with text splitting
+    texts[1] = "hello world"
+    texts[2] = "functional programming"
+    texts[3] = "awk is awesome"
+    
+    words_count = flatMap("split_words", texts, all_words)
+    expect_equal(words_count, 7, "Should have 7 words total")
+    print "✓ flatMap with text splitting working"
+    
+    # Test with tag extraction
+    items[1] = "tag1,tag2,tag3"
+    items[2] = "tag4,tag5"
+    items[3] = "tag6"
+    
+    tags_count = flatMap("get_tags", items, all_tags)
+    expect_equal(tags_count, 6, "Should have 6 tags total")
+    print "✓ flatMap with tag extraction working"
+    
+    # Test with range creation
+    ranges[1] = 2
+    ranges[2] = 3
+    ranges[3] = 1
+    
+    numbers_count = flatMap("create_range", ranges, all_numbers)
+    expect_equal(numbers_count, 6, "Should have 6 numbers total (1,2,1,2,3,1)")
+    print "✓ flatMap with range creation working"
+    print ""
+    
+    # Test 2: take function
+    print "Test 2: Take Function"
+    
+    numbers[1] = 1
+    numbers[2] = 2
+    numbers[3] = 3
+    numbers[4] = 4
+    numbers[5] = 5
+    
+    # Take first 3 elements (order may vary due to AWK iteration)
+    first_three_count = take(3, numbers, first_three)
+    expect_equal(first_three_count, 3, "Should take 3 elements")
+    # Check that we have 3 elements (order may vary)
+    expect_true(first_three[1] >= 1 && first_three[1] <= 5, "First element should be between 1-5")
+    expect_true(first_three[2] >= 1 && first_three[2] <= 5, "Second element should be between 1-5")
+    expect_true(first_three[3] >= 1 && first_three[3] <= 5, "Third element should be between 1-5")
+    print "✓ Take first 3 elements working"
+    
+    # Take more than available
+    all_count = take(10, numbers, all_elements)
+    expect_equal(all_count, 5, "Should take all 5 elements")
+    # Check that we have all elements (order may vary)
+    expect_true(all_elements[1] >= 1 && all_elements[1] <= 5, "First element should be between 1-5")
+    expect_true(all_elements[5] >= 1 && all_elements[5] <= 5, "Last element should be between 1-5")
+    print "✓ Take more than available working"
+    
+    # Take zero elements
+    zero_count = take(0, numbers, zero_elements)
+    expect_equal(zero_count, 0, "Should take 0 elements")
+    print "✓ Take zero elements working"
+    print ""
+    
+    # Test 3: drop function
+    print "Test 3: Drop Function"
+    
+    # Drop first 2 elements (order may vary due to AWK iteration)
+    remaining_count = drop(2, numbers, remaining)
+    expect_equal(remaining_count, 3, "Should have 3 remaining elements")
+    # Check that we have 3 remaining elements (order may vary)
+    expect_true(remaining[1] >= 1 && remaining[1] <= 5, "First remaining should be between 1-5")
+    expect_true(remaining[2] >= 1 && remaining[2] <= 5, "Second remaining should be between 1-5")
+    expect_true(remaining[3] >= 1 && remaining[3] <= 5, "Third remaining should be between 1-5")
+    print "✓ Drop first 2 elements working"
+    
+    # Drop all elements
+    none_count = drop(5, numbers, none)
+    expect_equal(none_count, 0, "Should have 0 remaining elements")
+    print "✓ Drop all elements working"
+    
+    # Drop more than available
+    over_drop_count = drop(10, numbers, over_dropped)
+    expect_equal(over_drop_count, 0, "Should have 0 remaining elements")
+    print "✓ Drop more than available working"
+    
+    # Drop zero elements
+    no_drop_count = drop(0, numbers, no_dropped)
+    expect_equal(no_drop_count, 5, "Should have all 5 elements")
+    # Check that we have all elements (order may vary)
+    expect_true(no_dropped[1] >= 1 && no_dropped[1] <= 5, "First element should be between 1-5")
+    expect_true(no_dropped[5] >= 1 && no_dropped[5] <= 5, "Last element should be between 1-5")
+    print "✓ Drop zero elements working"
+    print ""
+    
+    # Test 4: Edge cases
+    print "Test 4: Edge Cases"
+    
+    # Test with empty array
+    empty_take_count = take(3, empty_array, empty_take_result)
+    expect_equal(empty_take_count, 0, "Take from empty should return 0")
+    print "✓ Take from empty array working"
+    
+    empty_drop_count = drop(2, empty_array, empty_drop_result)
+    expect_equal(empty_drop_count, 0, "Drop from empty should return 0")
+    print "✓ Drop from empty array working"
+    
+    empty_flatmap_count = flatMap("split_words", empty_array, empty_flatmap_result)
+    expect_equal(empty_flatmap_count, 0, "flatMap from empty should return 0")
+    print "✓ flatMap from empty array working"
+    
+    # Test with single element array
+    single[1] = "test"
+    single_take_count = take(1, single, single_take_result)
+    expect_equal(single_take_count, 1, "Take 1 from single should return 1")
+    expect_equal(single_take_result[1], "test", "Should get the single element")
+    print "✓ Take from single element working"
+    
+    single_drop_count = drop(1, single, single_drop_result)
+    expect_equal(single_drop_count, 0, "Drop 1 from single should return 0")
+    print "✓ Drop from single element working"
+    print ""
+    
+    # Test 5: Integration with existing functions
+    print "Test 5: Integration with Existing Functions"
+    
+    # Take then map
+    taken_count = take(3, numbers, taken)
+    doubled_count = map("double", taken, doubled_taken)
+    expect_equal(doubled_count, 3, "Should have 3 doubled elements")
+    # Check that we have doubled values (order may vary)
+    expect_true(doubled_taken[1] >= 2 && doubled_taken[1] <= 10, "First doubled should be between 2-10")
+    expect_true(doubled_taken[2] >= 2 && doubled_taken[2] <= 10, "Second doubled should be between 2-10")
+    expect_true(doubled_taken[3] >= 2 && doubled_taken[3] <= 10, "Third doubled should be between 2-10")
+    print "✓ Take + Map integration working"
+    
+    # Drop then filter
+    dropped_count = drop(2, numbers, dropped)
+    positive_count = filter("is_positive", dropped, positive_dropped)
+    expect_equal(positive_count, 3, "Should have 3 positive elements")
+    print "✓ Drop + Filter integration working"
+    
+    # flatMap then take
+    flatmapped_count = flatMap("split_words", texts, flatmapped)
+    taken_words_count = take(3, flatmapped, taken_words)
+    expect_equal(taken_words_count, 3, "Should take 3 words")
+    print "✓ flatMap + Take integration working"
+    print ""
+    
+    # Test 6: Real-world scenarios
+    print "Test 6: Real-world Scenarios"
+    
+    # Process log lines and extract words
+    log_lines[1] = "ERROR: Database connection failed"
+    log_lines[2] = "INFO: User logged in successfully"
+    log_lines[3] = "DEBUG: Memory usage normal"
+    
+    # Extract all words from logs
+    all_log_words_count = flatMap("split_words", log_lines, all_log_words)
+    expect_equal(all_log_words_count, 13, "Should have 13 words total (4+5+4)")
+    print "✓ Log processing with flatMap working"
+    
+    # Take first 5 words
+    first_five_count = take(5, all_log_words, first_five_words)
+    expect_equal(first_five_count, 5, "Should take 5 words")
+    print "✓ Taking first 5 words working"
+    
+    # Drop first 3 words
+    remaining_words_count = drop(3, all_log_words, remaining_words)
+    expect_equal(remaining_words_count, 10, "Should have 10 remaining words (13-3)")
+    print "✓ Dropping first 3 words working"
+    print ""
+    
+    print "=== Phase 2 Utilities Test Summary ==="
+    print "Total tests: 6"
+    print "Passed: 6"
+    print "Failed: 0"
+    print "🎉 All Phase 2 utilities tests passed!"
+    print ""
+    print "Features verified:"
+    print "✓ flatMap() - Array transformation and flattening"
+    print "✓ take() - Take first n elements from array"
+    print "✓ drop() - Drop first n elements from array"
+    print "✓ Edge cases (empty arrays, single elements, boundary conditions)"
+    print "✓ Integration with existing functional programming features"
+    print "✓ Real-world scenarios (log processing, text analysis)"
+} 
\ No newline at end of file