package geniusweb.issuevalue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.utilities.immutablelist.Range; public class NumberValueSetTest { private static final BigDecimal ZERO_THREE = new BigDecimal("0.3"); private static final BigDecimal TWELVE_SIX = new BigDecimal("12.6"); private static final BigDecimal TWELVE_TWO = new BigDecimal("12.2"); private final static NumberValueSet set = new NumberValueSet(TWELVE_TWO, TWELVE_SIX, ZERO_THREE); private final static NumberValueSet set1 = new NumberValueSet(TWELVE_TWO, TWELVE_SIX, ZERO_THREE); private final static NumberValueSet setb = new NumberValueSet(TWELVE_TWO, new BigDecimal("12.6000000000001"), ZERO_THREE); private final static NumberValueSet set10 = new NumberValueSet( BigDecimal.ZERO, BigDecimal.ONE, new BigDecimal("0.1")); private final String asString = "{\"range\":{\"low\":12.2,\"high\":12.6,\"step\":0.3}}"; private final static NumberValueSet bigset = new NumberValueSet( BigDecimal.ZERO, BigDecimal.TEN, new BigDecimal("0.00003")); @Test(expected = IllegalArgumentException.class) public void constructor1Test() { new NumberValueSet(TWELVE_SIX, TWELVE_TWO, ZERO_THREE); } @Test(expected = IllegalArgumentException.class) public void constructor2Test() { new NumberValueSet(BigDecimal.ZERO, BigDecimal.TEN, new BigDecimal("-1")); } @Test public void iteratorTest() { assertEquals(new NumberValue(BigDecimal.ZERO), bigset.iterator().next()); } @Test public void testSerialize() throws JsonProcessingException { ObjectMapper jackson = new ObjectMapper(); String jsonstr = jackson.writeValueAsString(set); //System.out.println(json); assertEquals(asString, jsonstr); } @Test public void testDeserialize() throws JsonParseException, JsonMappingException, IOException { ObjectMapper jackson = new ObjectMapper(); ValueSet p = jackson.readValue(asString, NumberValueSet.class); assertEquals(set, p); } @Test public void containsTest() { assertTrue(bigset.contains(new NumberValue(new BigDecimal("0.00393")))); } @Test public void containsFirstTest() { assertTrue(bigset.contains(new NumberValue(BigDecimal.ZERO))); } @Test public void containsLastTest() { assertTrue(set10.contains(new NumberValue(BigDecimal.ONE))); } @Test public void equalsTest() { assertEquals(set, set1); assertEquals(set1, set); assertNotEquals(set, null); assertNotEquals(null, set); assertNotEquals(set, setb); } @Test public void getRangeTest() { assertEquals(new Range(TWELVE_TWO, TWELVE_SIX, ZERO_THREE), set.getRange()); } @Test public void testGet() { assertEquals(new NumberValue(TWELVE_TWO), set.get(BigInteger.ZERO)); assertEquals(new NumberValue(TWELVE_TWO), set.get(0)); assertEquals(new NumberValue(TWELVE_TWO.add(ZERO_THREE)), set.get(BigInteger.ONE)); } @Test public void testSize() { assertEquals(new BigInteger("2"), set.size()); assertEquals(new BigInteger("11"), set10.size()); } }