gcs/gcs_test: adds generic tests for all matching strats

This commit is contained in:
Conner Fromknecht 2018-07-19 20:53:44 -07:00 committed by Olaoluwa Osuntokun
parent 36301f211d
commit bf1e1be935

View file

@ -217,36 +217,71 @@ func TestGCSFilterMatch(t *testing.T) {
} }
} }
// TestGCSFilterMatchAny checks that both the built and copied filters match a // AnyMatcher is the function signature of our matching algorithms.
// list correctly, logging any false positives without failing on them. type AnyMatcher func(key [gcs.KeySize]byte, data [][]byte) (bool, error)
func TestGCSFilterMatchAny(t *testing.T) {
match, err := filter.MatchAny(key, contents2) // TestGCSFilterMatchAnySuite checks that all of our matching algorithms
if err != nil { // properly match a list correctly when using built or copied filters, logging
t.Fatalf("Filter match any failed: %s", err.Error()) // any false positives without failing on them.
func TestGCSFilterMatchAnySuite(t *testing.T) {
funcs := []struct {
name string
matchAny func(*gcs.Filter) AnyMatcher
}{
{
"default",
func(f *gcs.Filter) AnyMatcher {
return f.MatchAny
},
},
{
"hash",
func(f *gcs.Filter) AnyMatcher {
return f.HashMatchAny
},
},
{
"zip",
func(f *gcs.Filter) AnyMatcher {
return f.ZipMatchAny
},
},
} }
if match {
t.Logf("False positive match, should be 1 in 2**%d!", P) for _, test := range funcs {
} t.Run(test.name, func(t *testing.T) {
match, err = filter2.MatchAny(key, contents2) contentsCopy := make([][]byte, len(contents2))
if err != nil { copy(contentsCopy, contents2)
t.Fatalf("Filter match any failed: %s", err.Error())
} match, err := test.matchAny(filter)(key, contentsCopy)
if match { if err != nil {
t.Logf("False positive match, should be 1 in 2**%d!", P) t.Fatalf("Filter match any failed: %s", err.Error())
} }
contents2 = append(contents2, []byte("Nate")) if match {
match, err = filter.MatchAny(key, contents2) t.Logf("False positive match, should be 1 in 2**%d!", P)
if err != nil { }
t.Fatalf("Filter match any failed: %s", err.Error()) match, err = test.matchAny(filter2)(key, contentsCopy)
} if err != nil {
if !match { t.Fatalf("Filter match any failed: %s", err.Error())
t.Fatal("Filter didn't match any when it should have!") }
} if match {
match, err = filter2.MatchAny(key, contents2) t.Logf("False positive match, should be 1 in 2**%d!", P)
if err != nil { }
t.Fatalf("Filter match any failed: %s", err.Error()) contentsCopy = append(contentsCopy, []byte("Nate"))
} match, err = test.matchAny(filter)(key, contentsCopy)
if !match { if err != nil {
t.Fatal("Filter didn't match any when it should have!") t.Fatalf("Filter match any failed: %s", err.Error())
}
if !match {
t.Fatal("Filter didn't match any when it should have!")
}
match, err = test.matchAny(filter2)(key, contentsCopy)
if err != nil {
t.Fatalf("Filter match any failed: %s", err.Error())
}
if !match {
t.Fatal("Filter didn't match any when it should have!")
}
})
} }
} }