@@ -755,4 +755,71 @@ public void testMixedCollectionTypes() {
755755
756756 Assert .assertTrue (IterableUtils .elementsEqual (arrayList , linkedList ));
757757 }
758+
759+ @ Test
760+ public void testLimitWithFewerElements () {
761+ List <Integer > list = List .of (1 , 2 , 3 , 4 , 5 );
762+ Iterable <Integer > limited = IterableUtils .limit (list , 3 );
763+
764+ List <Integer > result = new ArrayList <>();
765+ limited .forEach (result ::add );
766+
767+ Assert .assertEquals (List .of (1 , 2 , 3 ), result );
768+ }
769+
770+ @ Test
771+ public void testLimitWithMoreElements () {
772+ List <Integer > list = List .of (1 , 2 , 3 );
773+ Iterable <Integer > limited = IterableUtils .limit (list , 5 );
774+
775+ List <Integer > result = new ArrayList <>();
776+ limited .forEach (result ::add );
777+
778+ Assert .assertEquals (List .of (1 , 2 , 3 ), result );
779+ }
780+
781+ @ Test
782+ public void testLimitWithZero () {
783+ List <Integer > list = List .of (1 , 2 , 3 , 4 , 5 );
784+ Iterable <Integer > limited = IterableUtils .limit (list , 0 );
785+
786+ List <Integer > result = new ArrayList <>();
787+ limited .forEach (result ::add );
788+
789+ Assert .assertTrue (result .isEmpty ());
790+ }
791+
792+ @ Test
793+ public void testLimitWithEmptyIterable () {
794+ List <Integer > list = Collections .emptyList ();
795+ Iterable <Integer > limited = IterableUtils .limit (list , 3 );
796+
797+ Assert .assertFalse (limited .iterator ().hasNext ());
798+ }
799+
800+ @ Test (expected = NullPointerException .class )
801+ public void testLimitWithNullIterable () {
802+ Iterable <Integer > limited = IterableUtils .limit (null , 3 );
803+
804+ Assert .assertFalse (limited .iterator ().hasNext ());
805+ }
806+
807+ @ Test (expected = IllegalArgumentException .class )
808+ public void testLimitWithNegativeSize () {
809+ List <Integer > list = List .of (1 , 2 , 3 );
810+ IterableUtils .limit (list , -1 );
811+ }
812+
813+ @ Test
814+ public void testLimitWithRemove () {
815+ List <Integer > list = new ArrayList <>(Arrays .asList (1 , 2 , 3 , 4 , 5 ));
816+ Iterable <Integer > limited = IterableUtils .limit (list , 2 );
817+
818+ Iterator <Integer > iterator = limited .iterator ();
819+ iterator .next (); // 1
820+ iterator .remove ();
821+ iterator .next (); // 2
822+
823+ Assert .assertEquals (Arrays .asList (2 , 3 , 4 , 5 ), list );
824+ }
758825}
0 commit comments