Codebase list libautobox-core-perl / af5739b
Merge branch 'master' of github.com:scrottie/autobox-Core Conflicts: t/grep.t Scott Walters 14 years ago
45 changed file(s) with 680 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
0 *.swp
1 *.swo
2 *.tmp
3 *.bak
4 blib/
5 Makefile
6 pm_to_blib
7 cover_db
8 *.gz
9 nytprof/
10 .prove
11 autobox-Core*
12 .build
652652 sub lcfirst ($) { CORE::lcfirst($_[0]); }
653653 sub length ($) { CORE::length($_[0]); }
654654 sub ord ($) { CORE::ord($_[0]); }
655 sub pack ($;@) { CORE::pack(@_); }
655 sub pack ($;@) { CORE::pack(shift, @_); }
656656 sub reverse ($) { CORE::reverse($_[0]); }
657 sub rindex ($@) { CORE::rindex($_[0], $_[1], @_[2.. $#_]); }
657
658 sub rindex ($@) {
659 return CORE::rindex($_[0], $_[1]) if @_ == 2;
660 return CORE::rindex($_[0], $_[1], @_[2.. $#_]);
661 }
662
658663 sub sprintf ($@) { CORE::sprintf($_[0], $_[1], @_[2.. $#_]); }
659 sub substr ($@) { CORE::substr($_[0], $_[1], @_[2 .. $#_]); }
664
665 sub substr ($@) {
666 return CORE::substr($_[0], $_[1]) if @_ == 2;
667 return CORE::substr($_[0], $_[1], @_[2 .. $#_]);
668 }
669
660670 sub uc ($) { CORE::uc($_[0]); }
661671 sub ucfirst ($) { CORE::ucfirst($_[0]); }
662672 sub unpack ($;@) { CORE::unpack($_[0], @_[1..$#_]); }
666676 sub m ($$) { [ $_[0] =~ m{$_[1]} ] }
667677 sub nm ($$) { [ $_[0] !~ m{$_[1]} ] }
668678 sub s ($$$) { $_[0] =~ s{$_[1]}{$_[2]} }
669 sub split ($$) { [ split $_[1], $_[0] ] }
679 sub split ($$) { wantarray ? split $_[1], $_[0] : [ split $_[1], $_[0] ] }
670680
671681 sub eval ($) { CORE::eval "$_[0]"; }
672682 sub system ($;@) { CORE::system @_; }
848858
849859 sub delete (\%@) { my $hash = CORE::shift; my @res = (); CORE::foreach(@_) { push @res, CORE::delete $hash->{$_}; } CORE::wantarray ? @res : \@res }
850860 sub exists (\%$) { my $hash = CORE::shift; CORE::exists $hash->{$_[0]}; }
851 sub keys (\%) { [ CORE::keys %{$_[0]} ] }
852 sub values (\%) { [ CORE::values %{$_[0]} ] }
861 sub keys (\%) { wantarray ? CORE::keys %{$_[0]} : [ CORE::keys %{$_[0]} ] }
862 sub values (\%) { wantarray ? CORE::values %{$_[0]} : [ CORE::values %{$_[0]} ] }
853863
854864 sub at (\%@) { $_[0]->{@_[1..$#_]}; }
855865 sub get(\%@) { $_[0]->{@_[1..$#_]}; }
10031013 # Functions for real @ARRAYs
10041014 # "pop", "push", "shift", "splice", "unshift"
10051015
1006 sub pop (\@) { CORE::pop @{$_[0]}; wantarray ? @{$_[0]} : $_[0] }
1007
1008 sub push (\@;@) { my $arr = CORE::shift; CORE::push @$arr, @_; $arr; }
1016 sub pop (\@) { CORE::pop @{$_[0]}; }
1017
1018 sub push (\@;@) { my $arr = CORE::shift; CORE::push @$arr, @_; wantarray ? return @$arr : $arr; }
10091019
10101020 sub unshift (\@;@) { my $a = CORE::shift; CORE::unshift(@$a, @_); wantarray ? @$a : $a; }
10111021
10131023
10141024 sub vdelete(\@$) { my $arr = CORE::shift; @$arr = CORE::grep {$_ ne $_[0]} @$arr; wantarray ? @$arr : $arr }
10151025
1016 sub shift (\@;@) { my $arr = CORE::shift; CORE::shift @$arr; wantarray ? @$arr : $arr} # last to prevent having to prefix normal shift calls with CORE::
1026 sub shift (\@;@) { my $arr = CORE::shift; CORE::shift @$arr; } # last to prevent having to prefix normal shift calls with CORE::
10171027
10181028 sub undef ($) { $_[0] = [] }
10191029
11011111
11021112 sub for {
11031113 my $arr = CORE::shift; my $sub = CORE::shift;
1104 for(my $i = 0; $i < $#$arr; $i++) {
1114 for(my $i = 0; $i <= $#$arr; $i++) {
11051115 $sub->($i, $arr->[$i], $arr);
11061116 }
11071117 }
00 use Test::More;
1 BEGIN { plan tests => 69 };
1 BEGIN { plan tests => 72 };
22 use autobox::Core;
33
44 #####################################################################
8686 ok($a->[0] == 1 && $a->[@$a-1] == 10);
8787 $a = 10->to(1);
8888 ok($a->[0] == 10 && $a->[@$a-1] == 1);
89 my @a = 1->to(10);
90 is_deeply \@a, [ 1 .. 10 ];
8991
9092 $a = 1->upto(10);
9193 ok($a->[0] == 1 && $a->[@$a-1] == 10);
9294
95 @a = 1->upto(10);
96 is_deeply \@a, [ 1 .. 10 ];
97
9398 $a = 10->downto(1);
9499 ok($a->[0] == 10 && $a->[@$a-1] == 1);
100
101 @a = 10->downto(1);
102 is_deeply \@a, [ reverse 1 .. 10 ];
95103
96104 $a = 1;
97105 ok(10->times(sub {$a++}) == 10);
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my %struct = (
7 ARRAY => [ 'foo' ],
8 HASH => { 'foo' => 1 },
9 CODE => sub { 'foo' },
10 );
11
12 foreach my $reftype ( keys %struct ) {
13 $struct{$reftype}->bless("Object");
14 is ref $struct{$reftype}, "Object";
15 }
16
17 TODO: {
18 todo_skip "Make it work for Regexp, Scalar and Glob", 3;
19 my %todo = (
20 Regexp => qr/foo/,
21 SCALAR => \'foo',
22 GLOB => \*STDIN,
23 );
24
25 foreach my $reftype ( keys %todo ) {
26 $todo{$reftype}->bless("Object");
27 is ref $todo{$reftype}, "Object";
28 }
29 }
30
0 use Test::More qw(no_plan);
1
2 use autobox::Core;
3
4 my $line = "This has a new line\n";
5
6 $line->chomp;
7
8 is $line, "This has a new line";
9
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = "This is a string";
7
8 my $char = $string->chop;
9
10 is $string, "This is a strin", "Chop modifies the string";
11 is $char, "g", "... and returns the last character";
12
13 TODO: {
14
15 todo_skip "Chop should work on lists too", 2;
16
17 my @list = qw(foo bar baz);
18
19 my $char = @list->chop;
20
21 is $char, 'z';
22
23 is_deeply \@list, [ 'fo', 'ba', 'ba' ];
24 }
25
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 is 65->chr, chr(65);
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6
7 my $times = sub { $_[0] * $_[1] };
8
9 my $times_two = $times->curry(2);
10 my $times_four = $times->curry(4);
11
12 is $times_two->(5), 10;
13 is $times_four->(5), 20;
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my %hash = ( foo => 1, bar => 2, baz => 3 );
7
8 my @glued;
9 %hash->each( sub { push @glued, $_[0] . $_[1] } );
10
11 is_deeply [ sort @glued ], [ qw(bar2 baz3 foo1) ];
12
13 my @array = values %hash;
14
15 my @added;
16 @array->each( sub { push @added, $_[0] + 1 } );
17
18 is_deeply [ sort @added ], [ qw(2 3 4) ];
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar baz);
7
8 my @returned = @array->elements;
9
10 is_deeply \@returned, \@array;
11
12 my $count = @array->elements;
13
14 is $count, 3;
15
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar baz);
7
8 is @array->elems, 3;
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar baz);
7
8 my @returned = @array->flatten;
9
10 is_deeply \@returned, \@array;
11
12 my $count = @array->flatten;
13
14 is $count, 3;
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(1 2 3);
7
8 my @added;
9 @array->for( sub { my ($i, $v, $arr) = @_; push @added, $i + $v + @$arr } );
10
11 is_deeply [ @added ], [ qw(4 6 8) ];
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(1 2 3);
7
8 my @added;
9 @array->foreach( sub { push @added, $_[0] + 1 } );
10
11 is_deeply [ sort @added ], [ qw(2 3 4) ];
00 #!/usr/bin/env perl
11
2 use Test::More 'no_plan';
3 use strict;
4 use warnings;
5
26 use autobox::Core;
3 use Test::More 'no_plan';
47
5 my @array = qw( foo bar baz );
8 my @array = qw(1 2 3);
9
10 my @odd = @array->grep(sub { $_ % 2 });
11
12 is_deeply \@odd, [qw(1 3)], "Expected coderef grep results";
13
14 my $arrayref = @array->grep( sub { 'foo' } );
15
16 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
17
18 @array = qw( foo bar baz );
619 my $d;
720
821 ok ( eval { @array->grep( sub { 42 } || 1) }, "Should accept code refs" );
1124 is_deeply( $d = @array->grep('foo'), [qw( foo )], "Works with SCALAR" );
1225 is_deeply( $d = @array->grep('zar'), [], "Works with SCALAR" );
1326 is_deeply( $d = @array->grep(qr/^ba/), [qw( bar baz )], "Works with Regexp" );
14 if( $] >= 5.010 ) {
15 is_deeply( $d = @array->grep(+{ boo => 'boy' }), [], "Works with HASH" );
16 is_deeply( $d = @array->grep([qw(boo boy)]), [], "Works with ARRAY" );
17 is_deeply( $d = @array->grep([qw(foo baz)]), [qw(foo baz)], "Works with ARRAY" );
18 }
1927 is_deeply( $d = @array->grep(sub { /^ba/ }), [qw( bar baz )], "... as with Code refs" );
2028
2129 # context
2230 my @d = @array->grep(qr/^ba/);
2331
2432 is scalar @d, 2, "Returns an array in list context";
33 SKIP: {
34 skip "Only for 5.10", 1, if $] < 5.010;
2535
36 my @names = qw(barney booey moe);
37
38 is_deeply( [ @names->grep(qr/^b/) ], [ qw(barney booey) ] );
39 is_deeply( $d = @array->grep(+{ boo => 'boy' }), [], "Works with HASH" );
40 is_deeply( $d = @array->grep([qw(boo boy)]), [], "Works with ARRAY" );
41 is_deeply( $d = @array->grep([qw(foo baz)]), [qw(foo baz)], "Works with ARRAY" );
42 }
43
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3 use autobox::Core;
4
5 my $string = "I like pie";
6 my $substr = "pie";
7
8 is $string->index($substr), 7;
9 is $string->index($substr, 8), -1;
10
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @list = qw(h i t h e r e);
7
8 is @list->join(''), 'hithere';
9 is @list->join(' '), 'h i t h e r e';
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my %hash = ( foo => 1, bar => 2, baz => 3 );
7
8 is_deeply [ sort %hash->keys ], [ qw( bar baz foo ) ];
9
10 my $arrayref = %hash->keys;
11
12 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = "THIS IS A STRING";
7
8 is $string->lc, "this is a string";
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = "THIS IS A STRING";
7
8 is $string->lcfirst, 'tHIS IS A STRING';
9
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = "THIS IS A STRING";
7
8 is $string->length, 16;
9
10 my @array = qw(foo bar baz);
11
12 is @array->length, 3;
13
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6
7 {
8 my @array = qw(1 2 3);
9
10 my @added = @array->map(sub { ++$_ });
11
12 is_deeply \@added, [qw(2 3 4)];
13
14 my $arrayref = @array->map( sub { 'foo' } );
15
16 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
17 }
18
19 {
20 my @array = qw(1 2 3);
21
22 my $add = sub { ++$_ };
23
24 my @added = $add->map(@array);
25
26 is_deeply \@added, [qw(2 3 4)];
27
28 my $arrayref = $add->map(@added);
29
30 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
31 }
32
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $num = 0.5;
7 my $e = 1E-10;
8
9 cmp_ok( abs($num->abs - abs($num)), '<', $e );
10 cmp_ok( abs($num->cos - cos($num)), '<', $e );
11 cmp_ok( abs($num->exp - exp($num)), '<', $e );
12 cmp_ok( abs($num->int - int($num)), '<', $e );
13 cmp_ok( abs($num->log - log($num)), '<', $e );
14 cmp_ok( abs($num->oct - oct($num)), '<', $e );
15 cmp_ok( abs(05->hex - hex(05)), '<', $e );
16 cmp_ok( abs($num->sin - sin($num)), '<', $e );
17 cmp_ok( abs($num->sqrt - sqrt($num)), '<', $e );
18
19 cmp_ok( $num->rand, '<=', $num );
20 cmp_ok( abs($num->atan2($num) - atan2($num, $num)), '<', $e );
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 is 'A'->ord, ord('A');
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 is 'nN'->pack(42, 4711), pack('nN', 42, 4711);
7 is '(sl)<'->pack(-42, 4711), pack('(sl)<', -42, 4711);
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar);
7
8 is @array->pop, 'bar';
9
10 is_deeply \@array, [qw(foo)];
0 use Test::More qw(no_plan);
1 use Test::Output;
2 use strict;
3 use warnings;
4
5 use autobox::Core;
6
7 my $message = "This is an important message";
8 my @array = qw(this is an important message);
9
10 SKIP: {
11 my $has_test_output = eval { require Test::Output };
12
13 skip "Don't have Test::Output", 2, if not $has_test_output;
14 Test::Output::stdout_is( sub { $message->print }, $message );
15 Test::Output::stdout_is( sub { @array->print }, "@array" );
16 }
17
18 # We need at least one test so that Test::Harness doesn't complain in
19 # case we had to skip above
20
21 ok 1;
22
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar);
7
8 my @returned = @array->push('baz');
9
10 is_deeply \@array, [qw(foo bar baz)];
11 is_deeply \@returned, [qw(foo bar baz)];
12
13 my $arrayref = @array->push('baz');
14
15 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
16 my $array = [qw(foo bar)];
17
18 $array->push('baz');
19
20 is_deeply $array, [qw(foo bar baz)];
21
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my %struct = (
7 ARRAY => [ 'foo' ],
8 HASH => { 'foo' => 1 },
9 CODE => sub { 'foo' },
10 );
11
12 foreach my $reftype ( keys %struct ) {
13 is $struct{$reftype}->ref, $reftype;
14 }
15
16 TODO: {
17 todo_skip "Make it work for Regexp, Scalar and Glob", 3;
18 my %todo = (
19 Regexp => qr/foo/,
20 SCALAR => \'foo',
21 GLOB => \*STDIN,
22 );
23
24 foreach my $reftype ( keys %todo ) {
25 is $todo{$reftype}->ref, $reftype;
26 }
27 }
28
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 is "Hello"->reverse, "olleH";
7
8 my @list = qw(foo bar baz);
9
10 is_deeply [@list->reverse], [qw(baz bar foo)];
11
12 my $arrayref = @list->reverse;
13
14 is ref $arrayref, "ARRAY", "returns an arrayref in scalar context";
15
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3 use autobox::Core;
4
5 my $string = "I like pie pie";
6 my $substr = "pie";
7
8 is $string->rindex($substr), rindex($string, $substr);
9 is $string->rindex($substr, 12), rindex($string, $substr, 12);
10
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = 'HELLO';
7 $string->s('^HE', 'Hu');
8
9 is $string, 'HuLLO';
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $message = "This is an important message";
7 my @array = qw(this is an important message);
8
9 SKIP: {
10 my $has_test_output = eval { require Test::Output };
11
12 skip "Don't have Test::Output", 1, if not $has_test_output;
13 Test::Output::stdout_is( sub { $message->say }, $message . "\n" );
14 Test::Output::stdout_is( sub { @array->say }, "@array\n" );
15 }
16
17 # We need at least one test so that Test::Harness doesn't complain in
18 # case we had to skip above
19
20 ok 1;
21
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar);
7
8 is @array->shift, 'foo';
9
10 is_deeply \@array, [qw(bar)];
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar baz);
7
8 is @array->size, 3;
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar baz);
7
8 my @returned = @array->sort;
9
10 is_deeply \@returned, [qw(bar baz foo)];
11
12 @returned = @array->sort(sub { $_[1] cmp $_[0] });
13
14 is_deeply \@returned, [qw(foo baz bar)];
15
16 my $arrayref = @array->sort;
17
18 is ref $arrayref, "ARRAY", "Returns an arrayref in scalar context";
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 is_deeply ["hi there"->split(qr/ */)], [qw(h i t h e r e)];
7
8 my $arrayref = "hi there"->split(qr/ */);
9
10 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
11
0 use Test::More qw(no_plan);
1 use Test::Output;
2 use strict;
3 use warnings;
4
5 use autobox::Core;
6
7 my $format = "%.2f";
8
9 is $format->sprintf(2/3), "0.67";
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3 use autobox::Core;
4
5 my $s = "The black cat climbed the green tree";
6 my $color = $s->substr(4, 5);
7
8 is $color, 'black';
9
10 my $middle = $s->substr(4, -11);
11
12 is $middle, 'black cat climbed the';
13
14 my $end = $s->substr(14);
15 is $end, 'climbed the green tree';
16
17 my $tail = $s->substr(-4);
18 is $tail, 'tree';
19
20 my $z = $s->substr(-4, 2);
21
22 is $z, 'tr';
23
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = "this is a string";
7
8 is $string->uc, "THIS IS A STRING";
9
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my $string = "this is a string";
7
8 is $string->ucfirst, "This is a string";
9
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 is 'W'->unpack("foo"), unpack('W', "foo");
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my @array = qw(foo bar);
7
8 my @returned = @array->unshift('baz');
9
10 is_deeply \@array, [qw(baz foo bar)];
11 is_deeply \@returned, [qw(baz foo bar)];
12
13 my $arrayref = @array->unshift('baz');
14
15 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
16
17 my $array = [qw(foo bar)];
18
19 $array->unshift('baz');
20
21 is_deeply $array, [qw(baz foo bar)];
22
0 use Test::More qw(no_plan);
1 use strict;
2 use warnings;
3
4 use autobox::Core;
5
6 my %hash = ( foo => 1, bar => 2, baz => 3 );
7
8 is_deeply [ sort %hash->values ], [ qw( 1 2 3 ) ];
9
10 my $arrayref = %hash->values;
11
12 is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context";
13
0
1 use Test::More qw(no_plan);
2 use strict;
3 use warnings;
4
5 use autobox::Core;
6
7 my $foo = '';
8
9 is $foo->vec(0, 32), vec($foo, 0, 32); # 0x5065726C, 'Perl'
10 is $foo->vec(2, 16), vec($foo, 2, 16); # 0x5065, 'PerlPe'
11 is $foo->vec(3, 16), vec($foo, 3, 16); # 0x726C, 'PerlPerl'