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