source: issuevalue/src/test/java/geniusweb/issuevalue/NumberValueTest.java@ 30

Last change on this file since 30 was 30, checked in by bart, 3 years ago

Fixed memory leak. MOPAC2. removed jcenter build dependencies

File size: 3.7 KB
Line 
1package geniusweb.issuevalue;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.math.BigDecimal;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.LinkedList;
10import java.util.List;
11
12import org.junit.Test;
13
14import com.fasterxml.jackson.core.JsonParseException;
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.JsonMappingException;
17import com.fasterxml.jackson.databind.ObjectMapper;
18
19import tudelft.utilities.junit.GeneralTests;
20
21public class NumberValueTest extends GeneralTests<NumberValue> {
22 private NumberValue value = new NumberValue("12.31");
23 private NumberValue value1 = new NumberValue("12.31");
24 private NumberValue valueb = new NumberValue("12.310000000000000001");
25 private String serialized = "12.31";
26
27 private String bigserialized = "8748703924870439218876954320948372058";
28 private NumberValue bigvalue = new NumberValue(bigserialized);
29
30 @Override
31 public List<List<NumberValue>> getGeneralTestData() {
32 List<List<NumberValue>> list = new LinkedList<>();
33 list.add(Arrays.asList(value, value1));
34 list.add(Arrays.asList(valueb));
35 return list;
36 }
37
38 @Override
39 public List<String> getGeneralTestStrings() {
40 return Arrays.asList("12.31", "12.310000000000000001");
41 }
42
43 @Test
44 public void testSerialize() throws JsonProcessingException {
45 ObjectMapper jackson = new ObjectMapper();
46 assertEquals(serialized, jackson.writeValueAsString(value));
47 }
48
49 @Test
50 public void testDeserialize() throws IOException {
51 ObjectMapper jackson = new ObjectMapper();
52 assertEquals(value, jackson.readValue(serialized, NumberValue.class));
53
54 }
55
56 @Test
57 public void testDeserializeFromValue() throws IOException {
58 ObjectMapper jackson = new ObjectMapper();
59 assertEquals(value, jackson.readValue(serialized, Value.class));
60
61 }
62
63 @Test
64 public void testBigSerialize() throws JsonProcessingException {
65 ObjectMapper jackson = new ObjectMapper();
66 assertEquals(bigserialized, jackson.writeValueAsString(bigvalue));
67 }
68
69 @Test
70 public void testBigDeserialize() throws IOException {
71 ObjectMapper jackson = new ObjectMapper();
72 assertEquals(bigvalue,
73 jackson.readValue(bigserialized, NumberValue.class));
74
75 }
76
77 @Test
78 public void testSerializeShorts() throws IOException {
79 ObjectMapper jackson = new ObjectMapper();
80 System.out.println(
81 jackson.writeValueAsString(new short[] { 1, 2, 3, 4, 5 }));
82
83 }
84
85 @Test
86 public void testDeserializeShorts() throws IOException {
87 ObjectMapper jackson = new ObjectMapper();
88 @SuppressWarnings("unchecked")
89 ArrayList<Integer> list = jackson.readValue("[1,2,3,4,5]",
90 ArrayList.class);
91 System.out.println(list);
92 System.out.println(list.get(0).getClass());
93
94 }
95
96 @Test
97 public void testDeserializeMix() throws IOException {
98 ObjectMapper jackson = new ObjectMapper();
99 @SuppressWarnings("unchecked")
100 ArrayList<Object> list = jackson.readValue("[\"short\",1,2,3,4,5]",
101 ArrayList.class);
102 System.out.println(list);
103 System.out.println(list.get(0).getClass());
104
105 }
106
107 /**
108 * Showing that we CAN deserialize big numbers without double quotes
109 * correctly, if we tell jackson upfront that it's a BigDecimal.
110 */
111 @Test
112 public void testDeserializePlainNumber()
113 throws JsonParseException, JsonMappingException, IOException {
114 ObjectMapper jackson = new ObjectMapper();
115 String valstr = "483958743698732691487326987569213874694328974329874328947320984372498327493827432987231874681273648127";
116 BigDecimal val = jackson.readValue(valstr, BigDecimal.class);
117 assertEquals(valstr, val.toString());
118 }
119
120 @Test
121 public void testNumberBlab() throws IOException {
122 ObjectMapper jackson = new ObjectMapper();
123 // jackson.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
124 assertEquals(value, jackson.readValue("12.31", Value.class));
125
126 }
127
128}
Note: See TracBrowser for help on using the repository browser.