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;
19  
20  import junit.framework.*;
21  import java.io.ByteArrayOutputStream;
22  import java.io.ByteArrayInputStream;
23  import java.io.ObjectOutputStream;
24  import java.io.ObjectInputStream;
25  import java.io.IOException;
26  
27  public class DummyStepInterpolatorTest
28    extends TestCase {
29  
30    public DummyStepInterpolatorTest(String name) {
31      super(name);
32    }
33  
34    public void testNoReset() {
35  
36      double[]   y    =   { 0.0, 1.0, -2.0 };
37      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
38      interpolator.storeTime(0);
39      interpolator.shift();
40      interpolator.storeTime(1);
41  
42      double[] result = interpolator.getInterpolatedState();
43      for (int i = 0; i < result.length; ++i) {
44        assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
45      }
46  
47    }
48  
49    public void testFixedState()
50      throws DerivativeException {
51  
52      double[]   y    =   { 1.0, 3.0, -4.0 };
53      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, 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          assertTrue(Math.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          assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
68      }
69  
70    }
71  
72    public void testSerialization()
73    throws DerivativeException, IntegratorException,
74           IOException, ClassNotFoundException {
75  
76      double[]   y    =   { 0.0, 1.0, -2.0 };
77      DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
78      interpolator.storeTime(0);
79      interpolator.shift();
80      interpolator.storeTime(1);
81  
82      ByteArrayOutputStream bos = new ByteArrayOutputStream();
83      ObjectOutputStream    oos = new ObjectOutputStream(bos);
84      oos.writeObject(interpolator);
85  
86      assertTrue(bos.size () > 150);
87      assertTrue(bos.size () < 250);
88  
89      ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
90      ObjectInputStream     ois = new ObjectInputStream(bis);
91      DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject();
92  
93      dsi.setInterpolatedTime(0.5);
94      double[] result = dsi.getInterpolatedState();
95      for (int i = 0; i < result.length; ++i) {
96          assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
97      }
98  
99    }
100 
101   public void testImpossibleSerialization()
102   throws DerivativeException, IntegratorException,
103          IOException, ClassNotFoundException {
104 
105     double[] y = { 0.0, 1.0, -2.0 };
106     AbstractStepInterpolator interpolator = new BadStepInterpolator(y, true);
107     interpolator.storeTime(0);
108     interpolator.shift();
109     interpolator.storeTime(1);
110 
111     ByteArrayOutputStream bos = new ByteArrayOutputStream();
112     ObjectOutputStream    oos = new ObjectOutputStream(bos);
113     try {
114         oos.writeObject(interpolator);
115         fail("an exception should have been thrown");
116     } catch (IOException ioe) {
117         // expected behavior
118         assertNull(ioe.getMessage());
119     } catch (Exception e) {
120         fail("wrong exception caught");
121     }
122 
123   }
124 
125   private static class BadStepInterpolator extends DummyStepInterpolator {
126       public BadStepInterpolator() {
127           super();
128       }
129       public BadStepInterpolator(double[] y, boolean forward) {
130           super(y, forward);
131       }
132       protected void doFinalize()
133       throws DerivativeException {
134           throw new DerivativeException(null);
135       }
136   };
137 
138 
139   public void testSerializationError()
140   throws DerivativeException, IntegratorException,
141          IOException, ClassNotFoundException {
142 
143     double[] y = { 0.0, 1.0, -2.0 };
144     ErrorGeneratingInterpolator interpolator =
145         new ErrorGeneratingInterpolator(y, true);
146     interpolator.storeTime(0);
147     interpolator.shift();
148     interpolator.storeTime(1);
149 
150     ByteArrayOutputStream bos = new ByteArrayOutputStream();
151     ObjectOutputStream    oos = new ObjectOutputStream(bos);
152     oos.writeObject(interpolator);
153 
154     assertTrue(bos.size () > 250);
155     assertTrue(bos.size () < 300);
156 
157     ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
158     ObjectInputStream     ois = new ObjectInputStream(bis);
159     try {
160         ois.readObject();
161         fail("an exception should have been thrown");
162     } catch (IOException ioe) {
163         // expected behavior
164         assertNull(ioe.getMessage());
165     } catch (Exception e) {
166         fail("wrong exception caught");
167     }
168 
169   }
170 
171   private static class ErrorGeneratingInterpolator extends DummyStepInterpolator {
172       public ErrorGeneratingInterpolator() {
173           super();
174       }
175       protected ErrorGeneratingInterpolator(double[] y, boolean forward) {
176           super(y, forward);
177       }
178       public void computeInterpolatedState(double theta, double oneMinusThetaH)
179       throws DerivativeException {
180           throw new DerivativeException(null);
181       }
182       private static final long serialVersionUID = 0x3f6ab636f0c93571L;
183   }
184 
185   public static Test suite() {
186     return new TestSuite(DummyStepInterpolatorTest.class);
187   }
188 
189 }