package net.sf.javaml.core; import static org.junit.Assert.*; import java.util.HashSet; import java.util.Set; import org.junit.Test; public class TestSparseInstance { @Test public void test(){ SparseInstance si = new SparseInstance(); assertTrue(si.isEmpty()); assertEquals(0, si.size()); assertFalse(si.containsKey(1)); assertTrue(si.entrySet().isEmpty()); assertFalse(si.containsKey(new Object())); assertFalse(si.containsValue(new Object())); si.clear(); si = new SparseInstance(0, 9); // default value 5 assertEquals(9, (int)(double)si.get(new Object())); //si. si = new SparseInstance(new double[]{1, 2, 3}, 5.0); si = new SparseInstance(new double[]{1, 2, 3}, new Integer(5)); si = new SparseInstance(new double[]{1, 2, 3}, 7, null); assertEquals(1, (int)(double)si.get(0)); assertEquals(2, (int)(double)si.get(1)); assertEquals(3, (int)(double)si.get(2)); assertEquals(7, (int)(double)si.get(3)); assertEquals(7, (int)(double)si.get(new Object())); assertEquals(3, si.size()); assertFalse(si.isEmpty()); si = new SparseInstance(new double[]{0.0, 2, 3, 0.0}, 0.0); assertEquals(2, si.size()); assertTrue(si.containsKey(1)); assertTrue(si.containsKey(2)); assertEquals(2, (int)(double)si.value(1)); assertEquals(3, (int)(double)si.value(2)); assertTrue(si.containsValue(3.0)); assertTrue(si.containsValue(2.0)); assertFalse(si.keySet().isEmpty()); si.put(0, 4.0); assertEquals(4, (int)(double)si.value(0)); assertEquals(3, si.entrySet().size()); si.remove(0); assertEquals(2, si.entrySet().size()); assertEquals(2, si.values().size()); assertEquals(4, si.noAttributes()); si.removeAttribute(1); si.removeAttribute(5); assertNotNull(si.toString()); si.hashCode(); assertFalse(si.equals(new Object())); assertTrue(si.equals(si)); assertFalse(si.equals(null)); assertFalse(si.equals(new Integer(5))); assertFalse(si.equals(new SparseInstance())); assertFalse(si.equals(new SparseInstance(new double[]{0.0, 2, 3, 0.0}, 0.0))); assertTrue(si.equals(si.copy())); assertTrue(si.copy().equals(si.copy())); assertTrue(si.copy().equals(si)); si.clear(); si = new SparseInstance(new double[]{0.0, 2, 3, 0.0}, 0.0); Set s = new HashSet(); for(int i = 0; i < 5; i++) s.add(i); si.removeAttributes(s); assertTrue(si.isEmpty()); assertTrue(si.noAttributes() == 0); si = new SparseInstance(5); si.setNoAttributes(10); assertTrue(si.noAttributes() == 10); } }