File: time.t

package info (click to toggle)
libtest-effects-perl 0.002000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 505; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,411 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
use Test::Effects;

# How long we're going to test timing for...
my $sleepy_time = 0.25;

# Use a +/- 10% margin of error...
my ($min, $max) = ($sleepy_time * 0.9, $sleepy_time * 1.1);

# Short sleep...
sub nap {
    my $time = shift;
    select undef, undef, undef, $time;
    return $time;
}

# Select a random testing context...
my @contexts = (
    [ void_return   => undef          ],
    [ scalar_return => $sleepy_time   ],
    [ list_return   => [$sleepy_time] ],
);
sub select_random_context { return @{ $contexts[rand @contexts] } }

# The various possible test specifications...
my %timing_spec = (
    'empty hash'   => {},
    'empty array'  => [],
    'number'       => $max,
    'array'        => [$min, $max],
    'hash min'     => { min => $min },
    'hash max'     => { max => $max },
    'hash min/max' => { min => $min, max => $max },
);

# How many tests in total???
plan tests => 2 * keys %timing_spec;

# Run them all...
for my $test (keys %timing_spec) {
    # Test quietly...
    effects_ok { nap $sleepy_time }
               TIME {
                    select_random_context(),
               }
               => "Didn't oversleep: $test";

    # Test verbosely...
    effects_ok { nap $sleepy_time }
               VERBOSE {
                    TIME => 1,
                    select_random_context(),
               }
               => "Didn't oversleep: $test";
}

done_testing();