コード例 #1
0
 @Test
 public void run() {
   IntegerSum sum = new IntegerSum(0);
   Interval.oneTo(5).run(() -> sum.add(1));
   Assert.assertEquals(5, sum.getIntSum());
   IntegerSum sum2 = new IntegerSum(0);
   Interval.fromTo(5, 1).run(() -> sum2.add(1));
   Assert.assertEquals(5, sum2.getIntSum());
 }
コード例 #2
0
 @Test
 public void forEachWith() {
   IntegerSum sum = new IntegerSum(0);
   Interval.oneTo(5)
       .forEachWith((Integer each, Integer parameter) -> sum.add(each + parameter), 0);
   Assert.assertEquals(15, sum.getIntSum());
   IntegerSum sum2 = new IntegerSum(0);
   Interval.fromTo(5, 1)
       .forEachWith((Integer each, Integer parameter) -> sum2.add(each + parameter), 0);
   Assert.assertEquals(15, sum2.getIntSum());
 }
コード例 #3
0
  @Test
  public void forEachWithIndex() {
    IntegerSum sum = new IntegerSum(0);
    Interval.oneTo(5)
        .forEachWithIndex((ObjectIntProcedure<Integer>) (each, index) -> sum.add(each + index));
    Assert.assertEquals(25, sum.getIntSum());
    IntegerSum zeroSum = new IntegerSum(0);
    Interval.fromTo(0, -4)
        .forEachWithIndex((ObjectIntProcedure<Integer>) (each, index) -> zeroSum.add(each + index));
    Assert.assertEquals(0, zeroSum.getIntSum());

    Verify.assertThrows(
        IndexOutOfBoundsException.class, () -> Interval.zeroTo(10).forEachWithIndex(null, -1, 10));
  }