@Test public void iterator() { Interval zero = Interval.zero(); Iterator<Integer> zeroIterator = zero.iterator(); Assert.assertTrue(zeroIterator.hasNext()); Assert.assertEquals(Integer.valueOf(0), zeroIterator.next()); Assert.assertFalse(zeroIterator.hasNext()); Interval oneToFive = Interval.oneTo(5); Iterator<Integer> oneToFiveIterator = oneToFive.iterator(); for (int i = 1; i < 6; i++) { Assert.assertTrue(oneToFiveIterator.hasNext()); Assert.assertEquals(Integer.valueOf(i), oneToFiveIterator.next()); } Verify.assertThrows(NoSuchElementException.class, (Runnable) oneToFiveIterator::next); Interval threeToNegativeThree = Interval.fromTo(3, -3); Iterator<Integer> threeToNegativeThreeIterator = threeToNegativeThree.iterator(); for (int i = 3; i > -4; i--) { Assert.assertTrue(threeToNegativeThreeIterator.hasNext()); Assert.assertEquals(Integer.valueOf(i), threeToNegativeThreeIterator.next()); } Verify.assertThrows( NoSuchElementException.class, (Runnable) threeToNegativeThreeIterator::next); Verify.assertThrows( UnsupportedOperationException.class, () -> Interval.zeroTo(10).iterator().remove()); }
@Test public void getLast() { Assert.assertEquals(Integer.valueOf(-10), Interval.fromTo(10, -10).by(-5).getLast()); Assert.assertEquals(Integer.valueOf(-10), Interval.fromTo(10, -12).by(-5).getLast()); Assert.assertEquals(Integer.valueOf(10), Interval.fromTo(-10, 10).by(5).getLast()); Assert.assertEquals(Integer.valueOf(10), Interval.fromTo(-10, 12).by(5).getLast()); Assert.assertEquals(Integer.valueOf(0), Interval.zero().getLast()); }
@Test public void contains() { Assert.assertTrue(Interval.zero().contains(0)); Assert.assertTrue(Interval.oneTo(5).containsAll(1, 5)); Assert.assertTrue(Interval.oneTo(5).containsNone(6, 7)); Assert.assertFalse(Interval.oneTo(5).containsAll(1, 6)); Assert.assertFalse(Interval.oneTo(5).containsNone(1, 6)); Assert.assertFalse(Interval.oneTo(5).contains(0)); Assert.assertTrue(Interval.fromTo(-1, -5).containsAll(-1, -5)); Assert.assertFalse(Interval.fromTo(-1, -5).contains(1)); Assert.assertTrue(Interval.zero().contains(Integer.valueOf(0))); Assert.assertFalse(Interval.oneTo(5).contains(Integer.valueOf(0))); Assert.assertFalse(Interval.fromTo(-1, -5).contains(Integer.valueOf(1))); Assert.assertFalse(Interval.zeroTo(5).contains(new Object())); }
@Test public void product() { Assert.assertEquals(0, Interval.zero().product().intValue()); Assert.assertEquals(0, Interval.fromTo(-1, 1).product().intValue()); Assert.assertEquals(2, Interval.fromTo(-2, -1).product().intValue()); Assert.assertEquals(-6, Interval.fromTo(-3, -1).product().intValue()); Assert.assertEquals(200, Interval.fromToBy(10, 20, 10).product().intValue()); Assert.assertEquals(200, Interval.fromToBy(-10, -20, -10).product().intValue()); Assert.assertEquals(-6000, Interval.fromToBy(-10, -30, -10).product().intValue()); Assert.assertEquals(6000, Interval.fromToBy(30, 10, -10).product().intValue()); Assert.assertEquals(6000, Interval.fromToBy(30, 10, -10).reverseThis().product().intValue()); }
@Test public void factorial() { Verify.assertThrows(IllegalStateException.class, () -> Interval.fromTo(-1, -5).factorial()); Assert.assertEquals(1, Interval.zero().factorial().intValue()); Assert.assertEquals(1, Interval.oneTo(1).factorial().intValue()); Assert.assertEquals(6, Interval.oneTo(3).factorial().intValue()); Assert.assertEquals(2432902008176640000L, Interval.oneTo(20).factorial().longValue()); Assert.assertEquals(new BigInteger("51090942171709440000"), Interval.oneTo(21).factorial()); Assert.assertEquals( new BigInteger("1405006117752879898543142606244511569936384000000000"), Interval.oneTo(42).factorial()); }
@Test public void size() { Assert.assertEquals(100, Interval.fromTo(1, 100).size()); Assert.assertEquals(50, Interval.fromToBy(1, 100, 2).size()); Assert.assertEquals(34, Interval.fromToBy(1, 100, 3).size()); Assert.assertEquals(25, Interval.fromToBy(1, 100, 4).size()); Assert.assertEquals(20, Interval.fromToBy(1, 100, 5).size()); Assert.assertEquals(17, Interval.fromToBy(1, 100, 6).size()); Assert.assertEquals(15, Interval.fromToBy(1, 100, 7).size()); Assert.assertEquals(13, Interval.fromToBy(1, 100, 8).size()); Assert.assertEquals(12, Interval.fromToBy(1, 100, 9).size()); Assert.assertEquals(10, Interval.fromToBy(1, 100, 10).size()); Assert.assertEquals(11, Interval.fromTo(0, 10).size()); Assert.assertEquals(1, Interval.zero().size()); Assert.assertEquals(11, Interval.fromTo(0, -10).size()); Assert.assertEquals(3, Interval.evensFromTo(2, -2).size()); Assert.assertEquals(2, Interval.oddsFromTo(2, -2).size()); }