/********************************************************************************/
/* Copyright 2009-2014 Brown University -- Alexander Tarvo, Steven P. Reiss */
/*********************************************************************************
* Copyright 2014, Brown University, Providence, RI. *
* *
* All Rights Reserved *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* *
* + Redistributions of source code must retain the above copyright notice, *
* this list of conditions and the following disclaimer. *
* + Redistributions in binary form must reproduce the above copyright notice, *
* this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* + Neither the name of the Brown University nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN *
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) *
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
********************************************************************************/
package modelgentest;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;
import java.io.*;
import edu.brown.cs.iolog.*;
import edu.brown.cs.iolog.IOSequenceMatcher;
/**
*
* @author iscander
*/
public class IOSequenceMatcherTest {
ArrayList<Long> m_arrEventTimeBtrace;
ArrayList<Long> m_arrEventTimeJava;
ArrayList<Integer> m_arrEventTypeBtrace;
ArrayList<Integer> m_arrEventTypeJava;
File m_dirTestIO;
public IOSequenceMatcherTest() {
}
@BeforeClass
public static void setUpClass() throws Exception
{
}
@AfterClass
public static void tearDownClass() throws Exception
{
}
@Before
public void setUp() {
File dirHome = new File(System.getProperty("user.home"));
File dirRamp = new File(dirHome, ".ramp");
File dirTest = new File (dirRamp, "test");
m_dirTestIO = new File(dirTest, "testIOMatch");
}
@After
public void tearDown()
{
}
/*
* Load both Btrace/Java test data from given files and initialize test data structures
*/
void initTest(String strBtraceFileName,
String strJavaFileName)
{
m_arrEventTimeBtrace = new ArrayList<Long>();
m_arrEventTimeJava = new ArrayList<Long>();
m_arrEventTypeBtrace = new ArrayList<Integer>();
m_arrEventTypeJava = new ArrayList<Integer>();
File dirTestBtraceFile = new File(m_dirTestIO, strBtraceFileName);
loadTimes(dirTestBtraceFile.getPath(),
m_arrEventTimeBtrace,
m_arrEventTypeBtrace);
File dirTestJavaFile = new File(m_dirTestIO, strJavaFileName);
loadTimes(dirTestJavaFile.getPath(),
m_arrEventTimeJava,
m_arrEventTypeJava);
}
/*
* Load test data from the specified file name into the given data structures
*/
void loadTimes(String strFilePath,
ArrayList<Long> arrEventTime,
ArrayList<Integer> arrEventType)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(strFilePath));
String strText;
while ((strText = reader.readLine()) != null)
{
String[] arrText = strText.split(",");
Long lTime = Long.parseLong(arrText[0]);
Integer lEvent = Integer.parseInt(arrText[1]);
arrEventTime.add(lTime);
arrEventType.add(lEvent);
}
}catch(Exception e)
{
System.err.println("Failed to load test cases from the file "+strFilePath);
}
}
@Test
public void testMatch1()
{
initTest("readBtrace1", "readJava1");
IOSequenceMatcher matcher = new IOSequenceMatcher(m_arrEventTimeBtrace,
m_arrEventTypeBtrace,
m_arrEventTimeJava,
m_arrEventTypeJava);
double dblResult = matcher.match();
assertEquals(matcher.getTotalBtraceIOTime(),15);
assertEquals(matcher.getTotalJavaIOTime(),13);
assertEquals(matcher.getDiscrepancy(),10);
assertTrue((dblResult>0.71)&&(dblResult < 0.72));
}
@Test
public void testMatch2()
{
initTest("readBtrace2", "readJava2");
IOSequenceMatcher matcher = new IOSequenceMatcher(m_arrEventTimeBtrace,
m_arrEventTypeBtrace,
m_arrEventTimeJava,
m_arrEventTypeJava);
double dblResult = matcher.match();
assertEquals(matcher.getTotalBtraceIOTime(),8);
assertEquals(matcher.getTotalJavaIOTime(),15);
assertEquals(matcher.getDiscrepancy(),12);
assertTrue((dblResult>1.04)&&(dblResult < 1.05));
}
}