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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.math.ode;
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;
27
public class DummyStepInterpolatorTest
30
public DummyStepInterpolatorTest(String name) {
34
public void testNoReset() {
36
double[] y = { 0.0, 1.0, -2.0 };
37
DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
38
interpolator.storeTime(0);
40
interpolator.storeTime(1);
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);
49
public void testFixedState()
50
throws DerivativeException {
52
double[] y = { 1.0, 3.0, -4.0 };
53
DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
54
interpolator.storeTime(0);
56
interpolator.storeTime(1);
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);
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);
72
public void testSerialization()
73
throws DerivativeException, IntegratorException,
74
IOException, ClassNotFoundException {
76
double[] y = { 0.0, 1.0, -2.0 };
77
DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
78
interpolator.storeTime(0);
80
interpolator.storeTime(1);
82
ByteArrayOutputStream bos = new ByteArrayOutputStream();
83
ObjectOutputStream oos = new ObjectOutputStream(bos);
84
oos.writeObject(interpolator);
86
assertTrue(bos.size () > 150);
87
assertTrue(bos.size () < 250);
89
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
90
ObjectInputStream ois = new ObjectInputStream(bis);
91
DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject();
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);
101
public void testImpossibleSerialization()
102
throws DerivativeException, IntegratorException,
103
IOException, ClassNotFoundException {
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);
111
ByteArrayOutputStream bos = new ByteArrayOutputStream();
112
ObjectOutputStream oos = new ObjectOutputStream(bos);
114
oos.writeObject(interpolator);
115
fail("an exception should have been thrown");
116
} catch (IOException ioe) {
118
assertNull(ioe.getMessage());
119
} catch (Exception e) {
120
fail("wrong exception caught");
125
private static class BadStepInterpolator extends DummyStepInterpolator {
126
public BadStepInterpolator() {
129
public BadStepInterpolator(double[] y, boolean forward) {
132
protected void doFinalize()
133
throws DerivativeException {
134
throw new DerivativeException(null);
139
public void testSerializationError()
140
throws DerivativeException, IntegratorException,
141
IOException, ClassNotFoundException {
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);
150
ByteArrayOutputStream bos = new ByteArrayOutputStream();
151
ObjectOutputStream oos = new ObjectOutputStream(bos);
152
oos.writeObject(interpolator);
154
assertTrue(bos.size () > 250);
155
assertTrue(bos.size () < 300);
157
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
158
ObjectInputStream ois = new ObjectInputStream(bis);
161
fail("an exception should have been thrown");
162
} catch (IOException ioe) {
164
assertNull(ioe.getMessage());
165
} catch (Exception e) {
166
fail("wrong exception caught");
171
private static class ErrorGeneratingInterpolator extends DummyStepInterpolator {
172
public ErrorGeneratingInterpolator() {
175
protected ErrorGeneratingInterpolator(double[] y, boolean forward) {
178
public void computeInterpolatedState(double theta, double oneMinusThetaH)
179
throws DerivativeException {
180
throw new DerivativeException(null);
182
private static final long serialVersionUID = 0x3f6ab636f0c93571L;
185
public static Test suite() {
186
return new TestSuite(DummyStepInterpolatorTest.class);