File: 10-basics.t

package info (click to toggle)
libfile-map-perl 0.71-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 784 kB
  • sloc: perl: 365; ansic: 97; makefile: 3
file content (79 lines) | stat: -rw-r--r-- 2,103 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!perl

use strict;
use warnings;
use File::Map qw/:map lock_map advise/;
use IO::Handle;
use Scalar::Util qw/tainted/;
use Test::More tests => 24;
use Test::Warnings;
use Test::Fatal qw/lives_ok/;

open my $self, '<:raw', $0 or die "Couldn't open self: $!";
my $slurped = do { local $/; <$self> };

{
	my $mmaped;
	lives_ok { map_handle($mmaped, $self, '<') } "map succeeded";
	ok(defined $mmaped,               "mmaped is defined");
	ok( length $mmaped > 300,         "length of mmaped is big enough");
	ok($mmaped eq $slurped,           "slurped is mmaped");
	is($mmaped, $slurped,             "slurped is mmaped");
	lives_ok { advise($mmaped, "normal") } "Advising";
	ok(!tainted($mmaped), 'map is not tainted');
}

close $self or die "Couldn't close self: $!";

{
	my $mmaped;
	lives_ok { map_file($mmaped, $0) } "map succeeded";
	ok(defined $mmaped,          "mmaped is defined");
	ok( length $mmaped > 300,    "length of mmaped is big enough");
	is($mmaped, $slurped,        "slurped is mmaped");

	lives_ok { unmap($mmaped) } "Unmapping";
}

{
	my %hash;
	lives_ok { map_file($hash{map}, $0) } 'mapping self into a hash';
	is($hash{map}, $slurped, 'Correctly autovifivies hash entry');
}

open my $copy, "+<:raw", undef or die "Couldn't create tempfile: $!";
$copy->autoflush(1);
print $copy $slurped;

{
	my $mmaped;
	lives_ok { map_handle($mmaped, $copy, '+<') } "map succeeded";
	ok(defined $mmaped,                  "mmaped is defined");
	ok( length $mmaped > 300,            "length of mmaped is big enough");
	is($mmaped, $slurped,                "slurped is mmaped");

	s/e/a/g for ($mmaped, $slurped);

	is($mmaped, $slurped, "slurped is mmaped after translation");

	$mmaped  =~ tr/r/t/ ;
	$slurped =~ tr/r/t/;

	is($mmaped, $slurped, "Translated");

	{
		no warnings 'substr';
		$mmaped = 1;
		like($mmaped, qr/^1/, '$mmaped should be like 1');

		my $ref = \$slurped;
		my $strval = "$ref";
		$mmaped = $ref;
		like($mmaped, qr/^\Q$strval\E/, '$mmaped should handle reference assignment');
	}
}


{
	lives_ok { map_anonymous(my $mmap, 4096) } "mapped an anonymous piece of memory";
}