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.math.ode.sampling;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.ByteArrayInputStream;
26  import java.io.ObjectOutputStream;
27  import java.io.ObjectInputStream;
28  import java.io.IOException;
29  
30  import org.apache.commons.math.ode.DerivativeException;
31  import org.apache.commons.math.ode.sampling.AbstractStepInterpolator;
32  import org.apache.commons.math.ode.sampling.DummyStepInterpolator;
33  import org.junit.Test;
34  
35  public class DummyStepInterpolatorTest {
36  
37    @Test
38    public void testNoReset() throws DerivativeException {
39  
40      double[]   y    =   { 0.0, 1.0, -2.0 };
41      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
42      interpolator.storeTime(0);
43      interpolator.shift();
44      interpolator.storeTime(1);
45  
46      double[] result = interpolator.getInterpolatedState();
47      for (int i = 0; i < result.length; ++i) {
48        assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
49      }
50  
51    }
52  
53    @Test
54    public void testFixedState()
55      throws DerivativeException {
56  
57      double[]   y    =   { 1.0, 3.0, -4.0 };
58      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
59      interpolator.storeTime(0);
60      interpolator.shift();
61      interpolator.storeTime(1);
62  
63      interpolator.setInterpolatedTime(0.1);
64      double[] result = interpolator.getInterpolatedState();
65      for (int i = 0; i < result.length; ++i) {
66          assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
67      }
68  
69      interpolator.setInterpolatedTime(0.5);
70      result = interpolator.getInterpolatedState();
71      for (int i = 0; i < result.length; ++i) {
72          assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
73      }
74  
75    }
76  
77    @Test
78    public void testSerialization()
79    throws DerivativeException, IOException, ClassNotFoundException {
80  
81      double[]   y    =   { 0.0, 1.0, -2.0 };
82      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
83      interpolator.storeTime(0);
84      interpolator.shift();
85      interpolator.storeTime(1);
86  
87      ByteArrayOutputStream bos = new ByteArrayOutputStream();
88      ObjectOutputStream    oos = new ObjectOutputStream(bos);
89      oos.writeObject(interpolator);
90  
91      assertTrue(bos.size () > 150);
92      assertTrue(bos.size () < 250);
93  
94      ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
95      ObjectInputStream     ois = new ObjectInputStream(bis);
96      DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject();
97  
98      dsi.setInterpolatedTime(0.5);
99      double[] result = dsi.getInterpolatedState();
100     for (int i = 0; i < result.length; ++i) {
101         assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
102     }
103 
104   }
105 
106   @Test
107   public void testImpossibleSerialization()
108   throws IOException {
109 
110     double[] y = { 0.0, 1.0, -2.0 };
111     AbstractStepInterpolator interpolator = new BadStepInterpolator(y, true);
112     interpolator.storeTime(0);
113     interpolator.shift();
114     interpolator.storeTime(1);
115 
116     ByteArrayOutputStream bos = new ByteArrayOutputStream();
117     ObjectOutputStream    oos = new ObjectOutputStream(bos);
118     try {
119         oos.writeObject(interpolator);
120         fail("an exception should have been thrown");
121     } catch (IOException ioe) {
122         // expected behavior
123         assertEquals(0, ioe.getMessage().length());
124     } catch (Exception e) {
125         fail("wrong exception caught");
126     }
127 
128   }
129 
130   private static class BadStepInterpolator extends DummyStepInterpolator {
131 	  @SuppressWarnings("unused")
132 	  public BadStepInterpolator() {
133 	  }
134 	  public BadStepInterpolator(double[] y, boolean forward) {
135 		  super(y, forward);
136 	  }
137 	  @Override
138 	  protected void doFinalize()
139 	  throws DerivativeException {
140           throw new DerivativeException(null);
141       }
142   }
143 
144 }