svnno****@sourc*****
svnno****@sourc*****
2008年 12月 18日 (木) 23:59:18 JST
Revision: 2278 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2278 Author: daisuke_m Date: 2008-12-18 23:59:18 +0900 (Thu, 18 Dec 2008) Log Message: ----------- テスト拡充。 Modified Paths: -------------- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ArrayEssentialStack.java artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialQueue.java artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialStack.java artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ResolvableQueue.java artemis/trunk/org.jiemamy.core/src/test/java/org/jiemamy/utils/ResolvableQueueTest.java -------------- next part -------------- Modified: artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ArrayEssentialStack.java =================================================================== --- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ArrayEssentialStack.java 2008-12-18 11:22:42 UTC (rev 2277) +++ artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ArrayEssentialStack.java 2008-12-18 14:59:18 UTC (rev 2278) @@ -42,6 +42,13 @@ /** * {@inheritDoc} */ + public boolean isEmpty() { + return stack.isEmpty(); + } + + /** + * {@inheritDoc} + */ public Iterator<E> iterator() { return stack.iterator(); } Modified: artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialQueue.java =================================================================== --- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialQueue.java 2008-12-18 11:22:42 UTC (rev 2277) +++ artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialQueue.java 2008-12-18 14:59:18 UTC (rev 2278) @@ -18,6 +18,8 @@ */ package org.jiemamy.utils; +import java.util.NoSuchElementException; + /** * 本質的なキューのインターフェイス。 * @@ -29,17 +31,47 @@ */ public interface EssentialQueue<E> extends Iterable<E> { + /** + * キューをクリアする。 + */ void clear(); + /** + * キューから要素を1つ取り出す。 + * + * @return 取り出した要素 + * @throws NoSuchElementException 取り出す要素が無い場合 + */ E dequeue(); + /** + * キューに要素を追加する。 + * @param element 追加する要素 + */ void enqueue(E element); + /** + * 与えられたキューの内容を、このキューに全て追加する。 + * @param queue + */ void enqueue(EssentialQueue<E> queue); + /** + * 取り出す要素があるかどうか調べる。 + * @return キューが空の場合{@code true} + */ boolean isEmpty(); + /** + * 次にキューから取り出される要素を取得する。キューから削除は行わない。 + * @return 次にキューから取り出される要素 + * @throws NoSuchElementException 取り出す要素が無い場合 + */ E peek(); + /** + * キューから取り出せる要素の数を取得する。 + * @return 要素の数 + */ int size(); } Modified: artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialStack.java =================================================================== --- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialStack.java 2008-12-18 11:22:42 UTC (rev 2277) +++ artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/EssentialStack.java 2008-12-18 14:59:18 UTC (rev 2278) @@ -31,6 +31,8 @@ void clear(); + boolean isEmpty(); + E peek(); E peek(int n); Modified: artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ResolvableQueue.java =================================================================== --- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ResolvableQueue.java 2008-12-18 11:22:42 UTC (rev 2277) +++ artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/utils/ResolvableQueue.java 2008-12-18 14:59:18 UTC (rev 2278) @@ -43,6 +43,9 @@ */ public E dequeue() { resolve(); + if (stack.isEmpty()) { + throw new NoSuchElementException(); + } return (E) stack.peek().dequeue(); } Modified: artemis/trunk/org.jiemamy.core/src/test/java/org/jiemamy/utils/ResolvableQueueTest.java =================================================================== --- artemis/trunk/org.jiemamy.core/src/test/java/org/jiemamy/utils/ResolvableQueueTest.java 2008-12-18 11:22:42 UTC (rev 2277) +++ artemis/trunk/org.jiemamy.core/src/test/java/org/jiemamy/utils/ResolvableQueueTest.java 2008-12-18 14:59:18 UTC (rev 2278) @@ -20,7 +20,11 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import java.util.Iterator; +import java.util.NoSuchElementException; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -31,7 +35,7 @@ */ public class ResolvableQueueTest { - private ResolvableQueue<TestElement> queue; + private ResolvableQueue<Element> queue; /** @@ -40,7 +44,7 @@ */ @Before public void setUp() throws Exception { - queue = new ResolvableQueue<TestElement>(); + queue = new ResolvableQueue<Element>(); } /** @@ -53,10 +57,10 @@ } @Test - public void test01_キューとして機能する() throws Exception { - queue.enqueue(new TestElement('a')); - queue.enqueue(new TestElement('b')); - queue.enqueue(new TestElement('c')); + public void test01_キューとして普通に機能する() throws Exception { + queue.enqueue(new Element('a')); + queue.enqueue(new Element('b')); + queue.enqueue(new Element('c')); assertThat(queue.dequeue().ch, is('a')); assertThat(queue.dequeue().ch, is('b')); @@ -65,9 +69,9 @@ @Test public void test02_可分割キューとして機能する() throws Exception { - queue.enqueue(new TestElement('a')); - queue.enqueue(new ResolvableTestElement("xyz")); - queue.enqueue(new TestElement('c')); + queue.enqueue(new Element('a')); + queue.enqueue(new ResolvableElement("xyz")); + queue.enqueue(new Element('c')); assertThat(queue.dequeue().ch, is('a')); assertThat(queue.dequeue().ch, is('x')); @@ -77,72 +81,213 @@ } @Test - public void test03_複数段可分割キューとして機能する() throws Exception { - queue.enqueue(new TestElement('a')); - queue.enqueue(new ResolvableTestElement2("opq", "xyz")); - queue.enqueue(new TestElement('c')); + public void test03_多段可分割キューとして機能する() throws Exception { + queue.enqueue(new Element('a')); + queue.enqueue(new MultiResolvableElement("opq", "xyz")); + queue.enqueue(new Element('c')); assertThat(queue.dequeue().ch, is('a')); + assertThat(queue.dequeue().ch, is('o')); assertThat(queue.dequeue().ch, is('p')); assertThat(queue.dequeue().ch, is('q')); + assertThat(queue.dequeue().ch, is('x')); assertThat(queue.dequeue().ch, is('y')); assertThat(queue.dequeue().ch, is('z')); + assertThat(queue.dequeue().ch, is('c')); } + @Test + public void test04_キューをまとめてキューに追加できる() throws Exception { + queue.enqueue(new Element('a')); + queue.enqueue(new MultiResolvableElement("opq", "xyz")); + queue.enqueue(new Element('c')); + + ResolvableQueue<Element> queue2 = new ResolvableQueue<Element>(); + queue2.enqueue(new Element('A')); + queue2.enqueue(new MultiResolvableElement("OPQ", "XYZ")); + queue2.enqueue(new Element('C')); + + queue.enqueue(queue2); + + assertThat(queue.dequeue().ch, is('a')); + + assertThat(queue.dequeue().ch, is('o')); + assertThat(queue.dequeue().ch, is('p')); + assertThat(queue.dequeue().ch, is('q')); + + assertThat(queue.dequeue().ch, is('x')); + assertThat(queue.dequeue().ch, is('y')); + assertThat(queue.dequeue().ch, is('z')); + + assertThat(queue.dequeue().ch, is('c')); + + // 以下、queue2としてまとめてenqueueしたもの + + assertThat(queue.dequeue().ch, is('A')); + + assertThat(queue.dequeue().ch, is('O')); + assertThat(queue.dequeue().ch, is('P')); + assertThat(queue.dequeue().ch, is('Q')); + + assertThat(queue.dequeue().ch, is('X')); + assertThat(queue.dequeue().ch, is('Y')); + assertThat(queue.dequeue().ch, is('Z')); + + assertThat(queue.dequeue().ch, is('C')); + } + + @Test + public void test05_キューから取り出せるサイズとisEmptyの整合性() throws Exception { + // これは・・・実装難しいかな? + // resolveしないままサイズをはかれないから…。 + // Resolvableに size() メソッドを定義しても良い。 + // とにかくdequeueまではresolveしないことが重要。 + + assertThat(queue.isEmpty(), is(true)); + assertThat(queue.size(), is(0)); + + queue.enqueue(new Element('a')); + + assertThat(queue.isEmpty(), is(false)); + assertThat(queue.size(), is(1)); + + queue.enqueue(new MultiResolvableElement("opq", "xyz")); + + assertThat(queue.isEmpty(), is(false)); + assertThat(queue.size(), is(7)); + + queue.enqueue(new Element('c')); + + assertThat(queue.isEmpty(), is(false)); + assertThat(queue.size(), is(8)); + + queue.clear(); + + assertThat(queue.isEmpty(), is(true)); + assertThat(queue.size(), is(0)); + } + + @Test + public void test06_peekではキューからの削除が行われない() throws Exception { + queue.enqueue(new Element('a')); + queue.enqueue(new MultiResolvableElement("opq", "xyz")); + queue.enqueue(new Element('c')); + + assertThat(queue.peek().ch, is('a')); + assertThat(queue.peek().ch, is('a')); + queue.dequeue(); + assertThat(queue.peek().ch, is('o')); + assertThat(queue.peek().ch, is('o')); + queue.dequeue(); + assertThat(queue.peek().ch, is('p')); + assertThat(queue.peek().ch, is('p')); + queue.dequeue(); + assertThat(queue.peek().ch, is('q')); + assertThat(queue.peek().ch, is('q')); + queue.dequeue(); + assertThat(queue.peek().ch, is('x')); + assertThat(queue.peek().ch, is('x')); + queue.dequeue(); + assertThat(queue.peek().ch, is('y')); + assertThat(queue.peek().ch, is('y')); + queue.dequeue(); + assertThat(queue.peek().ch, is('z')); + assertThat(queue.peek().ch, is('z')); + queue.dequeue(); + assertThat(queue.peek().ch, is('c')); + assertThat(queue.peek().ch, is('c')); + } + + @Test + public void test07_Iteratorテスト() throws Exception { + queue.enqueue(new Element('a')); + queue.enqueue(new MultiResolvableElement("opq", "xyz")); + queue.enqueue(new Element('c')); + String ref = "aopqxyzc"; + + int index = 0; + Iterator<Element> iterator = queue.iterator(); + while (iterator.hasNext()) { + assertThat(iterator.next().ch, is(ref.charAt(index))); + index++; + } + // 8回回ったハズ + assertThat(index, is(8)); + + // イテレータを回しても、キューは崩されていないこと + assertThat(queue.peek().ch, is('a')); + } + + @Test + public void test07_要素がない時に取得を行うとNoSuchElementException() throws Exception { + try { + queue.dequeue(); + fail(); + } catch (NoSuchElementException e) { + // success + } + try { + queue.peek(); + fail(); + } catch (NoSuchElementException e) { + // success + } + } + - private static class ResolvableTestElement implements Resolvable<TestElement> { + private static class Element { - String str; + char ch; - ResolvableTestElement(String str) { + Element(char c) { + ch = c; + } + } + + private static class MultiResolvableElement implements Resolvable<Element> { + + String[] str; + + + MultiResolvableElement(String... str) { this.str = str; } /** * {@inheritDoc} */ - public EssentialQueue<TestElement> resolve() { - EssentialQueue<TestElement> queue = new LinkedEssentialQueue<TestElement>(); - char[] array = str.toCharArray(); - for (char c : array) { - queue.enqueue(new TestElement(c)); + public EssentialQueue<Element> resolve() { + ResolvableQueue<Element> queue = new ResolvableQueue<Element>(); + for (String s : str) { + queue.enqueue(new ResolvableElement(s)); } return queue; } } - private static class ResolvableTestElement2 implements Resolvable<TestElement> { + private static class ResolvableElement implements Resolvable<Element> { - String[] str; + String str; - ResolvableTestElement2(String... str) { + ResolvableElement(String str) { this.str = str; } /** * {@inheritDoc} */ - public EssentialQueue<TestElement> resolve() { - ResolvableQueue<TestElement> queue = new ResolvableQueue<TestElement>(); - for (String s : str) { - queue.enqueue(new ResolvableTestElement(s)); + public EssentialQueue<Element> resolve() { + EssentialQueue<Element> queue = new LinkedEssentialQueue<Element>(); + char[] array = str.toCharArray(); + for (char c : array) { + queue.enqueue(new Element(c)); } return queue; } } - - private static class TestElement { - - char ch; - - - TestElement(char c) { - ch = c; - } - } }