/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2008 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;

public class InterpolationSupportTest {

    private static final double[] x_sorted = {1.0,2.0,3.0,4.0,5.0};
    private static final double[] x_unsorted = {1.0,3.0,5.0,4.0,2.0};
    
    private static final double[][] original_data = {{1.1,1.1},
                                                     {2.1,2.1},
                                                     {3.1,3.1},
                                                     {4.1,4.1},
                                                     {6.1,6.1},
                                                     {10.0,10.0}};
    
    private double[][] data = null;

	@Before
	public void setUp() throws Exception {
	    data = new double[original_data.length][];
		for(int i=0;i<original_data.length;i++) {
		    data[i] = new double[original_data[i].length];
		    for(int j=0;j<original_data[i].length;j++)
		        data[i][j] = original_data[i][j];
		}
	}

	@After
	public void tearDown() throws Exception {
        data = null;
	}

    @Test
    public void testInterpolate() {
        double[][] result = InterpolationSupport.Interpolate(x_sorted,data,0,InterpolationSupport.LINEAR);
        
        assertNotNull(result);
        for(int i=0;i<result.length;i++) {
            assertNotNull(result[i]);
            assertAbout((double)(i+1),result[i][1]);
        }
        
        result = InterpolationSupport.Interpolate(x_unsorted,data,0,InterpolationSupport.LINEAR);
        
        assertNotNull(result);
        for(int i=0;i<result.length;i++) {
            assertNotNull(result[i]);
            assertAbout(x_unsorted[i],result[i][1]);
        }
        
        
    }
    
    private void assertAbout(double expected, double actual) {
        asssertInDelta(1.0E-05,expected,actual);
    }
    
    private void asssertInDelta(double delta, double expected, double actual) {
        assert(actual >= expected-delta && actual <= expected+delta);
    }
}
