Search in sources :

Example 1 with Integers

use of java8.lang.Integers in project streamsupport by stefan-zobel.

the class Collection8Test method populatedDeque.

/**
 * Returns a new deque of given size containing consecutive
 * Integers 0 ... n - 1.
 */
private static ArrayDeque<Integer> populatedDeque(int n) {
    // Randomize various aspects of memory layout, including
    // capacity slop and wraparound.
    final ArrayDeque<Integer> q;
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    switch(rnd.nextInt(6)) {
        case 0:
            q = new ArrayDeque<Integer>();
            break;
        case 1:
            q = new ArrayDeque<Integer>(0);
            break;
        case 2:
            q = new ArrayDeque<Integer>(1);
            break;
        case 3:
            q = new ArrayDeque<Integer>(Math.max(0, n - 1));
            break;
        case 4:
            q = new ArrayDeque<Integer>(n);
            break;
        case 5:
            q = new ArrayDeque<Integer>(n + 1);
            break;
        default:
            throw new AssertionError();
    }
    switch(rnd.nextInt(3)) {
        case 0:
            q.addFirst(42);
            assertEquals((Integer) 42, q.removeLast());
            break;
        case 1:
            q.addLast(42);
            assertEquals((Integer) 42, q.removeFirst());
            break;
        case 2:
            /* do nothing */
            break;
        default:
            throw new AssertionError();
    }
    assertTrue(q.isEmpty());
    if (rnd.nextBoolean())
        for (int i = 0; i < n; i++) assertTrue(q.offerLast((Integer) i));
    else
        for (int i = n; --i >= 0; ) q.addFirst((Integer) i);
    assertEquals(n, q.size());
    if (n > 0) {
        assertFalse(q.isEmpty());
        assertEquals((Integer) 0, q.peekFirst());
        assertEquals((Integer) (n - 1), q.peekLast());
    }
    return q;
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Aggregations

ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)1