Skip to content

Commit

Permalink
WIP: add quick-test for FromString and fromString2
Browse files Browse the repository at this point in the history
Shows that the quick-test fails for FromString.
  • Loading branch information
smyrman committed Mar 4, 2022
1 parent ab39fa5 commit 7868550
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions id_test.go
Expand Up @@ -5,8 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
)

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 7868550

Please sign in to comment.