summaryrefslogtreecommitdiff
path: root/lib/Internals.t
blob: d3fce9c1c810c240cfbd7d6613a75a01419480a7 (plain)
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
#!/usr/bin/perl -Tw

BEGIN {
    if( $ENV{PERL_CORE} ) {
        @INC = '../lib';
        chdir 't';
    }
}

use Test::More tests => 78;

my $ro_err = qr/^Modification of a read-only value attempted/;

### Read-only scalar
my $foo;

ok( !Internals::SvREADONLY $foo );
$foo = 3;
is($foo, 3);

ok(  Internals::SvREADONLY $foo, 1 );
ok(  Internals::SvREADONLY $foo );
eval { $foo = 'foo'; };
like($@, $ro_err, q/Can't modify read-only scalar/);
eval { undef($foo); };
like($@, $ro_err, q/Can't undef read-only scalar/);
is($foo, 3);

ok( !Internals::SvREADONLY $foo, 0 );
ok( !Internals::SvREADONLY $foo );
$foo = 'foo';
is($foo, 'foo');

### Read-only array
my @foo;

ok( !Internals::SvREADONLY @foo );
@foo = (1..3);
is(scalar(@foo), 3);
is($foo[2], 3);

ok(  Internals::SvREADONLY @foo, 1 );
ok(  Internals::SvREADONLY @foo );
eval { undef(@foo); };
like($@, $ro_err, q/Can't undef read-only array/);
eval { delete($foo[2]); };
like($@, $ro_err, q/Can't delete from read-only array/);
eval { shift(@foo); };
like($@, $ro_err, q/Can't shift read-only array/);
eval { push(@foo, 'bork'); };
like($@, $ro_err, q/Can't push onto read-only array/);
eval { @foo = qw/foo bar/; };
like($@, $ro_err, q/Can't reassign read-only array/);

ok( !Internals::SvREADONLY @foo, 0 );
ok( !Internals::SvREADONLY @foo );
eval { @foo = qw/foo bar/; };
is(scalar(@foo), 2);
is($foo[1], 'bar');

### Read-only array element

ok( !Internals::SvREADONLY $foo[2] );
$foo[2] = 'baz';
is($foo[2], 'baz');

ok(  Internals::SvREADONLY $foo[2], 1 );
ok(  Internals::SvREADONLY $foo[2] );

$foo[0] = 99;
is($foo[0], 99, 'Rest of array still modifiable');

shift(@foo);
ok(  Internals::SvREADONLY $foo[1] );
eval { $foo[1] = 'bork'; };
like($@, $ro_err, 'Read-only array element moved');
is($foo[1], 'baz');

ok( !Internals::SvREADONLY $foo[2] );
$foo[2] = 'qux';
is($foo[2], 'qux');

unshift(@foo, 'foo');
ok( !Internals::SvREADONLY $foo[1] );
ok(  Internals::SvREADONLY $foo[2] );

eval { $foo[2] = 86; };
like($@, $ro_err, q/Can't modify read-only array element/);
eval { undef($foo[2]); };
like($@, $ro_err, q/Can't undef read-only array element/);
TODO: {
    local $TODO = 'Due to restricted hashes implementation';
    eval { delete($foo[2]); };
    like($@, $ro_err, q/Can't delete read-only array element/);
}

ok( !Internals::SvREADONLY $foo[2], 0 );
ok( !Internals::SvREADONLY $foo[2] );
$foo[2] = 'xyzzy';
is($foo[2], 'xyzzy');

### Read-only hash
my %foo;

ok( !Internals::SvREADONLY %foo );
%foo = ('foo' => 1, 2 => 'bar');
is(scalar(keys(%foo)), 2);
is($foo{'foo'}, 1);

ok(  Internals::SvREADONLY %foo, 1 );
ok(  Internals::SvREADONLY %foo );
eval { undef(%foo); };
like($@, $ro_err, q/Can't undef read-only hash/);
TODO: {
    local $TODO = 'Due to restricted hashes implementation';
    eval { %foo = ('ping' => 'pong'); };
    like($@, $ro_err, q/Can't modify read-only hash/);
}
eval { $foo{'baz'} = 123; };
like($@, qr/Attempt to access disallowed key/, q/Can't add to a read-only hash/);

# These ops are allow for Hash::Util functionality
$foo{2} = 'qux';
is($foo{2}, 'qux', 'Can modify elements in a read-only hash');
my $qux = delete($foo{2});
ok(! exists($foo{2}), 'Can delete keys from a read-only hash');
is($qux, 'qux');
$foo{2} = 2;
is($foo{2}, 2, 'Can add back deleted keys in a read-only hash');

ok( !Internals::SvREADONLY %foo, 0 );
ok( !Internals::SvREADONLY %foo );

### Read-only hash values

ok( !Internals::SvREADONLY $foo{foo} );
$foo{'foo'} = 'bar';
is($foo{'foo'}, 'bar');

ok(  Internals::SvREADONLY $foo{foo}, 1 );
ok(  Internals::SvREADONLY $foo{foo} );
eval { $foo{'foo'} = 88; };
like($@, $ro_err, q/Can't modify a read-only hash value/);
eval { undef($foo{'foo'}); };
like($@, $ro_err, q/Can't undef a read-only hash value/);
my $bar = delete($foo{'foo'});
ok(! exists($foo{'foo'}), 'Can delete a read-only hash value');
is($bar, 'bar');

ok( !Internals::SvREADONLY $foo{foo}, 0 );
ok( !Internals::SvREADONLY $foo{foo} );

is(  Internals::SvREFCNT($foo), 1 );
{
    my $bar = \$foo;
    is(  Internals::SvREFCNT($foo), 2 );
    is(  Internals::SvREFCNT($bar), 1 );
}
is(  Internals::SvREFCNT($foo), 1 );

is(  Internals::SvREFCNT(@foo), 1 );
is(  Internals::SvREFCNT($foo[2]), 1 );
is(  Internals::SvREFCNT(%foo), 1 );
is(  Internals::SvREFCNT($foo{foo}), 1 );

is(  Internals::SvREFCNT($foo, 2), 2, "update ref count");
is(  Internals::SvREFCNT($foo), 2, "check we got the stored value");

# the reference count is a U16, but was returned as an IV resulting in
# different values between 32 and 64-bit builds
my $big_count = 0xFFFFFFF0; # -16 32-bit signed
is( Internals::SvREFCNT($foo, $big_count), $big_count,
    "set reference count unsigned");
is( Internals::SvREFCNT($foo), $big_count, "reference count unsigned");

Internals::SvREFCNT($foo, 1 );