github.com/webmafia/fast@v0.10.0/binary/fuzz_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func FuzzBuffer(f *testing.F) {
     9  	f.Add("foobar", int64(123), 456.789)
    10  	f.Add("räksmörgås", int64(-123), -456.789)
    11  
    12  	f.Fuzz(func(t *testing.T, s string, i int64, f float64) {
    13  		w := NewBufferWriter(64)
    14  		w.WriteString(s)
    15  		w.WriteVarint(i)
    16  		w.WriteFloat64(f)
    17  
    18  		r := NewBufferReader(w.Bytes())
    19  
    20  		if res := r.ReadString(len(s)); res != s {
    21  			t.Errorf("expected '%s', got '%s'", s, res)
    22  		}
    23  
    24  		if res := r.ReadVarint(); res != i {
    25  			t.Errorf("expected '%d', got '%d'", i, res)
    26  		}
    27  
    28  		if res := r.ReadFloat64(); res != f {
    29  			t.Errorf("expected '%f', got '%f'", f, res)
    30  		}
    31  	})
    32  }
    33  
    34  func FuzzStreamReader(f *testing.F) {
    35  	f.Add("foobar", int64(123), 456.789)
    36  	f.Add("räksmörgås", int64(-123), -456.789)
    37  
    38  	f.Fuzz(func(t *testing.T, s string, i int64, f float64) {
    39  		w := NewBufferWriter(64)
    40  		w.WriteString(s)
    41  		w.WriteVarint(i)
    42  		w.WriteFloat64(f)
    43  
    44  		b := bytes.NewBuffer(w.Bytes())
    45  		r := NewStreamReader(b)
    46  
    47  		if res := r.ReadString(len(s)); res != s {
    48  			t.Errorf("expected '%s', got '%s'", s, res)
    49  		}
    50  
    51  		if res := r.ReadVarint(); res != i {
    52  			t.Errorf("expected '%d', got '%d'", i, res)
    53  		}
    54  
    55  		if res := r.ReadFloat64(); res != f {
    56  			t.Errorf("expected '%f', got '%f'", f, res)
    57  		}
    58  	})
    59  }