1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertNotEquals;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.math.BigDecimal;
|
---|
9 | import java.math.BigInteger;
|
---|
10 |
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
15 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
17 |
|
---|
18 | import tudelft.utilities.immutablelist.Range;
|
---|
19 |
|
---|
20 | public class NumberValueSetTest {
|
---|
21 |
|
---|
22 | private static final BigDecimal ZERO_THREE = new BigDecimal("0.3");
|
---|
23 | private static final BigDecimal TWELVE_SIX = new BigDecimal("12.6");
|
---|
24 | private static final BigDecimal TWELVE_TWO = new BigDecimal("12.2");
|
---|
25 | private final static NumberValueSet set = new NumberValueSet(TWELVE_TWO, TWELVE_SIX, ZERO_THREE);
|
---|
26 | private final static NumberValueSet set1 = new NumberValueSet(TWELVE_TWO, TWELVE_SIX, ZERO_THREE);
|
---|
27 | private final static NumberValueSet setb = new NumberValueSet(TWELVE_TWO, new BigDecimal("12.6000000000001"),
|
---|
28 | ZERO_THREE);
|
---|
29 | private final static NumberValueSet set10 = new NumberValueSet(BigDecimal.ZERO, BigDecimal.ONE,
|
---|
30 | new BigDecimal("0.1"));
|
---|
31 |
|
---|
32 | private final String asString = "{\"range\":[12.2,12.6,0.3]}";
|
---|
33 | private final static NumberValueSet bigset = new NumberValueSet(BigDecimal.ZERO, BigDecimal.TEN,
|
---|
34 | new BigDecimal("0.00003"));
|
---|
35 |
|
---|
36 | @Test(expected = IllegalArgumentException.class)
|
---|
37 | public void testConstructor1() {
|
---|
38 | new NumberValueSet(TWELVE_SIX, TWELVE_TWO, ZERO_THREE);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Test(expected = IllegalArgumentException.class)
|
---|
42 | public void testConstructor2() {
|
---|
43 | new NumberValueSet(BigDecimal.ZERO, BigDecimal.TEN, new BigDecimal("-1"));
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Test
|
---|
47 | public void testIterator() {
|
---|
48 | assertEquals(new NumberValue(BigDecimal.ZERO), bigset.iterator().next());
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Test
|
---|
52 | public void testSerialize() throws JsonProcessingException {
|
---|
53 | ObjectMapper jackson = new ObjectMapper();
|
---|
54 |
|
---|
55 | String json = jackson.writeValueAsString(set);
|
---|
56 | System.out.println(json);
|
---|
57 | assertEquals(asString, json);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | public void testDeserialize() throws JsonParseException, JsonMappingException, IOException {
|
---|
62 | ObjectMapper jackson = new ObjectMapper();
|
---|
63 | ValueSet p = jackson.readValue(asString, NumberValueSet.class);
|
---|
64 | assertEquals(set, p);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Test
|
---|
68 | public void containsTest() {
|
---|
69 | assertTrue(bigset.contains(new NumberValue(new BigDecimal("0.00393"))));
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test
|
---|
73 | public void containsFirstTest() {
|
---|
74 | assertTrue(bigset.contains(new NumberValue(BigDecimal.ZERO)));
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test
|
---|
78 | public void containsLastTest() {
|
---|
79 | assertTrue(set10.contains(new NumberValue(BigDecimal.ONE)));
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Test
|
---|
83 | public void equalsTest() {
|
---|
84 | assertEquals(set, set1);
|
---|
85 | assertEquals(set1, set);
|
---|
86 | assertNotEquals(set, null);
|
---|
87 | assertNotEquals(null, set);
|
---|
88 | assertNotEquals(set, setb);
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Test
|
---|
93 | public void getRangeTest() {
|
---|
94 | assertEquals(new Range(TWELVE_TWO, TWELVE_SIX, ZERO_THREE), set.getRange());
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Test
|
---|
98 | public void testGet() {
|
---|
99 | assertEquals(new NumberValue(TWELVE_TWO), set.get(BigInteger.ZERO));
|
---|
100 | assertEquals(new NumberValue(TWELVE_TWO), set.get(0));
|
---|
101 | assertEquals(new NumberValue(TWELVE_TWO.add(ZERO_THREE)), set.get(BigInteger.ONE));
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void testSize() {
|
---|
106 | assertEquals(new BigInteger("2"), set.size());
|
---|
107 | assertEquals(new BigInteger("11"), set10.size());
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|