001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.ode.sampling;
019
020 import static org.junit.Assert.assertEquals;
021 import static org.junit.Assert.assertTrue;
022 import static org.junit.Assert.fail;
023
024 import java.io.ByteArrayOutputStream;
025 import java.io.ByteArrayInputStream;
026 import java.io.ObjectOutputStream;
027 import java.io.ObjectInputStream;
028 import java.io.IOException;
029
030 import org.apache.commons.math.ode.DerivativeException;
031 import org.apache.commons.math.ode.sampling.AbstractStepInterpolator;
032 import org.apache.commons.math.ode.sampling.DummyStepInterpolator;
033 import org.junit.Test;
034
035 public class DummyStepInterpolatorTest {
036
037 @Test
038 public void testNoReset() throws DerivativeException {
039
040 double[] y = { 0.0, 1.0, -2.0 };
041 DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
042 interpolator.storeTime(0);
043 interpolator.shift();
044 interpolator.storeTime(1);
045
046 double[] result = interpolator.getInterpolatedState();
047 for (int i = 0; i < result.length; ++i) {
048 assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
049 }
050
051 }
052
053 @Test
054 public void testFixedState()
055 throws DerivativeException {
056
057 double[] y = { 1.0, 3.0, -4.0 };
058 DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
059 interpolator.storeTime(0);
060 interpolator.shift();
061 interpolator.storeTime(1);
062
063 interpolator.setInterpolatedTime(0.1);
064 double[] result = interpolator.getInterpolatedState();
065 for (int i = 0; i < result.length; ++i) {
066 assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
067 }
068
069 interpolator.setInterpolatedTime(0.5);
070 result = interpolator.getInterpolatedState();
071 for (int i = 0; i < result.length; ++i) {
072 assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
073 }
074
075 }
076
077 @Test
078 public void testSerialization()
079 throws DerivativeException, IOException, ClassNotFoundException {
080
081 double[] y = { 0.0, 1.0, -2.0 };
082 DummyStepInterpolator interpolator = new DummyStepInterpolator(y, true);
083 interpolator.storeTime(0);
084 interpolator.shift();
085 interpolator.storeTime(1);
086
087 ByteArrayOutputStream bos = new ByteArrayOutputStream();
088 ObjectOutputStream oos = new ObjectOutputStream(bos);
089 oos.writeObject(interpolator);
090
091 assertTrue(bos.size () > 150);
092 assertTrue(bos.size () < 250);
093
094 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
095 ObjectInputStream ois = new ObjectInputStream(bis);
096 DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject();
097
098 dsi.setInterpolatedTime(0.5);
099 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 }