Last active
October 5, 2018 10:51
-
-
Save fbiville/f2a2ecfcd216acb3fb42ab59b89be912 to your computer and use it in GitHub Desktop.
Golang property-based testing example
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 ( | |
| "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