File: DurationTest.php

package info (click to toggle)
php-timer 7.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,720 kB
  • sloc: php: 321; xml: 46; makefile: 10
file content (94 lines) | stat: -rw-r--r-- 3,783 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
<?php declare(strict_types=1);
/*
 * This file is part of phpunit/php-timer.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace SebastianBergmann\Timer;

use function round;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Duration::class)]
#[UsesClass(Timer::class)]
final class DurationTest extends TestCase
{
    /**
     * @return list<array{0: string, 1: float}>
     */
    public static function durationProvider(): array
    {
        return [
            ['00:00',        round((0 * 1000000))],
            ['00:00.001',    round((.001 * 1000000))],
            ['00:00.010',    round((.01 * 1000000))],
            ['00:00.100',    round((.1 * 1000000))],
            ['00:00.999',    round((.999 * 1000000))],
            ['00:00.999',    round((.9999 * 1000000))],
            ['00:01',        round((1 * 1000000))],
            ['00:02',        round((2 * 1000000))],
            ['00:59.900',    round((59.9 * 1000000))],
            ['00:59.990',    round((59.99 * 1000000))],
            ['00:59.999',    round((59.999 * 1000000))],
            ['00:59.999',    round((59.9999 * 1000000))],
            ['00:59.001',    round((59.001 * 1000000))],
            ['00:59.010',    round((59.01 * 1000000))],
            ['01:00',        round((60 * 1000000))],
            ['01:01',        round((61 * 1000000))],
            ['02:00',        round((120 * 1000000))],
            ['02:01',        round((121 * 1000000))],
            ['59:59.900',    round((3599.9 * 1000000))],
            ['59:59.990',    round((3599.99 * 1000000))],
            ['59:59.999',    round((3599.999 * 1000000))],
            ['59:59.999',    round((3599.9999 * 1000000))],
            ['59:59.001',    round((3599.001 * 1000000))],
            ['59:59.010',    round((3599.01 * 1000000))],
            ['01:00:00',     round((3600 * 1000000))],
            ['01:00:01',     round((3601 * 1000000))],
            ['01:00:01.900', round((3601.9 * 1000000))],
            ['01:00:01.990', round((3601.99 * 1000000))],
            ['01:00:01.999', round((3601.999 * 1000000))],
            ['01:00:01.999', round((3601.9999 * 1000000))],
            ['01:00:59.999', round((3659.9999 * 1000000))],
            ['01:00:59.001', round((3659.001 * 1000000))],
            ['01:00:59.010', round((3659.01 * 1000000))],
            ['01:59:59.999', round((7199.9999 * 1000000))],
        ];
    }

    public function testCanBeCreatedFromNanoseconds(): void
    {
        $duration = Duration::fromNanoseconds(1);

        $this->assertSame(1.0, $duration->asNanoseconds());
        $this->assertSame(1.0E-3, $duration->asMicroseconds());
        $this->assertSame(1.0E-6, $duration->asMilliseconds());
        $this->assertSame(1.0E-9, $duration->asSeconds());
    }

    public function testCanBeCreatedFromMicroseconds(): void
    {
        $duration = Duration::fromMicroseconds(1);

        $this->assertSame(1000.0, $duration->asNanoseconds());
        $this->assertSame(1.0, $duration->asMicroseconds());
        $this->assertSame(1.0E-3, $duration->asMilliseconds());
        $this->assertSame(1.0E-6, $duration->asSeconds());
    }

    #[DataProvider('durationProvider')]
    #[TestDox('Formats $microseconds microseconds as "$string"')]
    public function testCanBeFormattedAsString(string $string, float $microseconds): void
    {
        $duration = Duration::fromMicroseconds($microseconds);

        $this->assertSame($string, $duration->asString());
    }
}