|
11 | 11 | import java.time.Instant; |
12 | 12 | import java.util.Arrays; |
13 | 13 | import java.util.Collections; |
| 14 | +import java.util.List; |
14 | 15 | import javax.json.Json; |
15 | 16 | import javax.json.JsonObject; |
16 | 17 | import javax.json.JsonReader; |
@@ -288,5 +289,70 @@ public void vByteArray1() { |
288 | 289 | testDeserialization("VByteArray1", vByteArray1); |
289 | 290 | testDeserialization("VByteArray1a", vByteArray1); |
290 | 291 | } |
291 | | - |
| 292 | + |
| 293 | + /** |
| 294 | + * Tests serialization and de-serialization of a Double.NaN. See {@link JsonVTypeBuilder#add} and |
| 295 | + * {@link VTypeToJsonV1#toVNumber} |
| 296 | + */ |
| 297 | + @Test |
| 298 | + public void testDoubleNaN(){ |
| 299 | + VDouble vDouble = VDouble.of(Double.NaN, Alarm.none(), Time.of(Instant.EPOCH), Display.none()); |
| 300 | + JsonObject jsonObject = VTypeToJson.toJson(vDouble); |
| 301 | + assertEquals("NaN", jsonObject.getString("value")); |
| 302 | + vDouble = (VDouble)VTypeToJson.toVType(jsonObject); |
| 303 | + assertTrue(Double.isNaN(vDouble.getValue())); |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void testDoublePositiveInfinity(){ |
| 308 | + VDouble vDouble = VDouble.of(Double.POSITIVE_INFINITY, Alarm.none(), Time.of(Instant.EPOCH), Display.none()); |
| 309 | + JsonObject jsonObject = VTypeToJson.toJson(vDouble); |
| 310 | + assertEquals(Double.toString(Double.POSITIVE_INFINITY), jsonObject.getString("value")); |
| 311 | + vDouble = (VDouble)VTypeToJson.toVType(jsonObject); |
| 312 | + assertTrue(vDouble.getValue().equals(Double.POSITIVE_INFINITY)); |
| 313 | + } |
| 314 | + |
| 315 | + @Test |
| 316 | + public void testDoubleNegativeInfinity(){ |
| 317 | + VDouble vDouble = VDouble.of(Double.NEGATIVE_INFINITY, Alarm.none(), Time.of(Instant.EPOCH), Display.none()); |
| 318 | + JsonObject jsonObject = VTypeToJson.toJson(vDouble); |
| 319 | + assertEquals(VTypeJsonMapper.NEG_INF, jsonObject.getString("value")); |
| 320 | + vDouble = (VDouble)VTypeToJson.toVType(jsonObject); |
| 321 | + assertTrue(vDouble.getValue().equals(Double.NEGATIVE_INFINITY)); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + public void testDoubleNaNInArray(){ |
| 326 | + VDoubleArray vDoubleArray1 = VDoubleArray.of(ArrayDouble.of(0, Double.NaN, 2), Alarm.none(), Time.of(Instant.ofEpochSecond(0, 0)), Display.none()); |
| 327 | + JsonObject jsonObject = VTypeToJson.toJson(vDoubleArray1); |
| 328 | + List valueObject = (List)jsonObject.get("value"); |
| 329 | + assertEquals(VTypeJsonMapper.NAN_QUOTED, valueObject.get(1).toString()); |
| 330 | + vDoubleArray1 = (VDoubleArray)VTypeToJson.toVType(jsonObject); |
| 331 | + assertTrue(Double.isNaN(vDoubleArray1.getData().getDouble(1))); |
| 332 | + } |
| 333 | + |
| 334 | + @Test |
| 335 | + public void testDoublePositiveInfinityInArray(){ |
| 336 | + VDoubleArray vDoubleArray1 = VDoubleArray.of(ArrayDouble.of(0, Double.POSITIVE_INFINITY, 2), Alarm.none(), Time.of(Instant.ofEpochSecond(0, 0)), Display.none()); |
| 337 | + JsonObject jsonObject = VTypeToJson.toJson(vDoubleArray1); |
| 338 | + List valueObject = (List)jsonObject.get("value"); |
| 339 | + assertEquals(VTypeJsonMapper.POS_INF_QUOTED, valueObject.get(1).toString()); |
| 340 | + vDoubleArray1 = (VDoubleArray)VTypeToJson.toVType(jsonObject); |
| 341 | + assertTrue(Double.isInfinite(vDoubleArray1.getData().getDouble(1))); |
| 342 | + } |
| 343 | + |
| 344 | + @Test |
| 345 | + public void testDoubleNegativeInfinityInArray(){ |
| 346 | + VDoubleArray vDoubleArray1 = VDoubleArray.of(ArrayDouble.of(0, Double.NEGATIVE_INFINITY, 2), Alarm.none(), Time.of(Instant.ofEpochSecond(0, 0)), Display.none()); |
| 347 | + JsonObject jsonObject = VTypeToJson.toJson(vDoubleArray1); |
| 348 | + List valueObject = (List)jsonObject.get("value"); |
| 349 | + assertEquals(VTypeJsonMapper.NEG_INF_QUOTED, valueObject.get(1).toString()); |
| 350 | + vDoubleArray1 = (VDoubleArray)VTypeToJson.toVType(jsonObject); |
| 351 | + assertTrue(Double.isInfinite(vDoubleArray1.getData().getDouble(1))); |
| 352 | + } |
| 353 | + |
| 354 | + @Test(expected = NumberFormatException.class) |
| 355 | + public void testGetDoubleFromStringNonParsableValue(){ |
| 356 | + VTypeToJsonV1.getDoubleFromJsonString("invalid"); |
| 357 | + } |
292 | 358 | } |
0 commit comments