Make D::PP threadsafe with tests
[p5sagit/Devel-PeekPoke.git] / t / 03torture.t
1 use strict;
2 use warnings;
3
4 # T::M appears to leak, emit the TAP by hand
5 #use Test::More 'no_plan';
6
7 sub is {
8   my $str = $_[0] eq $_[1] ? 'ok' : 'not ok';
9
10   {
11     lock($::TEST_COUNT);
12     $::TEST_COUNT++;
13     printf STDOUT ("%s %u - %s\n",
14       $str,
15       $::TEST_COUNT,
16       $_[2] || '',
17     );
18   }
19   threads->yield if $INC{'threads.pm'};
20 }
21
22 use Devel::PeekPoke qw/peek poke peek_address poke_address/;
23 use Devel::PeekPoke::Constants qw/PTR_SIZE PTR_PACK_TYPE/;
24
25 my $str = 'for mutilation and mayhem';
26 my $len = length($str);
27 my $str_pv_addr = unpack(PTR_PACK_TYPE, pack('p', $str) );
28
29 is( peek($str_pv_addr, $len + 1), $str . "\0", 'peek as expected (with NUL termination)' );
30
31 for (1 .. ($ENV{AUTOMATED_TESTING} ? 200 : 20 ) ) {
32   for my $poke_size (2 .. $len) {
33     my $replace_chunk = 'a' . ( '0' x ($poke_size-1) );
34     for my $poke_start ( 0 .. ($len - $poke_size) ) {
35       $replace_chunk++;
36
37       my $expecting = $str;
38       substr($expecting, $poke_start, $poke_size, $replace_chunk);
39
40       poke($str_pv_addr+$poke_start, $replace_chunk);
41       is($str, $expecting, 'String matches expectation after poke');
42     }
43   }
44 }
45
46 if ($ENV{AUTOMATED_TESTING} and ! $INC{'threads.pm'}) {
47   my $vsz;
48   if (-f "/proc/$$/stat") {
49     my $proc_stat = do { local (@ARGV, $/) = "/proc/$$/stat"; <> };
50     ($vsz) = map { $_ / 1024 }
51       (split (/\s+/, $proc_stat))[-22];  # go backwards because the %s of the procname can contain anything
52   }
53
54   printf STDERR "#\n# VSIZE:%dKiB\n", $vsz
55     if $vsz;
56 }
57
58 print "1..$::TEST_COUNT\n" unless $INC{'threads.pm'};