Created
August 2, 2011 09:03
-
-
Save ijt/1119858 to your computer and use it in GitHub Desktop.
Quickcheck of sorting idempotency in Go (golang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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