예제 #1
0
func TestMergeSort(t *testing.T) {
	arr := array.NewWithElements(3, 6, 9, 1, 0, 11, 8)
	fmt.Printf("before mergesort: ")
	arr.Print()
	MergeSort(arr, 0, arr.Size()-1)
	fmt.Printf("after mergesort: ")
	arr.Print()

	expected := array.NewWithElements(0, 1, 3, 6, 8, 9, 11)

	if !arr.IsMemeberWiseEqual(expected) {
		t.Errorf("Sorting error")
	}
}
예제 #2
0
func TestHeapSort(t *testing.T) {
	arr := array.NewWithElements(4, 6, 3, 22, 34, 5, 9, 20)

	expected := array.NewWithElements(3, 4, 5, 6, 9, 20, 22, 34)
	HeapSort(arr)

	for i := 0; i < expected.Size(); i++ {
		item1, _ := arr.Get(i)
		item2, _ := expected.Get(i)

		if item1 != item2 {
			t.Errorf("Item[%d] = %d is not equal to the sorted array Item[%d] = %d", i, item1, i, item2)
		}
	}
}
예제 #3
0
func TestQuickSort(t *testing.T) {
	arr := array.NewWithElements(3, 6, 9, 1, 0, 11, 8)
	fmt.Printf("before quick sort: ")
	arr.Print()

	// choose how to select pivot element
	fn := SelectRandomPivot()
	//fn := SelectRightAsPivot()

	QuickSort(arr, 0, arr.Size()-1, fn)
	fmt.Printf("after quick sort: ")
	arr.Print()

	expected := array.NewWithElements(0, 1, 3, 6, 8, 9, 11)

	if !arr.IsMemeberWiseEqual(expected) {
		t.Errorf("Sorting error")
	}
}
예제 #4
0
파일: heap_test.go 프로젝트: yikliu/golib
func TestHeap(t *testing.T) {
	arr := arraylist.NewWithElements(4, 6, 3, 22, 34, 5, 9, 20)

	heap := New(arr)

	heap.data.Print()

	if head, _ := heap.data.Get(0); head != 34 {
		t.Errorf("heapfiy failed")
	}
}