View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.legacy.ode.sampling;
19  
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  
27  import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
28  import org.apache.commons.math4.core.jdkmath.JdkMath;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  public class DummyStepInterpolatorTest {
33  
34    @Test
35    public void testNoReset() throws MaxCountExceededException {
36  
37      double[]   y    =   { 0.0, 1.0, -2.0 };
38      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true);
39      interpolator.storeTime(0);
40      interpolator.shift();
41      interpolator.storeTime(1);
42  
43      double[] result = interpolator.getInterpolatedState();
44      for (int i = 0; i < result.length; ++i) {
45        Assert.assertTrue(JdkMath.abs(result[i] - y[i]) < 1.0e-10);
46      }
47    }
48  
49    @Test
50    public void testFixedState() throws MaxCountExceededException {
51  
52      double[]   y    =   { 1.0, 3.0, -4.0 };
53      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true);
54      interpolator.storeTime(0);
55      interpolator.shift();
56      interpolator.storeTime(1);
57  
58      interpolator.setInterpolatedTime(0.1);
59      double[] result = interpolator.getInterpolatedState();
60      for (int i = 0; i < result.length; ++i) {
61          Assert.assertTrue(JdkMath.abs(result[i] - y[i]) < 1.0e-10);
62      }
63  
64      interpolator.setInterpolatedTime(0.5);
65      result = interpolator.getInterpolatedState();
66      for (int i = 0; i < result.length; ++i) {
67          Assert.assertTrue(JdkMath.abs(result[i] - y[i]) < 1.0e-10);
68      }
69    }
70  
71    @Test
72    public void testSerialization()
73    throws IOException, ClassNotFoundException, MaxCountExceededException {
74  
75      double[]   y    =   { 0.0, 1.0, -2.0 };
76      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true);
77      interpolator.storeTime(0);
78      interpolator.shift();
79      interpolator.storeTime(1);
80  
81      ByteArrayOutputStream bos = new ByteArrayOutputStream();
82      ObjectOutputStream    oos = new ObjectOutputStream(bos);
83      oos.writeObject(interpolator);
84  
85      Assert.assertTrue(bos.size () > 300);
86      Assert.assertTrue(bos.size () < 500);
87  
88      ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
89      ObjectInputStream     ois = new ObjectInputStream(bis);
90      DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject();
91  
92      dsi.setInterpolatedTime(0.5);
93      double[] result = dsi.getInterpolatedState();
94      for (int i = 0; i < result.length; ++i) {
95          Assert.assertTrue(JdkMath.abs(result[i] - y[i]) < 1.0e-10);
96      }
97    }
98  
99    @Test
100   public void testImpossibleSerialization()
101   throws IOException {
102 
103     double[] y = { 0.0, 1.0, -2.0 };
104     AbstractStepInterpolator interpolator = new BadStepInterpolator(y, true);
105     interpolator.storeTime(0);
106     interpolator.shift();
107     interpolator.storeTime(1);
108 
109     ByteArrayOutputStream bos = new ByteArrayOutputStream();
110     ObjectOutputStream    oos = new ObjectOutputStream(bos);
111     try {
112         oos.writeObject(interpolator);
113         Assert.fail("an exception should have been thrown");
114     } catch (LocalException le) {
115         // expected behavior
116     }
117   }
118 
119   private static final class BadStepInterpolator extends DummyStepInterpolator {
120       private static final long serialVersionUID = 1;
121       BadStepInterpolator(double[] y, boolean forward) {
122           super(y, new double[y.length], forward);
123       }
124       @Override
125       protected void doFinalize() {
126           throw new LocalException();
127       }
128   }
129 
130   private static final class LocalException extends RuntimeException {
131     private static final long serialVersionUID = 1L;
132   }
133 }