Example #1
0
func initSmallCollection(n int) *pq.Queue {
	q := pq.NewQueue()
	for i := 0; i < 10; i++ {
		q.Push(&message{rand.Intn(n)})
	}
	return q
}
Example #2
0
func initCollection() *pq.Queue {
	q := pq.NewQueue()
	for i := 0; i < 100; i++ {
		q.Push(&message{rand.Int()})
	}
	return q
}
Example #3
0
func TestHeapRemove(t *testing.T) {
	q := pq.NewQueue()
	q.Push(&message{8})
	q.Push(&message{3})
	q.Push(&message{9})
	q.Push(&message{1})
	q.Push(&message{10})
	q.Push(&message{2})
	q.Push(&message{5})
	q.Push(&message{6})
	q.Push(&message{7})
	q.Push(&message{4})
	heap.Init(q)

	r := heap.Remove(q, 5).(pq.Queueable)
	fmt.Println("removed:", r)

	if r.Priority() != 9 {
		t.Fatalf("Remove() fails, incorrect member removed.", r.Priority())
	}

	mb, _ := q.Member(0)
	if mb.Priority() != 1 {
		t.Fatalf("Remove() fails, unexpected value for min member")
	}
}
Example #4
0
func TestLess(t *testing.T) {
	q := pq.NewQueue()
	q.Push(&message{132})
	q.Push(&message{256})
	if !q.Less(0, 1) {
		t.Fatalf("Less() fails.")
	}
}
Example #5
0
func TestSwap(t *testing.T) {
	q := pq.NewQueue()
	q.Push(&message{132})
	q.Push(&message{256})
	q.Swap(0, 1)
	x, _ := q.Member(0)
	y, _ := q.Member(1)
	if x.Priority() != 256 || y.Priority() != 132 {
		t.Fatalf("Swap() fails.")
	}
}
Example #6
0
func TestPopElement(t *testing.T) {
	q := pq.NewQueue()
	q.Push(&message{128})
	q.Push(&message{256})
	q.Push(&message{512})

	x, ok := q.Pop().(pq.Queueable)
	if !ok {
		t.Fatalf("Pop() fails.")
	}
	if x.Priority() != 128 && q.Len() != 2 {
		t.Fatalf("Pop() fails, wrong element or Q is wrong size.")
	}
}
Example #7
0
func main() {

	// Add directly to Q object
	q := pq.NewQueue()
	q.Add(&Alert{DEBUG, "Debug Foo"})
	q.Add(&Alert{EMERG, "THIS IS FOOBAR!"})
	q.Add(&Alert{ERR, "Error Baz"})
	q.Add(&Alert{INFO, "Info Bar"})

	q.Add(&Log{ALERT, "Alert log message."})
	q.Add(&Log{CRIT, "Critial log message."})
	q.Add(&Log{WARN, "Warning log message."})

	for q.Len() > 0 {
		if prio, ok := q.Remove().(ExampleQueue); ok {
			fmt.Println(prio)
		}
	}

	// Using heap interface
	q = pq.NewQueue()
	Init(q)
	Push(q, &Alert{DEBUG, "Debug Foo"})
	Push(q, &Alert{EMERG, "THIS IS FOOBAR!"})
	Push(q, &Alert{ERR, "Error Baz"})
	Push(q, &Alert{INFO, "Info Bar"})

	Push(q, &Log{ALERT, "Alert log message."})
	Push(q, &Log{CRIT, "Critial log message."})
	Push(q, &Log{WARN, "Warning log message."})

	for q.Len() > 0 {
		if prio, ok := Pop(q).(ExampleQueue); ok {
			fmt.Println(prio)
		}
	}
}