Skip to content

Commit

Permalink
Update fuzz function
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 21, 2024
1 parent 8774978 commit c10a94e
Showing 1 changed file with 55 additions and 97 deletions.
152 changes: 55 additions & 97 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package intervals_test

import (
"crypto/rand"
"encoding/binary"
"slices"
"testing"

Expand Down Expand Up @@ -128,187 +127,146 @@ func FuzzSymmetricDifference(f *testing.F) {
}

func fuzz(f *testing.F, ff func(t *testing.T, x, y Set[elems.Uint8])) {
data := make([]byte, 8*4)
data := make([]byte, 32)
args := make([]any, len(data))

for i := 0; i < 10; i++ {
if _, err := rand.Read(data); err != nil {
f.Fatal(err)
}

f.Add(
binary.LittleEndian.Uint64(data),
binary.LittleEndian.Uint64(data[8:]),
binary.LittleEndian.Uint64(data[16:]),
binary.LittleEndian.Uint64(data[24:]),
)
}

f.Fuzz(func(t *testing.T, x1, x2, y1, y2 uint64) {
xs, ys := make([]byte, 8*2), make([]byte, 8*2)

for i, u64 := range []uint64{x1, x2} {
binary.LittleEndian.PutUint64(xs[i*8:], u64)
for i, v := range data {
args[i] = v
}

for i, u64 := range []uint64{y1, y2} {
binary.LittleEndian.PutUint64(ys[i*8:], u64)
}
f.Add(args...)
}

f.Fuzz(func(
t *testing.T,
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15,
y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15 byte,
) {
xs := []byte{x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15}
ys := []byte{y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15}

slices.Sort(xs)
slices.Sort(ys)

var x, y Set[elems.Uint8]
var x, y []Interval[elems.Uint8]

for i, j := 0, len(xs); i < j; i += 2 {
if lo, hi := elems.Uint8(xs[i]), elems.Uint8(xs[i+1]); lo < hi {
x.AddRange(lo, hi)
x = append(x, Range(lo, hi))
}
}

for i, j := 0, len(ys); i < j; i += 2 {
if lo, hi := elems.Uint8(ys[i]), elems.Uint8(ys[i+1]); lo < hi {
y.AddRange(lo, hi)
y = append(y, Range(lo, hi))
}
}

ff(t, x, y)
ff(t, plainNormalize(x), plainNormalize(y))
})
}

func plainNormalize(s []Interval[elems.Uint8]) Set[elems.Uint8] {
return plainUnion(s)
}

func plainIsSubsetOf(x, y Set[elems.Uint8]) bool {
var universe [256]uint8
var universe [256]bool

for x, s := range []Set[elems.Uint8]{x, y} {
for j, s := range []Set[elems.Uint8]{x, y} {
for _, r := range s {
for i := r.Low; i < r.High; i++ {
universe[i] = uint8(x + 1)
universe[i] = j == 0
}
}
}

return !slices.Contains(universe[:], 1)
return !slices.Contains(universe[:], true)
}

func plainIntersection(x, y Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]uint8
func plainIntersection(sets ...Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]int

for _, s := range []Set[elems.Uint8]{x, y} {
for _, s := range sets {
for _, r := range s {
for i := r.Low; i < r.High; i++ {
universe[i]++
}
}
}

var z Set[elems.Uint8]

lo := -1

for i, v := range universe {
if v == 2 {
if lo < 0 {
lo = i
}
} else {
if lo >= 0 {
z = append(z, Range(elems.Uint8(lo), elems.Uint8(i)))
lo = -1
}
}
n := len(sets)
if n == 0 {
return nil
}

return z
return toSet(&universe, func(v int) bool { return v == n })
}

func plainUnion(x, y Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]uint8
func plainUnion(sets ...Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]bool

for _, s := range []Set[elems.Uint8]{x, y} {
for _, s := range sets {
for _, r := range s {
for i := r.Low; i < r.High; i++ {
universe[i]++
}
}
}

var z Set[elems.Uint8]

lo := -1

for i, v := range universe {
if v > 0 {
if lo < 0 {
lo = i
}
} else {
if lo >= 0 {
z = append(z, Range(elems.Uint8(lo), elems.Uint8(i)))
lo = -1
universe[i] = true
}
}
}

return z
return toSet(&universe, func(v bool) bool { return v })
}

func plainDifference(x, y Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]uint8
func plainDifference(sets ...Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]bool

for x, s := range []Set[elems.Uint8]{x, y} {
for j, s := range sets {
for _, r := range s {
for i := r.Low; i < r.High; i++ {
universe[i] = uint8(x + 1)
}
}
}

var z Set[elems.Uint8]

lo := -1

for i, v := range universe {
if v == 1 {
if lo < 0 {
lo = i
}
} else {
if lo >= 0 {
z = append(z, Range(elems.Uint8(lo), elems.Uint8(i)))
lo = -1
universe[i] = j == 0
}
}
}

return z
return toSet(&universe, func(v bool) bool { return v })
}

func plainSymmetricDifference(x, y Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]uint8
func plainSymmetricDifference(sets ...Set[elems.Uint8]) Set[elems.Uint8] {
var universe [256]bool

for _, s := range []Set[elems.Uint8]{x, y} {
for _, s := range sets {
for _, r := range s {
for i := r.Low; i < r.High; i++ {
universe[i]++
universe[i] = !universe[i]
}
}
}

var z Set[elems.Uint8]
return toSet(&universe, func(v bool) bool { return v })
}

func toSet[T any](universe *[256]T, predicate func(T) bool) Set[elems.Uint8] {
var x Set[elems.Uint8]

lo := -1

for i, v := range universe {
if v == 1 {
if predicate(v) {
if lo < 0 {
lo = i
}
} else {
if lo >= 0 {
z = append(z, Range(elems.Uint8(lo), elems.Uint8(i)))
x = append(x, Range(elems.Uint8(lo), elems.Uint8(i)))
lo = -1
}
}
}

return z
return x
}

0 comments on commit c10a94e

Please sign in to comment.