-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This introduces `Math\{maxv, minv}` and aliases `Math\{max, min}` to them for migration purposes. The intended end state is that `C\{max, min}` are moved into the Math namespaces to become `Math\{max, min}`. See #25. Reviewed By: fredemmott Differential Revision: D6095022 fbshipit-source-id: d1199d2d4f4a2d0cdbfe552679b833c50b7ac13b
- Loading branch information
1 parent
70925a4
commit 8f4ab2e
Showing
3 changed files
with
113 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
namespace HH\Lib\Math; | ||
|
||
/** | ||
* Returns the largest of all input numbers. | ||
* | ||
* To find the smallest number, see `Math\min`. | ||
* For Traversables, see `C\max`. | ||
*/ | ||
function maxv<T as num>(T $first, T ...$rest): T { | ||
$max = $first; | ||
foreach ($rest as $number) { | ||
if ($number > $max) { | ||
$max = $number; | ||
} | ||
} | ||
return $max; | ||
} | ||
|
||
/** | ||
* Returns the smallest of all input numbers. | ||
* | ||
* To find the largest number, see `Math\max`. | ||
* For Traversables, see `C\min`. | ||
*/ | ||
function minv<T as num>(T $first, T ...$rest): T { | ||
$min = $first; | ||
foreach ($rest as $number) { | ||
if ($number < $min) { | ||
$min = $number; | ||
} | ||
} | ||
return $min; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
use namespace HH\Lib\Math; | ||
use function Facebook\FBExpect\expect; | ||
|
||
/** | ||
* @emails oncall+hack_prod_infra | ||
*/ | ||
final class MathCompareTest extends PHPUnit_Framework_TestCase { | ||
|
||
public static function provideTestMaxv(): array<mixed> { | ||
return array( | ||
tuple(1, vec[2], 2), | ||
tuple(2, vec[1], 2), | ||
tuple(1.0, vec[2.0], 2.0), | ||
tuple(2.0, vec[1.0], 2.0), | ||
tuple(1, vec[1], 1), | ||
tuple(-2, vec[-1], -1), | ||
tuple(1.0, vec[2], 2), | ||
tuple(1, vec[2.0], 2.0), | ||
tuple(-1, vec[1, 2, 3, 4, 5], 5), | ||
tuple(-1, vec[5, 4, 3, 2, 1], 5), | ||
); | ||
} | ||
|
||
/** @dataProvider provideTestMaxv */ | ||
public function testMaxv<T as num>( | ||
T $first, | ||
Container<T> $rest, | ||
T $expected, | ||
): void { | ||
expect(Math\maxv($first, ...$rest))->toBeSame($expected); | ||
} | ||
|
||
public static function provideTestMinv(): array<mixed> { | ||
return array( | ||
tuple(1, vec[2], 1), | ||
tuple(2, vec[1], 1), | ||
tuple(1.0, vec[2.0], 1.0), | ||
tuple(2.0, vec[1.0], 1.0), | ||
tuple(1, vec[1], 1), | ||
tuple(-2, vec[-1], -2), | ||
tuple(1.0, vec[2], 1.0), | ||
tuple(1, vec[2.0], 1), | ||
tuple(1, vec[-1, -2, -3, -4, -5], -5), | ||
tuple(1, vec[-5, -4, -3, -2, -1], -5), | ||
); | ||
} | ||
|
||
/** @dataProvider provideTestMinv */ | ||
public function testMinv<T as num>( | ||
T $first, | ||
Container<T> $rest, | ||
T $expected, | ||
): void { | ||
expect(Math\minv($first, ...$rest))->toBeSame($expected); | ||
} | ||
} |