From ec94d0214c086f84af3f1d1a66e5833451aa02d9 Mon Sep 17 00:00:00 2001 From: "M. Waisberg" Date: Sun, 7 Jul 2024 08:13:32 +0300 Subject: [PATCH] Test leap years --- .../zmanim/hebrewcalendar/JewishDate.java | 15 +++---- .../hebrewcalendar/UT_DaysInJewishMonth.java | 17 ++++++++ .../hebrewcalendar/UT_JewishLeapYear.java | 39 +++++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java index c9bfe0a5..3a2ecd6d 100644 --- a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java +++ b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java @@ -309,7 +309,8 @@ public int getMoladChalakim() { * the month. As with other cases in this class, this is 1-based, not zero-based. * @return the number of days in the month in the given year */ - private static int getLastDayOfGregorianMonth(int year, int month) { + // @VisibleForTesting + protected static int getLastDayOfGregorianMonth(int year, int month) { return YearMonth.of(year, month).lengthOfMonth(); } @@ -1280,12 +1281,8 @@ public Object clone() { return clone; } - /** - * Overrides {@link Object#hashCode()}. - * @see Object#hashCode() - */ - @Override - public int hashCode() { - return Integer.hashCode(gregorianAbsDate); - } + @Override + public int hashCode() { + return gregorianAbsDate; + } } diff --git a/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_DaysInJewishMonth.java b/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_DaysInJewishMonth.java index 2e549b98..4aaa18b5 100644 --- a/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_DaysInJewishMonth.java +++ b/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_DaysInJewishMonth.java @@ -4,8 +4,14 @@ package com.kosherjava.zmanim.hebrewcalendar; +import static org.junit.Assert.assertEquals; + import org.junit.*; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.Month; + /** * Validate the days in a Hebrew month (in various types of years) are correct. */ @@ -111,4 +117,15 @@ private void assertShalemLeap(int year) { Assert.assertTrue(jewishDate.isJewishLeapYear( )); } + @Test + public void earlyGregorian() { + LocalDate localDate = LocalDate.of(1582, Month.OCTOBER, 15); + JewishDate jewishDate = new JewishDate(localDate); + + assertEquals(DayOfWeek.FRIDAY, localDate.getDayOfWeek()); + assertEquals(5343, jewishDate.getJewishYear()); + assertEquals(JewishDate.TISHREI, jewishDate.getJewishMonth()); + assertEquals(19, jewishDate.getJewishDayOfMonth()); + } + } // End of UT_DaysInJewishMonth class diff --git a/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_JewishLeapYear.java b/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_JewishLeapYear.java index 96c582e3..1ce779e4 100644 --- a/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_JewishLeapYear.java +++ b/src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_JewishLeapYear.java @@ -4,8 +4,12 @@ package com.kosherjava.zmanim.hebrewcalendar; +import static org.junit.Assert.assertEquals; + import org.junit.*; +import java.time.Month; + /** * Verify correct calculations of when a Hebrew leap year occurs. */ @@ -63,4 +67,39 @@ private void shouldNotBeLeapYear(int year) { Assert.assertFalse(jewishDate.isJewishLeapYear( )); } + @Test + public void icu() { + for (int y = 0; y < 10000; y++) { + assertEquals(isLeapYearICU(y), JewishDate.isJewishLeapYear(y)); + } + } + + /** android.icu.util.HebrewCalendar */ + public static boolean isLeapYearICU(int year) { + int x = (year * 12 + 17) % 19; + return x >= ((x < 0) ? -7 : 12); + } + + @Test + public void leapYears(){ + final int MONTH_FEBRUARY = Month.FEBRUARY.getValue(); + for (int y = 0; y < 10000; y++) { + assertEquals(isLeapYearWiki(y), JewishDate.getLastDayOfGregorianMonth(y, MONTH_FEBRUARY) == 29); + } + } + + /** https://en.wikipedia.org/wiki/Leap_year#Algorithm */ + private static boolean isLeapYearWiki(int year) { + if (year % 4 != 0) { + return false; + } + if (year % 100 != 0) { + return true; + } + if (year % 400 != 0) { + return false; + } + return true; + } + } // End of UT_JewishLeapYear class