This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
better handle freeing of code blocks in /(?{...})/
[perl5.git] / t / op / vec.t
CommitLineData
a687059c
LW
1#!./perl
2
60ab2483
SP
3BEGIN {
4 chdir 't' if -d 't';
6b0e82e1 5 require './test.pl';
43ece5b1 6 set_up_inc('../lib');
60ab2483
SP
7}
8
33a10326 9plan( tests => 37 );
a687059c 10
210db7fc 11
60ab2483 12is(vec($foo,0,1), 0);
9f621bb0 13is(length($foo), undef);
a687059c 14vec($foo,0,1) = 1;
60ab2483
SP
15is(length($foo), 1);
16is(unpack('C',$foo), 1);
17is(vec($foo,0,1), 1);
a687059c 18
60ab2483 19is(vec($foo,20,1), 0);
a687059c 20vec($foo,20,1) = 1;
60ab2483
SP
21is(vec($foo,20,1), 1);
22is(length($foo), 3);
23is(vec($foo,1,8), 0);
a687059c 24vec($foo,1,8) = 0xf1;
60ab2483
SP
25is(vec($foo,1,8), 0xf1);
26is((unpack('C',substr($foo,1,1)) & 255), 0xf1);
27is(vec($foo,2,4), 1);;
28is(vec($foo,3,4), 15);
deb3007b 29vec($Vec, 0, 32) = 0xbaddacab;
60ab2483
SP
30is($Vec, "\xba\xdd\xac\xab");
31is(vec($Vec, 0, 32), 3135089835);
a687059c 32
4ebbc975
GS
33# ensure vec() handles numericalness correctly
34$foo = $bar = $baz = 0;
35vec($foo = 0,0,1) = 1;
36vec($bar = 0,1,1) = 1;
37$baz = $foo | $bar;
60ab2483
SP
38ok($foo eq "1" && $foo == 1);
39ok($bar eq "2" && $bar == 2);
40ok("$foo $bar $baz" eq "1 2 3");
fe58ced6
MG
41
42# error cases
43
44$x = eval { vec $foo, 0, 3 };
5a2b2173 45like($@, qr/^Illegal number of bits in vec/);
60ab2483 46$@ = undef;
fe58ced6 47$x = eval { vec $foo, 0, 0 };
5a2b2173 48like($@, qr/^Illegal number of bits in vec/);
60ab2483 49$@ = undef;
fe58ced6 50$x = eval { vec $foo, 0, -13 };
5a2b2173 51like($@, qr/^Illegal number of bits in vec/);
60ab2483 52$@ = undef;
fe58ced6 53$x = eval { vec($foo, -1, 4) = 2 };
5a2b2173 54like($@, qr/^Negative offset to vec in lvalue context/);
60ab2483
SP
55$@ = undef;
56ok(! vec('abcd', 7, 8));
246fae53
MG
57
58# UTF8
59# N.B. currently curiously coded to circumvent bugs elswhere in UTF8 handling
60
61$foo = "\x{100}" . "\xff\xfe";
62$x = substr $foo, 1;
60ab2483
SP
63is(vec($x, 0, 8), 255);
64$@ = undef;
246fae53 65eval { vec($foo, 1, 8) };
60ab2483
SP
66ok(! $@);
67$@ = undef;
246fae53 68eval { vec($foo, 1, 8) = 13 };
60ab2483 69ok(! $@);
83bcbc61 70if ($::IS_EBCDIC) {
60ab2483 71 is($foo, "\x8c\x0d\xff\x8a\x69");
210db7fc
PP
72}
73else {
60ab2483 74 is($foo, "\xc4\x0d\xc3\xbf\xc3\xbe");
210db7fc 75}
33b45480 76$foo = "\x{100}" . "\xff\xfe";
246fae53
MG
77$x = substr $foo, 1;
78vec($x, 2, 4) = 7;
60ab2483 79is($x, "\xff\xf7");
246fae53
MG
80
81# mixed magic
82
83$foo = "\x61\x62\x63\x64\x65\x66";
60ab2483 84is(vec(substr($foo, 2, 2), 0, 16), 25444);
246fae53 85vec(substr($foo, 1,3), 5, 4) = 3;
60ab2483 86is($foo, "\x61\x62\x63\x34\x65\x66");
24aef97f
HS
87
88# A variation of [perl #20933]
89{
90 my $s = "";
91 vec($s, 0, 1) = 0;
92 vec($s, 1, 1) = 1;
93 my @r;
94 $r[$_] = \ vec $s, $_, 1 for (0, 1);
60ab2483 95 ok(!(${ $r[0] } != 0 || ${ $r[1] } != 1));
24aef97f 96}
0607bed5
EB
97
98
99my $destroyed;
100{ package Class; DESTROY { ++$destroyed; } }
101
102$destroyed = 0;
103{
104 my $x = '';
105 vec($x,0,1) = 0;
106 $x = bless({}, 'Class');
107}
2154eca7 108is($destroyed, 1, 'Timely scalar destruction with lvalue vec');
ee3818ca 109
2484f8db
FC
110use constant roref => \1;
111eval { for (roref) { vec($_,0,1) = 1 } };
ee3818ca
FC
112like($@, qr/^Modification of a read-only value attempted at /,
113 'err msg when modifying read-only refs');
fc9668ae
DM
114
115
116{
117 # downgradeable utf8 strings should be downgraded before accessing
118 # the byte string.
119 # See the p5p thread with Message-ID:
120 # <CAMx+QJ6SAv05nmpnc7bmp0Wo+sjcx=ssxCcE-P_PZ8HDuCQd9A@mail.gmail.com>
121
122
123 my $x = substr "\x{100}\xff\xfe", 1; # a utf8 string with all ords < 256
124 my $v;
125 $v = vec($x, 0, 8);
126 is($v, 255, "downgraded utf8 try 1");
127 $v = vec($x, 0, 8);
128 is($v, 255, "downgraded utf8 try 2");
129}
33a10326
FC
130
131# [perl #128260] assertion failure with \vec %h, \vec @h
132{
133 my %h = 1..100;
134 my @a = 1..100;
135 is ${\vec %h, 0, 1}, vec(scalar %h, 0, 1), '\vec %h';
136 is ${\vec @a, 0, 1}, vec(scalar @a, 0, 1), '\vec @a';
137}