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.math3.ode.sampling;
019    
020    
021    import java.io.ByteArrayInputStream;
022    import java.io.ByteArrayOutputStream;
023    import java.io.IOException;
024    import java.io.ObjectInputStream;
025    import java.io.ObjectOutputStream;
026    
027    import org.apache.commons.math3.exception.MaxCountExceededException;
028    import org.apache.commons.math3.util.FastMath;
029    import org.junit.Assert;
030    import org.junit.Test;
031    
032    public class DummyStepInterpolatorTest {
033    
034      @Test
035      public void testNoReset() throws MaxCountExceededException {
036    
037        double[]   y    =   { 0.0, 1.0, -2.0 };
038        DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true);
039        interpolator.storeTime(0);
040        interpolator.shift();
041        interpolator.storeTime(1);
042    
043        double[] result = interpolator.getInterpolatedState();
044        for (int i = 0; i < result.length; ++i) {
045          Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10);
046        }
047    
048      }
049    
050      @Test
051      public void testFixedState() throws MaxCountExceededException {
052    
053        double[]   y    =   { 1.0, 3.0, -4.0 };
054        DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true);
055        interpolator.storeTime(0);
056        interpolator.shift();
057        interpolator.storeTime(1);
058    
059        interpolator.setInterpolatedTime(0.1);
060        double[] result = interpolator.getInterpolatedState();
061        for (int i = 0; i < result.length; ++i) {
062            Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10);
063        }
064    
065        interpolator.setInterpolatedTime(0.5);
066        result = interpolator.getInterpolatedState();
067        for (int i = 0; i < result.length; ++i) {
068            Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10);
069        }
070    
071      }
072    
073      @Test
074      public void testSerialization()
075      throws IOException, ClassNotFoundException, MaxCountExceededException {
076    
077        double[]   y    =   { 0.0, 1.0, -2.0 };
078        DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true);
079        interpolator.storeTime(0);
080        interpolator.shift();
081        interpolator.storeTime(1);
082    
083        ByteArrayOutputStream bos = new ByteArrayOutputStream();
084        ObjectOutputStream    oos = new ObjectOutputStream(bos);
085        oos.writeObject(interpolator);
086    
087        Assert.assertTrue(bos.size () > 300);
088        Assert.assertTrue(bos.size () < 500);
089    
090        ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
091        ObjectInputStream     ois = new ObjectInputStream(bis);
092        DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject();
093    
094        dsi.setInterpolatedTime(0.5);
095        double[] result = dsi.getInterpolatedState();
096        for (int i = 0; i < result.length; ++i) {
097            Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10);
098        }
099    
100      }
101    
102      @Test
103      public void testImpossibleSerialization()
104      throws IOException {
105    
106        double[] y = { 0.0, 1.0, -2.0 };
107        AbstractStepInterpolator interpolator = new BadStepInterpolator(y, true);
108        interpolator.storeTime(0);
109        interpolator.shift();
110        interpolator.storeTime(1);
111    
112        ByteArrayOutputStream bos = new ByteArrayOutputStream();
113        ObjectOutputStream    oos = new ObjectOutputStream(bos);
114        try {
115            oos.writeObject(interpolator);
116            Assert.fail("an exception should have been thrown");
117        } catch (LocalException le) {
118            // expected behavior
119        }
120      }
121    
122      private static class BadStepInterpolator extends DummyStepInterpolator {
123          @SuppressWarnings("unused")
124          public BadStepInterpolator() {
125          }
126          public BadStepInterpolator(double[] y, boolean forward) {
127              super(y, new double[y.length], forward);
128          }
129          @Override
130          protected void doFinalize() {
131              throw new LocalException();
132          }
133      }
134    
135      private static class LocalException extends RuntimeException {
136        private static final long serialVersionUID = 1L;
137      }
138    
139    }