Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2010 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
     20 import static org.junit.contrib.truth.Truth.ASSERT;
     21 
     22 import com.google.common.annotations.GwtCompatible;
     23 import com.google.common.annotations.GwtIncompatible;
     24 import com.google.common.collect.testing.IteratorTester;
     25 
     26 import junit.framework.AssertionFailedError;
     27 import junit.framework.TestCase;
     28 
     29 import java.util.Iterator;
     30 import java.util.NoSuchElementException;
     31 
     32 /** Tests for {@link AbstractLinkedIterator}. */
     33 @GwtCompatible(emulated = true)
     34 public class AbstractLinkedIteratorTest extends TestCase {
     35   @GwtIncompatible("Too slow")
     36   public void testDoublerExhaustive() {
     37     new IteratorTester<Integer>(3, UNMODIFIABLE, ImmutableList.of(1, 2),
     38         IteratorTester.KnownOrder.KNOWN_ORDER) {
     39       @Override
     40       protected Iterator<Integer> newTargetIterator() {
     41         return newDoubler(1, 2);
     42       }
     43     }.test();
     44   }
     45 
     46   public void testDoubler() {
     47     Iterable<Integer> doubled = new Iterable<Integer>() {
     48       @Override
     49       public Iterator<Integer> iterator() {
     50         return newDoubler(2, 32);
     51       }
     52     };
     53     ASSERT.that(doubled).hasContentsInOrder(2, 4, 8, 16, 32);
     54   }
     55 
     56   public void testSampleCode() {
     57     Iterable<Integer> actual = new Iterable<Integer>() {
     58       @Override
     59       public Iterator<Integer> iterator() {
     60         Iterator<Integer> powersOfTwo = new AbstractLinkedIterator<Integer>(1) {
     61           protected Integer computeNext(Integer previous) {
     62             return (previous == 1 << 30) ? null : previous * 2;
     63           }
     64         };
     65         return powersOfTwo;
     66       }
     67     };
     68     ASSERT.that(actual).hasContentsInOrder(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
     69         8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608,
     70         16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824);
     71   }
     72 
     73   public void testEmpty() {
     74     Iterator<Object> empty = newEmpty();
     75     assertFalse(empty.hasNext());
     76     try {
     77       empty.next();
     78       fail();
     79     } catch (NoSuchElementException expected) {
     80     }
     81     try {
     82       empty.remove();
     83       fail();
     84     } catch (UnsupportedOperationException expected) {
     85     }
     86   }
     87 
     88   public void testBroken() {
     89     Iterator<Object> broken = newBroken();
     90     assertTrue(broken.hasNext());
     91     // We can't retrieve even the known first element:
     92     try {
     93       broken.next();
     94       fail();
     95     } catch (MyException expected) {
     96     }
     97     try {
     98       broken.next();
     99       fail();
    100     } catch (MyException expected) {
    101     }
    102   }
    103 
    104   private static Iterator<Integer> newDoubler(int first, final int last) {
    105     return new AbstractLinkedIterator<Integer>(first) {
    106       @Override
    107       protected Integer computeNext(Integer previous) {
    108         return (previous == last) ? null : previous * 2;
    109       }
    110     };
    111   }
    112 
    113   private static <T> Iterator<T> newEmpty() {
    114     return new AbstractLinkedIterator<T>(null) {
    115       @Override
    116       protected T computeNext(T previous) {
    117         throw new AssertionFailedError();
    118       }
    119     };
    120   }
    121 
    122   private static Iterator<Object> newBroken() {
    123     return new AbstractLinkedIterator<Object>("UNUSED") {
    124       @Override
    125       protected Object computeNext(Object previous) {
    126         throw new MyException();
    127       }
    128     };
    129   }
    130 
    131   private static class MyException extends RuntimeException {}
    132 }
    133