xref: /openbsd-src/gnu/usr.bin/perl/dist/threads-shared/t/hv_simple.t (revision b39c515898423c8d899e35282f4b395f7cad3298)
1*b39c5158Smillertuse strict;
2*b39c5158Smillertuse warnings;
3*b39c5158Smillert
4*b39c5158SmillertBEGIN {
5*b39c5158Smillert    use Config;
6*b39c5158Smillert    if (! $Config{'useithreads'}) {
7*b39c5158Smillert        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8*b39c5158Smillert        exit(0);
9*b39c5158Smillert    }
10*b39c5158Smillert}
11*b39c5158Smillert
12*b39c5158Smillertuse ExtUtils::testlib;
13*b39c5158Smillert
14*b39c5158Smillertsub ok {
15*b39c5158Smillert    my ($id, $ok, $name) = @_;
16*b39c5158Smillert
17*b39c5158Smillert    # You have to do it this way or VMS will get confused.
18*b39c5158Smillert    if ($ok) {
19*b39c5158Smillert        print("ok $id - $name\n");
20*b39c5158Smillert    } else {
21*b39c5158Smillert        print("not ok $id - $name\n");
22*b39c5158Smillert        printf("# Failed test at line %d\n", (caller)[2]);
23*b39c5158Smillert    }
24*b39c5158Smillert
25*b39c5158Smillert    return ($ok);
26*b39c5158Smillert}
27*b39c5158Smillert
28*b39c5158SmillertBEGIN {
29*b39c5158Smillert    $| = 1;
30*b39c5158Smillert    print("1..16\n");   ### Number of tests that will be run ###
31*b39c5158Smillert};
32*b39c5158Smillert
33*b39c5158Smillertuse threads;
34*b39c5158Smillertuse threads::shared;
35*b39c5158Smillertok(1, 1, 'Loaded');
36*b39c5158Smillert
37*b39c5158Smillert### Start of Testing ###
38*b39c5158Smillert
39*b39c5158Smillertmy %hash;
40*b39c5158Smillertshare(%hash);
41*b39c5158Smillert$hash{"foo"} = "bar";
42*b39c5158Smillertok(2,$hash{"foo"} eq "bar","Check hash get");
43*b39c5158Smillertthreads->create(sub { $hash{"bar"} = "thread1"})->join();
44*b39c5158Smillertthreads->create(sub { ok(3,$hash{"bar"} eq "thread1", "Check thread get and write")})->join();
45*b39c5158Smillert{
46*b39c5158Smillert    my $foo = delete($hash{"bar"});
47*b39c5158Smillert    ok(4, $foo eq "thread1", "Check delete, want 'thread1' got '$foo'");
48*b39c5158Smillert    $foo = delete($hash{"bar"});
49*b39c5158Smillert    ok(5, !defined $foo, "Check delete on empty value");
50*b39c5158Smillert}
51*b39c5158Smillertok(6, keys %hash == 1, "Check keys");
52*b39c5158Smillert$hash{"1"} = 1;
53*b39c5158Smillert$hash{"2"} = 2;
54*b39c5158Smillert$hash{"3"} = 3;
55*b39c5158Smillertok(7, keys %hash == 4, "Check keys");
56*b39c5158Smillertok(8, exists($hash{"1"}), "Exist on existing key");
57*b39c5158Smillertok(9, !exists($hash{"4"}), "Exists on non existing key");
58*b39c5158Smillertmy %seen;
59*b39c5158Smillertforeach my $key ( keys %hash) {
60*b39c5158Smillert    $seen{$key}++;
61*b39c5158Smillert}
62*b39c5158Smillertok(10, $seen{1} == 1, "Keys..");
63*b39c5158Smillertok(11, $seen{2} == 1, "Keys..");
64*b39c5158Smillertok(12, $seen{3} == 1, "Keys..");
65*b39c5158Smillertok(13, $seen{"foo"} == 1, "Keys..");
66*b39c5158Smillert
67*b39c5158Smillert# bugid #24407: the stringification of the numeric 1 got allocated to the
68*b39c5158Smillert# wrong thread memory pool, which crashes on Windows.
69*b39c5158Smillertok(14, exists $hash{1}, "Check numeric key");
70*b39c5158Smillert
71*b39c5158Smillertthreads->create(sub { %hash = () })->join();
72*b39c5158Smillertok(15, keys %hash == 0, "Check clear");
73*b39c5158Smillert
74*b39c5158Smillertok(16, is_shared(%hash), "Check for sharing");
75*b39c5158Smillert
76*b39c5158Smillertexit(0);
77*b39c5158Smillert
78*b39c5158Smillert# EOF
79