File Coverage

File:t/02_stats.t
Coverage:100.0%

linestmtbrancondsubtimecode
1
1
1
1
16669
3
43
use strict;
2
1
1
1
6
2
31
use warnings;
3
4
1
1
1
412
19502
9
use Test::More;
5
1
1
1
610
7442
193
use Test::Deep;
6
7
1
1
1
236
3
2296
use App::plackbench::Stats;
8
9
1
101069
subtest 'new'                => \&test_new;
10
1
490
subtest 'insert'             => \&test_insert;
11
1
471
subtest 'count'              => \&test_count;
12
1
465
subtest 'mean'               => \&test_mean;
13
1
461
subtest 'median'             => \&test_median;
14
1
466
subtest 'min'                => \&test_min;
15
1
462
subtest 'max'                => \&test_max;
16
1
458
subtest 'standard deviation' => \&test_standard_deviation;
17
1
462
subtest 'percentile'         => \&test_percentile;
18
19
1
492
done_testing();
20
21sub test_new {
22
1
538
    my $stats = App::plackbench::Stats->new( 2, 1 );
23
1
9
    ok( $stats->isa('App::plackbench::Stats'),
24        'new() should return an instance of App::plackbench::Stats' );
25
1
292
    cmp_deeply(
26        $stats,
27        noclass( [ 1, 2 ] ),
28        'arguments should be copied, sorted and blessed'
29    );
30
1
7228
    return;
31}
32
33sub test_insert {
34
1
489
    my $stats = App::plackbench::Stats->new(10);
35
36
1
4
    $stats->insert(9);
37
1
4
    cmp_deeply($stats, noclass([9, 10]), 'should insert the new number in the list');
38
39
1
1020
    $stats->insert(12);
40
1
5
    cmp_deeply($stats, noclass([9, 10, 12]), 'should insert the new number in the list in the right order');
41
42
1
973
    $stats->insert(11);
43
1
5
    cmp_deeply($stats, noclass([9, 10, 11, 12]), 'should insert the new number in the list in the right order');
44
45
1
1000
    return;
46}
47
48sub test_count {
49
1
438
    my $stats = App::plackbench::Stats->new( 1, 2, 3, 4, 5 );
50
1
4
    is( $stats->count(), 5,
51        'count() should return the number of items in the object' );
52
53
1
273
    $stats = App::plackbench::Stats->new();
54
1
4
    is( $stats->count(), 0, 'count() should be 0 for empty lists' );
55
56
1
266
    return;
57}
58
59sub test_mean {
60
1
431
    my $stats = App::plackbench::Stats->new( 1, 2, 3, 4, 5 );
61
1
5
    is( $stats->mean(), 3, 'mean() should return the average' );
62
63
1
267
    $stats = App::plackbench::Stats->new();
64
1
4
    is( $stats->mean(), undef, 'mean() should return undef for an empty list' );
65
66
1
297
    return;
67}
68
69sub test_min {
70
1
424
    my $stats = App::plackbench::Stats->new( 5, 3, 0, 1, 4 );
71
1
4
    is( $stats->min(), 0, 'min() should return the smallest number' );
72
73
1
271
    $stats = App::plackbench::Stats->new();
74
1
5
    is( $stats->min(), undef, 'min() should return undef for an empty list' );
75
1
266
    return;
76}
77
78sub test_max {
79
1
432
    my $stats = App::plackbench::Stats->new( 3, 0, 5, 1, 4 );
80
1
4
    is( $stats->max(), 5, 'max() should return the largest number' );
81
82
1
269
    $stats = App::plackbench::Stats->new();
83
1
5
    is( $stats->max(), undef, 'max() should return undef for an empty list' );
84
85
1
262
    return;
86}
87
88sub test_median {
89
1
423
    my $stats = App::plackbench::Stats->new( 3, 0, 5, 1, 4 );
90
1
5
    is( $stats->median(), 3, 'median() should return the median' );
91
92
1
269
    $stats = App::plackbench::Stats->new( 0, 1, 3, 4 );
93
1
5
    is( $stats->median(), 2, 'median() should return the average between the two medians for an odd number of items' );
94
95
1
271
    $stats = App::plackbench::Stats->new();
96
1
5
    is( $stats->median(), undef, 'median() should return undef for an empty list' );
97
98
1
267
    return;
99}
100
101sub test_standard_deviation {
102
1
421
    my $stats = App::plackbench::Stats->new(2, 4, 4, 4, 5, 5, 7, 9);
103
1
5
    is( $stats->standard_deviation(), 2, 'standard_deviation() should return the standard_deviation' );
104
105
1
271
    $stats = App::plackbench::Stats->new();
106
1
5
    is( $stats->standard_deviation(), 0, 'standard_deviation() should return 0 for an empty list' );
107
108
1
264
    return;
109}
110
111sub test_percentile {
112
1
423
    my $stats = App::plackbench::Stats->new( 9, 8, 7, 6, 5, 4, 3, 2, 1 );
113
1
4
    is( $stats->percentile(100),
114        $stats->max(), '100th percentile should return the largest number' );
115
1
268
    is( $stats->percentile(50),
116        $stats->median(), '50th percentile should return median' );
117
1
265
    is( $stats->percentile(0),
118        $stats->min(), '0th percentile should return the smallest number' );
119
120
1
266
    $stats = App::plackbench::Stats->new();
121
1
4
    is( $stats->percentile(50), undef, 'percentile() should return undef for an empty list' );
122
123
1
264
    return;
124}
125
126
1
100
1;