about summary refs log tree commit diff stats
path: root/awk/rawk/scratch/tests_old/stdlib/README.md
blob: 1b7b02893f934406b4ad75233b1e3f4d0be92fc7 (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
# 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
```