in src/Assert.hack [607:650]
public function assertIsSorted<Tv>(
Traversable<Tv> $collection,
(function(Tv, Tv): bool) $comparator,
string $message = '',
): void {
// Note: the way we maintain the pair of values to be compared may seem
// weird and convoluted. However, there is a reason for this weirdness.
// Because $collection is a Traversable (we're trying to be general here),
// we can't index directly into it, so at some point we'd have to do a
// comparison like $comparator($prev_item, $current_item). Unfortunately
// there seems to be no way to initialize $prev_item so as to satisfy the
// Hack type checker. If we init it to null, Hack complains that we are
// passing a nullable into $comparator(Tv,Tv). If we omit it, Hack complains
// of an undefined variable. And we can't init it to the first item of the
// collection either, because we can only foreach into it and by then it's
// too late.
$pair = Vector {};
$index = 0;
foreach ($collection as $item) {
if ($pair->count() < 2) {
$pair->add($item);
} else {
$pair[0] = $pair[1];
$pair[1] = $item;
}
if (($pair->count() === 2) && !$comparator($pair[0], $pair[1])) {
$main_message = $message === '' ? 'Collection is not sorted' : $message;
$failure_detail = \sprintf(
'at pos %d, %s and %s are in the wrong order',
$index,
\var_export($pair[0], true),
\var_export($pair[1], true),
);
throw new ExpectationFailedException(
$main_message.': '.$failure_detail,
);
}
$index++;
}
}