Files
breeze/tests/habit_calendar_behavior.zi

239 lines
8.1 KiB
Ziran

#import, file "../src/habits/habit_types.zi";
#import, file "../src/habits/habit_calendar.zi";
#import, file "../src/habits/habit_days.zi";
#import, file "../src/habits/habit_schedule.zi";
#program_export
CalendarAnswer :: () -> s32 {
if habit_weekday_bit(20260105) != 0 { return 1 }
if habit_weekday_bit(20260925) != 4 { return 2 }
if habit_weekday_bit(20240229) != 3 { return 3 }
if habit_weekday_bit(20000229) != 1 { return 4 }
if habit_weekday_bit(21000229) != -1 { return 5 }
if habit_weekday_bit(20261301) != -1 { return 6 }
return 42
}
Planned :: (rule: *HabitSchedule, day: s32) -> s32 {
return ifx HabitScheduleMatches(rule, day) then 1 else 0
}
#program_export
ScheduleAnswer :: () -> s32 {
rule: HabitSchedule
rule.repeat = HABIT_REPEAT_DAILY
rule.every = 2
rule.start = 20261013
if Planned(*rule, 20261013) != 1 || Planned(*rule, 20261014) != 0 ||
Planned(*rule, 20261015) != 1 || Planned(*rule, 20261011) != 0 {
return 101
}
// Monday, Wednesday and Thursday every week.
rule = HabitSchedule.{}
rule.repeat = HABIT_REPEAT_WEEKLY
rule.every = 1
rule.weekdays = 1 + 4 + 8
if Planned(*rule, 20261012) != 1 || Planned(*rule, 20261013) != 0 ||
Planned(*rule, 20261014) != 1 || Planned(*rule, 20261015) != 1 ||
Planned(*rule, 20261018) != 0 {
return 102
}
rule.every = 2
rule.weekdays = 1
rule.start = 20261012
if Planned(*rule, 20261012) != 1 || Planned(*rule, 20261019) != 0 ||
Planned(*rule, 20261026) != 1 {
return 103
}
// The 1st, the 15th and the 31st, which is a shorter month's last day.
rule = HabitSchedule.{}
rule.repeat = HABIT_REPEAT_MONTHLY
rule.every = 1
rule.month_days = 1 + (1 << 14) + (1 << 30)
if Planned(*rule, 20261015) != 1 || Planned(*rule, 20261016) != 0 ||
Planned(*rule, 20260228) != 1 || Planned(*rule, 20260227) != 0 ||
Planned(*rule, 20261101) != 1 {
return 104
}
rule.month_days = 0
rule.week_of_month = -1
rule.weekdays = 16
if Planned(*rule, 20261030) != 1 || Planned(*rule, 20261023) != 0 {
return 105
}
rule.week_of_month = 1
rule.weekdays = 1
if Planned(*rule, 20261005) != 1 || Planned(*rule, 20261012) != 0 {
return 106
}
rule = HabitSchedule.{}
rule.repeat = HABIT_REPEAT_YEARLY
rule.every = 2
rule.start = 20241206
if Planned(*rule, 20261206) != 1 || Planned(*rule, 20261205) != 0 ||
Planned(*rule, 20251206) != 0 {
return 107
}
rule = HabitSchedule.{}
rule.repeat = HABIT_REPEAT_FORMULA
rule.start = 20261013
if !HabitScheduleSetFormula(*rule, "days % 3 == 0") ||
Planned(*rule, 20261016) != 1 || Planned(*rule, 20261014) != 0 {
return 108
}
rule.start = 20260101
if !HabitScheduleSetFormula(*rule, "weekday <= 5 and nth = 1") ||
Planned(*rule, 20261001) != 1 || Planned(*rule, 20261003) != 0 ||
Planned(*rule, 20261008) != 0 {
return 109
}
if !HabitScheduleSetFormula(*rule, "daysleft == 0 || (month - 1) % 3 == 0 && day == 1") ||
Planned(*rule, 20261031) != 1 || Planned(*rule, 20261001) != 1 ||
Planned(*rule, 20261101) != 0 {
return 110
}
if !HabitFormulaValid("not (weekday = 6 or weekday = 7)") ||
!HabitFormulaValid("-days / 2 >= -3") ||
HabitFormulaValid("days %% 3") || HabitFormulaValid("hours == 1") ||
HabitFormulaValid("(days") || HabitFormulaValid("") ||
HabitFormulaValid("days 3") || HabitFormulaValid("dayswhere") {
return 111
}
rule.repeat = HABIT_REPEAT_NONE
if Planned(*rule, 20261013) != 0 { return 112 }
return 42
}
// A changed schedule plans the days from the change on; earlier days keep
// the plan they had.
#program_export
HistoryAnswer :: () -> s32 {
// Without anything recorded, a schedule plans every day.
fresh: Habit
weekly: HabitSchedule
weekly.repeat = HABIT_REPEAT_WEEKLY
weekly.weekdays = 1
if !HabitScheduleSet(*fresh, weekly, 20261013, false) ||
fresh.schedule_count != 1 ||
habit_scheduled_day(*fresh, 20261006) != 0 {
return 200
}
habit: Habit
recorded: [1]HabitDay
recorded[0].day_index = 20261001
recorded[0].completed = 1
habit.days = *recorded[0]
habit.day_count = 1
habit.day_capacity = 1
if !HabitScheduleSet(*habit, weekly, 20261013, false) { return 201 }
if habit.schedule_count != 2 || habit.weekdays != 1 { return 202 }
if habit_scheduled_day(*habit, 20261006) != 1 ||
habit_scheduled_day(*habit, 20261014) != 0 ||
habit_scheduled_day(*habit, 20261019) != 1 {
return 203
}
if HabitScheduleSet(*habit, weekly, 20261013, false) { return 204 }
daily := HabitScheduleFromWeekdays(0)
if !HabitScheduleSet(*habit, daily, 20261013, false) ||
habit.schedule_count != 1 || habit.weekdays != 0 {
return 205
}
if !HabitScheduleSet(*habit, weekly, 20261013, false) ||
!HabitScheduleSet(*habit, daily, 20261020, false) ||
habit.schedule_count != 3 {
return 206
}
if habit_scheduled_day(*habit, 20261006) != 1 ||
habit_scheduled_day(*habit, 20261014) != 0 ||
habit_scheduled_day(*habit, 20261021) != 1 {
return 207
}
formula: HabitSchedule
formula.repeat = HABIT_REPEAT_FORMULA
unused HabitScheduleSetFormula(*formula, "days % 2 == 0")
if !HabitScheduleSet(*habit, formula, 20261101, false) ||
habit.schedules[3].start != 20261101 {
return 208
}
text: [HABIT_SCHEDULE_TEXT_SIZE]u8
written := HabitScheduleWrite(*habit, text[:])
copy: Habit
copy.days = habit.days
copy.day_count = 1
copy.day_capacity = 1
HabitScheduleRead(*copy, written)
if copy.schedule_count != habit.schedule_count { return 209 }
index: s32 = 0
while index < copy.schedule_count {
if copy.schedules[index].from != habit.schedules[index].from ||
!HabitScheduleSame(*copy.schedules[index], *habit.schedules[index]) {
return 210
}
index += 1
}
if habit_scheduled_day(*copy, 20261103) != 1 ||
habit_scheduled_day(*copy, 20261102) != 0 {
return 211
}
bad: HabitSchedule
bad.repeat = HABIT_REPEAT_FORMULA
unused HabitScheduleSetFormula(*bad, "days %")
if HabitScheduleSet(*habit, bad, 20261102, false) { return 212 }
described: [256]u8
if HabitScheduleDescribe(*weekly, described[:]) != "every week on mon" {
return 213
}
return 42
}
#program_export
Answer :: () -> s32 {
if CalendarAnswer() != 42 { return 1 }
if ScheduleAnswer() != 42 { return 2 }
if HistoryAnswer() != 42 { return 3 }
habit: Habit
if habit_scheduled_day(*habit, 20260925) != 1 { return 7 }
habit.weekdays = 16
if habit_scheduled_day(*habit, 20260925) != 1 { return 8 }
if habit_scheduled_day(*habit, 20260105) != 0 { return 9 }
habit.weekdays = HABIT_SCHEDULE_OFF
if habit_scheduled_day(*habit, 20260925) != 0 { return 10 }
habit.counter_enabled = 1
habit.counter_target = 3
if habit_counting_enabled(*habit) != 1 ||
habit_counter_target(*habit) != 3 { return 11 }
days: [2]HabitDay
days[0].day_index = 20260925
days[0].count = 2
days[0].completed = 1
days[1].day_index = 20260926
days[1].count = 3
habit.days = *days[0]
habit.day_count = 2
habit.day_capacity = 2
if HabitCompletedFor(*habit, 20260925) ||
!HabitCompletedOnDay(days[:], 20260926, true, 3) ||
!HabitCompletedOnDay(days[:], 20260925, false, 3) ||
HabitCompletedOnDay(days[:], 20260924, false, 1) {
return 12
}
habit.day_count = 3
if HabitCompletedFor(*habit, 20260926) == false { return 13 }
habit.day_count = 2
habit.counter_target = 0
days[0].count = 0
if habit_counter_target(*habit) != 0 ||
!HabitCompletedFor(*habit, 20260925) ||
HabitDayCount(*habit, 20260925) != 0 ||
HabitCompletedFor(*habit, 20260926) ||
HabitCompletedFor(*habit, 20260924) {
return 14
}
days[0].session_count = 1
if HabitCompletedFor(*habit, 20260925) { return 15 }
days[0].session_count = 0
days[0].completed = 0
if HabitCompletedFor(*habit, 20260925) { return 16 }
return 42
}