xref: /aosp_15_r20/external/grpc-grpc/src/php/tests/unit_tests/TimevalTest.php (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1<?php
2/*
3 *
4 * Copyright 2015 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19class TimevalTest extends \PHPUnit\Framework\TestCase
20{
21    private $time;
22
23    public function setUp(): void
24    {
25    }
26
27    public function tearDown(): void
28    {
29        unset($this->time);
30    }
31
32    public function testConstructorWithInt()
33    {
34        $this->time = new Grpc\Timeval(1234);
35        $this->assertNotNull($this->time);
36        $this->assertSame('Grpc\Timeval', get_class($this->time));
37    }
38
39    public function testConstructorWithNegative()
40    {
41        $this->time = new Grpc\Timeval(-123);
42        $this->assertNotNull($this->time);
43        $this->assertSame('Grpc\Timeval', get_class($this->time));
44    }
45
46    public function testConstructorWithZero()
47    {
48        $this->time = new Grpc\Timeval(0);
49        $this->assertNotNull($this->time);
50        $this->assertSame('Grpc\Timeval', get_class($this->time));
51    }
52
53    public function testConstructorWithOct()
54    {
55        $this->time = new Grpc\Timeval(0123);
56        $this->assertNotNull($this->time);
57        $this->assertSame('Grpc\Timeval', get_class($this->time));
58    }
59
60    public function testConstructorWithHex()
61    {
62        $this->time = new Grpc\Timeval(0x1A);
63        $this->assertNotNull($this->time);
64        $this->assertSame('Grpc\Timeval', get_class($this->time));
65    }
66
67    public function testConstructorWithFloat()
68    {
69        if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
70            $this->markTestSkipped('implicit float to int cast deprecated in 8.1+');
71        }
72        $this->time = new Grpc\Timeval(123.456);
73        $this->assertNotNull($this->time);
74        $this->assertSame('Grpc\Timeval', get_class($this->time));
75        $timeFromInt = new Grpc\Timeval(123);
76        $this->assertSame(0, Grpc\Timeval::compare($this->time, $timeFromInt));
77    }
78
79    public function testConstructorWithBigInt()
80    {
81        $this->time = new Grpc\Timeval(7200000000); // > 2^32
82        $this->assertNotNull($this->time);
83        $this->assertSame('Grpc\Timeval', get_class($this->time));
84        $halfHour = new Grpc\Timeval(1800000000); // < 2^31
85        $hour = $halfHour->add($halfHour);
86        $twoHour = $hour->add($hour);
87        $this->assertSame(0, Grpc\Timeval::compare($this->time, $twoHour));
88    }
89
90    public function testAddAndSubtractWithBigInt()
91    {
92        $time = new Grpc\Timeval(7200000000);
93        $delta = new Grpc\Timeval(7200000000);
94        $delta2 = new Grpc\Timeval(7200000000*2);
95        $time2 = $time->add($delta2);
96        $time2 = $time2->subtract($delta);
97        $time2 = $time2->subtract($delta);
98        $this->assertSame(0, Grpc\Timeval::compare($time, $time2));
99    }
100
101    public function testCompareSame()
102    {
103        $zero = Grpc\Timeval::zero();
104        $this->assertSame(0, Grpc\Timeval::compare($zero, $zero));
105    }
106
107    public function testPastIsLessThanZero()
108    {
109        $zero = Grpc\Timeval::zero();
110        $past = Grpc\Timeval::infPast();
111        $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero));
112        $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past));
113    }
114
115    public function testFutureIsGreaterThanZero()
116    {
117        $zero = Grpc\Timeval::zero();
118        $future = Grpc\Timeval::infFuture();
119        $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future));
120        $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero));
121    }
122
123    /**
124     * @depends testFutureIsGreaterThanZero
125     */
126    public function testNowIsBetweenZeroAndFuture()
127    {
128        $zero = Grpc\Timeval::zero();
129        $future = Grpc\Timeval::infFuture();
130        $now = Grpc\Timeval::now();
131        $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now));
132        $this->assertLessThan(0, Grpc\Timeval::compare($now, $future));
133    }
134
135    public function testNowAndAdd()
136    {
137        $now = Grpc\Timeval::now();
138        $this->assertNotNull($now);
139        $delta = new Grpc\Timeval(1000);
140        $deadline = $now->add($delta);
141        $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now));
142    }
143
144    public function testNowAndSubtract()
145    {
146        $now = Grpc\Timeval::now();
147        $delta = new Grpc\Timeval(1000);
148        $deadline = $now->subtract($delta);
149        $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now));
150    }
151
152    public function testAddAndSubtract()
153    {
154        $now = Grpc\Timeval::now();
155        $delta = new Grpc\Timeval(1000);
156        $deadline = $now->add($delta);
157        $back_to_now = $deadline->subtract($delta);
158        $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now));
159    }
160
161    public function testAddAndSubtractBigInt()
162    {
163        $now = Grpc\Timeval::now();
164        $delta = new Grpc\Timeval(7200000000);
165        $deadline = $now->add($delta);
166        $back_to_now = $deadline->subtract($delta);
167        $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now));
168    }
169
170
171    public function testSimilar()
172    {
173        $a = Grpc\Timeval::now();
174        $delta = new Grpc\Timeval(1000);
175        $b = $a->add($delta);
176        $thresh = new Grpc\Timeval(1100);
177        $this->assertTrue(Grpc\Timeval::similar($a, $b, $thresh));
178        $thresh = new Grpc\Timeval(900);
179        $this->assertFalse(Grpc\Timeval::similar($a, $b, $thresh));
180    }
181
182    public function testSleepUntil()
183    {
184        $curr_microtime = microtime(true);
185        $now = Grpc\Timeval::now();
186        $delta = new Grpc\Timeval(1000);
187        $deadline = $now->add($delta);
188        $deadline->sleepUntil();
189        $done_microtime = microtime(true);
190        $this->assertTrue(($done_microtime - $curr_microtime) > 0.0009);
191    }
192
193    public function testConstructorInvalidParam()
194    {
195        $this->expectException(\InvalidArgumentException::class);
196        $delta = new Grpc\Timeval('abc');
197    }
198
199    public function testAddInvalidParam()
200    {
201        $this->expectException(\InvalidArgumentException::class);
202        $a = Grpc\Timeval::now();
203        $a->add(1000);
204    }
205
206    public function testSubtractInvalidParam()
207    {
208        $this->expectException(\InvalidArgumentException::class);
209        $a = Grpc\Timeval::now();
210        $a->subtract(1000);
211    }
212
213    public function testCompareInvalidParam()
214    {
215        $this->expectException(\InvalidArgumentException::class);
216        $a = Grpc\Timeval::compare(1000, 1100);
217    }
218
219    public function testSimilarInvalidParam()
220    {
221        $this->expectException(\InvalidArgumentException::class);
222        $a = Grpc\Timeval::similar(1000, 1100, 1200);
223        $this->assertNull($delta);
224    }
225}
226