/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 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.fit;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;

import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.io.sql.DracoSQLRead;

public abstract class RegressionTestSkeleton {
	
	private static final boolean RELATIVE_DELTA = true;
	
	protected static final String NORRIS_FILE = "resources/norris.sql-draco";
	protected static final String NOINT1_FILE = "resources/noint1.sql-draco";
	protected static final String NOINT2_FILE = "resources/noint2.sql-draco";
	protected static final String WAMPLER1_FILE = "resources/wampler1.sql-draco";
	protected static final String PONTIUS_FILE = "resources/pontius.sql-draco";
	protected static final String MISRA1A_FILE = "resources/misra1a.sql-draco";
	protected static final String KIRBY2_FILE = "resources/kirby2.sql-draco";
	protected static final String BENNETT5_FILE = "resources/bennett5.sql-draco";
	
	protected static final double DELTA = 1.0E-9;
	
	StatTableModel norris = null;
	StatTableModel noint1 = null;
	StatTableModel noint2 = null;
	StatTableModel wampler1 = null;
	StatTableModel pontius = null;
	StatTableModel misra1a = null;
	StatTableModel kirby2 = null;
	StatTableModel bennett5 = null;
	
	@Before
	public void setUp() throws Exception {
		norris = loadModel(NORRIS_FILE);
		noint1 = loadModel(NOINT1_FILE);
		noint2 = loadModel(NOINT2_FILE);
		wampler1 = loadModel(WAMPLER1_FILE);
		pontius = loadModel(PONTIUS_FILE);
		misra1a = loadModel(MISRA1A_FILE);
		kirby2 = loadModel(KIRBY2_FILE);
		bennett5 = loadModel(BENNETT5_FILE);
	}

	@After
	public void tearDown() throws Exception {
		norris = null;
		noint1 = null;
		noint2 = null;
		wampler1 = null;
		pontius = null;
		misra1a = null;
		kirby2 = null;
		bennett5 =  null;
	}
	
	protected StatTableModel loadModel(String filename) {
		DracoSQLRead reader = new DracoSQLRead(filename);
		StatTableModel ret = reader.loadModel();
		reader.close();
		return ret;
	}
	
	protected void assertCloseTo(double expected, double actual, double delta) {
		
		double drel = delta;
		if(RELATIVE_DELTA && expected != 0.0)
			drel = Math.abs(expected*delta);
		
		assertTrue(expected+drel/2.0 > actual);
		assertTrue(expected-drel/2.0 < actual);
	}
}
