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