File: 30-util.t

package info (click to toggle)
libdevel-nytprof-perl 6.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,552 kB
  • sloc: perl: 5,616; javascript: 4,033; ansic: 107; makefile: 23
file content (173 lines) | stat: -rw-r--r-- 5,969 bytes parent folder | download | duplicates (2)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use strict;
use warnings;
use Test::More tests => 67;

use Devel::NYTProf::Util qw(
    fmt_time fmt_incl_excl_time
    html_safe_filename
    trace_level
    get_abs_paths_alternation_regex
    get_alternation_regex
    strip_prefix_from_paths
    fmt_float
    calculate_median_absolute_deviation
    _dumper
);
use Cwd;
use File::Spec;

my $us = "µs";

is(fmt_time(0), "0s");

is(fmt_time(1.1253e-10), "0ns");
is(fmt_time(1.1253e-9), "1ns");
is(fmt_time(1.1253e-8), "11ns");
is(fmt_time(1.1253e-7), "113ns");
is(fmt_time(1.1253e-6), "1$us");
is(fmt_time(1.1253e-5), "11$us");
is(fmt_time(1.1253e-4), "113$us");
is(fmt_time(1.1253e-3), "1.13ms");
is(fmt_time(1.1253e-2), "11.3ms");
is(fmt_time(1.1253e-1), "113ms");
is(fmt_time(1.1253e-0), "1.13s");
is(fmt_time(1.1253e+1), "11.3s");
is(fmt_time(1.1253e+2), "113s");
is(fmt_time(1.1253e+3), "1125s");

is(fmt_incl_excl_time(3, 3), "3.00s");
is(fmt_incl_excl_time(3, 2), "3.00s (2.00+1.00)");
is(fmt_incl_excl_time(3, 2.997), "3.00s (3.00+3.00ms)");
is(fmt_incl_excl_time(0.1, 0.0997), "100ms (99.7+300$us)");
is(fmt_incl_excl_time(4e-5, 1e-5), "40$us (10+30)");

is html_safe_filename('/foo/bar'), 'foo-bar';
is html_safe_filename('\foo\bar'), 'foo-bar';
is html_safe_filename('\foo/bar'), 'foo-bar';
is html_safe_filename('C:foo'), 'C-foo';
is html_safe_filename('C:\foo'), 'C-foo';
is html_safe_filename('<lots>of|\'really\'special*"chars"?'), 'lots-of-really-special-chars';
is html_safe_filename('no.dots.please'), 'no-dots-please';

my $trace_level = (($ENV{NYTPROF}||'') =~ m/\b trace=(\d+) /x) ? $1 : 0;
is trace_level(), $trace_level, "trace_level $trace_level";

my $inc = [
          '../blib/arch',
          '../blib/lib',
          '/usr/home/username//devel-nytprof/t',
          '/usr/home/username//devel-nytprof/blib/arch',
          '/usr/home/username//devel-nytprof/blib/lib',
          't/lib',
          '/usr/local/lib/perl5/site_perl/mach/5.32',
          '/usr/local/lib/perl5/site_perl',
          '/usr/local/lib/perl5/5.32/mach',
          '/usr/local/lib/perl5/5.32',
          '/usr/local/lib/perl5/site_perl/mach/5.32',
          '/usr/local/lib/perl5/site_perl',
          '/usr/local/lib/perl5/5.32/mach',
          '/usr/local/lib/perl5/5.32'
        ];

my $x1 = get_abs_paths_alternation_regex($inc, qr/^|\[/);
is(ref($x1), 'Regexp', "compiled regex");

my $x2 = get_abs_paths_alternation_regex($inc);
is(ref($x2), 'Regexp', "compiled regex");
isnt($x1, $x2, "different regexes");

my $x3 = get_alternation_regex($inc);
is(ref($x3), 'Regexp', "compiled regex");
isnt($x1, $x3, "different regexes");

{
    local $@;
    my $x4;
    eval{ $x4 = get_abs_paths_alternation_regex([], qr/^|\[/); };
    like($@, qr/No paths/, "get_abs_paths_alternation_regex(): got expected exception: first argument is empty");
}

my $rv;
my $p = File::Spec->catfile(cwd(), 't', 'test02.p');
{
    local $@;
    eval { strip_prefix_from_paths({}, $p, '^'); };
    like($@, qr/first argument must be array ref/,
        "strip_prefix_from_paths(): exception with non-arrayref 1st argument");
}
$rv = strip_prefix_from_paths($inc);
ok(! defined $rv, "strip_prefix_from_paths() returned undefined value with no defined paths");
{
    local $@;
    eval { strip_prefix_from_paths($inc, $p, '^'); };
    like($@, qr/second argument must be array ref/,
        "strip_prefix_from_paths(): exception with non-arrayref 2nd argument");
}
$rv = strip_prefix_from_paths($inc, [$p], '^');
ok(! defined $rv, "strip_prefix_from_paths(): anchor specified");
$rv = strip_prefix_from_paths($inc, [$p], undef, undef);
ok(! defined $rv, "strip_prefix_from_paths(): anchor and replacement unspecified");
$rv = strip_prefix_from_paths([], [$p], undef, undef);
ok(! defined $rv, "strip_prefix_from_paths(): empty first argument");

my %floats = (
    444444  => { default => '444444', prec3 => '444444' },
    44444.4 => { default => '44444.40000', prec3 => '44444.400' },
    4444.44 => { default => '4444.44000', prec3 => '4444.440' },
    444.444 => { default => '444.44400', prec3 => '444.444' },
    44.4444 => { default => '44.44440', prec3 => '44.444' },
    4.44444 => { default => '4.44444', prec3 => '4.444' },
    .444444 => { default => '0.44444', prec3 => '0.444' },
    .044444 => { default => '0.04444', prec3 => '0.044' },
);
for my $k (sort {$b <=> $a} keys %floats) {
    my $l = fmt_float($k);
    is($l, $floats{$k}{default}, "fmt_float applied to $k");
    my $m = fmt_float($k, 3);
    is($m, $floats{$k}{prec3}, "fmt_float, precision 3, applied to $k");
}

my $val = '0.00004';
is(fmt_float($val), '4.0e-5', "fmt_float, applied to $val");

my ($median_ref, @values);
@values = ( 1, 2, 3, 4, 5 );
{
    local $@;
    eval { $median_ref = calculate_median_absolute_deviation(@values); };
    like($@, qr/No array ref given/, "calculate_median_absolute_deviation() must take array ref");
}
$median_ref = calculate_median_absolute_deviation(\@values);
is($median_ref->[0], 1, "median distance to median value");
is($median_ref->[1], 3, "median value");

@values = ( 0, 0, 0, 1, 2, 3, 4, 5 );
my $ign = 'ignore zeroes';
$median_ref = calculate_median_absolute_deviation(\@values, $ign);
is($median_ref->[0], 1, "median distance to median value; $ign");
is($median_ref->[1], 3, "median value; $ign");

$median_ref = calculate_median_absolute_deviation(\@values);
is($median_ref->[0], 2, "median distance to median value");
is($median_ref->[1], 2, "median value");

$median_ref = calculate_median_absolute_deviation([]);
is_deeply($median_ref, [0,0],
    "No values in arrayref passed to calculate_median_absolute_deviation()");

my $arr = [ 1 .. 3 ];
my $hsh = { gamma => 'delta', alpha => 'beta' };
my @rvs = _dumper($arr, $hsh);
like($rvs[0], qr/
    \$VAR1\s+=\s+\[\s+1,\n
    \s+2,\n
    \s+3\n
    \];\n
    /sx, "array ref dumped as expected");
like($rvs[1], qr/
    \$VAR2\s+=\s+\{\n
    \s+?'alpha'\s+=>\s+'beta',\n
    \s+?'gamma'\s+=>\s+'delta'\n
    \};\n
/sx, "hash ref dumped as expected");