Fix memory corruption - use a stack temp-value for the artificial string
[p5sagit/Devel-PeekPoke.git] / t / 03torture.t
CommitLineData
36e403bb 1use strict;
2use warnings;
3
4use Test::More;
5
6use Devel::PeekPoke qw/peek poke peek_address poke_address/;
7use Devel::PeekPoke::Constants qw/PTR_SIZE PTR_PACK_TYPE/;
8
9my $str = 'for mutilation and mayhem';
10my $len = length($str);
11my $str_pv_addr = unpack(PTR_PACK_TYPE, pack('p', $str) );
12
13is( peek($str_pv_addr, $len + 1), $str . "\0", 'peek as expected (with NUL termination)' );
14
15for my $poke_size (2 .. $len) {
16 my $replace_chunk = 'a' . ( '0' x ($poke_size-1) );
17 for my $poke_start ( 0 .. ($len - $poke_size) ) {
18 $replace_chunk++;
19
20 my $expecting = $str;
21 substr($expecting, $poke_start, $poke_size, $replace_chunk);
22
23 poke($str_pv_addr+$poke_start, $replace_chunk);
24 is($str, $expecting, 'String matches expectation after poke');
25 }
26}
27
28done_testing;