diff options
author | Dominik Stadler <centic@apache.org> | 2016-10-15 06:54:20 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2016-10-15 06:54:20 +0000 |
commit | 2ae9b30a6cf3edd4c686ca3cc80cb13002683dba (patch) | |
tree | 8e12fc163d5352326ceda2793d0a3b367e67c4b3 /src/testcases/org | |
parent | 1bda7f6ef6a8bf05c311eeb9b9dc9619c046ca95 (diff) | |
download | poi-2ae9b30a6cf3edd4c686ca3cc80cb13002683dba.tar.gz poi-2ae9b30a6cf3edd4c686ca3cc80cb13002683dba.zip |
Bug 60029: apply suggested fix and fix some IntelliJ warnings
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1765018 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org')
-rw-r--r-- | src/testcases/org/apache/poi/ss/formula/functions/TestDays360.java | 225 |
1 files changed, 122 insertions, 103 deletions
diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestDays360.java b/src/testcases/org/apache/poi/ss/formula/functions/TestDays360.java index dbe41a883c..159477057e 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestDays360.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestDays360.java @@ -33,114 +33,133 @@ import org.junit.Test; public final class TestDays360 { - /** - * @param month 1-based - */ - private static Date makeDate(int year, int month, int day) { - Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day); - return cal.getTime(); - } - - private static Date decrementDay(Date d) { - Calendar c = LocaleUtil.getLocaleCalendar(); - c.setTime(d); - c.add(Calendar.DAY_OF_MONTH, -1); - return c.getTime(); - } - - @Test - public void testBasic() { - confirm(120, 2009, 1, 15, 2009, 5, 15); - confirm(158, 2009, 1, 26, 2009, 7, 4); - - // same results in leap years - confirm(120, 2008, 1, 15, 2008, 5, 15); - confirm(158, 2008, 1, 26, 2008, 7, 4); - - // longer time spans - confirm(562, 2008, 8, 11, 2010, 3, 3); - confirm(916, 2007, 2, 23, 2009, 9, 9); - - // other tests - confirm(1, makeDate(1993, 2, 28), makeDate(1993, 3, 1), false); + /** + * @param month 1-based + */ + private static Date makeDate(int year, int month, int day) { + Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day); + return cal.getTime(); + } + + private static Date decrementDay(Date d) { + Calendar c = LocaleUtil.getLocaleCalendar(); + c.setTime(d); + c.add(Calendar.DAY_OF_MONTH, -1); + return c.getTime(); + } + + @Test + public void testBasic() { + confirm(120, 2009, 1, 15, 2009, 5, 15); + confirm(158, 2009, 1, 26, 2009, 7, 4); + + // same results in leap years + confirm(120, 2008, 1, 15, 2008, 5, 15); + confirm(158, 2008, 1, 26, 2008, 7, 4); + + // longer time spans + confirm(562, 2008, 8, 11, 2010, 3, 3); + confirm(916, 2007, 2, 23, 2009, 9, 9); + + // other tests + confirm(1, makeDate(1993, 2, 28), makeDate(1993, 3, 1), false); confirm(1, makeDate(1996, 2, 29), makeDate(1996, 3, 1), false); confirm(-2, makeDate(1993, 2, 28), makeDate(1993, 2, 28), false); confirm(3, makeDate(1993, 2, 28), makeDate(1993, 3, 1), true); confirm(2, makeDate(1996, 2, 29), makeDate(1996, 3, 1), true); - } - - private static void confirm(int expResult, int y1, int m1, int d1, int y2, int m2, int d2) { - confirm(expResult, makeDate(y1, m1, d1), makeDate(y2, m2, d2), false); - confirm(-expResult, makeDate(y2, m2, d2), makeDate(y1, m1, d1), false); - } - - /** - * The <tt>method</tt> parameter only makes a difference when the second parameter - * is the last day of the month that does <em>not</em> have 30 days. - */ - @Test - public void testMonthBoundaries() { - // jan - confirmMonthBoundary(false, 2001, 1, 0, 0, 2, 3, 4); - confirmMonthBoundary(true, 2001, 1, 0, 0, 1, 2, 3); - // feb - confirmMonthBoundary(false, 2001, 2,-2, 1, 2, 3, 4); - confirmMonthBoundary(true, 2001, 2, 0, 1, 2, 3, 4); - // mar - confirmMonthBoundary(false, 2001, 3, 0, 0, 2, 3, 4); - confirmMonthBoundary(true, 2001, 3, 0, 0, 1, 2, 3); - // apr - confirmMonthBoundary(false, 2001, 4, 0, 1, 2, 3, 4); - confirmMonthBoundary(true, 2001, 4, 0, 1, 2, 3, 4); - // may - confirmMonthBoundary(false, 2001, 5, 0, 0, 2, 3, 4); - confirmMonthBoundary(true, 2001, 5, 0, 0, 1, 2, 3); - // jun - confirmMonthBoundary(false, 2001, 6, 0, 1, 2, 3, 4); - confirmMonthBoundary(true, 2001, 6, 0, 1, 2, 3, 4); + + // from https://support.office.com/en-us/article/DAYS360-function-B9A509FD-49EF-407E-94DF-0CBDA5718C2A + confirm(1, makeDate(2011, 1, 30), makeDate(2011, 2, 1), false); + confirm(360, makeDate(2011, 1, 1), makeDate(2011, 12, 31), false); + confirm(30, makeDate(2011, 1, 1), makeDate(2011, 2, 1), false); + } + + private static void confirm(int expResult, int y1, int m1, int d1, int y2, int m2, int d2) { + confirm(expResult, makeDate(y1, m1, d1), makeDate(y2, m2, d2), false); + confirm(-expResult, makeDate(y2, m2, d2), makeDate(y1, m1, d1), false); + } + + /** + * The <tt>method</tt> parameter only makes a difference when the second parameter + * is the last day of the month that does <em>not</em> have 30 days. + */ + @Test + public void testMonthBoundaries() { + // jan + confirmMonthBoundary(false, 2001, 1, 0, 0, 2, 3, 4); + confirmMonthBoundary(true, 2001, 1, 0, 0, 1, 2, 3); + // feb + confirmMonthBoundary(false, 2001, 2,-2, 1, 2, 3, 4); + confirmMonthBoundary(true, 2001, 2, 0, 1, 2, 3, 4); + // mar + confirmMonthBoundary(false, 2001, 3, 0, 0, 2, 3, 4); + confirmMonthBoundary(true, 2001, 3, 0, 0, 1, 2, 3); + // apr + confirmMonthBoundary(false, 2001, 4, 0, 1, 2, 3, 4); + confirmMonthBoundary(true, 2001, 4, 0, 1, 2, 3, 4); + // may + confirmMonthBoundary(false, 2001, 5, 0, 0, 2, 3, 4); + confirmMonthBoundary(true, 2001, 5, 0, 0, 1, 2, 3); + // jun + confirmMonthBoundary(false, 2001, 6, 0, 1, 2, 3, 4); + confirmMonthBoundary(true, 2001, 6, 0, 1, 2, 3, 4); // leap year confirmMonthBoundary(false, 2012, 2, -1, 1, 2, 3, 4); confirmMonthBoundary(true, 2012, 2, 0, 1, 2, 3, 4); - } - - - /** - * @param monthNo 1-based - * @param diffs - */ - private static void confirmMonthBoundary(boolean method, int year, int monthNo, int...diffs) { - Date firstDayOfNextMonth = makeDate(year, monthNo+1, 1); - Date secondArg = decrementDay(firstDayOfNextMonth); - Date firstArg = secondArg; - - for (int expResult : diffs) { - confirm(expResult, firstArg, secondArg, method); - firstArg = decrementDay(firstArg); - } - - } - private static void confirm(int expResult, Date firstArg, Date secondArg, boolean method) { - - ValueEval ve; - if (method) { - // TODO enable 3rd arg - - ve = invokeDays360(convert(firstArg), convert(secondArg), BoolEval.valueOf(method)); - } else { - ve = invokeDays360(convert(firstArg), convert(secondArg)); - } - assertTrue("wrong return type (" + ve.getClass().getName() + ")", ve instanceof NumberEval); - - NumberEval numberEval = (NumberEval) ve; - String err = String.format(Locale.ROOT, "days360(%tF,%tF,%b) wrong result", firstArg, secondArg, method); - assertEquals(err, expResult, numberEval.getNumberValue(), 0); - } - - private static ValueEval invokeDays360(ValueEval...args) { - return new Days360().evaluate(args, -1, -1); - } - - private static NumberEval convert(Date d) { - return new NumberEval(HSSFDateUtil.getExcelDate(d)); - } -} + // bug 60029 + Date start = makeDate(2018, 2, 28); + Date end = makeDate(2018, 3, 31); + confirm(30, start, end, false); + + // examples from https://support.office.com/en-us/article/DAYS360-function-B9A509FD-49EF-407E-94DF-0CBDA5718C2A + start = makeDate(2011, 1, 30); + end = makeDate(2011, 2, 1); + confirm(1, start, end, false); + + start = makeDate(2011, 1, 1); + end = makeDate(2011, 12, 31); + confirm(360, start, end, false); + + start = makeDate(2011, 1, 1); + end = makeDate(2011, 2, 1); + confirm(30, start, end, false); + } + + + /** + * @param monthNo 1-based + */ + private static void confirmMonthBoundary(boolean method, int year, int monthNo, int...diffs) { + Date firstDayOfNextMonth = makeDate(year, monthNo+1, 1); + Date secondArg = decrementDay(firstDayOfNextMonth); + Date firstArg = secondArg; + + for (int expResult : diffs) { + confirm(expResult, firstArg, secondArg, method); + firstArg = decrementDay(firstArg); + } + + } + private static void confirm(int expResult, Date firstArg, Date secondArg, boolean method) { + ValueEval ve; + if (method) { + ve = invokeDays360(convert(firstArg), convert(secondArg), BoolEval.TRUE); + } else { + ve = invokeDays360(convert(firstArg), convert(secondArg)); + } + assertTrue("wrong return type (" + ve.getClass().getName() + ")", ve instanceof NumberEval); + + NumberEval numberEval = (NumberEval) ve; + String err = String.format(Locale.ROOT, "days360(%tF,%tF,%b) wrong result", firstArg, secondArg, method); + assertEquals(err, expResult, numberEval.getNumberValue(), 0); + } + + private static ValueEval invokeDays360(ValueEval...args) { + return new Days360().evaluate(args, -1, -1); + } + + private static NumberEval convert(Date d) { + return new NumberEval(HSSFDateUtil.getExcelDate(d)); + } +} |