Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort testcase input sorted unintentionally #256

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions sorts/sorts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func testFramework(t *testing.T, sortingFunction func([]int) []int) {
for _, test := range sortTests {
t.Run(test.name, func(t *testing.T) {
actual := sortingFunction(test.input)
actual := sortingFunction(cloneIntSlice(test.input))
pos, sorted := compareSlices(actual, test.expected)
if !sorted {
if pos == -1 {
Expand Down Expand Up @@ -60,69 +60,42 @@ func TestSelection(t *testing.T) {

//END TESTS

func BenchmarkBubble(b *testing.B) {
func benchmarkFramework(b *testing.B, sortingFunction func([]int) []int) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
bubbleSort(test.input)
b.StopTimer()
input := cloneIntSlice(test.input)
b.StartTimer()
sortingFunction(input)
}
}
}

func BenchmarkBubble(b *testing.B) {
benchmarkFramework(b, bubbleSort)
}
func BenchmarkInsertion(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
InsertionSort(test.input)
}
}
benchmarkFramework(b, InsertionSort)
}

func BenchmarkMerge(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
Mergesort(test.input)
}
}
benchmarkFramework(b, Mergesort)
}

func BenchmarkHeap(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
HeapSort(test.input)
}
}
benchmarkFramework(b, HeapSort)
}

func BenchmarkQuick(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
QuickSort(test.input)
}
}
benchmarkFramework(b, QuickSort)
}

func BenchmarkShell(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
ShellSort(test.input)
}
}
benchmarkFramework(b, ShellSort)
}

func BenchmarkRadix(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
RadixSort(test.input)
}
}
benchmarkFramework(b, RadixSort)
}

// Very Slow, consider commenting
func BenchmarkSelection(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
SelectionSort(test.input)
}
}
benchmarkFramework(b, SelectionSort)
}

func compareSlices(a []int, b []int) (int, bool) {
Expand Down
11 changes: 9 additions & 2 deletions sorts/sorts_testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func makeRandomSignedSlice(size int) []int {
}

func getSortedVersion(a []int) []int {
vzvu3k6k marked this conversation as resolved.
Show resolved Hide resolved
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
return a
b := cloneIntSlice(a)
sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
return b
}

func cloneIntSlice(src []int) []int {
vals := make([]int, len(src))
copy(vals, src)
return vals
}