/
increment.t
132 lines (103 loc) 路 3.24 KB
/
increment.t
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
use v6;
use Test;
plan 37;
#L<S03/Autoincrement precedence>
=begin description
Verify that autoincrement/autodecrement work properly.
(Overflow cases are handled in S03-operators/overflow.t)
=end description
{
my $a = Mu;
is($a++, 0, 'Mu++ == 0');
#?niecza todo '#88'
$a = Mu;
is $a--, 0 , 'postincrement (& decrement) returns 0 the first time';
$a = 'z';
is($a++, 'z', 'magical ++ should not be numified');
isa-ok($a, "Str", "it isa Str");
is $a, 'aa', 'magical ++ is carrying properly';
}
my %a = ('a' => 1);
%a{"a"}++;
is(%a{'a'}, 2, "hash key");
my %b = ('b' => 1);
my $var = 'b';
%b{$var}++;
is(%b{$var}, 2, "hash key via var");
my @a = (1);
@a[0]++;
is(@a[0], 2, "array elem");
my @b = (1);
my $moo = 0;
@b[$moo]++;
is(@b[$moo], 2, "array elem via var");
is($moo, 0, "var was not touched");
# Test that the expression to increment will only be evaluated once.
{
my $was_in_foo;
my sub foo () { $was_in_foo++; 0 };
my @array = (42);
is(++@array[+foo()], 43, "++ evaluates the expression to increment only once (1)");
is($was_in_foo, 1, "++ evaluates the expression to increment only once (2)");
}
# Test case courtesy of Limbic_Region
{
my $curr = 4;
my @array = 1..5;
is @array[$curr], 5, "postincrements in array subscripts work";
@array[ --$curr ]++;
is $curr, 3, "postincrements in array subscripts work";
is @array[$curr], 5, "postincrements in array subscripts work";
}
# test incrementing literals
# all of those can be detected at compile time, so use eval_dies_ok here
{
eval_dies_ok ' 4++ ', "can't postincrement a literal number";
eval_dies_ok ' ++4 ', "can't preincrement a literal number";
eval_dies_ok ' 4-- ', "can't postdecrement a literal number";
eval_dies_ok ' --4 ', "can't predecrement a literal number";
eval_dies_ok ' "x"++ ', "can't postincrement a literal string";
eval_dies_ok ' ++"x" ', "can't preincrement a literal string";
eval_dies_ok ' "x"-- ', "can't postdecrement a literal string";
eval_dies_ok ' --"x" ', "can't predecrement a literal string";
}
# this used to be a rakudo regression
{
my $x = 2.0;
$x += 1;
ok $x == 3.0, 'can add Int to Rat with +=';
my Rat $y = 2.0;
$y += 1;
ok $y == 3.0, 'can add Int to Rat with += and type constraint';
}
{
my $x = 2.0.Num;
$x += 1;
ok $x == 3.0, 'can add Int to Num with +=';
my Num $y = 2.0.Num;
$y += 1;
ok $y == 3.0, 'can add Int to Num with += and type constraint';
}
# also a Rakudo regression
{
my $x = Bool::False;
is ++$x, Bool::True, '++ on False works';
$x = Bool::False;
is $x.succ, Bool::True, '.succ on False works';
$x = Bool::True;
is --$x, Bool::False, '-- on True works';
$x = Bool::True;
is $x.pred, Bool::False, '.succ on False works';
}
# RT #74912
#?niecza todo 'Works fine in niecza...'
eval_dies_ok 'my $x = 0; ++++$x',
'can not double-increment, because the return value is not a container';
{
is state $i++, 0, '++ is allowed on declarator';
is $i, 1, '...and actually increments variable';
is my $j.++, 0, '.++ is allowed on declarator';
is $j, 1, '...and actually increments variable';
is my $k.defined, False, 'method is allowed on declarator';
}
# vim: ft=perl6