Skip to content

Commit

Permalink
Fix comparisons with NAN
Browse files Browse the repository at this point in the history
  • Loading branch information
gordinskiy committed Oct 28, 2024
1 parent f23349f commit 37a7014
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ public static function notSame($value, $expect, $message = '')
*/
public static function greaterThan($value, $limit, $message = '')
{
if ($value <= $limit) {
if (!($value > $limit)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value greater than %2$s. Got: %s',
static::valueToString($value),
Expand All @@ -909,7 +909,7 @@ public static function greaterThan($value, $limit, $message = '')
*/
public static function greaterThanEq($value, $limit, $message = '')
{
if ($value < $limit) {
if (!($value >= $limit)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
static::valueToString($value),
Expand All @@ -929,7 +929,7 @@ public static function greaterThanEq($value, $limit, $message = '')
*/
public static function lessThan($value, $limit, $message = '')
{
if ($value >= $limit) {
if (!$value < $limit) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value less than %2$s. Got: %s',
static::valueToString($value),
Expand All @@ -949,7 +949,7 @@ public static function lessThan($value, $limit, $message = '')
*/
public static function lessThanEq($value, $limit, $message = '')
{
if ($value > $limit) {
if (!($value <= $limit)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value less than or equal to %2$s. Got: %s',
static::valueToString($value),
Expand All @@ -972,7 +972,7 @@ public static function lessThanEq($value, $limit, $message = '')
*/
public static function range($value, $min, $max, $message = '')
{
if ($value < $min || $value > $max) {
if (!($value >= $min) || !($value <= $max)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value between %2$s and %3$s. Got: %s',
static::valueToString($value),
Expand Down Expand Up @@ -1401,7 +1401,7 @@ public static function length($value, $length, $message = '')
*/
public static function minLength($value, $min, $message = '')
{
if (static::strlen($value) < $min) {
if (!(static::strlen($value) >= $min)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
static::valueToString($value),
Expand All @@ -1423,7 +1423,7 @@ public static function minLength($value, $min, $message = '')
*/
public static function maxLength($value, $max, $message = '')
{
if (static::strlen($value) > $max) {
if (!(static::strlen($value) <= $max)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
static::valueToString($value),
Expand All @@ -1448,7 +1448,7 @@ public static function lengthBetween($value, $min, $max, $message = '')
{
$length = static::strlen($value);

if ($length < $min || $length > $max) {
if (!($length >= $min) || !($length <= $max)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
static::valueToString($value),
Expand Down Expand Up @@ -1805,7 +1805,7 @@ public static function count($array, $number, $message = '')
*/
public static function minCount($array, $min, $message = '')
{
if (\count($array) < $min) {
if (!(\count($array) >= $min)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
\count($array),
Expand All @@ -1825,7 +1825,7 @@ public static function minCount($array, $min, $message = '')
*/
public static function maxCount($array, $max, $message = '')
{
if (\count($array) > $max) {
if (!(\count($array) <= $max)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
\count($array),
Expand All @@ -1848,7 +1848,7 @@ public static function countBetween($array, $min, $max, $message = '')
{
$count = \count($array);

if ($count < $min || $count > $max) {
if (!($count >= $min) || !($count <= $max)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
$count,
Expand Down
33 changes: 33 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,34 @@ public function getTests()
array('notSame', array(1, true), true),
array('greaterThan', array(1, 0), true),
array('greaterThan', array(0, 0), false),
array('greaterThan', array(NAN, 1), false),
array('greaterThan', array(1, NAN), false),
array('greaterThan', array(NAN, NAN), false),
array('greaterThanEq', array(2, 1), true),
array('greaterThanEq', array(1, 1), true),
array('greaterThanEq', array(0, 1), false),
array('greaterThanEq', array(NAN, 1), false),
array('greaterThanEq', array(1, NAN), false),
array('greaterThanEq', array(NAN, NAN), false),
array('lessThan', array(0, 1), true),
array('lessThan', array(1, 1), false),
array('lessThan', array(NAN, 1), false),
array('lessThan', array(1, NAN), false),
array('lessThan', array(NAN, NAN), false),
array('lessThanEq', array(0, 1), true),
array('lessThanEq', array(1, 1), true),
array('lessThanEq', array(2, 1), false),
array('lessThanEq', array(NAN, 1), false),
array('lessThanEq', array(1, NAN), false),
array('lessThanEq', array(NAN, NAN), false),
array('range', array(1, 1, 2), true),
array('range', array(2, 1, 2), true),
array('range', array(0, 1, 2), false),
array('range', array(3, 1, 2), false),
array('range', array(NAN, 1, 2), false),
array('range', array(2, NAN, 2), false),
array('range', array(2, 1, NAN), false),
array('range', array(2, NAN, NAN), false),
array('oneOf', array(1, array(1, 2, 3)), true),
array('oneOf', array(1, array('1', '2', '3')), false),
array('inArray', array(1, array(1, 2, 3)), true),
Expand Down Expand Up @@ -390,15 +406,18 @@ public function getTests()
array('upper', array(''), false),
array('length', array('abcd', 4), true),
array('length', array('abc', 4), false),
array('length', array('abc', NAN), false),
array('length', array('abcde', 4), false),
array('length', array('äbcd', 4), true, true),
array('length', array('äbc', 4), false, true),
array('length', array('äbcde', 4), false, true),
array('length', array('あbcd', 4), true, true), // 'HIRAGANA LETTER A' (U+3042)
array('length', array('あbcd', 4), NAN, true),
array('length', array('あbc', 4), false, true),
array('length', array('あbcde', 4), false, true),
array('minLength', array('abcd', 4), true),
array('minLength', array('abcde', 4), true),
array('minLength', array('abcde', NAN), false),
array('minLength', array('abc', 4), false),
array('minLength', array('äbcd', 4), true, true),
array('minLength', array('äbcde', 4), true, true),
Expand All @@ -408,16 +427,21 @@ public function getTests()
array('minLength', array('あbc', 4), false, true),
array('maxLength', array('abcd', 4), true),
array('maxLength', array('abc', 4), true),
array('maxLength', array('abc', NAN), false),
array('maxLength', array('abcde', 4), false),
array('maxLength', array('äbcd', 4), true, true),
array('maxLength', array('äbc', 4), true, true),
array('maxLength', array('äbcde', 4), false, true),
array('maxLength', array('あbcd', 4), true, true),
array('maxLength', array('あbc', 4), true, true),
array('maxLength', array('あbc', NAN), false, true),
array('maxLength', array('あbcde', 4), false, true),
array('lengthBetween', array('abcd', 3, 5), true),
array('lengthBetween', array('abc', 3, 5), true),
array('lengthBetween', array('abcde', 3, 5), true),
array('lengthBetween', array('abcde', 3, NAN), false),
array('lengthBetween', array('abcde', NAN, 5), false),
array('lengthBetween', array('abcde', NAN, NAN), false),
array('lengthBetween', array('ab', 3, 5), false),
array('lengthBetween', array('abcdef', 3, 5), false),
array('lengthBetween', array('äbcd', 3, 5), true, true),
Expand All @@ -428,6 +452,9 @@ public function getTests()
array('lengthBetween', array('あbcd', 3, 5), true, true),
array('lengthBetween', array('あbc', 3, 5), true, true),
array('lengthBetween', array('あbcde', 3, 5), true, true),
array('lengthBetween', array('あbcde', NAN, 5), false, true),
array('lengthBetween', array('あbcde', 3, NAN), false, true),
array('lengthBetween', array('あbcde', NAN, NAN), false, true),
array('lengthBetween', array('あb', 3, 5), false, true),
array('lengthBetween', array('あbcdef', 3, 5), false, true),
array('fileExists', array(__FILE__), true),
Expand Down Expand Up @@ -486,18 +513,24 @@ public function getTests()
array('validArrayKey', array(new ToStringClass('testString')), false),
array('validArrayKey', array(self::getResource()), false),
array('count', array(array(0, 1, 2), 3), true),
array('count', array(array(0, 1, 2), NAN), false),
array('count', array(array(0, 1, 2), 2), false),
array('minCount', array(array(0), 2), false),
array('minCount', array(array(0, 1), 2), true),
array('minCount', array(array(0, 1, 2), 2), true),
array('minCount', array(array(0, 1, 2), NAN), false),
array('maxCount', array(array(0, 1, 2), 2), false),
array('maxCount', array(array(0, 1), 2), true),
array('maxCount', array(array(0), 2), true),
array('maxCount', array(array(0), NAN), false),
array('countBetween', array(array(0, 1, 2), 4, 5), false),
array('countBetween', array(array(0, 1, 2), 3, 5), true),
array('countBetween', array(array(0, 1, 2), 1, 2), false),
array('countBetween', array(array(0, 1, 2), 2, 5), true),
array('countBetween', array(array(0, 1, 2), 2, 3), true),
array('countBetween', array(array(0, 1, 2), NAN, 3), false),
array('countBetween', array(array(0, 1, 2), 2, NAN), false),
array('countBetween', array(array(0, 1, 2), NAN, NAN), false),
array('isList', array(array(1, 2, 3)), true),
array('isList', array(array()), true),
array('isList', array(array(0 => 1, 2 => 3)), false),
Expand Down

0 comments on commit 37a7014

Please sign in to comment.