テストプログラム

TDUListTest


package tdu;

import static org.junit.Assert.*;

import java.util.Iterator;

import org.junit.Test;

public class TDUListTest {
	@Test
	public void testString(){
		TDUList<String> list = new TDUList<String>();
		list.add("abc");
		list.add(null);
		list.add("ghi");
		assertEquals(3,list.size());
		Iterator<String> iterator = list.iterator();
		assertTrue(iterator.hasNext());
		assertEquals("abc",iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals(null,iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals("ghi",iterator.next());
		assertFalse(iterator.hasNext());	
		list.set(1,"def");
		assertEquals("[abc, def, ghi]",list.toString());
	}
	@Test
	public void testDoouble(){
		TDUList<Double> list = new TDUList<Double>();
		list.add(1.23);
		list.add(null);
		list.add(7.89);
		assertEquals(3,list.size());
		Iterator<Double> iterator = list.iterator();
		assertTrue(iterator.hasNext());
		assertEquals(new Double(1.23),iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals(null,iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals(new Double(7.89),iterator.next());
		assertFalse(iterator.hasNext());
		list.set(1, 4.56);
		assertEquals("[1.23, 4.56, 7.89]",list.toString());
	}
	
}

TDUListIteratorTest


package tdu;

import static org.junit.Assert.*;

import org.junit.Test;

public class TDUListIteratorTest {
	private Node<String> getList(){
		Node<String> n0 = new Node<String>();
		Node<String> n3 = new Node<String>("abc");
		Node<String> n2 = new Node<String>(null);
		Node<String> n1 = new Node<String>("ghi");
		n1.next=n0;
		n2.next=n1;
		n3.next=n2;
		return n3;
	}
	private Node<String> getEmptyList(){
		return new Node<String>();
	}
	@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();
	}
}