Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active October 5, 2018 10:51
Show Gist options
  • Select an option

  • Save fbiville/f2a2ecfcd216acb3fb42ab59b89be912 to your computer and use it in GitHub Desktop.

Select an option

Save fbiville/f2a2ecfcd216acb3fb42ab59b89be912 to your computer and use it in GitHub Desktop.
Golang property-based testing example
package main
import (
"testing"
"testing/quick"
)
func TestSlicePrepend(t *testing.T) {
f := func(head string, tail []string) bool {
result := prepend(head, tail)
return len(result) == 1+len(tail) && result[0] == head && SliceEqual(result[1:], tail)
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func SliceEqual(slice1 []string, slice2 []string) bool {
if len(slice1) != len(slice2) {
return false
}
for i := range slice1 {
if slice1[i] != slice2[i] {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment