Ancient

 view release on metacpan or  search on metacpan

t/1035-util-hof-valid.t  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use lib 'blib/lib', 'blib/arch';

use_ok('util');
use util qw(
    first any all none
    final
    first_gt first_lt first_ge first_le first_eq first_ne
    final_gt final_lt final_ge final_le final_eq final_ne
    any_gt any_lt any_ge any_le any_eq any_ne
    all_gt all_lt all_ge all_le all_eq all_ne
    none_gt none_lt none_ge none_le none_eq none_ne
);

# Note: util::count is for counting substrings, not list elements.
# For list counting with callbacks, use count_cb with named predicates.

# Note: first/any/all/none/count take a LIST (not arrayref)
# The specialized predicates (*_gt, *_lt, etc.) take an ARRAYREF

# ============================================
# first - find first matching element
# ============================================

subtest 'first basic' => sub {
    my @nums = (1, 2, 3, 4, 5);

    is(first(sub { $_ > 3 }, @nums), 4, 'first: > 3');
    is(first(sub { $_ == 1 }, @nums), 1, 'first: == 1');
    is(first(sub { $_ == 5 }, @nums), 5, 'first: == 5');
    is(first(sub { $_ > 10 }, @nums), undef, 'first: no match');
};

subtest 'first edge cases' => sub {
    # Empty list
    is(first(sub { 1 }), undef, 'first: empty list');

    # Single element
    is(first(sub { 1 }, 42), 42, 'first: single match');
    is(first(sub { 0 }, 42), undef, 'first: single no match');

    # First element matches
    is(first(sub { $_ == 1 }, 1, 2, 3), 1, 'first: first element');

    # With hashes
    my @users = (
        {name => 'alice', age => 30},
        {name => 'bob', age => 25},
        {name => 'charlie', age => 35},
    );
    my $found = first(sub { $_->{age} > 28 }, @users);
    is($found->{name}, 'alice', 'first: hash predicate');

    # Finding undef element
    my $result = first(sub { !defined $_ }, undef, 1, 2);
    is($result, undef, 'first: finds undef');
};

# ============================================
# any - true if any match
# ============================================

subtest 'any basic' => sub {
    my @nums = (1, 2, 3, 4, 5);

    ok(any(sub { $_ > 3 }, @nums), 'any: some > 3');
    ok(any(sub { $_ == 1 }, @nums), 'any: one == 1');
    ok(!any(sub { $_ > 10 }, @nums), 'any: none > 10');
};

subtest 'any edge cases' => sub {
    # Empty list
    ok(!any(sub { 1 }), 'any: empty list is false');

    # Single element
    ok(any(sub { 1 }, 42), 'any: single match');



( run in 1.024 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )