Skip to content

Commit 37a7014

Browse files
committed
Fix comparisons with NAN
1 parent f23349f commit 37a7014

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/Assert.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ public static function notSame($value, $expect, $message = '')
889889
*/
890890
public static function greaterThan($value, $limit, $message = '')
891891
{
892-
if ($value <= $limit) {
892+
if (!($value > $limit)) {
893893
static::reportInvalidArgument(\sprintf(
894894
$message ?: 'Expected a value greater than %2$s. Got: %s',
895895
static::valueToString($value),
@@ -909,7 +909,7 @@ public static function greaterThan($value, $limit, $message = '')
909909
*/
910910
public static function greaterThanEq($value, $limit, $message = '')
911911
{
912-
if ($value < $limit) {
912+
if (!($value >= $limit)) {
913913
static::reportInvalidArgument(\sprintf(
914914
$message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
915915
static::valueToString($value),
@@ -929,7 +929,7 @@ public static function greaterThanEq($value, $limit, $message = '')
929929
*/
930930
public static function lessThan($value, $limit, $message = '')
931931
{
932-
if ($value >= $limit) {
932+
if (!$value < $limit) {
933933
static::reportInvalidArgument(\sprintf(
934934
$message ?: 'Expected a value less than %2$s. Got: %s',
935935
static::valueToString($value),
@@ -949,7 +949,7 @@ public static function lessThan($value, $limit, $message = '')
949949
*/
950950
public static function lessThanEq($value, $limit, $message = '')
951951
{
952-
if ($value > $limit) {
952+
if (!($value <= $limit)) {
953953
static::reportInvalidArgument(\sprintf(
954954
$message ?: 'Expected a value less than or equal to %2$s. Got: %s',
955955
static::valueToString($value),
@@ -972,7 +972,7 @@ public static function lessThanEq($value, $limit, $message = '')
972972
*/
973973
public static function range($value, $min, $max, $message = '')
974974
{
975-
if ($value < $min || $value > $max) {
975+
if (!($value >= $min) || !($value <= $max)) {
976976
static::reportInvalidArgument(\sprintf(
977977
$message ?: 'Expected a value between %2$s and %3$s. Got: %s',
978978
static::valueToString($value),
@@ -1401,7 +1401,7 @@ public static function length($value, $length, $message = '')
14011401
*/
14021402
public static function minLength($value, $min, $message = '')
14031403
{
1404-
if (static::strlen($value) < $min) {
1404+
if (!(static::strlen($value) >= $min)) {
14051405
static::reportInvalidArgument(\sprintf(
14061406
$message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
14071407
static::valueToString($value),
@@ -1423,7 +1423,7 @@ public static function minLength($value, $min, $message = '')
14231423
*/
14241424
public static function maxLength($value, $max, $message = '')
14251425
{
1426-
if (static::strlen($value) > $max) {
1426+
if (!(static::strlen($value) <= $max)) {
14271427
static::reportInvalidArgument(\sprintf(
14281428
$message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
14291429
static::valueToString($value),
@@ -1448,7 +1448,7 @@ public static function lengthBetween($value, $min, $max, $message = '')
14481448
{
14491449
$length = static::strlen($value);
14501450

1451-
if ($length < $min || $length > $max) {
1451+
if (!($length >= $min) || !($length <= $max)) {
14521452
static::reportInvalidArgument(\sprintf(
14531453
$message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
14541454
static::valueToString($value),
@@ -1805,7 +1805,7 @@ public static function count($array, $number, $message = '')
18051805
*/
18061806
public static function minCount($array, $min, $message = '')
18071807
{
1808-
if (\count($array) < $min) {
1808+
if (!(\count($array) >= $min)) {
18091809
static::reportInvalidArgument(\sprintf(
18101810
$message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
18111811
\count($array),
@@ -1825,7 +1825,7 @@ public static function minCount($array, $min, $message = '')
18251825
*/
18261826
public static function maxCount($array, $max, $message = '')
18271827
{
1828-
if (\count($array) > $max) {
1828+
if (!(\count($array) <= $max)) {
18291829
static::reportInvalidArgument(\sprintf(
18301830
$message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
18311831
\count($array),
@@ -1848,7 +1848,7 @@ public static function countBetween($array, $min, $max, $message = '')
18481848
{
18491849
$count = \count($array);
18501850

1851-
if ($count < $min || $count > $max) {
1851+
if (!($count >= $min) || !($count <= $max)) {
18521852
static::reportInvalidArgument(\sprintf(
18531853
$message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
18541854
$count,

tests/AssertTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,34 @@ public function getTests()
233233
array('notSame', array(1, true), true),
234234
array('greaterThan', array(1, 0), true),
235235
array('greaterThan', array(0, 0), false),
236+
array('greaterThan', array(NAN, 1), false),
237+
array('greaterThan', array(1, NAN), false),
238+
array('greaterThan', array(NAN, NAN), false),
236239
array('greaterThanEq', array(2, 1), true),
237240
array('greaterThanEq', array(1, 1), true),
238241
array('greaterThanEq', array(0, 1), false),
242+
array('greaterThanEq', array(NAN, 1), false),
243+
array('greaterThanEq', array(1, NAN), false),
244+
array('greaterThanEq', array(NAN, NAN), false),
239245
array('lessThan', array(0, 1), true),
240246
array('lessThan', array(1, 1), false),
247+
array('lessThan', array(NAN, 1), false),
248+
array('lessThan', array(1, NAN), false),
249+
array('lessThan', array(NAN, NAN), false),
241250
array('lessThanEq', array(0, 1), true),
242251
array('lessThanEq', array(1, 1), true),
243252
array('lessThanEq', array(2, 1), false),
253+
array('lessThanEq', array(NAN, 1), false),
254+
array('lessThanEq', array(1, NAN), false),
255+
array('lessThanEq', array(NAN, NAN), false),
244256
array('range', array(1, 1, 2), true),
245257
array('range', array(2, 1, 2), true),
246258
array('range', array(0, 1, 2), false),
247259
array('range', array(3, 1, 2), false),
260+
array('range', array(NAN, 1, 2), false),
261+
array('range', array(2, NAN, 2), false),
262+
array('range', array(2, 1, NAN), false),
263+
array('range', array(2, NAN, NAN), false),
248264
array('oneOf', array(1, array(1, 2, 3)), true),
249265
array('oneOf', array(1, array('1', '2', '3')), false),
250266
array('inArray', array(1, array(1, 2, 3)), true),
@@ -390,15 +406,18 @@ public function getTests()
390406
array('upper', array(''), false),
391407
array('length', array('abcd', 4), true),
392408
array('length', array('abc', 4), false),
409+
array('length', array('abc', NAN), false),
393410
array('length', array('abcde', 4), false),
394411
array('length', array('äbcd', 4), true, true),
395412
array('length', array('äbc', 4), false, true),
396413
array('length', array('äbcde', 4), false, true),
397414
array('length', array('あbcd', 4), true, true), // 'HIRAGANA LETTER A' (U+3042)
415+
array('length', array('あbcd', 4), NAN, true),
398416
array('length', array('あbc', 4), false, true),
399417
array('length', array('あbcde', 4), false, true),
400418
array('minLength', array('abcd', 4), true),
401419
array('minLength', array('abcde', 4), true),
420+
array('minLength', array('abcde', NAN), false),
402421
array('minLength', array('abc', 4), false),
403422
array('minLength', array('äbcd', 4), true, true),
404423
array('minLength', array('äbcde', 4), true, true),
@@ -408,16 +427,21 @@ public function getTests()
408427
array('minLength', array('あbc', 4), false, true),
409428
array('maxLength', array('abcd', 4), true),
410429
array('maxLength', array('abc', 4), true),
430+
array('maxLength', array('abc', NAN), false),
411431
array('maxLength', array('abcde', 4), false),
412432
array('maxLength', array('äbcd', 4), true, true),
413433
array('maxLength', array('äbc', 4), true, true),
414434
array('maxLength', array('äbcde', 4), false, true),
415435
array('maxLength', array('あbcd', 4), true, true),
416436
array('maxLength', array('あbc', 4), true, true),
437+
array('maxLength', array('あbc', NAN), false, true),
417438
array('maxLength', array('あbcde', 4), false, true),
418439
array('lengthBetween', array('abcd', 3, 5), true),
419440
array('lengthBetween', array('abc', 3, 5), true),
420441
array('lengthBetween', array('abcde', 3, 5), true),
442+
array('lengthBetween', array('abcde', 3, NAN), false),
443+
array('lengthBetween', array('abcde', NAN, 5), false),
444+
array('lengthBetween', array('abcde', NAN, NAN), false),
421445
array('lengthBetween', array('ab', 3, 5), false),
422446
array('lengthBetween', array('abcdef', 3, 5), false),
423447
array('lengthBetween', array('äbcd', 3, 5), true, true),
@@ -428,6 +452,9 @@ public function getTests()
428452
array('lengthBetween', array('あbcd', 3, 5), true, true),
429453
array('lengthBetween', array('あbc', 3, 5), true, true),
430454
array('lengthBetween', array('あbcde', 3, 5), true, true),
455+
array('lengthBetween', array('あbcde', NAN, 5), false, true),
456+
array('lengthBetween', array('あbcde', 3, NAN), false, true),
457+
array('lengthBetween', array('あbcde', NAN, NAN), false, true),
431458
array('lengthBetween', array('あb', 3, 5), false, true),
432459
array('lengthBetween', array('あbcdef', 3, 5), false, true),
433460
array('fileExists', array(__FILE__), true),
@@ -486,18 +513,24 @@ public function getTests()
486513
array('validArrayKey', array(new ToStringClass('testString')), false),
487514
array('validArrayKey', array(self::getResource()), false),
488515
array('count', array(array(0, 1, 2), 3), true),
516+
array('count', array(array(0, 1, 2), NAN), false),
489517
array('count', array(array(0, 1, 2), 2), false),
490518
array('minCount', array(array(0), 2), false),
491519
array('minCount', array(array(0, 1), 2), true),
492520
array('minCount', array(array(0, 1, 2), 2), true),
521+
array('minCount', array(array(0, 1, 2), NAN), false),
493522
array('maxCount', array(array(0, 1, 2), 2), false),
494523
array('maxCount', array(array(0, 1), 2), true),
495524
array('maxCount', array(array(0), 2), true),
525+
array('maxCount', array(array(0), NAN), false),
496526
array('countBetween', array(array(0, 1, 2), 4, 5), false),
497527
array('countBetween', array(array(0, 1, 2), 3, 5), true),
498528
array('countBetween', array(array(0, 1, 2), 1, 2), false),
499529
array('countBetween', array(array(0, 1, 2), 2, 5), true),
500530
array('countBetween', array(array(0, 1, 2), 2, 3), true),
531+
array('countBetween', array(array(0, 1, 2), NAN, 3), false),
532+
array('countBetween', array(array(0, 1, 2), 2, NAN), false),
533+
array('countBetween', array(array(0, 1, 2), NAN, NAN), false),
501534
array('isList', array(array(1, 2, 3)), true),
502535
array('isList', array(array()), true),
503536
array('isList', array(array(0 => 1, 2 => 3)), false),

0 commit comments

Comments
 (0)