Created
November 28, 2014 09:16
-
-
Save pujoheadsoft/91fa382ad99e7654eec3 to your computer and use it in GitHub Desktop.
[Java] 要素追加時の優先度設定と優先度の更新が可能な優先度つきキュー(テスト)
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.Arrays; | |
import junit.framework.Assert; | |
import org.junit.Test; | |
public class TestSettablePriorityQueue { | |
/* 単純に処理を委譲している部分のテストはしていません */ | |
@Test | |
public void testOfferWithPriority() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(2, 9); | |
queue.offer(3, 2); | |
queue.offer(4, 15); | |
queue.add(5, 11); | |
queue.add(6, 1); | |
Assert.assertEquals(6, queue.poll().intValue()); | |
Assert.assertEquals(3, queue.poll().intValue()); | |
Assert.assertEquals(2, queue.poll().intValue()); | |
Assert.assertEquals(5, queue.remove().intValue()); | |
Assert.assertEquals(4, queue.remove().intValue()); | |
} | |
@Test | |
public void testOfferWithoutPriority() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(5); | |
queue.offer(7); | |
queue.offer(2); | |
queue.add(1); | |
queue.add(6); | |
Assert.assertEquals(1, queue.poll().intValue()); | |
Assert.assertEquals(2, queue.poll().intValue()); | |
Assert.assertEquals(5, queue.poll().intValue()); | |
Assert.assertEquals(6, queue.remove().intValue()); | |
Assert.assertEquals(7, queue.remove().intValue()); | |
} | |
// 優先度つき追加と優先度なし追加の混合パターン | |
@Test | |
public void testOfferWithPriorityAndWithoutPriority() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(5, 0); | |
queue.offer(2); | |
queue.offer(1, 1); | |
queue.offer(6); | |
// 設定した優先度は無視され要素の順になることを確認 | |
Assert.assertEquals(1, queue.poll().intValue()); | |
Assert.assertEquals(2, queue.poll().intValue()); | |
Assert.assertEquals(5, queue.poll().intValue()); | |
Assert.assertEquals(6, queue.poll().intValue()); | |
queue.offer(5, 0); | |
queue.offer(2); | |
queue.offer(1, 1); | |
queue.offer(6); | |
// 後から要素に優先度を設定 | |
queue.updatePriority(2, 2); | |
queue.updatePriority(6, 3); | |
// 設定した優先度の順になることを確認 | |
Assert.assertEquals(5, queue.poll().intValue()); | |
Assert.assertEquals(1, queue.poll().intValue()); | |
Assert.assertEquals(2, queue.poll().intValue()); | |
Assert.assertEquals(6, queue.poll().intValue()); | |
} | |
@Test | |
public void testPeek() { | |
SettablePriorityQueue<String, Integer> queue = new SettablePriorityQueue<String, Integer>(); | |
queue.offer("aaa", 7); | |
queue.offer("bbb", 1); | |
queue.offer("ccc", 13); | |
Assert.assertEquals("bbb", queue.peek()); | |
} | |
@Test | |
public void testUpdatePriorityAtPoll() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(2, 9); | |
queue.offer(3, 2); | |
queue.offer(4, 15); | |
queue.offer(5, 11); | |
queue.offer(6, 1); | |
Assert.assertEquals(6, queue.poll().intValue()); | |
Assert.assertEquals(3, queue.poll().intValue()); | |
queue.updatePriority(4, 3); | |
Assert.assertEquals(4, queue.poll().intValue()); | |
Assert.assertEquals(2, queue.poll().intValue()); | |
Assert.assertEquals(5, queue.poll().intValue()); | |
} | |
@Test | |
public void testUpdatePriorityAtToString() { | |
SettablePriorityQueue<String, Integer> actionQueue = new SettablePriorityQueue<String, Integer>(); | |
actionQueue.offer("X", 5); | |
actionQueue.offer("Y", 6); | |
actionQueue.offer("Z", 7); | |
Assert.assertEquals("[X, Y, Z]", actionQueue.toString()); | |
actionQueue.updatePriority("Y", 1); | |
Assert.assertEquals("[Y, X, Z]", actionQueue.toString()); | |
} | |
@Test | |
public void testGetPriority() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(2, 11); | |
queue.offer(3, 3); | |
queue.offer(4, 7); | |
queue.offer(5, 0); | |
queue.offer(6, -1); | |
Assert.assertEquals(11, queue.getPriority(2).intValue()); | |
Assert.assertEquals(3, queue.getPriority(3).intValue()); | |
Assert.assertEquals(7, queue.getPriority(4).intValue()); | |
Assert.assertEquals(0, queue.getPriority(5).intValue()); | |
Assert.assertEquals(-1, queue.getPriority(6).intValue()); | |
} | |
@Test | |
public void testGetPriorityAfterRemove() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(2, 11); | |
queue.offer(3, 3); | |
queue.offer(4, 8); | |
queue.remove(2); | |
Assert.assertNull(queue.getPriority(2)); | |
Assert.assertEquals(3, queue.getPriority(3).intValue()); | |
Assert.assertEquals(8, queue.getPriority(4).intValue()); | |
queue.clear(); | |
Assert.assertNull(queue.getPriority(3)); | |
Assert.assertNull(queue.getPriority(4)); | |
} | |
@Test | |
public void testRemoveAll() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(2, 11); | |
queue.offer(3, 3); | |
queue.offer(4, 7); | |
queue.offer(5, 0); | |
queue.offer(6, -1); | |
queue.offer(7, 2); | |
queue.removeAll(Arrays.asList(3, 4, 5)); | |
Assert.assertEquals(3, queue.size()); | |
Assert.assertEquals(11, queue.getPriority(2).intValue()); | |
Assert.assertEquals(-1, queue.getPriority(6).intValue()); | |
Assert.assertNull(queue.getPriority(3)); | |
Assert.assertNull(queue.getPriority(4)); | |
Assert.assertNull(queue.getPriority(5)); | |
Assert.assertEquals(6, queue.poll().intValue()); | |
Assert.assertEquals(7, queue.poll().intValue()); | |
Assert.assertEquals(2, queue.poll().intValue()); | |
} | |
@Test(expected = NullPointerException.class) | |
public void testNullPriority() { | |
SettablePriorityQueue<Integer, Integer> queue = new SettablePriorityQueue<Integer, Integer>(); | |
queue.offer(1, null); | |
} | |
@Test(expected = ClassCastException.class) | |
public void testNotComparable() { | |
class X {} | |
SettablePriorityQueue<X, Integer> queue = new SettablePriorityQueue<X, Integer>(); | |
queue.offer(new X()); | |
queue.offer(new X()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment