@@ -392,4 +392,110 @@ void main() {
392392 'Actual: SimpleIterable:[3, 2, 1] '
393393 'Which: does not contain <5>' );
394394 });
395+
396+ test ('isSorted' , () {
397+ final sorted = [4 , 8 , 15 , 16 , 23 , 42 ];
398+ final mismatchAtStart = [8 , 4 , 15 , 16 , 23 , 42 ];
399+ final mismatchInMiddle = [4 , 8 , 16 , 15 , 23 , 42 ];
400+ final mismatchAtEnd = [4 , 8 , 15 , 16 , 42 , 23 ];
401+ final singleElement = [42 ];
402+ final twoElementsSorted = [42 , 143 ];
403+ final twoElementsUnsorted = [143 , 42 ];
404+
405+ shouldPass (sorted, isSorted <num >());
406+ shouldFail (
407+ mismatchAtStart,
408+ isSorted <num >(),
409+ 'Expected: is sorted '
410+ 'Actual: [8, 4, 15, 16, 23, 42] '
411+ 'Which: found elements out of order at <0>: <8> and <4>' );
412+ shouldFail (
413+ mismatchInMiddle,
414+ isSorted <num >(),
415+ 'Expected: is sorted '
416+ 'Actual: [4, 8, 16, 15, 23, 42] '
417+ 'Which: found elements out of order at <2>: <16> and <15>' );
418+ shouldFail (
419+ mismatchAtEnd,
420+ isSorted <num >(),
421+ 'Expected: is sorted '
422+ 'Actual: [4, 8, 15, 16, 42, 23] '
423+ 'Which: found elements out of order at <4>: <42> and <23>' );
424+ shouldPass (singleElement, isSorted <num >());
425+ shouldPass (twoElementsSorted, isSorted <num >());
426+ shouldFail (
427+ twoElementsUnsorted,
428+ isSorted <num >(),
429+ 'Expected: is sorted '
430+ 'Actual: [143, 42] '
431+ 'Which: found elements out of order at <0>: <143> and <42>' );
432+ });
433+
434+ test ('isSortedUsing' , () {
435+ final sorted = [1 , 2 , 3 ];
436+ final unsorted = [1 , 3 , 2 ];
437+ final reverseSorted = [3 , 2 , 1 ];
438+
439+ int alwaysEqualCompare (int x, int y) => 0 ;
440+ int throwingCompare (int x, int y) => throw Error ();
441+
442+ shouldPass (sorted, isSortedUsing ((int x, int y) => x - y));
443+ shouldFail (
444+ unsorted,
445+ isSortedUsing ((int x, int y) => x - y),
446+ 'Expected: is sorted '
447+ 'Actual: [1, 3, 2] '
448+ 'Which: found elements out of order at <1>: <3> and <2>' );
449+ shouldPass (reverseSorted, isSortedUsing ((int x, int y) => y - x));
450+
451+ shouldPass (unsorted, isSortedUsing (alwaysEqualCompare));
452+
453+ shouldFail (
454+ sorted,
455+ isSortedUsing (throwingCompare),
456+ 'Expected: is sorted '
457+ 'Actual: [1, 2, 3] '
458+ 'Which: got error <Instance of \' Error\' > at <0> '
459+ 'when comparing <1> and <2>' );
460+ });
461+
462+ test ('isSortedBy' , () {
463+ final sorted = ['y' , 'zz' , 'bbbb' , 'aaaa' ];
464+ final unsorted = ['y' , 'bbbb' , 'aaaa' , 'zz' ];
465+ final sortedDueToSameKey = ['zzz' , 'abc' , 'def' , 'aaa' ];
466+
467+ num throwingKey (String s) => throw Error ();
468+
469+ shouldPass (sorted, isSortedBy <String , num >((String s) => s.length));
470+ shouldFail (
471+ unsorted,
472+ isSortedBy <String , num >((String s) => s.length),
473+ 'Expected: is sorted '
474+ 'Actual: [\' y\' , \' bbbb\' , \' aaaa\' , \' zz\' ] '
475+ 'Which: found elements out of order at <2>: \' aaaa\' and \' zz\' ' );
476+ shouldPass (
477+ sortedDueToSameKey, isSortedBy <String , num >((String s) => s.length));
478+
479+ shouldFail (
480+ sorted,
481+ isSortedBy (throwingKey),
482+ 'Expected: is sorted '
483+ 'Actual: [\' y\' , \' zz\' , \' bbbb\' , \' aaaa\' ] '
484+ 'Which: got error <Instance of \' Error\' > at <0> '
485+ 'when getting key of \' y\' ' );
486+ });
487+
488+ test ('isSortedByCompare' , () {
489+ final sorted = ['aaaa' , 'bbbb' , 'zz' , 'y' ];
490+ final unsorted = ['y' , 'bbbb' , 'aaaa' , 'zz' ];
491+
492+ shouldPass (sorted,
493+ isSortedByCompare ((String s) => s.length, (a, b) => b.compareTo (a)));
494+ shouldFail (
495+ unsorted,
496+ isSortedByCompare ((String s) => s.length, (a, b) => b.compareTo (a)),
497+ 'Expected: is sorted '
498+ 'Actual: [\' y\' , \' bbbb\' , \' aaaa\' , \' zz\' ] '
499+ 'Which: found elements out of order at <0>: \' y\' and \' bbbb\' ' );
500+ });
395501}
0 commit comments