Skip to content

Commit a499f5d

Browse files
committed
Fix NaN handling in Summary::isSorted() and isReversed().
1 parent 563a18c commit a499f5d

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Fix `Sort::sort` dropping elements when iterable has duplicate keys
99
* Fix `Sort::asort` dropping elements when iterable has duplicate keys
1010
* Fix `Summary::isEmpty` to rewind non-Generator iterators before checking.
11+
* Fix NaN handling in `Summary::isSorted` and `isReversed`
1112
* Skip NaN values in `Reduce::toMin/toMax/toMinMax` to prevent incorrect results
1213
* Throw InvalidArgumentException for non-serializable objects in coercive comparison mode
1314
* Throw InvalidArgumentException for non-positive count in `Transform::tee`

src/Summary.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public static function allUnique(iterable $data, bool $strict = true): bool
112112
public static function isSorted(iterable $data): bool
113113
{
114114
foreach (Single::pairwise($data) as [$lhs, $rhs]) {
115+
if ((\is_float($lhs) && \is_nan($lhs)) || (\is_float($rhs) && \is_nan($rhs))) {
116+
return false;
117+
}
115118
if ($rhs < $lhs) {
116119
return false;
117120
}
@@ -134,6 +137,9 @@ public static function isSorted(iterable $data): bool
134137
public static function isReversed(iterable $data): bool
135138
{
136139
foreach (Single::pairwise($data) as [$lhs, $rhs]) {
140+
if ((\is_float($lhs) && \is_nan($lhs)) || (\is_float($rhs) && \is_nan($rhs))) {
141+
return false;
142+
}
137143
if ($rhs > $lhs) {
138144
return false;
139145
}

tests/Summary/IsReversedTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static function dataProviderForArrayTrue(): array
3737
[
3838
[null]
3939
],
40+
[
41+
[NAN]
42+
],
4043
[
4144
[null, null]
4245
],
@@ -117,6 +120,18 @@ public static function dataProviderForArrayFalse(): array
117120
[
118121
[['bb'], ['a', 'a']]
119122
],
123+
[
124+
[3, NAN, 1]
125+
],
126+
[
127+
[NAN, 2, 1]
128+
],
129+
[
130+
[3, 2, NAN]
131+
],
132+
[
133+
[NAN, NAN]
134+
],
120135
];
121136
}
122137

@@ -150,6 +165,9 @@ public static function dataProviderForGeneratorsTrue(): array
150165
[
151166
$gen([null])
152167
],
168+
[
169+
$gen([NAN])
170+
],
153171
[
154172
$gen([null, null])
155173
],
@@ -234,6 +252,18 @@ public static function dataProviderForGeneratorsFalse(): array
234252
[
235253
$gen([['bb'], ['a', 'a']])
236254
],
255+
[
256+
$gen([3, NAN, 1])
257+
],
258+
[
259+
$gen([NAN, 2, 1])
260+
],
261+
[
262+
$gen([3, 2, NAN])
263+
],
264+
[
265+
$gen([NAN, NAN])
266+
],
237267
];
238268
}
239269

@@ -267,6 +297,9 @@ public static function dataProviderForIteratorsTrue(): array
267297
[
268298
$iter([null])
269299
],
300+
[
301+
$iter([NAN])
302+
],
270303
[
271304
$iter([null, null])
272305
],
@@ -351,6 +384,18 @@ public static function dataProviderForIteratorsFalse(): array
351384
[
352385
$iter([['bb'], ['a', 'a']])
353386
],
387+
[
388+
$iter([3, NAN, 1])
389+
],
390+
[
391+
$iter([NAN, 2, 1])
392+
],
393+
[
394+
$iter([3, 2, NAN])
395+
],
396+
[
397+
$iter([NAN, NAN])
398+
],
354399
];
355400
}
356401

@@ -384,6 +429,9 @@ public static function dataProviderForTraversablesTrue(): array
384429
[
385430
$trav([null])
386431
],
432+
[
433+
$trav([NAN])
434+
],
387435
[
388436
$trav([null, null])
389437
],
@@ -468,6 +516,18 @@ public static function dataProviderForTraversablesFalse(): array
468516
[
469517
$trav([['bb'], ['a', 'a']])
470518
],
519+
[
520+
$trav([3, NAN, 1])
521+
],
522+
[
523+
$trav([NAN, 2, 1])
524+
],
525+
[
526+
$trav([3, 2, NAN])
527+
],
528+
[
529+
$trav([NAN, NAN])
530+
],
471531
];
472532
}
473533
}

tests/Summary/IsSortedTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static function dataProviderForArrayTrue(): array
3737
[
3838
[null],
3939
],
40+
[
41+
[NAN],
42+
],
4043
[
4144
[0, 1],
4245
],
@@ -117,6 +120,18 @@ public static function dataProviderForArrayFalse(): array
117120
[
118121
[['a', 'a'], ['b']],
119122
],
123+
[
124+
[1, NAN, 3],
125+
],
126+
[
127+
[NAN, 1, 2],
128+
],
129+
[
130+
[1, 2, NAN],
131+
],
132+
[
133+
[NAN, NAN],
134+
],
120135
];
121136
}
122137

@@ -150,6 +165,9 @@ public static function dataProviderForGeneratorsTrue(): array
150165
[
151166
$gen([null])
152167
],
168+
[
169+
$gen([NAN])
170+
],
153171
[
154172
$gen([0, 1])
155173
],
@@ -234,6 +252,18 @@ public static function dataProviderForGeneratorsFalse(): array
234252
[
235253
$gen([['a', 'a'], ['b']])
236254
],
255+
[
256+
$gen([1, NAN, 3])
257+
],
258+
[
259+
$gen([NAN, 1, 2])
260+
],
261+
[
262+
$gen([1, 2, NAN])
263+
],
264+
[
265+
$gen([NAN, NAN])
266+
],
237267
];
238268
}
239269

@@ -267,6 +297,9 @@ public static function dataProviderForIteratorsTrue(): array
267297
[
268298
$iter([null])
269299
],
300+
[
301+
$iter([NAN])
302+
],
270303
[
271304
$iter([0, 1])
272305
],
@@ -351,6 +384,18 @@ public static function dataProviderForIteratorsFalse(): array
351384
[
352385
$iter([['a', 'a'], ['b']])
353386
],
387+
[
388+
$iter([1, NAN, 3])
389+
],
390+
[
391+
$iter([NAN, 1, 2])
392+
],
393+
[
394+
$iter([1, 2, NAN])
395+
],
396+
[
397+
$iter([NAN, NAN])
398+
],
354399
];
355400
}
356401

@@ -384,6 +429,9 @@ public static function dataProviderForTraversablesTrue(): array
384429
[
385430
$trav([null])
386431
],
432+
[
433+
$trav([NAN])
434+
],
387435
[
388436
$trav([0, 1])
389437
],
@@ -468,6 +516,18 @@ public static function dataProviderForTraversablesFalse(): array
468516
[
469517
$trav([['a', 'a'], ['b']])
470518
],
519+
[
520+
$trav([1, NAN, 3])
521+
],
522+
[
523+
$trav([NAN, 1, 2])
524+
],
525+
[
526+
$trav([1, 2, NAN])
527+
],
528+
[
529+
$trav([NAN, NAN])
530+
],
471531
];
472532
}
473533
}

0 commit comments

Comments
 (0)