github.com/scottcagno/storage@v1.8.0/pkg/bw/v2/w_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"testing"
     8  )
     9  
    10  func TestWriter_Write(t *testing.T) {
    11  
    12  	var bb bytes.Buffer
    13  	w := NewWriter(&bb, 64)
    14  
    15  	n, err := w.Write([]byte("this is a test, this is only a test."))
    16  	if err != nil {
    17  		t.Error(err)
    18  	}
    19  	fmt.Printf("off=%d, data=%q (len=%d)\n", n, bb.Bytes(), len(bb.Bytes()))
    20  
    21  	n, err = w.Write([]byte("and this is another test."))
    22  	if err != nil {
    23  		t.Error(err)
    24  	}
    25  	fmt.Printf("off=%d, data=%q (len=%d)\n", n, bb.Bytes(), len(bb.Bytes()))
    26  }
    27  
    28  func TestBufferedWriter(t *testing.T) {
    29  
    30  	var bb bytes.Buffer
    31  
    32  	bw := bufio.NewWriterSize(&bb, 36)
    33  
    34  	n, err := bw.Write([]byte("this is a test, this is only a test."))
    35  	if err != nil {
    36  		t.Error(err)
    37  	}
    38  
    39  	//err = bw.Flush()
    40  	//if err != nil {
    41  	//	t.Error(err)
    42  	//}
    43  
    44  	n, err = bw.Write([]byte("and this is another test."))
    45  	if err != nil {
    46  		t.Error(err)
    47  	}
    48  	fmt.Printf("off=%d, data=%q (len=%d)\n", n, bb.Bytes(), len(bb.Bytes()))
    49  	fmt.Printf("off=%d, data=%q (len=%d)\n", n, bb.Bytes(), len(bb.Bytes()))
    50  
    51  }