|
25 | 25 |
|
26 | 26 | import static junit.framework.TestCase.assertEquals; |
27 | 27 | import static junit.framework.TestCase.assertFalse; |
| 28 | +import static org.junit.Assert.assertNotSame; |
28 | 29 | import static org.junit.Assert.assertTrue; |
29 | 30 | import static org.ta4j.core.TestUtils.assertNumEquals; |
30 | 31 | import static org.ta4j.core.TestUtils.assertNumNotEquals; |
31 | 32 | import static org.ta4j.core.num.NaN.NaN; |
32 | 33 |
|
| 34 | +import java.io.ByteArrayInputStream; |
| 35 | +import java.io.ByteArrayOutputStream; |
33 | 36 | import java.io.IOException; |
34 | 37 | import java.io.InputStream; |
| 38 | +import java.io.ObjectInputStream; |
| 39 | +import java.io.ObjectOutputStream; |
35 | 40 | import java.math.BigDecimal; |
36 | 41 | import java.math.MathContext; |
37 | 42 | import java.math.RoundingMode; |
@@ -346,4 +351,29 @@ public void sqrtOddExponent() { |
346 | 351 | assertNumEquals("547722.55750516611345696978280080", sqrt); |
347 | 352 | } |
348 | 353 |
|
| 354 | + @Test |
| 355 | + public void testSerialization() throws Exception { |
| 356 | + Num numVal = numFunction.apply(1.3); |
| 357 | + serializeDeserialize(numVal); |
| 358 | + } |
| 359 | + |
| 360 | + private static void serializeDeserialize(Num o) throws IOException, ClassNotFoundException { |
| 361 | + byte[] array; |
| 362 | + try (var baos = new ByteArrayOutputStream()) { |
| 363 | + try (var out = new ObjectOutputStream(baos)) { |
| 364 | + out.writeObject(o); |
| 365 | + array = baos.toByteArray(); |
| 366 | + } |
| 367 | + |
| 368 | + } |
| 369 | + try (var baos = new ByteArrayInputStream(array)) { |
| 370 | + try (var out = new ObjectInputStream(baos)) { |
| 371 | + var deserialized = (Num) out.readObject(); |
| 372 | + assertNotSame(o, deserialized); |
| 373 | + assertEquals(deserialized.doubleValue(), o.doubleValue()); |
| 374 | + } |
| 375 | + |
| 376 | + } |
| 377 | + } |
| 378 | + |
349 | 379 | } |
0 commit comments