Unit tests for priority queue

Below are unit tests for priority queue written during the lab:


import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import org.junit.Test;

public class UnitTests {
	@Test
	public void testIsEmpty() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		assertTrue(queue.isEmpty());
		queue.add("food");
		assertFalse(queue.isEmpty());

	}

	@Test
	public void testSize() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		assertEquals(0, queue.size());
		queue.add("food");
		assertEquals(1, queue.size());
		queue.add("apple");
		assertEquals(2, queue.size());
		queue.add("spork");
		assertEquals(3, queue.size());
	}

	@Test
	public void singleItemTest() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.add("kiwi");
		assertEquals("kiwi", queue.element());
		assertEquals(1, queue.size());
		assertFalse(queue.isEmpty());
	}

	@Test
	public void testElement() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.add("boo");
		queue.add("achoo");
		assertEquals("achoo", queue.element());
		assertEquals("achoo", queue.element());
	}

	@Test
	public void threeItemsTest() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.add("kiwi");
		queue.add("apple");
		queue.add("orange");

		assertTrue(queue.isEmpty());
		assertEquals(3, queue.size());
		assertEquals("apple", queue.element());
	}

	@Test
	public void iteratorTest() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.add("kiwi");
		queue.add("apple");
		queue.add("orange");

		String[] expected = { "apple", "kiwi", "orange" };
		int i = 0;

		for (String fruit : queue) {
			assertEquals(expected[i++], fruit);
		}
	}

	@Test
	public void testIterator2() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.add("apples");
		queue.add("kiwi");
		queue.add("banana");

		ArrayList<String> fruit1 = new ArrayList<String>();
		fruit1.add("apples");
		fruit1.add("banana");
		fruit1.add("kiwi");

		ArrayList<String> fruit2 = new ArrayList<String>();
		for (String fruit : queue) {
			fruit2.add(fruit);
		}

		assertEquals(fruit1, fruit2);
	}

	@Test
	public void removeTest() {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.add("kiwi");
		queue.add("apple");
		queue.add("orange");

		assertEquals("apple", queue.remove());
		assertEquals(2, queue.size());

		assertEquals("kiwi", queue.element());
		assertEquals("kiwi", queue.remove());
		assertEquals(1, queue.size());

		assertEquals("orange", queue.remove());
		assertTrue(queue.isEmpty());
		assertEquals(0, queue.size());
	}

	@Test
	public void testIterator() {
		OurQueue<Integer> queue = new OurPriorityQueue<Integer>();
		int testSum = 0;
		queue.add(1);
		queue.add(2);
		queue.add(3);

		for (int element : queue) {
			testSum = testSum + element;
		}
		assertEquals(6, testSum);
	}

	@Test(expected = NoSuchElementException.class)
	public void testNoSuchElementException() throws NoSuchElementException {
		OurQueue<String> queue = new OurPriorityQueue<String>();
		queue.remove();
	}

}

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.