File: unshift.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 (203 lines) | stat: -rw-r--r-- 6,039 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use v6.c;
use Test;

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

=begin description

Unshift tests

=end description

plan 76;

# basic unshift tests

{
    my @unshift = ();

    is(+@unshift, 0, 'we have an empty array');

    unshift(@unshift, 1);
    is(+@unshift, 1, 'we have 1 element in the array');
    is(@unshift[0], 1, 'we found the right element');

    unshift(@unshift, 2);
    is(+@unshift, 2, 'we have 2 elements in the array');
    is(@unshift[0], 2, 'we found the right element');
    is(@unshift[1], 1, 'we found the right element');

    unshift(@unshift, 3);
    is(+@unshift, 3, 'we have 3 element in the array');
    is(@unshift[0], 3, 'we found the right element');
    is(@unshift[1], 2, 'we found the right element');
    is(@unshift[2], 1, 'we found the right element');

    unshift(@unshift, 4);
    is(+@unshift, 4, 'we have 4 element in the array');
    is(@unshift[0], 4, 'we found the right element');
    is(@unshift[1], 3, 'we found the right element');
    is(@unshift[2], 2, 'we found the right element');
    is(@unshift[3], 1, 'we found the right element');
}

# try other variations on calling unshift()

{
    my @unshift = ();

    my $val = 100;

    unshift @unshift, $val;
    is(+@unshift, 1, 'we have 1 element in the array');
    is(@unshift[0], $val, 'unshift @array, $val worked');

    @unshift.unshift(200);
    is(+@unshift, 2, 'we have 2 elements in the array');
    is(@unshift[0], 200, '@unshift.unshift(200) works');
    is(@unshift[1], $val, 'unshift @array, $val worked');

    @unshift.unshift(400);
    is(+@unshift, 3, 'we have 3 elements in the array');
    is(@unshift[0], 400, '@unshift.unshift(400) works');
    is(@unshift[1], 200, '@unshift.unshift(200) works');
    is(@unshift[2], $val, 'unshift @array, $val worked');
}

# try unshifting more than one element using prepend for 1-arg semantics

{
    my @unshift = ();

    prepend @unshift, (1, 2, 3);
    is(+@unshift, 3, 'we have 3 elements in the array');
    is(@unshift[0], 1, 'got the expected element');
    is(@unshift[1], 2, 'got the expected element');
    is(@unshift[2], 3, 'got the expected element');

    my @val2 = (4, 5);
    prepend @unshift, @val2;
    is(+@unshift, 5, 'we have 5 elements in the array');
    is(@unshift[0], 4, 'got the expected element');
    is(@unshift[1], 5, 'got the expected element');
    is(@unshift[2], 1, 'got the expected element');
    is(@unshift[3], 2, 'got the expected element');
    is(@unshift[4], 3, 'got the expected element');

    prepend @unshift, 6, 7, 8;
    is(+@unshift, 8, 'we have 8 elements in the array');
    is(@unshift[0], 6, 'got the expected element');
    is(@unshift[1], 7, 'got the expected element');
    is(@unshift[2], 8, 'got the expected element');
    is(@unshift[3], 4, 'got the expected element');
    is(@unshift[4], 5, 'got the expected element');
    is(@unshift[5], 1, 'got the expected element');
    is(@unshift[6], 2, 'got the expected element');
    is(@unshift[7], 3, 'got the expected element');
}

{
    my @unshift = ();

    unshift @unshift, (1, 2, 3);
    is(+@unshift, 1, 'we have 1 element in the array');
    is(@unshift[0][0], 1, 'got the expected element');
    is(@unshift[0][1], 2, 'got the expected element');
    is(@unshift[0][2], 3, 'got the expected element');

    my @val2 = (4, 5);
    unshift @unshift, @val2;
    is(+@unshift, 2, 'we have 5 elements in the array');
    is(@unshift[0][0], 4, 'got the expected element');
    is(@unshift[0][1], 5, 'got the expected element');
    is(@unshift[1][0], 1, 'got the expected element');
    is(@unshift[1][1], 2, 'got the expected element');
    is(@unshift[1][2], 3, 'got the expected element');

    unshift @unshift, [6, 7, 8];
    is(+@unshift, 3, 'we have 8 elements in the array');
    is(@unshift[0][0], 6, 'got the expected element');
    is(@unshift[0][1], 7, 'got the expected element');
    is(@unshift[0][2], 8, 'got the expected element');
    is(@unshift[1][0], 4, 'got the expected element');
    is(@unshift[1][1], 5, 'got the expected element');
    is(@unshift[2][0], 1, 'got the expected element');
    is(@unshift[2][1], 2, 'got the expected element');
    is(@unshift[2][2], 3, 'got the expected element');
}

# now for the unshift() on an uninitialized array issue
{
    my @unshift;

    unshift @unshift, 42;
    is(+@unshift, 1, 'we have 1 element in the array');
    is(@unshift[0], 42, 'got the element expected');

    unshift @unshift, 2000;
    is(+@unshift, 2, 'we have 2 elements in the array');
    is(@unshift[0], 2000, 'got the element expected');
    is(@unshift[1], 42, 'got the element expected');
}

# testing some edge cases
{
    my @unshift = 0 ... 5;
    is(+@unshift, 6, 'starting length is 6');

    unshift(@unshift);
    is(+@unshift, 6, 'length is still 6');

    @unshift.push();
    is(+@unshift, 6, 'length is still 6');
}

# testing some error cases
{
    throws-like 'unshift()', X::TypeCheck::Argument, 'unshift() requires arguments';
    throws-like '42.unshift(3)', X::Multi::NoMatch, '.unshift should not work on scalars';
}

# Push with Inf arrays (waiting on answers to perl6-compiler email)
# {
#     my @unshift = 1 .. Inf;
#     # best not to uncomment this it just go on forever
#     todo_throws_ok { 'unshift @unshift, 10' }, '?? what should this error message be ??', 'cannot unshift onto a Inf array';
# }

# RT #69548
{
     my $x = 1;
     my @a = ();
     unshift @a, $x;
     ++$x;

     is @a[0], 1, 'New element created by unshift(@a, $x) isn\'t affected by changes to $x';
}

# RT #69548
{
    my $x = 1;
    my @a = ();
    unshift @a, $x;
    ++@a[0];

    is $x, 1, '$x isn\'t affected by changes to new element created by unshift(@a, $x)';
}

{
    my @a = <b c>;
    @a.unshift(0);
    is @a.join(','), '0,b,c', 'can unshift an element that boolifies to False';
}


# RT #119061
#?niecza todo "https://github.com/sorear/niecza/issues/184"
{
    my Int @a;
    throws-like '@a.unshift: "a"', X::TypeCheck,
        "cannot unshift strings onto in Int array";
}

# vim: ft=perl6