summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/dist/threads-shared/t/clone.t
blob: fcb3e71d93a48a64561441303439554551f605eb (plain) (blame)
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
use strict;
use warnings;

BEGIN {
    use Config;
    if (! $Config{'useithreads'}) {
        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
        exit(0);
    }
}

use ExtUtils::testlib;

sub ok {
    my ($id, $ok, $name) = @_;

    # You have to do it this way or VMS will get confused.
    if ($ok) {
        print("ok $id - $name\n");
    } else {
        print("not ok $id - $name\n");
        printf("# Failed test at line %d\n", (caller)[2]);
    }

    return ($ok);
}

BEGIN {
    $| = 1;
    print("1..40\n");   ### Number of tests that will be run ###
};

my $test = 1;

use threads;
use threads::shared;
ok($test++, 1, 'Loaded');

### Start of Testing ###

{
    my $x = shared_clone(14);
    ok($test++, $x == 14, 'number');

    $x = shared_clone('test');
    ok($test++, $x eq 'test', 'string');
}

{
    my %hsh = ('foo' => 2);
    eval {
        my $x = shared_clone(%hsh);
    };
    ok($test++, $@ =~ /Usage:/, '1 arg');

    threads->create(sub {})->join();  # Hide leaks, etc.
}

{
    my $x = 'test';
    my $foo :shared = shared_clone($x);
    ok($test++, $foo eq 'test', 'cloned string');

    $foo = shared_clone(\$x);
    ok($test++, $$foo eq 'test', 'cloned scalar ref');

    threads->create(sub {
        ok($test++, $$foo eq 'test', 'cloned scalar ref in thread');
    })->join();

    $test++;
}

{
    my $foo :shared;
    $foo = shared_clone(\$foo);
    ok($test++, ref($foo) eq 'REF', 'Circular ref typ');
    ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref');

    threads->create(sub {
        ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref in thread');

        my ($x, $y, $z);
        $x = \$y; $y = \$z; $z = \$x;
        $foo = shared_clone($x);
    })->join();

    $test++;

    ok($test++, threads::shared::_id($$foo) == threads::shared::_id($$$$$foo),
                    'Cloned circular refs from thread');
}

{
    my @ary = (qw/foo bar baz/);
    my $ary = shared_clone(\@ary);

    ok($test++, $ary->[1] eq 'bar', 'Cloned array');
    $ary->[1] = 99;
    ok($test++, $ary->[1] == 99, 'Clone mod');
    ok($test++, $ary[1] eq 'bar', 'Original array');

    threads->create(sub {
        ok($test++, $ary->[1] == 99, 'Clone mod in thread');

        $ary[1] = 'bork';
        $ary->[1] = 'thread';
    })->join();

    $test++;

    ok($test++, $ary->[1] eq 'thread', 'Clone mod from thread');
    ok($test++, $ary[1] eq 'bar', 'Original array');
}

{
    my $hsh :shared = shared_clone({'foo' => [qw/foo bar baz/]});
    ok($test++, is_shared($hsh), 'Shared hash ref');
    ok($test++, is_shared($hsh->{'foo'}), 'Shared hash ref elem');
    ok($test++, $$hsh{'foo'}[1] eq 'bar', 'Cloned structure');
}

{
    my $obj = \do { my $bork = 99; };
    bless($obj, 'Bork');
    Internals::SvREADONLY($$obj, 1) if ($] >= 5.008003);

    my $bork = shared_clone($obj);
    ok($test++, $$bork == 99, 'cloned scalar ref object');
    ok($test++, ($] < 5.008003) || Internals::SvREADONLY($$bork), 'read-only');
    ok($test++, ref($bork) eq 'Bork', 'Object class');

    threads->create(sub {
        ok($test++, $$bork == 99, 'cloned scalar ref object in thread');
        ok($test++, ($] < 5.008003) || Internals::SvREADONLY($$bork), 'read-only');
        ok($test++, ref($bork) eq 'Bork', 'Object class');
    })->join();

    $test += 3;
}

{
    my $scalar = 'zip';

    my $obj = {
        'ary' => [ 1, 'foo', [ 86 ], { 'bar' => [ 'baz' ] } ],
        'ref' => \$scalar,
    };

    $obj->{'self'} = $obj;

    bless($obj, 'Foo');

    my $copy :shared;

    threads->create(sub {
        $copy = shared_clone($obj);

        ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
        ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
        ok($test++, is_shared($copy->{'ary'}->[2]), 'Shared element in cloned obj');
    })->join();

    $test += 3;

    ok($test++, ref($copy) eq 'Foo', 'Obj cloned by thread');
    ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
    ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
    ok($test++, $copy->{'ary'}->[3]->{'bar'}->[0] eq 'baz', 'Deeply cloned');
    ok($test++, ref($copy) eq 'Foo', 'Cloned object class');
}

{
    my $foo = \*STDIN;
    my $copy :shared;
    eval {
        $copy = shared_clone($foo);
    };
    ok($test++, $@ =~ /Unsupported/, 'Cannot clone GLOB - fatal');
    ok($test++, ! defined($copy), 'Nothing cloned');

    $threads::shared::clone_warn = 1;
    my $warn;
    $SIG{'__WARN__'} = sub { $warn = shift; };
    $copy = shared_clone($foo);
    ok($test++, $warn =~ /Unsupported/, 'Cannot clone GLOB - warning');
    ok($test++, ! defined($copy), 'Nothing cloned');

    $threads::shared::clone_warn = 0;
    undef($warn);
    $copy = shared_clone($foo);
    ok($test++, ! defined($warn), 'Cannot clone GLOB - silent');
    ok($test++, ! defined($copy), 'Nothing cloned');
}

exit(0);

# EOF