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