課題2テストコード

古いコードは別ページへ

test/NodeTest.java


package test;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.List;
import java.util.ArrayList;
import kotae.Node;
public class NodeTest {
	@Test
	public void constructorTest0(){	
		Node<String> n1 = new Node<String>();
		assertNull(n1.get());
		assertNull(n1.next);
	}
	@Test
	public void constructorTest1(){	
		Node<String> n1 = new Node<String>("abc");
		assertEquals("abc", n1.get());
		assertNull(n1.next);
	}
	@Test
	public void constructorTest2(){	
		Node<Integer> n1 = new Node<Integer>(1);
		assertEquals(new Integer(1), n1.get());
		assertNull(n1.next);
	}
	@Test
	public void setTest1(){
		Node<String> n1 = new Node<String>();
		n1.set("abc");
		assertEquals("abc", n1.get());
		assertNull(n1.next);	
	}
	@Test
	public void setTest2(){	
		Node<Integer> n1= new Node<Integer>();
		n1.set(1);
		assertEquals(new Integer(1), n1.get());
		assertNull(n1.next);
	}
	@Test
	public void toStringTest(){	
		Node<String> n3 = new Node<String>("def");
		assertEquals("def", n3.toString());
		Node<Integer> n4 = new Node<Integer>(2);
		assertEquals("2", n4.toString());
		List<Double> o5 = new ArrayList<Double>();
		o5.add(1.23);
		o5.add(4.56);
		o5.add(7.89);
		Node<List<Double>> n5 = new Node<List<Double>>(o5);
		assertEquals("[1.23, 4.56, 7.89]",n5.toString());
	}
}

kadai/TDUListIterator1Test.java


package test;
import static org.junit.Assert.*;
import java.util.NoSuchElementException;
import kadai.TDUListIterator1;
import org.junit.Before;
import org.junit.Test;
public class TDUListIterator1Test {
	private TDUListIterator1 iterator;
	@Before
	public void setUp() throws Exception {
		iterator = new TDUListIterator1(null,0);
	}
	@Test(expected=UnsupportedOperationException.class)
	public void testAdd() {
		iterator.add(null);
		fail("This test must not reach here.");
	}
	@Test
	public void testHasNext() {
		assertFalse(iterator.hasNext());
	}
	@Test
	public void testHasPrevious() {
		assertFalse(iterator.hasPrevious());
	}
	@Test(expected=NoSuchElementException.class)
	public void testNext() {
		iterator.next();
		fail("This test must not reach here.");
	}
	@Test
	public void testNextIndex() {
		assertEquals(0,iterator.nextIndex());
	}
	@Test(expected=NoSuchElementException.class)
	public void testPrevious() {
		iterator.previous();
	}
	@Test
	public void testPreviousIndex() {
		assertEquals(-1,iterator.previousIndex());
	}
	@Test(expected=UnsupportedOperationException.class)
	public void testRemove() {
		iterator.remove();
		fail("This test must not reach here.");
	}
	@Test(expected=UnsupportedOperationException.class)
	public void testSet() {
		iterator.set(null);
		fail("This test must not reach here.");
	}
}

test/TDUList2Test.java


package test;
import static org.junit.Assert.*;
import java.util.List;
import kadai.TDUList1;
import kotae.Node;
import kotae.TDUList2;
import org.junit.Test;
public class TDUList2Test {
	@Test
	public void testTDUList2(){
		List<String> list = new TDUList2();
		assertTrue(list instanceof TDUList1);
	}
	@Test
	public void testSize() {
		TDUList2 list = new TDUList2();
		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>("efg");
		n1.next=n0;
		n2.next=n1;
		n3.next=n2;
		list.node=n0;
		assertEquals(0,list.size());
		list.node=n1;
		assertEquals(1,list.size());
		list.node=n2;
		assertEquals(2,list.size());
		list.node=n3;
		assertEquals(3,list.size());
		assertEquals(3,list.size());
	}
}

test/TDUList3Test.java


package test;
import static org.junit.Assert.*;
import kadai.TDUList3;
import org.junit.Test;
public class TDUList3Test {
	@Test
	public void testAdd() {
		TDUList3 list = new TDUList3();
		assertEquals(0,list.size());
		list.add("abc");
		assertEquals(1,list.size());
		list.add(null);
		assertEquals(2,list.size());
		list.add("ghi");
		assertEquals(3,list.size());
	}
}

test/TDUListIterator3Test.java


package test;
import static org.junit.Assert.*;
import java.util.NoSuchElementException;
import kadai.TDUListIterator1;
import kotae.Node;
import kotae.TDUListIterator3;
import org.junit.Test;
public class TDUListIterator3Test {
	static 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;
	}
	static Node<String> getEmptyList(){
		return new Node<String>();
	}
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor00(){
		assertNotNull(new TDUListIterator3(getEmptyList(),0));
		new TDUListIterator3(getEmptyList(),-1);
	}
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor01(){
		new TDUListIterator3(getEmptyList(),1);
	}
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor30(){
		new TDUListIterator3(getList(),-1);
	}
	@Test
	public void testConstructor3(){
		assertTrue((new TDUListIterator3(getList(),0)) instanceof TDUListIterator1);
		assertTrue((new TDUListIterator3(getList(),1)) instanceof TDUListIterator1);
		assertTrue((new TDUListIterator3(getList(),2)) instanceof TDUListIterator1);
		assertTrue((new TDUListIterator3(getList(),3)) instanceof TDUListIterator1);
	}
	@Test(expected=IndexOutOfBoundsException.class)
	public void testConstructor34(){
		new TDUListIterator3(getList(),4);
	}
	@Test
	public void testAdd1() {
		Node<String> node=getEmptyList();
		TDUListIterator3 listIterator = new TDUListIterator3(node,0);
		listIterator.add("xyz");
		listIterator.add("012");
		assertEquals("xyz",node.get());
		assertEquals("012",node.next.get());
	}
	
	@Test
	public void testAdd2() {
		Node<String> node=getList();
		TDUListIterator3 listIterator = new TDUListIterator3(node,1);
		listIterator.add("xyz");
		listIterator.add("012");
		assertEquals("abc",node.get());
		assertEquals("xyz",node.next.get());
		assertEquals("012",node.next.next.get());
		assertEquals(null,node.next.next.next.get());
		assertEquals("ghi",node.next.next.next.next.get());
	}
	@Test
	public void testAdd3() {
		Node<String> node=getList();
		TDUListIterator3 listIterator = new TDUListIterator3(node,3);
		listIterator.add("xyz");
		listIterator.add("012");
		assertEquals("abc",node.get());
		assertEquals(null,node.next.get());
		assertEquals("ghi",node.next.next.get());
		assertEquals("xyz",node.next.next.next.get());
		assertEquals("012",node.next.next.next.next.get());
	}
	@Test
	public void testAdd4() {
		Node<String> node=getList();
		TDUListIterator3 listIterator = new TDUListIterator3(node,0);
		listIterator.add("xyz");
		listIterator.add("012");
		assertEquals("xyz",node.get());
		assertEquals("012",node.next.get());
		assertEquals("abc",node.next.next.get());
		assertEquals(null,node.next.next.next.get());
		assertEquals("ghi",node.next.next.next.next.get());
	}
	@Test
	public void testAdd5() {
		Node<String> node=getList();
		TDUListIterator3 listIterator = new TDUListIterator3(node,2);
		listIterator.add("xyz");
		listIterator.add("012");
		assertEquals("abc",node.get());
		assertEquals(null,node.next.get());
		assertEquals("xyz",node.next.next.get());
		assertEquals("012",node.next.next.next.get());
		assertEquals("ghi",node.next.next.next.next.get());
	}
	@Test(expected=NoSuchElementException.class)
	public void removeTest(){
		TDUListIterator3 listIterator = new TDUListIterator3(getList(),0);
		listIterator.next();
		listIterator.remove();
	}
	@Test(expected=NoSuchElementException.class)
	public void setTest(){
		TDUListIterator3 listIterator = new TDUListIterator3(getList(),0);
		listIterator.next();
		listIterator.set("xyz");	
	}
}

test/TDUList4Test.java


package test;
import static org.junit.Assert.*;
import static test.TDUListIterator3Test.*;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import kadai.TDUList4;
import kotae.TDUListIterator3;
import org.junit.Test;
public class TDUList4Test {
	@Test
	public void testListIterator() {
		ListIterator<String> listIterator = (new TDUList4()).listIterator(0);
		Class<?> superClass= new TDUListIterator3(null, 0).getClass();
		assertEquals(superClass, listIterator.getClass().getSuperclass());
	}
	@Test
	public void test0() {
		TDUList4 list = new TDUList4();
		Iterator<String> iterator = list.iterator();
		assertFalse(iterator.hasNext());
	}
	@Test(expected=NoSuchElementException.class)
	public void test01() {
		TDUList4 list = new TDUList4();
		Iterator<String> iterator = list.iterator();
		assertFalse(iterator.hasNext());
		iterator.next();
	}
	@Test
	public void test3() {
		TDUList4 list = new TDUList4();
		list.node = getList();
		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());
	}
	@Test(expected=NoSuchElementException.class)
	public void testAdd(){
		TDUList4 list = new TDUList4();
		list.add("hij");
		list.add(null);
		list.add("nop");
		Iterator<String> iterator = list.iterator();
		assertTrue(iterator.hasNext());
		assertEquals("hij",iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals(null,iterator.next());
		assertTrue(iterator.hasNext());
		assertEquals("nop",iterator.next());
		assertFalse(iterator.hasNext());
		iterator.next();
	}
}

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
	public void testNext2() {
		Node<String> node= getList();
		TDUListIterator4 listIterator = new TDUListIterator4(node,1);
		assertTrue(listIterator.hasNext());
		assertEquals(null,listIterator.next());
		listIterator.add("jkl");
		assertTrue(listIterator.hasNext());
		assertEquals("ghi",listIterator.next());
	}
}

test/TDUList5Test.java


package test;
import static org.junit.Assert.*;
import kadai.TDUList5;
import org.junit.Test;
public class TDUList5Test {
	@Test
	public void testSet0() {
		TDUList5 list = new TDUList5();
		list.add("abc");
		list.add(null);
		list.add("ghi");
		list.set(1,"456");
		list.set(0,"123");
		list.set(2,"789");
		assertEquals("789",list.get(2));
		assertEquals("123",list.get(0));
		assertEquals("456",list.get(1));
	}
}

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
	public void testSet3() {
		Node<String> list = getList();
		TDUListIterator5 listIterator = new TDUListIterator5(list,1);
		assertEquals(null,listIterator.next());
		listIterator.set("123");
		listIterator.add("jkl");
		assertEquals("ghi",listIterator.next());
		listIterator.set("456");
		assertEquals("abc",list.get());
		assertEquals("123",list.next.get());
		assertEquals("jkl",list.next.next.get());
		assertEquals("456",list.next.next.next.get());
	}
}

test/TDUListIteratorTest.java


package test;
import static org.junit.Assert.*;

import java.util.ListIterator;

import kadai.TDUList5;
import kotae.Node;
import kotae.TDUListIterator;
import kotae.TDUListIterator5;

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();
	}
	@Test(expected=IllegalStateException.class)
	public void testSet50() {
		TDUListIterator5 listIterator = new TDUListIterator5(getEmptyList(),0);
		listIterator.set("xyz");
	}
	@Test(expected=IllegalStateException.class)
	public void testSet51() {
		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 testSet52() {
		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
	public void testSet53() {
		Node<String> list = getList();
		TDUListIterator5 listIterator = new TDUListIterator5(list,1);
		assertEquals(null,listIterator.next());
		listIterator.set("123");
		listIterator.add("jkl");
		assertEquals("ghi",listIterator.next());
		listIterator.set("456");
		assertEquals("abc",list.get());
		assertEquals("123",list.next.get());
		assertEquals("jkl",list.next.next.get());
		assertEquals("456",list.next.next.next.get());
	}
}

坂本直志 <[email protected]>
東京電機大学工学部情報通信工学科