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 4 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
21 changes: 15 additions & 6 deletions sorts/sorts_testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type sortTest struct {
name string
}

var uarr []int = makeRandomUnsignedSlice(500_000)
var arr []int = makeRandomSignedSlice(500_000)
var uarr []int = makeRandomUnsignedSlice(5_000)
var arr []int = makeRandomSignedSlice(5_000)

var sortTests = []sortTest{
//Sorted slice
Expand All @@ -23,7 +23,7 @@ var sortTests = []sortTest{
//Reversed slice
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Unsigned"},
//500k unsigned int values sort
//5k unsigned int values sort
{uarr, getSortedVersion(uarr), "Large Random Unsigned"},

//Sorted slice
Expand All @@ -42,7 +42,7 @@ var sortTests = []sortTest{
{[]int{-5, 7, 4, -2, 6, 5, 8, 3, 2, -7, -1, 0, -3, 9, -6, -4, 10, 9, 1, -8, -9, -10},
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10}, "Random order Signed"},

//500k int values sort
//5k int values sort
{arr, getSortedVersion(arr), "Large Random Signed"},

//Empty slice
Expand Down Expand Up @@ -70,6 +70,15 @@ 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
}

// cloneIntSlice returns a copy of a given slice of int.
// Useful to protect a slice from in-place sorting functions.
func cloneIntSlice(src []int) []int {
vals := make([]int, len(src))
copy(vals, src)
return vals
}