Make D::PP threadsafe with tests
[p5sagit/Devel-PeekPoke.git] / t / 03torture.t
CommitLineData
36e403bb 1use strict;
2use warnings;
3
9b733ba1 4# T::M appears to leak, emit the TAP by hand
5#use Test::More 'no_plan';
6
9b733ba1 7sub is {
24538b08 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'};
9b733ba1 20}
36e403bb 21
22use Devel::PeekPoke qw/peek poke peek_address poke_address/;
23use Devel::PeekPoke::Constants qw/PTR_SIZE PTR_PACK_TYPE/;
24
25my $str = 'for mutilation and mayhem';
26my $len = length($str);
27my $str_pv_addr = unpack(PTR_PACK_TYPE, pack('p', $str) );
28
29is( peek($str_pv_addr, $len + 1), $str . "\0", 'peek as expected (with NUL termination)' );
30
24538b08 31for (1 .. ($ENV{AUTOMATED_TESTING} ? 200 : 20 ) ) {
9b733ba1 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++;
36e403bb 36
9b733ba1 37 my $expecting = $str;
38 substr($expecting, $poke_start, $poke_size, $replace_chunk);
36e403bb 39
9b733ba1 40 poke($str_pv_addr+$poke_start, $replace_chunk);
41 is($str, $expecting, 'String matches expectation after poke');
42 }
36e403bb 43 }
44}
45
24538b08 46if ($ENV{AUTOMATED_TESTING} and ! $INC{'threads.pm'}) {
9b733ba1 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
24538b08 58print "1..$::TEST_COUNT\n" unless $INC{'threads.pm'};