Created
September 27, 2014 23:42
-
-
Save jcar787/b77dcbd799e558d33c69 to your computer and use it in GitHub Desktop.
Unittest for the RandomizedQueue class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
import junit.framework.TestCase; | |
public class RandomizedQueueTest extends TestCase { | |
RandomizedQueue<Integer> ran; | |
public void setUp() throws Exception { | |
super.setUp(); | |
ran = new RandomizedQueue<Integer>(); | |
} | |
public void tearDown() throws Exception { | |
super.tearDown(); | |
ran = null; | |
} | |
public void testIsEmpty() { | |
assertTrue(ran.isEmpty()); | |
ran.enqueue(5); | |
assertFalse(ran.isEmpty()); | |
ran.dequeue(); | |
assertTrue(ran.isEmpty()); | |
} | |
public void testEnqueueAndDequeue() { | |
ran.enqueue(5); | |
ran.enqueue(22); | |
ran.enqueue(42); | |
ran.enqueue(16); | |
ran.enqueue(8); | |
System.out.print("testEnqueueAndDequeue "); | |
System.out.printf("%d ", ran.dequeue()); | |
System.out.printf("%d ", ran.dequeue()); | |
System.out.printf("%d ", ran.dequeue()); | |
System.out.printf("%d ", ran.dequeue()); | |
System.out.printf("%d\n", ran.dequeue()); | |
assertTrue(ran.isEmpty()); | |
try { | |
ran.enqueue(null); | |
fail("Should throw NullPointerException"); | |
} | |
catch(NullPointerException e) { | |
try { | |
ran.dequeue(); | |
fail("Should throw NoSuchElementException"); | |
} | |
catch(NoSuchElementException ex) { } | |
} | |
} | |
public void testSample() { | |
ran.enqueue(5); | |
ran.enqueue(22); | |
ran.enqueue(42); | |
ran.enqueue(16); | |
ran.enqueue(8); | |
System.out.print("testSample "); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d ", ran.sample()); | |
System.out.printf("%d\n", ran.sample()); | |
assertEquals(5, ran.size()); | |
for (int i=0; i<5; i++) | |
ran.dequeue(); | |
assertTrue(ran.isEmpty()); | |
try { | |
ran.sample(); | |
fail("Should throw NoSuchElementException"); | |
} | |
catch (NoSuchElementException e) { } | |
} | |
public void testForIterator() { | |
ran.enqueue(5); | |
ran.enqueue(22); | |
ran.enqueue(42); | |
ran.enqueue(16); | |
ran.enqueue(8); | |
System.out.print("testForIterator "); | |
for(Integer i: ran) | |
System.out.printf("%d ", i); | |
System.out.println(); | |
} | |
public void testWhileIterator() { | |
ran.enqueue(5); | |
ran.enqueue(22); | |
ran.enqueue(42); | |
ran.enqueue(16); | |
ran.enqueue(8); | |
System.out.print("testWhileIterator "); | |
Iterator<Integer> it = ran.iterator(); | |
while(it.hasNext()) | |
System.out.printf("%d ", it.next()); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment