Skip to content

Instantly share code, notes, and snippets.

@ijt
Created August 2, 2011 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijt/1119858 to your computer and use it in GitHub Desktop.
Save ijt/1119858 to your computer and use it in GitHub Desktop.
Quickcheck of sorting idempotency in Go (golang)
package main
import (
"reflect"
"sort"
"testing"
"testing/quick"
)
type IntSlice []int
func (p IntSlice) Len() int { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func TestSortIsIdempotent(t *testing.T) {
prop := func(nums IntSlice) bool {
sort.Sort(nums)
nums2 := IntSlice(make([]int, len(nums)))
copy(nums2, nums)
sort.Sort(nums2)
return reflect.DeepEqual(nums, nums2)
}
if err := quick.Check(prop, nil); err != nil {
t.Error(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment