Skip to content

Commit

Permalink
fix some of the broken tests
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923794 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Feb 13, 2025
1 parent f61ddde commit 50dcbd3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 22 deletions.
21 changes: 7 additions & 14 deletions poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -856,31 +856,24 @@ protected static int absoluteDay(LocalDateTime date, boolean use1904windowing)
* @throws IllegalArgumentException if date is invalid
*/
private static int absoluteDay(int year, int dayOfYear, boolean use1904windowing) {
return dayOfYear + daysInPriorYears(year, use1904windowing);
return dayOfYear + daysInPriorYears(year, dayOfYear, use1904windowing);
}

/**
* Return the number of days in prior years since 1900
*
* @return days number of days in years prior to yr.
* @param yr a year (1900 < yr < 4000)
* @param use1904windowing Should 1900 or 1904 date windowing be used?
* @throws IllegalArgumentException if year is outside of range.
*/

static int daysInPriorYears(int yr, boolean use1904windowing)
private static int daysInPriorYears(final int year, final int dayOfYear,
final boolean use1904windowing)
{
if ((!use1904windowing && yr < 1899) || (use1904windowing && yr < 1904)) {
if ((!use1904windowing && (year < 1900 && !isLastDay1899(year, dayOfYear)))
|| (use1904windowing && year < 1904)) {
throw new IllegalArgumentException("'year' must be 1900 or greater");
}

int yr1 = yr - 1;
int yr1 = year - 1;
int leapDays = yr1 / 4 // plus julian leap days in prior years
- yr1 / 100 // minus prior century years
+ yr1 / 400 // plus years divisible by 400
- 460; // leap days in previous 1900 years

return 365 * (yr - (use1904windowing ? 1904 : 1900)) + leapDays;
return 365 * (year - (use1904windowing ? 1904 : 1900)) + leapDays;
}

// set HH:MM:SS fields of cal to 00:00:00:000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testEDateProperValues() {
checkValue(1, 0, 1d);
checkValue(0, 1, 31d);
checkValue(1, 1, 32d);
checkValue(0, 0, /* BAD_DATE! */ -1.0d);
checkValue(0, 0, 0.0d);
checkValue(0, -2, /* BAD_DATE! */ -1.0d);
checkValue(0, -3, /* BAD_DATE! */ -1.0d);
checkValue(49104, 0, 49104d);
Expand Down Expand Up @@ -112,14 +112,14 @@ void testEDateInvalidValueEval() {
@Test
void testEDateBlankValueEval() {
NumberEval evaluate = (NumberEval) new EDate().evaluate(new ValueEval[]{BlankEval.instance, new NumberEval(0)}, null);
assertEquals(-1.0d, evaluate.getNumberValue(), 0);
assertEquals(0.0d, evaluate.getNumberValue(), 0);
}

@Test
void testEDateBlankRefValueEval() {
EDate eDate = new EDate();
NumberEval result = (NumberEval) eDate.evaluate(new ValueEval[]{new RefEvalImplementation(BlankEval.instance), new NumberEval(0)}, null);
assertEquals(-1.0d, result.getNumberValue(), 0, "0 startDate triggers BAD_DATE currently, thus -1.0!");
assertEquals(0.0d, result.getNumberValue(), 0, "0 startDate triggers BAD_DATE currently, thus -1.0!");

result = (NumberEval) eDate.evaluate(new ValueEval[]{new NumberEval(1), new RefEvalImplementation(BlankEval.instance)}, null);
assertEquals(1.0d, result.getNumberValue(), 0, "Blank is handled as 0 otherwise");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void testEOMonthProperValues() {
void testEOMonthBadDateValues() {
checkValue(0.0, -2, BAD_DATE);
checkValue(0.0, -3, BAD_DATE);
checkValue(DATE_1900_01_31, -1, BAD_DATE);
checkValue(DATE_1900_01_31, -2, BAD_DATE);
}

private void checkValue(double startDate, int monthInc, double expectedResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ protected void testReadOffice2007(String filename) throws IOException {

// Sanity check data
assertEquals("Values", s.getRow(0).getCell(0).toString());
assertEquals("10.0", s.getRow(2).getCell(0).toString());
assertEquals("10", s.getRow(2).getCell(0).toString());

// Check we found all the conditional formatting rules we should have
SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ void testBug61148() throws IOException {

eval.evaluateInCell(cell);

assertEquals("3.0", cell.toString());
assertEquals("3", cell.toString());
}
}

Expand Down Expand Up @@ -621,7 +621,7 @@ void testBug61532() throws IOException {

assertNotNull(eval.evaluateInCell(cell));

assertEquals("3.0", cell.toString());
assertEquals("3", cell.toString());
assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(3.0, cell.getNumericCellValue(), 0.01);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ void excelDateBorderCases() throws ParseException {
Date date32 = df.parse("1900-02-01");
assertEquals(32.0, DateUtil.getExcelDate(date32), 0.00001);
assertEquals(32.0, DateUtil.getExcelDate(DateUtil.toLocalDateTime(date32)), 0.00001);
Date dateMinus1 = df.parse("1899-12-31");
Date dateMinus1 = df.parse("1899-12-30");
assertEquals(/* BAD_DATE! */ -1.0, DateUtil.getExcelDate(dateMinus1), 0.00001);
assertEquals(/* BAD_DATE! */ -1.0, DateUtil.getExcelDate(DateUtil.toLocalDateTime(dateMinus1)), 0.00001);
}
Expand Down

0 comments on commit 50dcbd3

Please sign in to comment.