課題2テストコード
課題2-1関係
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<>();
assertNull(n1.get());
assertNull(n1.next);
}
@Test
public void constructorTest1(){
Node<String> n1 = new Node<>("abc");
assertEquals("abc", n1.get());
assertNull(n1.next);
}
@Test
public void constructorTest2(){
Node<Integer> n1 = new Node<>(1);
assertEquals(new Integer(1), n1.get());
assertNull(n1.next);
}
@Test
public void setTest1(){
Node<String> n1 = new Node<>();
n1.set("abc");
assertEquals("abc", n1.get());
assertNull(n1.next);
}
@Test
public void setTest2(){
Node<Integer> n1= new Node<>();
n1.set(1);
assertEquals(new Integer(1), n1.get());
assertNull(n1.next);
}
@Test
public void toStringTest(){
Node<String> n3 = new Node<>("def");
assertEquals("def", n3.toString());
Node<Integer> n4 = new Node<>(2);
assertEquals("2", n4.toString());
List<Double> o5 = new ArrayList<>();
o5.add(1.23);
o5.add(4.56);
o5.add(7.89);
Node<List<Double>> n5 = new Node<>(o5);
assertEquals("[1.23, 4.56, 7.89]",n5.toString());
}
}
課題2-2
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<>();
Node<String> n3 = new Node<>("abc");
Node<String> n2 = new Node<>(null);
Node<String> n1 = new Node<>("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());
}
}
課題2-3
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());
}
}
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<>();
Node<String> n3 = new Node<>("abc");
Node<String> n2 = new Node<>(null);
Node<String> n1 = new Node<>("ghi");
n1.next=n0;
n2.next=n1;
n3.next=n2;
return n3;
}
static Node<String> getEmptyList(){
return new Node<>();
}
@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");
}
}
課題2-4
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();
}
}
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<>()),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());
}
}
課題2-5
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));
}
}
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<>()),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());
}
}
課題2-6
package test;
import static org.junit.Assert.*;
import java.util.Iterator;
import kotae.TDUList;
import org.junit.Test;
public class TDUListTest {
@Test
public void testString(){
TDUList<String> list = new TDUList<>();
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<>();
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());
}
}
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<>(getEmptyList(),0));
new TDUListIterator<>(getEmptyList(),-1);
}
@Test(expected=IndexOutOfBoundsException.class)
public void testConstructor01(){
new TDUListIterator<>(getEmptyList(),1);
}
@Test(expected=IndexOutOfBoundsException.class)
public void testConstructor30(){
new TDUListIterator<>(getList(),-1);
}
@Test(expected=IllegalStateException.class)
public void testSet0() {
TDUListIterator<String> listIterator = new TDUListIterator<>(getEmptyList(),0);
listIterator.set("xyz");
}
@Test(expected=IllegalStateException.class)
public void testSet1() {
TDUListIterator<String> listIterator = new TDUListIterator<>(getEmptyList(),0);
listIterator.add("abc");
listIterator.set("xyz");
}
@Test(expected=UnsupportedOperationException.class)
public void removeTest(){
TDUListIterator<String> listIterator = new TDUListIterator<>(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());
}
}
坂本直志 <sakamoto@c.dendai.ac.jp>
東京電機大学工学部情報通信工学科