Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Layher <mdlayher@gmail.com>
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |