課題2テストコード
kadai/TDUSetTest.java
package kadai;
import static org.junit.Assert.*;
import java.util.Iterator;
import kadai.TDUSet;
import org.junit.Before;
import org.junit.Test;
public class TDUSetTest {
Integer[] array = {9,1,2,8,3,4,5,7,6};
TDUSet<Integer> set1;
TDUSet<Integer> set2;
@Before
public void setUp(){
set1 = new TDUSet<Integer>(new Integer[0]);
set2 = new TDUSet<Integer>(array);
}
@Test
public void testSize() {
assertEquals(0,set1.size());
assertEquals(array.length,set2.size());
}
@Test
public void testIterator() {
Iterator<Integer> it1 = set1.iterator();
assertFalse(it1.hasNext());
Iterator<Integer> it2 = set2.iterator();
for(int i=0 ; i< array.length; i++){
assertTrue(it2.hasNext());
assertEquals(array[i],it2.next());
}
assertFalse(it2.hasNext());
}
@Test
public void testToString0() {
assertEquals("[]",set1.toString());
assertEquals("[9, 1, 2, 8, 3, 4, 5, 7, 6]",set2.toString());
}
}
kadai/TDUIteratorTest.java
package test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kadai.TDUSet;
import org.junit.Before;
import org.junit.Test;
public class TDUIteratorTest {
private Integer[] array3 = {9,1,2};
private Integer[] array2 = {9,1,2,8,3,4,5,7,6};
private Iterator<Integer> iterator1;
private Iterator<Integer> iterator2;
private Iterator<Integer> iterator3;
@Before
public void setUp() throws Exception {
iterator1 = new TDUIterator<Integer>(new Integer[0]);
iterator2 = new TDUIterator<Integer>(array2);
iterator3 = new TDUIterator<Integer>(array3);
}
<E> E[] toArray(Iterator<E> it){
List<E> list = new ArrayList<E>();
while(it.hasNext()){
list.add(it.next());
}
@SuppressWarnings("unchecked")
E[] array = (E[]) list.toArray();
return array;
}
@Test
public void testHasNext() {
assertFalse(iterator1.hasNext());
}
@Test
public void testTotal(){
assertTrue(iterator3.hasNext());
assertEquals(9,iterator3.next().intValue());
assertTrue(iterator3.hasNext());
assertEquals(1,iterator3.next().intValue());
assertTrue(iterator3.hasNext());
assertEquals(2,iterator3.next().intValue());
assertFalse(iterator3.hasNext());
}
@Test
public void testNext() {
assertArrayEquals(new Integer[0],toArray(iterator1));
assertArrayEquals(array2,toArray(iterator2));
}
@Test(expected=UnsupportedOperationException.class)
public void testRemove(){
iterator1.remove();
}
}
kadai/TDUMap1Test.java
package test;
import static org.junit.Assert.*;
import java.util.Map;
import kadai.TDUMapEntry1;
import kaitou.TDUMap1;
import org.junit.Test;
public class TDUMap1Test {
@Test
public void testSizeTDUMapEntry() {
Map<String, Integer> m = new TDUMap1(null);
assertEquals(0,m.size());
final TDUMapEntry1 e1 = new TDUMapEntry1("abc",123);
m = new TDUMap1(e1);
assertEquals(1,m.size());
assertEquals(1,m.size());
final TDUMapEntry1 e2 = new TDUMapEntry1("def",456);
e1.left=e2;
m = new TDUMap1(e1);
assertEquals(2,m.size());
assertEquals(2,m.size());
final TDUMapEntry1 e3 = new TDUMapEntry1("ghi",789);
e1.right=e3;
m = new TDUMap1(e1);
assertEquals(3,m.size());
assertEquals(3,m.size());
final TDUMapEntry1 e4 = new TDUMapEntry1("jkl",12);
final TDUMapEntry1 e5 = new TDUMapEntry1("mno",345);
final TDUMapEntry1 e6 = new TDUMapEntry1("pqr",678);
e2.left=e4;
e2.right=e5;
e3.right=e6;
m = new TDUMap1(e1);
assertEquals(6,m.size());
assertEquals(6,m.size());
}
}
kadai/TDUMap2Test.java
package test;
import static org.junit.Assert.*;
import java.util.Map;
import kadai.TDUMapEntry1;
import kaitou.TDUMap2;
import org.junit.Test;
public class TDUMap2Test {
@Test
public void testEntrySet() {
Map<String, Integer> m = new TDUMap2(null);
TDUMapEntry1[] expect;
expect=new TDUMapEntry1[0];
assertArrayEquals(expect,m.entrySet().toArray());
assertArrayEquals(expect,m.entrySet().toArray());
final TDUMapEntry1 e1 = new TDUMapEntry1("abc",123);
m = new TDUMap2(e1);
expect=new TDUMapEntry1[]{new TDUMapEntry1("abc",123)};
assertArrayEquals(m.entrySet().toArray(),expect);
assertArrayEquals(m.entrySet().toArray(),expect);
final TDUMapEntry1 e2 = new TDUMapEntry1("def",456);
e1.left=e2;
m = new TDUMap2(e1);
expect=new TDUMapEntry1[]{
new TDUMapEntry1("def",456),
new TDUMapEntry1("abc",123)
};
assertArrayEquals(expect,m.entrySet().toArray());
assertArrayEquals(expect,m.entrySet().toArray());
final TDUMapEntry1 e3 = new TDUMapEntry1("ghi",789);
e1.right=e3;
m = new TDUMap2(e1);
expect=new TDUMapEntry1[]{
new TDUMapEntry1("def",456),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("ghi",789)
};
assertArrayEquals(expect,m.entrySet().toArray());
assertArrayEquals(expect,m.entrySet().toArray());
final TDUMapEntry1 e4 = new TDUMapEntry1("jkl",12);
e2.left=e4;
m = new TDUMap2(e1);
expect=new TDUMapEntry1[]{
new TDUMapEntry1("jkl",12),
new TDUMapEntry1("def",456),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("ghi",789)
};
assertArrayEquals(expect,m.entrySet().toArray());
assertArrayEquals(expect,m.entrySet().toArray());
final TDUMapEntry1 e5 = new TDUMapEntry1("mno",345);
e2.right=e5;
m = new TDUMap2(e1);
expect=new TDUMapEntry1[]{
new TDUMapEntry1("jkl",12),
new TDUMapEntry1("def",456),
new TDUMapEntry1("mno",345),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("ghi",789)
};
assertArrayEquals(expect,m.entrySet().toArray());
assertArrayEquals(expect,m.entrySet().toArray());
final TDUMapEntry1 e6 = new TDUMapEntry1("pqr",678);
e3.right=e6;
m = new TDUMap2(e1);
expect=new TDUMapEntry1[]{
new TDUMapEntry1("jkl",12),
new TDUMapEntry1("def",456),
new TDUMapEntry1("mno",345),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("pqr",678)
};
assertArrayEquals(expect,m.entrySet().toArray());
assertArrayEquals(expect,m.entrySet().toArray());
}
}
kadai/TDUMap3Test.java
package test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import java.util.Map;
import kadai.TDUMapEntry1;
import kaitou.TDUMap3;
import org.junit.Test;
public class TDUMap3Test {
@Test
public void testPutStringInteger() {
Map<String,Integer> map = new TDUMap3();
TDUMapEntry1[] expect;
expect=new TDUMapEntry1[0];
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("jkl",12),is(nullValue()));
expect=new TDUMapEntry1[]{new TDUMapEntry1("jkl",12)};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("jkl",13),is(12));
expect=new TDUMapEntry1[]{new TDUMapEntry1("jkl",13)};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("def",456),is(nullValue()));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("def",456),
new TDUMapEntry1("jkl",13)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("mno",345),is(nullValue()));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("def",456),
new TDUMapEntry1("jkl",13),
new TDUMapEntry1("mno",345)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("abc",123),is(nullValue()));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",456),
new TDUMapEntry1("jkl",13),
new TDUMapEntry1("mno",345)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("ghi",789),is(nullValue()));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",456),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("jkl",13),
new TDUMapEntry1("mno",345)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("pqr",678),is(nullValue()));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",456),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("jkl",13),
new TDUMapEntry1("mno",345),
new TDUMapEntry1("pqr",678)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put(new String("jkl"),901),is(13));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",456),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("jkl",901),
new TDUMapEntry1("mno",345),
new TDUMapEntry1("pqr",678)
};
assertArrayEquals(expect, map.entrySet().toArray());
assertThat(map.put("ABC",456),is(nullValue()));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("ABC",456),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",456),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("jkl",901),
new TDUMapEntry1("mno",345),
new TDUMapEntry1("pqr",678)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("def",234),is(456));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("ABC",456),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",234),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("jkl",901),
new TDUMapEntry1("mno",345),
new TDUMapEntry1("pqr",678)
};
assertArrayEquals(expect,map.entrySet().toArray());
assertThat(map.put("mno",567),is(345));
expect=new TDUMapEntry1[]{
new TDUMapEntry1("ABC",456),
new TDUMapEntry1("abc",123),
new TDUMapEntry1("def",234),
new TDUMapEntry1("ghi",789),
new TDUMapEntry1("jkl",901),
new TDUMapEntry1("mno",567),
new TDUMapEntry1("pqr",678)
};
assertArrayEquals(expect,map.entrySet().toArray());
}
}
kadai/TDUMapTest.java
package test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import java.util.Map;
import kadai.TDUMapEntry;
import kaitou.TDUMap;
import org.junit.Test;
public class TDUMapTest {
@Test
public void testSize() {
Map<String, Double> m = new TDUMap<String,Double>(null);
assertThat(m.size(),is(0));
assertThat(m.size(),is(0));
final TDUMapEntry<String,Double> e1 = new TDUMapEntry<String,Double>("abc",0.123);
m = new TDUMap<String, Double>(e1);
assertThat(m.size(),is(1));
assertThat(m.size(),is(1));
final TDUMapEntry<String, Double> e2 = new TDUMapEntry<String,Double>("def",0.456);
e1.left=e2;
m = new TDUMap<String, Double>(e1);
assertThat(m.size(),is(2));
assertThat(m.size(),is(2));
final TDUMapEntry<String, Double> e3 = new TDUMapEntry<String, Double>("ghi",0.789);
e1.right=e3;
m = new TDUMap<String, Double>(e1);
assertThat(m.size(),is(3));
assertThat(m.size(),is(3));
final TDUMapEntry<String, Double> e4 = new TDUMapEntry<String, Double>("jkl",0.12);
final TDUMapEntry<String, Double> e5 = new TDUMapEntry<String, Double>("mno",0.345);
final TDUMapEntry<String, Double> e6 = new TDUMapEntry<String, Double>("pqr",0.678);
e2.left=e4;
e2.right=e5;
e3.right=e6;
m = new TDUMap<String,Double>(e1);
assertThat(m.size(),is(6));
assertThat(m.size(),is(6));
}
@Test
public void testEntrySet() {
Map<String, Double> m = new TDUMap<String,Double>(null);
assertEquals("[]",m.entrySet().toString());
assertEquals("[]",m.entrySet().toString());
final TDUMapEntry<String, Double> e1 = new TDUMapEntry<String, Double>("abc",0.123);
m = new TDUMap<String,Double>(e1);
assertEquals("[abc=0.123]",m.entrySet().toString());
assertEquals("[abc=0.123]",m.entrySet().toString());
final TDUMapEntry<String, Double> e2 = new TDUMapEntry<String, Double>("def",0.456);
e1.left=e2;
m = new TDUMap<String,Double>(e1);
assertEquals("[def=0.456, abc=0.123]",m.entrySet().toString());
assertEquals("[def=0.456, abc=0.123]",m.entrySet().toString());
final TDUMapEntry<String, Double> e3 = new TDUMapEntry<String, Double>("ghi",0.789);
e1.right=e3;
m = new TDUMap<String,Double>(e1);
assertEquals("[def=0.456, abc=0.123, ghi=0.789]",m.entrySet().toString());
assertEquals("[def=0.456, abc=0.123, ghi=0.789]",m.entrySet().toString());
final TDUMapEntry<String, Double> e4 = new TDUMapEntry<String, Double>("jkl",0.12);
e2.left=e4;
m = new TDUMap<String,Double>(e1);
assertEquals("[jkl=0.12, def=0.456, abc=0.123, ghi=0.789]",m.entrySet().toString());
assertEquals("[jkl=0.12, def=0.456, abc=0.123, ghi=0.789]",m.entrySet().toString());
final TDUMapEntry<String, Double> e5 = new TDUMapEntry<String, Double>("mno",0.345);
e2.right=e5;
m = new TDUMap<String,Double>(e1);
assertEquals("[jkl=0.12, def=0.456, mno=0.345, abc=0.123, ghi=0.789]",m.entrySet().toString());
assertEquals("[jkl=0.12, def=0.456, mno=0.345, abc=0.123, ghi=0.789]",m.entrySet().toString());
final TDUMapEntry<String, Double> e6 = new TDUMapEntry<String, Double>("pqr",0.678);
e3.right=e6;
m = new TDUMap<String,Double>(e1);
assertEquals("[jkl=0.12, def=0.456, mno=0.345, abc=0.123, ghi=0.789, pqr=0.678]",m.entrySet().toString());
assertEquals("[jkl=0.12, def=0.456, mno=0.345, abc=0.123, ghi=0.789, pqr=0.678]",m.entrySet().toString());
}
@Test
public void testPut() {
Map<String,Double> map = new TDUMap<String,Double>();
assertEquals("[]",map.entrySet().toString());
assertEquals(null,map.put("jkl",0.12));
assertEquals("[jkl=0.12]",map.entrySet().toString());
assertEquals(new Double(0.12),map.put("jkl",0.13));
assertEquals("[jkl=0.13]",map.entrySet().toString());
assertEquals(null,map.put("def",0.456));
assertEquals("[def=0.456, jkl=0.13]",map.entrySet().toString());
assertEquals(null,map.put("mno",0.345));
assertEquals("[def=0.456, jkl=0.13, mno=0.345]",map.entrySet().toString());
assertEquals(null,map.put("abc",0.123));
assertEquals("[abc=0.123, def=0.456, jkl=0.13, mno=0.345]",map.entrySet().toString());
assertEquals(null,map.put("ghi",0.789));
assertEquals("[abc=0.123, def=0.456, ghi=0.789, jkl=0.13, mno=0.345]",map.entrySet().toString());
assertEquals(null,map.put("pqr",0.678));
assertEquals("[abc=0.123, def=0.456, ghi=0.789, jkl=0.13, mno=0.345, pqr=0.678]",map.entrySet().toString());
assertEquals(new Double(0.13),map.put(new String("jkl"),0.901));
assertEquals("[abc=0.123, def=0.456, ghi=0.789, jkl=0.901, mno=0.345, pqr=0.678]",map.entrySet().toString());
assertEquals(null,map.put("ABC",0.456));
assertEquals("[ABC=0.456, abc=0.123, def=0.456, ghi=0.789, jkl=0.901, mno=0.345, pqr=0.678]",map.entrySet().toString());
assertEquals(new Double(0.456),map.put("def",0.234));
assertEquals("[ABC=0.456, abc=0.123, def=0.234, ghi=0.789, jkl=0.901, mno=0.345, pqr=0.678]",map.entrySet().toString());
assertEquals(new Double(0.345),map.put("mno",0.567));
assertEquals("[ABC=0.456, abc=0.123, def=0.234, ghi=0.789, jkl=0.901, mno=0.567, pqr=0.678]",map.entrySet().toString());
}
}