about summary refs log tree commit diff stats
path: root/awk/rawk/tests/test_stdlib.rawk
blob: 480e7074b4728837327a3ca2911c69e726c63861 (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
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;
}