[New] Added RoundRobinInt with test
This commit is contained in:
parent
0b4e9aef7d
commit
6cfe7d7845
|
@ -0,0 +1,23 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
public class RoundRobinInt {
|
||||
private int start;
|
||||
private int stop;
|
||||
private int index;
|
||||
|
||||
public RoundRobinInt(int start, int stop) {
|
||||
if (start >= stop)
|
||||
throw new IllegalArgumentException(
|
||||
"A round robin start >= stop does not make sense: " + start + " >= " + stop);
|
||||
this.start = start;
|
||||
this.stop = stop;
|
||||
this.index = start;
|
||||
}
|
||||
|
||||
public int next() {
|
||||
int next = this.index++;
|
||||
if (this.index > this.stop)
|
||||
this.index = this.start;
|
||||
return next;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RoundRobinIntTest {
|
||||
|
||||
@Test
|
||||
public void shouldDoRoundRobin0_2() {
|
||||
RoundRobinInt rr = new RoundRobinInt(0,2);
|
||||
assertEquals(0, rr.next());
|
||||
assertEquals(1, rr.next());
|
||||
assertEquals(2, rr.next());
|
||||
assertEquals(0, rr.next());
|
||||
assertEquals(1, rr.next());
|
||||
assertEquals(2, rr.next());
|
||||
}
|
||||
@Test
|
||||
public void shouldDoRoundRobin1_2() {
|
||||
RoundRobinInt rr = new RoundRobinInt(1,2);
|
||||
assertEquals(1, rr.next());
|
||||
assertEquals(2, rr.next());
|
||||
assertEquals(1, rr.next());
|
||||
assertEquals(2, rr.next());
|
||||
}
|
||||
@Test
|
||||
public void shouldDoRoundRobinNeg10_2() {
|
||||
RoundRobinInt rr = new RoundRobinInt(-10,2);
|
||||
assertEquals(-10, rr.next());
|
||||
assertEquals(-9, rr.next());
|
||||
assertEquals(-8, rr.next());
|
||||
assertEquals(-7, rr.next());
|
||||
assertEquals(-6, rr.next());
|
||||
assertEquals(-5, rr.next());
|
||||
assertEquals(-4, rr.next());
|
||||
assertEquals(-3, rr.next());
|
||||
assertEquals(-2, rr.next());
|
||||
assertEquals(-1, rr.next());
|
||||
assertEquals(0, rr.next());
|
||||
assertEquals(1, rr.next());
|
||||
assertEquals(2, rr.next());
|
||||
assertEquals(-10, rr.next());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue