@@ -231,4 +231,59 @@ public void testLargeIterators() {
231231 list2 .set (9999 , -1 );
232232 Assert .assertFalse (IteratorUtils .elementsEqual (list1 .iterator (), list2 .iterator ()));
233233 }
234+
235+ @ Test
236+ public void testSizeWithMultipleElements () {
237+ List <String > list = Arrays .asList ("one" , "two" , "three" , "four" , "five" );
238+ Iterator <String > iterator = list .iterator ();
239+ Assert .assertEquals (5 , IteratorUtils .size (iterator ));
240+ Assert .assertFalse ("Iterator should be consumed after size operation" , iterator .hasNext ());
241+ }
242+
243+ @ Test
244+ public void testSizeWithEmptyIterator () {
245+ Assert .assertEquals (0 , IteratorUtils .size (Collections .emptyIterator ()));
246+ }
247+
248+ @ Test
249+ public void testSizeWithNullIterator () {
250+ Assert .assertEquals (0 ,IteratorUtils .size (null ));
251+ }
252+
253+ @ Test
254+ public void testSizeConsumesIterator () {
255+ List <String > list = Arrays .asList ("one" , "two" , "three" );
256+ Iterator <String > iterator = list .iterator ();
257+
258+ Assert .assertEquals (3 , IteratorUtils .size (iterator ));
259+ Assert .assertFalse ("Iterator should be consumed after size operation" , iterator .hasNext ());
260+ }
261+
262+ @ Test
263+ public void testSizeWithSingleElement () {
264+ List <String > singletonList = Collections .singletonList ("single" );
265+ Assert .assertEquals (1 , IteratorUtils .size (singletonList .iterator ()));
266+ }
267+
268+ @ Test
269+ public void testSizeWithCustomIterator () {
270+ Iterator <Integer > customIterator = new Iterator <>() {
271+ private int count = 0 ;
272+
273+ @ Override
274+ public boolean hasNext () {
275+ return count < 10 ;
276+ }
277+
278+ @ Override
279+ public Integer next () {
280+ if (!hasNext ()) {
281+ throw new NoSuchElementException ();
282+ }
283+ return count ++;
284+ }
285+ };
286+ Assert .assertEquals (10 , IteratorUtils .size (customIterator ));
287+ Assert .assertFalse ("Iterator should be consumed after size operation" , customIterator .hasNext ());
288+ }
234289}
0 commit comments