Skip to content

Commit

Permalink
schedgroup: tasks heap tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Layher <mdlayher@gmail.com>
  • Loading branch information
mdlayher committed Jan 12, 2020
1 parent 836df1e commit 294397d
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions heap_test.go
@@ -0,0 +1,69 @@
package schedgroup

import (
"container/heap"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func Test_tasksHeap(t *testing.T) {
newTask := func(d time.Duration) task {
return task{
// Static start time for consistency, no call function.
Deadline: time.Unix(0, 0).Add(d * time.Second),
}
}

tests := []struct {
name string
in, want []task
}{
{
name: "ordered",
in: []task{
newTask(1),
newTask(2),
newTask(3),
},
want: []task{
newTask(1),
newTask(2),
newTask(3),
},
},
{
name: "unordered",
in: []task{
newTask(3),
newTask(1),
newTask(2),
},
want: []task{
newTask(1),
newTask(2),
newTask(3),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Push and pop all tasks to verify the heap.Interface implementation.
var tasks tasks
for _, v := range tt.in {
heap.Push(&tasks, v)
}

var got []task
for tasks.Len() > 0 {
got = append(got, heap.Pop(&tasks).(task))
}

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Fatalf("unexpected output tasks (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 294397d

Please sign in to comment.