From 4098a882ca0dfa1f785ecf5800cb1efcbcfe44a4 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 24 Aug 2020 14:17:34 +0100 Subject: elementary priority queue --- queue/pq_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 queue/pq_test.go (limited to 'queue/pq_test.go') diff --git a/queue/pq_test.go b/queue/pq_test.go new file mode 100644 index 0000000..35e12fd --- /dev/null +++ b/queue/pq_test.go @@ -0,0 +1,42 @@ +package queue + +import ( + "algorithms/ds" + "fmt" + "testing" +) + +const minLength int = 1 +const maxLength int = 1000 +const factor int = 10 + +func TestElementaryPQ(t *testing.T) { + q := NewElementaryPQ(1) + for i := minLength; i <= maxLength; i *= factor { + test(q, i, t) + } +} + +func test(q PQ, l int, t *testing.T) { + cb := func(t *testing.T) { + t.Parallel() + for _, a := range ds.NewRandomArrayList(l, -1) { + q.Insert(a) + } + prev, started := 0, false + for !q.Empty() { + next := q.DeleteMax() + if started { + if next < prev { + t.Errorf("Expected element '%v' to be lower than previous '%v': %v", + next, prev, q) + } + prev = next + continue + } + started = true + prev = next + } + } + t.Run(fmt.Sprintf("%d", l), cb) +} -- cgit v1.2.3