Done combining individual recurrence ringsAt tests

This commit is contained in:
Phillip Hsu 2016-06-10 15:11:26 -07:00
parent e3c7e8ffce
commit 468641a49b

View File

@ -48,7 +48,7 @@ public class AlarmTest {
} }
@Test @Test
public void alarm_RingsAt_ReturnsCorrectRingTime() { public void alarm_RingsAt_NoRecurrence_ReturnsCorrectRingTime() {
GregorianCalendar now = new GregorianCalendar(); GregorianCalendar now = new GregorianCalendar();
for (int h = 0; h < 24; h++) { for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) { for (int m = 0; m < 60; m++) {
@ -56,28 +56,36 @@ public class AlarmTest {
int hC = now.get(HOUR_OF_DAY); // Current hour int hC = now.get(HOUR_OF_DAY); // Current hour
int mC = now.get(MINUTE); // Current minute int mC = now.get(MINUTE); // Current minute
Alarm a = Alarm.builder().hour(h).minutes(m).build(); Alarm a = Alarm.builder().hour(h).minutes(m).build();
long calculatedRingTime;
// Quantities until the ring time (h, m)
int hours = 0;
int minutes = 0;
if (h <= hC) { if (h <= hC) {
if (m <= mC) { if (m <= mC) {
calculatedRingTime = (23-hC+h)*3600000 + (60-mC+m)*60000; hours = 23 - hC + h;
minutes = 60 - mC + m;
} else { } else {
calculatedRingTime = (m-mC)*60000; minutes = m - mC;
if (h < hC) { if (h < hC) {
calculatedRingTime += (24-hC+h)*3600000; hours = 24 - hC + h;
} }
} }
} else { } else {
if (m <= mC) { if (m <= mC) {
calculatedRingTime = (h-hC-1)*3600000+(60-mC+m)*60000; hours = h - hC - 1;
minutes = 60 - mC + m;
} else { } else {
calculatedRingTime = (h-hC)*3600000+(m-mC)*60000; hours = h - hC;
minutes = m - mC;
} }
} }
now.setTimeInMillis(now.getTimeInMillis() + calculatedRingTime); now.add(HOUR_OF_DAY, hours);
now.add(MINUTE, minutes);
now.set(SECOND, 0); now.set(SECOND, 0);
now.set(MILLISECOND, 0); now.set(MILLISECOND, 0);
assertEquals(a.ringsAt(), now.getTimeInMillis()); assertEquals(a.ringsAt(), now.getTimeInMillis());
// VERY IMPORTANT TO RESET AT THE END!!!! THIS TOOK A WHOLE FUCKING DAY OF BUG HUNTING!!! // VERY IMPORTANT TO RESET AT THE END!!!!
now.setTimeInMillis(System.currentTimeMillis()); now.setTimeInMillis(System.currentTimeMillis());
} }
} }
@ -86,62 +94,78 @@ public class AlarmTest {
@Test @Test
public void alarm_RingsAt_RecurringDays_ReturnsCorrectRingTime() { public void alarm_RingsAt_RecurringDays_ReturnsCorrectRingTime() {
Calendar cal = new GregorianCalendar(); Calendar cal = new GregorianCalendar();
int weekDayToday = cal.get(Calendar.DAY_OF_WEEK); int D_C = cal.get(Calendar.DAY_OF_WEEK);
for (int h = 0; h < 24; h++) { for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) { for (int m = 0; m < 60; m++) {
for (int d = SUNDAY; d <= SATURDAY; d++) { for (int D = Calendar.SUNDAY; D <= Calendar.SATURDAY; D++) {
out.println(String.format("Testing %02d:%02d for day %d", h, m, d)); out.println("Testing (h, m, d) = ("+h+", "+m+", "+ (D-1) +")");
int hC = cal.get(HOUR_OF_DAY); // Current hour int hC = cal.get(HOUR_OF_DAY); // Current hour
int mC = cal.get(MINUTE); // Current minute int mC = cal.get(MINUTE); // Current minute
Alarm a = Alarm.builder().hour(h).minutes(m).build(); Alarm a = Alarm.builder().hour(h).minutes(m).build();
a.setRecurring(d, true); a.setRecurring(D - 1, true);
long calculatedRingTime;
boolean calculatedToNextDay = true; int days = 0;
int hours = 0;
int minutes = 0;
if (h <= hC) { if (h <= hC) {
if (m <= mC) { if (m <= mC) {
calculatedRingTime = (23 - hC + h) * 3600000 + (60 - mC + m) * 60000; // Subtract 1 from the days because the hours and minutes
} else { // calculation will already count to the next day.
calculatedRingTime = (m - mC) * 60000; if (D < D_C) {
if (h < hC) { days = Calendar.SATURDAY - D_C + D - 1;
calculatedRingTime += (24 - hC + h) * 3600000; } else if (D == D_C) {
days = 6;
} else { } else {
// h == hC days = D - D_C - 1;
calculatedToNextDay = false; }
// Subtract 1 from the hours because the minutes calculation
// will already count to the next hour.
hours = 23 - hC + h;
minutes = 60 - mC + m;
} else {
minutes = m - mC;
if (h < hC) {
if (D < D_C) {
days = Calendar.SATURDAY - D_C + D - 1;
} else if (D == D_C) {
days = 6;
} else {
days = D - D_C - 1;
}
hours = 24 - hC + h;
} else /*if (h == hC)*/ {
if (D < D_C) {
days = Calendar.SATURDAY - D_C + D;
} else if (D == D_C) {
days = 0; // upcoming on the same day
} else {
days = D - D_C;
}
} }
} }
} else { } else {
if (m <= mC) { if (D < D_C) {
calculatedRingTime = (h - hC - 1) * 3600000 + (60 - mC + m) * 60000; days = Calendar.SATURDAY - D_C + D;
} else if (D == D_C) {
days = 0;
} else { } else {
calculatedRingTime = (h - hC) * 3600000 + (m - mC) * 60000; days = D - D_C;
}
if (m <= mC) {
hours = h - hC - 1;
minutes = 60 - mC + m;
} else {
hours = h - hC;
minutes = m - mC;
} }
calculatedToNextDay = false;
} }
int day = d + 1; // Match up with day constant defined in Calendar class cal.add(HOUR_OF_DAY, 24 * days);
int amount = calculatedToNextDay ? d : day; // the amount to add on cal.add(HOUR_OF_DAY, hours);
if (day > weekDayToday) { cal.add(MINUTE, minutes);
calculatedRingTime += 24 * (amount - weekDayToday) * 3600000;
} else if (day < weekDayToday) {
calculatedRingTime += 24 * (Calendar.SATURDAY - weekDayToday + amount) * 3600000;
} else {
long initialTime = cal.getTimeInMillis();
// Temporarily add on whatever we have so far
cal.setTimeInMillis(initialTime + calculatedRingTime);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
if (calculatedToNextDay) {
// Temporarily subtract off a whole day's worth of millis
cal.add(HOUR_OF_DAY, -24);
}
if (cal.getTimeInMillis() <= System.currentTimeMillis()) {
calculatedRingTime += 24 * (calculatedToNextDay ? 6 : 7) * 3600000;
}
cal.setTimeInMillis(initialTime);
}
cal.setTimeInMillis(cal.getTimeInMillis() + calculatedRingTime);
cal.set(SECOND, 0); cal.set(SECOND, 0);
cal.set(MILLISECOND, 0); cal.set(MILLISECOND, 0);
assertEquals(a.ringsAt(), cal.getTimeInMillis()); assertEquals(a.ringsAt(), cal.getTimeInMillis());
@ -155,46 +179,53 @@ public class AlarmTest {
@Test @Test
public void alarm_RingsAt_AllRecurringDays_ReturnsCorrectRingTime() { public void alarm_RingsAt_AllRecurringDays_ReturnsCorrectRingTime() {
// The results of this test should be the same as the normal ringsAt test: // The results of this test should be the same as the normal ringsAt test:
// alarm_RingsAt_ReturnsCorrectRingTime(). // alarm_RingsAt_NoRecurrence_ReturnsCorrectRingTime().
GregorianCalendar now = new GregorianCalendar(); GregorianCalendar now = new GregorianCalendar();
for (int h = 0; h < 24; h++) { for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) { for (int m = 0; m < 60; m++) {
out.println(String.format("Testing %02d:%02d", h, m));
int hC = now.get(HOUR_OF_DAY); // Current hour int hC = now.get(HOUR_OF_DAY); // Current hour
int mC = now.get(MINUTE); // Current minute int mC = now.get(MINUTE); // Current minute
Alarm a = Alarm.builder().hour(h).minutes(m).build(); Alarm a = Alarm.builder().hour(h).minutes(m).build();
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
a.setRecurring(i, true); a.setRecurring(i, true);
} }
long calculatedRingTime;
// Quantities until the ring time (h, m)
int hours = 0;
int minutes = 0;
if (h <= hC) { if (h <= hC) {
if (m <= mC) { if (m <= mC) {
calculatedRingTime = (23-hC+h)*3600000 + (60-mC+m)*60000; hours = 23 - hC + h;
minutes = 60 - mC + m;
} else { } else {
calculatedRingTime = (m-mC)*60000; minutes = m - mC;
if (h < hC) { if (h < hC) {
calculatedRingTime += (24-hC+h)*3600000; hours = 24 - hC + h;
} }
} }
} else { } else {
if (m <= mC) { if (m <= mC) {
calculatedRingTime = (h-hC-1)*3600000+(60-mC+m)*60000; hours = h - hC - 1;
minutes = 60 - mC + m;
} else { } else {
calculatedRingTime = (h-hC)*3600000+(m-mC)*60000; hours = h - hC;
minutes = m - mC;
} }
} }
now.setTimeInMillis(now.getTimeInMillis() + calculatedRingTime); now.add(HOUR_OF_DAY, hours);
now.add(MINUTE, minutes);
now.set(SECOND, 0); now.set(SECOND, 0);
now.set(MILLISECOND, 0); now.set(MILLISECOND, 0);
assertEquals(a.ringsAt(), now.getTimeInMillis()); assertEquals(a.ringsAt(), now.getTimeInMillis());
// VERY IMPORTANT TO RESET AT THE END!!!! THIS TOOK A WHOLE FUCKING DAY OF BUG HUNTING!!! // VERY IMPORTANT TO RESET AT THE END!!!!
now.setTimeInMillis(System.currentTimeMillis()); now.setTimeInMillis(System.currentTimeMillis());
} }
} }
} }
@Test @Test
public void alarm_RingsAt_CurrentDayRecurring_ReturnsCorrectRingTime() { public void alarm_RingsAt_RecurringDayIsCurrentDay_ReturnsCorrectRingTime() {
Calendar cal = new GregorianCalendar(); Calendar cal = new GregorianCalendar();
int dC = cal.get(Calendar.DAY_OF_WEEK) - 1; // Current week day, converted to our values int dC = cal.get(Calendar.DAY_OF_WEEK) - 1; // Current week day, converted to our values