課題2テストコード

test/TDUListIterator4Test.java


package test;
import static org.junit.Assert.*;
import static test.TDUListIterator3Test.getList;
import java.util.NoSuchElementException;
import kotae.Node;
import kotae.TDUListIterator3;
import kotae.TDUListIterator4;
import org.junit.Test;
public class TDUListIterator4Test {
	@Test
	public void testConstructor() {
		TDUListIterator4 listIterator = new TDUListIterator4((new Node<String>()),0);
		assertTrue(listIterator instanceof TDUListIterator3);
	}
	@Test(expected=NoSuchElementException.class)
	public void testNext() {
		Node<String> node= getList();
		TDUListIterator4 listIterator = new TDUListIterator4(node,0);
		assertTrue(listIterator.hasNext());
		assertEquals("abc",listIterator.next());
		assertTrue(listIterator.hasNext());
		assertEquals(null,listIterator.next());
		assertTrue(listIterator.hasNext());
		assertEquals("ghi",listIterator.next());
		assertFalse(listIterator.hasNext());
		listIterator.next();
	}
}

test/TDUListIterator5Test.java


package test;
import static org.junit.Assert.*;
import static test.TDUListIterator3Test.*;
import org.junit.Test;
import java.util.ListIterator;
import kadai.TDUList5;
import kotae.Node;
import kotae.TDUListIterator4;
import kotae.TDUListIterator5;
public class TDUListIterator5Test {
	@Test
	public void testConstructor() {
		TDUListIterator5 listIterator = new TDUListIterator5((new Node<String>()),0);
		assertTrue(listIterator instanceof TDUListIterator4);
	}
	@Test
	public void testLast() {
		Node<String> node = getEmptyList();
		TDUListIterator5 listIterator = new TDUListIterator5(node,0);
		assertNull(listIterator.last);
		listIterator.add("abc");
		assertNull(listIterator.last);
		listIterator = new TDUListIterator5(node,0);
		assertEquals("abc",listIterator.next());
		assertEquals("abc",listIterator.last.get());
		listIterator.add("def");
		assertNull(listIterator.last);	
	}
	@Test(expected=IllegalStateException.class)
	public void testSet0() {
		TDUListIterator5 listIterator = new TDUListIterator5(getEmptyList(),0);
		listIterator.set("xyz");
	}
	@Test(expected=IllegalStateException.class)
	public void testSet1() {
		TDUList5 list = new TDUList5();
		list.add("abc");
		ListIterator<String> listIterator =  list.listIterator(0);
		assertEquals("abc",listIterator.next());
		listIterator.add("def");
		listIterator.set("xyz");
	}
	@Test
	public void testSet2() {
		Node<String> list = getList();
		TDUListIterator5 listIterator = new TDUListIterator5(list,0);
		assertEquals("abc",listIterator.next());
		listIterator.set("123");
		assertEquals(null,listIterator.next());
		listIterator.set("456");
		assertEquals("ghi",listIterator.next());
		listIterator.set("789");
		assertEquals("123",list.get());
		assertEquals("456",list.next.get());
		assertEquals("789",list.next.next.get());
	}
}

test/TDUListIteratorTest.java


package test;
import static org.junit.Assert.*;
import kotae.TDUListIterator;
import org.junit.Test;
import static test.TDUListIterator3Test.*;
public class TDUListIteratorTest {	
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor00(){
		assertNotNull(new TDUListIterator<String>(getEmptyList(),0));
		new TDUListIterator<String>(getEmptyList(),-1);
	}
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor01(){
		new TDUListIterator<String>(getEmptyList(),1);
	}
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor30(){
		new TDUListIterator<String>(getList(),-1);
	}
	@Test(expected=IllegalStateException.class)
	public void testSet0() {
		TDUListIterator<String> listIterator = new TDUListIterator<String>(getEmptyList(),0);
		listIterator.set("xyz");
	}
	@Test(expected=IllegalStateException.class)
	public void testSet1() {
		TDUListIterator<String> listIterator = new TDUListIterator<String>(getEmptyList(),0);
		listIterator.add("abc");
		listIterator.set("xyz");
	}
	@Test(expected=UnsupportedOperationException.class)
	public void removeTest(){
		TDUListIterator<String> listIterator = new TDUListIterator<String>(getList(),0);
		listIterator.next();
		listIterator.remove();
	}
}

坂本直志 <sakamoto@c.dendai.ac.jp>
東京電機大学工学部情報通信工学科