summaryrefslogtreecommitdiff
path: root/dist/Safe/t/safesort.t
blob: 383ad1ab26a3b4877a89204bebc31d8915d0df3f (plain)
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
#!./perl -w
$|=1;
BEGIN {
    if($ENV{PERL_CORE}) {
	chdir 't' if -d 't';
	@INC = '../lib';
    }
    require Config; import Config;
    if ($Config{'extensions'} !~ /\bOpcode\b/ && $Config{'osname'} ne 'VMS') {
        print "1..0\n";
        exit 0;
    }
}

use Safe 1.00;
use Test::More tests => 4;

my $safe = Safe->new('PLPerl');
$safe->permit_only(qw(:default sort));

my $func = $safe->reval(<<'EOS');

    # uses quotes in { "$a" <=> $b } to avoid the optimizer replacing the block
    # with a hardwired comparison
    { package Pkg; sub p_sort { return sort { "$a" <=> $b } qw(2 1 3); } }
                   sub l_sort { return sort { "$a" <=> $b } qw(2 1 3); }

    return sub { return join(",",l_sort()), join(",",Pkg::p_sort()) }

EOS

is $@, '', 'reval should not fail';
is ref $func, 'CODE', 'reval should return a CODE ref';

my ($l_sorted, $p_sorted) = $func->();
is $l_sorted, "1,2,3";
is $p_sorted, "1,2,3";