github.com/scottcagno/storage@v1.8.0/pkg/bw/v3/writer_test.go (about)

     1  package v3
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  var options = &Options{
    10  	pageSize:  64,
    11  	pageAlign: true,
    12  	autoFlush: false,
    13  }
    14  
    15  var dataToWrite1 = []byte("this is a test; it seems to work great!")
    16  var dataToWrite2 = []byte("this is a another test--it should overflow the first page. we'll see if it also works great!")
    17  
    18  func TestDataWriter_Write(t *testing.T) {
    19  
    20  	var bb bytes.Buffer
    21  	var nn int
    22  	dw := NewDataWriter(&bb, options)
    23  	n, err := dw.Write(dataToWrite2)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  	nn += n
    28  	err = dw.Flush()
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  	fmt.Printf("nn=%d, bb=%q\n", nn, bb.Bytes())
    33  }
    34  
    35  var result interface{}
    36  
    37  func BenchmarkDataWriter_Write(b *testing.B) {
    38  
    39  	var bb bytes.Buffer
    40  	w := NewDataWriter(&bb, options)
    41  	var x int
    42  	var err error
    43  
    44  	b.ReportAllocs()
    45  	b.ResetTimer()
    46  	for n := 0; n < b.N; n++ {
    47  		x, err = w.Write(dataToWrite2)
    48  		if err != nil {
    49  			b.Error(err)
    50  		}
    51  		err = w.Flush()
    52  		if err != nil {
    53  			b.Error(err)
    54  		}
    55  		if x != 128 {
    56  			b.Error("uhh, should have been 128")
    57  		}
    58  	}
    59  	// always store the result to a package level variable
    60  	// so the compiler cannot eliminate the Benchmark itself.
    61  	result = x
    62  }