import net.sf.javaml.core.* ; import static org.junit.Assert.*; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.junit.Test; public class TestDenseInstance { @Test public void test(){ DenseInstance di = new DenseInstance(new double[]{1, 2, 3}); assertTrue(di.value(0) == 1.0); assertTrue(di.value(1) == 2.0); assertTrue(di.value(2) == 3.0); assertFalse(di.isEmpty()); di.clear(); assertFalse(di.isEmpty()); // a dense instance is never empty assertTrue(di.containsKey(0)); di.put(0, 2.3); assertTrue(di.containsValue(2.3)); di.clear(); assertFalse(di.entrySet().isEmpty()); assertNotNull(di.get(0)); assertFalse(di.keySet().isEmpty()); Map m = new HashMap(); for(int i = 0; i < 10; i++) m.put(i, 2.3); try{ di.putAll(m); fail(); }catch(IndexOutOfBoundsException ioobe){} m.clear(); for(int i = 0; i < 3; i++) m.put(i, 2.3); di.putAll(m); try{ di.remove(0); fail(); }catch(Exception e){} assertTrue(di.size() == 3); assertFalse(di.values().isEmpty()); assertTrue(di.noAttributes() == 3); assertNotNull(di.toString()); assertEquals(di.hashCode(), di.copy().hashCode()); assertEquals(di, di.copy()); assertEquals(di, di.copy().copy()); assertEquals(di.copy(), di.copy()); assertEquals(di.copy(), di); assertEquals(new DenseInstance(new double[]{2.0, 3.0}), new DenseInstance(new double[]{2.0, 3.0})); assertFalse(new DenseInstance(new double[]{2.0, 3.0}).equals(new DenseInstance(new double[]{2.0}))); Set s = new HashSet(); for(int i = 0; i < 3; i++) s.add(i); di.removeAttributes(s); assertEquals(0, di.noAttributes()); di = new DenseInstance(new double[]{}, null); di = new DenseInstance(5); } // separated break-off tests @Test public void testBO(){ DenseInstance di = new DenseInstance(new double[]{1, 2, 3}); di.clear(); assertFalse(di.containsKey(-1)); assertFalse(di.containsKey(10)); assertFalse(di.containsKey(new Object())); di.put(0, 2.3); assertFalse(di.containsValue(new Object())); assertFalse(di.equals(null)); assertFalse(di.equals(new Object())); assertFalse(di.equals(new Integer(5))); } }