Exemplo n.º 1
0
func TestPQInts(t *testing.T) {
	_, err := pq.New(&pq.IntHeap{0, 1, 2, 3})
	if err == nil {
		t.Errorf("New() with data didn't error")
	}
	q, err := pq.New(new(pq.IntHeap))
	if q == nil || err != nil {
		t.Errorf("New() failed")
	}
	if q.Len() != 0 {
		t.Errorf("Len() on empty wasn't 0")
	}
	if q.Peek() != nil {
		t.Errorf("Peek() on empty wasn't nil")
	}
	for i := 10; i >= 0; i-- {
		q.Push(i)
		// t.Log("Heap is", q)
	}
	for i := 0; i <= 10; i++ {
		// t.Log("Heap is", q)
		if q.Peek().(int) != i {
			t.Error("Peek() wasn't", i)
		}
		if q.Pop().(int) != i {
			t.Error("Pop() wasn't", i)
		}
	}
}
Exemplo n.º 2
0
func main() {
	if len(os.Args) < 2 {
		return
	}

	// skip command name
	args := os.Args[1:]

	q := pq.New(func(i, j interface{}) bool { return i.(string) < j.(string) })

	for _, f := range args {
		in, err := os.Open(f)
		if err != nil {
			log.Fatal("Couldn't open file", f, ":", err)
		}
		scanner := bufio.NewScanner(in)
		scanner.Split(bufio.ScanWords)
		for scanner.Scan() {
			q.Push(scanner.Text())
		}
	}

	if q.Size() == 0 {
		return
	}

	fmt.Print(q.Pop().(string))
	for q.Size() != 0 {
		fmt.Print(" " + q.Pop().(string))
	}
	fmt.Print("\n")

	return
}