From 7868550d9c0c8c7ee32fcb55b9967b7f99316a7e Mon Sep 17 00:00:00 2001 From: Sindre Myren Date: Fri, 4 Mar 2022 10:00:06 +0100 Subject: [PATCH] WIP: add quick-test for FromString and fromString2 Shows that the quick-test fails for FromString. --- id_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/id_test.go b/id_test.go index cb9f943..dee694d 100644 --- a/id_test.go +++ b/id_test.go @@ -5,8 +5,10 @@ import ( "encoding/json" "errors" "fmt" + "math/rand" "reflect" "testing" + "testing/quick" "time" ) @@ -279,6 +281,87 @@ func BenchmarkFromString2(b *testing.B) { }) } +func TestFromStringQuick(t *testing.T) { + f := func(id1 ID, c byte) bool { + s1 := id1.String() + for i := range s1 { + s2 := []byte(s1) + s2[i] = c + id2, err := FromString(string(s2)) + if id1 == id2 && err == nil && c != s1[i] { + t.Logf("comparing XIDs:\na: %q\nb: %q (index %d changed to %c)", s1, s2, i, c) + return false + } + } + return true + } + err := quick.Check(f, &quick.Config{ + Values: func(args []reflect.Value, r *rand.Rand) { + i := r.Intn(len(encoding)) + args[0] = reflect.ValueOf(New()) + args[1] = reflect.ValueOf(byte(encoding[i])) + }, + MaxCount: 1000, + }) + if err != nil { + t.Error(err) + } +} + +func TestFromString2Quick(t *testing.T) { + f := func(id1 ID, c byte) bool { + s1 := id1.String() + for i := range s1 { + s2 := []byte(s1) + s2[i] = c + id2, err := fromString2(string(s2)) + if id1 == id2 && err == nil && c != s1[i] { + t.Logf("comparing XIDs:\na: %q\nb: %q (index %d changed to %c)", s1, s2, i, c) + return false + } + } + return true + } + err := quick.Check(f, &quick.Config{ + Values: func(args []reflect.Value, r *rand.Rand) { + i := r.Intn(len(encoding)) + args[0] = reflect.ValueOf(New()) + args[1] = reflect.ValueOf(byte(encoding[i])) + }, + MaxCount: 1000, + }) + if err != nil { + t.Error(err) + } +} + +func TestFromString2QuickInvalidChars(t *testing.T) { + f := func(id1 ID, c byte) bool { + s1 := id1.String() + for i := range s1 { + s2 := []byte(s1) + s2[i] = c + id2, err := fromString2(string(s2)) + if id1 == id2 && err == nil && c != s1[i] { + t.Logf("comparing XIDs:\na: %q\nb: %q (index %d changed to %c)", s1, s2, i, c) + return false + } + } + return true + } + err := quick.Check(f, &quick.Config{ + Values: func(args []reflect.Value, r *rand.Rand) { + i := r.Intn(0xFF) + args[0] = reflect.ValueOf(New()) + args[1] = reflect.ValueOf(byte(i)) + }, + MaxCount: 2000, + }) + if err != nil { + t.Error(err) + } +} + // func BenchmarkUUIDv1(b *testing.B) { // b.RunParallel(func(pb *testing.PB) { // for pb.Next() {