- package test;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import junit.framework.TestCase;
- class Robin {
- private int i;
- public Robin(int i) {
- this.i = i;
- }
- public int call() {
- return i;
- }
- }
- class RoundRobin {
- private Iterator
it; - private List
list; - public RoundRobin(List
list) { - this.list = list;
- it = list.iterator();
- }
- public int next() {
- // if we get to the end, start again
- if (!it.hasNext()) {
- it = list.iterator();
- }
- Robin robin = it.next();
- return robin.call();
- }
- }
- public class RoundRobinTest extends TestCase {
- List
items = new ArrayList (); - RoundRobin round;
- public void testOne() {
- items.add(new Robin(1));
- round = new RoundRobin(items);
- assertEquals(1, round.next());
- assertEquals(1, round.next());
- }
- public void testTwo() {
- items.add(new Robin(1));
- items.add(new Robin(2));
- round = new RoundRobin(items);
- assertEquals(1, round.next());
- assertEquals(2, round.next());
- assertEquals(1, round.next());
- assertEquals(2, round.next());
- }
- public void testThree() {
- items.add(new Robin(1));
- items.add(new Robin(2));
- items.add(new Robin(3));
- round = new RoundRobin(items);
- assertEquals(1, round.next());
- assertEquals(2, round.next());
- assertEquals(3, round.next());
- assertEquals(1, round.next());
- assertEquals(2, round.next());
- assertEquals(3, round.next());
- }
- }
Sunday, May 19, 2013
Round Robin in JAVA
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment