From 63cab00c1078761b9a8acfefad5b707d797a3a16 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 5 Jul 2012 11:42:38 +0300 Subject: [PATCH] better action timing sorting function --- timespans/action_timing.go | 9 +++++++- timespans/actions_test.go | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/timespans/action_timing.go b/timespans/action_timing.go index 0fd7062ed..5f6bc0b12 100644 --- a/timespans/action_timing.go +++ b/timespans/action_timing.go @@ -191,13 +191,20 @@ func (atpl ActionTimingPriotityList) Swap(i, j int) { } func (atpl ActionTimingPriotityList) Less(i, j int) bool { - return atpl[i].GetNextStartTime().Before(atpl[j].GetNextStartTime()) || atpl[i].Weight < atpl[j].Weight + if atpl[i].GetNextStartTime().Equal(atpl[j].GetNextStartTime()) { + return atpl[i].Weight < atpl[j].Weight + } + return atpl[i].GetNextStartTime().Before(atpl[j].GetNextStartTime()) } func (atpl ActionTimingPriotityList) Sort() { sort.Sort(atpl) } +func (at *ActionTiming) String() string { + return at.GetNextStartTime().String() + ",w: " + strconv.FormatFloat(at.Weight, 'f', -1, 64) +} + /* Serializes the action timing for the storage. Used for key-value storages. */ diff --git a/timespans/actions_test.go b/timespans/actions_test.go index 4ebec5e23..95e7aba79 100644 --- a/timespans/actions_test.go +++ b/timespans/actions_test.go @@ -281,6 +281,53 @@ func TestActionTimingLogFunction(t *testing.T) { } } +func TestActionTimingPriotityList(t *testing.T) { + at1 := &ActionTiming{Timing: &Interval{ + Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}, + MonthDays: MonthDays{1}, + StartTime: "00:00:00", + Weight: 20, + }} + at2 := &ActionTiming{Timing: &Interval{ + Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}, + MonthDays: MonthDays{2}, + StartTime: "00:00:00", + Weight: 10, + }} + var atpl ActionTimingPriotityList + atpl = append(atpl, at2, at1) + t.Log(atpl) + atpl.Sort() + if atpl[0] != at1 || atpl[1] != at2 { + t.Error("Timing list not sorted correctly: ", atpl) + } +} + +func TestActionTimingPriotityListWeight(t *testing.T) { + at1 := &ActionTiming{ + Timing: &Interval{ + Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}, + MonthDays: MonthDays{1}, + StartTime: "00:00:00", + }, + Weight: 10.0, + } + at2 := &ActionTiming{ + Timing: &Interval{ + Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}, + MonthDays: MonthDays{1}, + StartTime: "00:00:00", + }, + Weight: 20.0, + } + var atpl ActionTimingPriotityList + atpl = append(atpl, at2, at1) + atpl.Sort() + if atpl[0] != at1 || atpl[1] != at2 { + t.Error("Timing list not sorted correctly: ", atpl) + } +} + func TestActionTriggerPriotityList(t *testing.T) { at1 := &ActionTrigger{Weight: 10} at2 := &ActionTrigger{Weight: 20}