File: pop.t

package info (click to toggle)
rakudo 2016.12-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,200 kB
  • ctags: 1,323
  • sloc: perl: 45,091; java: 2,524; ansic: 1,761; cpp: 152; sh: 17; makefile: 15
file content (113 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use v6.c;
use Test;

# L<S32::Containers/"Array"/"=item pop">

=begin description

Pop tests

=end description

plan 37;

{ # pop() elements into variables
    my @pop = (-1, 1, 2, 3, 4);
    is(+@pop, 5, 'we have 4 elements in the array');

    my $a = pop(@pop);
    is($a, 4, 'pop(@pop) works');

    is(+@pop, 4, 'we have 3 elements in the array');
    $a = pop @pop;
    is($a, 3, 'pop @pop works');

    is(+@pop, 3, 'we have 2 elements in the array');
    $a = @pop.pop();
    is($a, 2, '@pop.pop() works');

    is(+@pop, 2, 'we have 1 element in the array');
    $a = @pop.pop;
    is($a, 1, '@pop.pop works');

    is(+@pop, 1, 'we have 1 element in the array');
    
    {
        $a = pop(@pop);
        is($a, -1, '@pop.pop works');

        is(+@pop, 0, 'we have no more element in the array');
        ok(!defined(pop(@pop)), 'after the array is exhausted pop() returns undefined');
        #?niecza skip 'undeclared name Failure'
        ok(pop(@pop) ~~ Failure, 'after the array is exhausted pop() returns Failure');
    }
} #13

{ # pop() elements inline
    my @pop = (1, 2, 3, 4);

    is(+@pop, 4, 'we have 4 elements in the array');
    is(pop(@pop), 4, 'inline pop(@pop) works');

    is(+@pop, 3, 'we have 3 elements in the array');
    is((pop @pop), 3, 'inline pop @pop works');

    is(+@pop, 2, 'we have 2 elements in the array');
    is(@pop.pop(), 2, 'inline @pop.pop() works');

    is(+@pop, 1, 'we have 1 element in the array');
    is(@pop.pop, 1, 'inline @pop.pop works');

    is(+@pop, 0, 'we have no more element in the array');
    ok(!defined(pop(@pop)), 'after the array is exhausted pop() returns undefined');
    #?niecza skip 'undeclared name Failure'
    ok(pop(@pop) ~~ Failure, 'after the array is exhausted pop() returns Failure');
} #11

# invocant syntax with inline arrays
{
    is([1, 2, 3].pop, 3, 'this will return 3');
    ok(!defined([].pop), 'this will return undefined');
    #?niecza skip 'undeclared name Failure'
    ok( [].pop ~~ Failure, '[].pop is a Failure' );
} #3

# some edge cases
{
    my @pop;
    ok(!defined(@pop.pop()), 'pop on an un-initialized array returns undefined');
    #?niecza skip 'undeclared name Failure'
    ok( @pop.pop() ~~ Failure, 'pop off uninitialized array is a Failure' );
}

# testing some error cases
{
    my @pop = 1 .. 5;
    throws-like 'pop', X::TypeCheck::Argument, 'pop() requires arguments';
    throws-like '42.pop', X::Method::NotFound, '.pop should not work on scalars';
    throws-like 'pop(@pop,10)', Exception,     'pop() should not allow extra arguments';
    throws-like '@pop.pop(10)', Exception,     '.pop() should not allow extra arguments';
    #?rakudo todo 'code does not die'
    ## TODO test for correct exception once the code dies
    throws-like '@pop.pop = 3', Exception,  'Cannot assign to a readonly variable or a value';
    throws-like 'pop(@pop) = 3', Exception, 'Cannot assign to a readonly variable or a value';
} #6

#?niecza     skip "may run forever"
{
    my @push = 1 .. Inf;
    throws-like 'pop @push', X::Cannot::Lazy, 'cannot pop from a lazy list';
} #1

# RT #111720
{
    my @a = 1,2,3;
    my $rt111720 = Array.new(@a) => "x";
    $rt111720.key[0];
    @a.pop();
    #?rakudo todo 'RT #111720'
    is $rt111720.keys.[0].join("-"), '1-2',
        'reading first key in sink context does not influence later code';
}

# vim: ft=perl6