Commit 0b758eed authored by Marcel's avatar Marcel
Browse files

Added missing DateFunctionTests, code cleanup

parent 3de88513
......@@ -81,6 +81,7 @@ public class AllTests {
suite.addTestSuite(SymbolFunctionTest.class);
suite.addTestSuite(TaxonomyTest.class);
suite.addTestSuite(TrigramTest.class);
suite.addTestSuite(DateFunctionTest.class);
// load package
//suite.addTestSuite(Test1000.class);
......
......@@ -49,239 +49,194 @@ public class DateFunctionTest extends TestCase {
try {
TestFramework frame = new TestFramework();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
// Add new DateDescription
DateDesc newDate = new DateDesc(frame.carDesc, "newDate", df.parse("1950-01-01 00:00"), df.parse("2099-01-01 00:00"), df);
DateDesc newDate = new DateDesc(frame.carDesc, "newDate", df.parse("1950-01-01 00:00:00"), df.parse("2099-01-01 00:00:00"), df);
// Add new DateFunction
DateFct f = newDate.addDateFct("DateFct", true, DateFct.DateFunctionPrecision.Hour);
DateFct f = newDate.addDateFct("DateFct", true, DateFct.DateFunctionPrecision.Second);
// Start testing
// Equaltest
DateAttribute date1 = newDate.getDateAttribute(df.parse("2014-04-15 00:00"));
DateAttribute date2 = newDate.getDateAttribute(df.parse("2014-04-15 00:00"));
DateAttribute date1 = newDate.getDateAttribute(df.parse("2014-04-15 00:00:00"));
DateAttribute date2 = newDate.getDateAttribute(df.parse("2014-04-15 00:00:00"));
Similarity sim = f.calculateSimilarity(date1, date2);
//assertTrue("Equaltest failed result: " + sim.getRoundedValue(), sim.getValue() == 1);
// Hour Tests
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 23:59"));
sim = f.calculateSimilarity(date1, date2);
////System.out.println("Similarity Hour [00:01 - 23:59]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.08333);
// Seconds Test
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 01:50"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Hour [01:00 - 01:50]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 02:00"));
assertTrue(sim.getValue() == 0.5);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Hour [01:00 - 02:00]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.95833);
assertTrue(sim.getValue() == 0);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 12:00"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 14:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Hour [00:00 - 12:00]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.5);
//System.out.println("\r\n=== Hour Loop Test || Start");
for (int i = 0; i < 24; i++) {
String hour = "";
if (i < 10) {
hour = "0";
}
hour += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-16 " + hour + ":03"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 00:03"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Hour [" + hour + ":03 - 00:03]: " + sim.getValue());
}
//System.out.println("=== Hour Loop Test || End\n");
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
assertTrue(sim.getValue() == 0);
date1 = newDate.getDateAttribute(df.parse("2014-04-17 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Hours [2014-04-17 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Hours [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Hours [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Minute Test
df = new SimpleDateFormat("yyyy-MM-dd kk:mm");
// Minutes Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Minute);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:30"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:30:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Minute [13:00 - 13:30]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.5);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Minute [13:00 - 13:00]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:31"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:31"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:31:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:31:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Minute [13:31 - 13:31]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:59"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:59:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Minute [13:00 - 13:59]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.01667);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:45"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:45:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Minute [13:00 - 13:45]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.25);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 14:45"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 14:45:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("XXX Similarity Minute [13:00 - 14:45]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0);
//System.out.println("\n=== Minute Loop Test || Start");
for (int i = 0; i < 60; i++) {
String minute = "";
if (i < 10) {
minute = "0";
}
minute += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:" + i));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Minute [13:00 - 13:" + minute + "]: " + sim.getValue());
}
//System.out.println("=== Minute Loop Test || End\n");
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
date1 = newDate.getDateAttribute(df.parse("2014-04-16 14:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Minutes [2014-04-16 14:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-04-17 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Minutes [2014-04-17 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Minutes [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Minutes [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Hour Tests
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Hour);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 23:59:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(round(sim.getValue()) == 0.08333);
// Seconds Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Second);
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
//System.out.println("\n=== Seconds Loop Test || Start");
for (int i = 0; i < 60; i++) {
String second = "";
if (i < 10) {
second = "0";
}
second += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:" + i));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Seconds [13:00:00 - 13:00:" + second + "]: " + sim.getValue());
}
//System.out.println("=== Second Loop Test || End\n");
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 01:50:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 02:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Seconds [13:01:00 - 13:00:30]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.95833);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 14:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
date1 = newDate.getDateAttribute(df.parse("2014-04-16 00:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 12:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Seconds [14:00:00 - 13:00:30]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.5);
date1 = newDate.getDateAttribute(df.parse("2014-04-17 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Seconds [2014-04-17 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Seconds [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Seconds [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Days Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Day);
df = new SimpleDateFormat("yyyy-MM-dd");
//System.out.println("\n=== Days Loop Test || Start");
for (int i = 1; i <= 31; i++) {
String day = "";
if (i < 10) {
day = "0";
}
day += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-" + day));
date2 = newDate.getDateAttribute(df.parse("2014-04-01"));
sim = f.calculateSimilarity(date1, date2);
//System.out.println("Similarity Days [2014-04-01 | 2014-04-" + day + "]: " + sim.getValue());
}
//System.out.println("=== Days Loop Test || End\n");
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 1);
date1 = newDate.getDateAttribute(df.parse("2014-05-15 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-05-31 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(round(sim.getValue()) == 0.48387);
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Days [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
//System.out.println("Similarity Days [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Month Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Month);
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 1);
date1 = newDate.getDateAttribute(df.parse("2014-12-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-06-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0.5);
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2015-05-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
// Year Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Year);
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 1);
} catch (Exception e) {
assertTrue("Excpetion in testCalculateSimylarityHOUR",false);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment