課題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/TDUMap1Test.java
package kadai;
import static org.junit.Assert.*;
import java.util.Map;
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 kadai;
import static org.junit.Assert.*;
import java.util.Map;
import kaitou.TDUMap2;
import org.junit.Test;
public class TDUMap2Test {
@Test
public void testEntrySet() {
Map<String, Integer> m = new TDUMap2(null);
assertEquals("[]",m.entrySet().toString());
final TDUMapEntry1 e1 = new TDUMapEntry1("abc",123);
m = new TDUMap2(e1);
assertEquals("[abc=123]",m.entrySet().toString());
final TDUMapEntry1 e2 = new TDUMapEntry1("def",456);
e1.left=e2;
m = new TDUMap2(e1);
assertEquals("[def=456, abc=123]",m.entrySet().toString());
final TDUMapEntry1 e3 = new TDUMapEntry1("ghi",789);
e1.right=e3;
m = new TDUMap2(e1);
assertEquals("[def=456, abc=123, ghi=789]",m.entrySet().toString());
final TDUMapEntry1 e4 = new TDUMapEntry1("jkl",12);
e2.left=e4;
m = new TDUMap2(e1);
assertEquals("[jkl=12, def=456, abc=123, ghi=789]",m.entrySet().toString());
final TDUMapEntry1 e5 = new TDUMapEntry1("mno",345);
e2.right=e5;
m = new TDUMap2(e1);
assertEquals("[jkl=12, def=456, mno=345, abc=123, ghi=789]",m.entrySet().toString());
final TDUMapEntry1 e6 = new TDUMapEntry1("pqr",678);
e3.right=e6;
m = new TDUMap2(e1);
assertEquals("[jkl=12, def=456, mno=345, abc=123, ghi=789, pqr=678]",m.entrySet().toString());
}
}
kadai/TDUMap3.java
package kadai;
import static org.junit.Assert.*;
import java.util.Map;
import kaitou.TDUMap3;
import org.junit.Test;
public class TDUMap3Test {
@Test
public void testPutStringInteger() {
Map<String,Integer> map = new TDUMap3();
assertEquals("[]",map.entrySet().toString());
assertEquals(null,map.put("jkl",12));
assertEquals("[jkl=12]",map.entrySet().toString());
assertEquals(new Integer(12),map.put("jkl",13));
assertEquals("[jkl=13]",map.entrySet().toString());
assertEquals(null,map.put("def",456));
assertEquals("[def=456, jkl=13]",map.entrySet().toString());
assertEquals(null,map.put("mno",345));
assertEquals("[def=456, jkl=13, mno=345]",map.entrySet().toString());
assertEquals(null,map.put("abc",123));
assertEquals("[abc=123, def=456, jkl=13, mno=345]",map.entrySet().toString());
assertEquals(null,map.put("ghi",789));
assertEquals("[abc=123, def=456, ghi=789, jkl=13, mno=345]",map.entrySet().toString());
assertEquals(null,map.put("pqr",678));
assertEquals("[abc=123, def=456, ghi=789, jkl=13, mno=345, pqr=678]",map.entrySet().toString());
assertEquals(new Integer(13),map.put(new String("jkl"),901));
assertEquals("[abc=123, def=456, ghi=789, jkl=901, mno=345, pqr=678]",map.entrySet().toString());
assertEquals(null,map.put("ABC",456));
assertEquals("[ABC=456, abc=123, def=456, ghi=789, jkl=901, mno=345, pqr=678]",map.entrySet().toString());
assertEquals(new Integer(456),map.put("def",234));
assertEquals("[ABC=456, abc=123, def=234, ghi=789, jkl=901, mno=345, pqr=678]",map.entrySet().toString());
assertEquals(new Integer(345),map.put("mno",567));
assertEquals("[ABC=456, abc=123, def=234, ghi=789, jkl=901, mno=567, pqr=678]",map.entrySet().toString());
}
}
kadai/TDUMapTest.java
package kadai;
import static org.junit.Assert.*;
import java.util.Map;
import kaitou.TDUMap;
import org.junit.Test;
public class TDUMapTest {
@Test
public void testSize() {
Map<String, Double> m = new TDUMap<String,Double>(null);
assertEquals(0,m.size());
final TDUMapEntry<String,Double> e1 = new TDUMapEntry<String,Double>("abc",0.123);
m = new TDUMap<String, Double>(e1);
assertEquals(1,m.size());
final TDUMapEntry<String, Double> e2 = new TDUMapEntry<String,Double>("def",0.456);
e1.left=e2;
m = new TDUMap<String, Double>(e1);
assertEquals(2,m.size());
final TDUMapEntry<String, Double> e3 = new TDUMapEntry<String, Double>("ghi",0.789);
e1.right=e3;
m = new TDUMap<String, Double>(e1);
assertEquals(3,m.size());
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);
assertEquals(6,m.size());
}
@Test
public void testEntrySet() {
Map<String, Double> m = new TDUMap<String,Double>(null);
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());
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());
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());
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());
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());
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());
}
@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());
}
}