github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/buf/multi_buffer_test.go (about)

     1  package buf_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"io"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/xmplusdev/xmcore/common"
    12  	. "github.com/xmplusdev/xmcore/common/buf"
    13  )
    14  
    15  func TestMultiBufferRead(t *testing.T) {
    16  	b1 := New()
    17  	common.Must2(b1.WriteString("ab"))
    18  
    19  	b2 := New()
    20  	common.Must2(b2.WriteString("cd"))
    21  	mb := MultiBuffer{b1, b2}
    22  
    23  	bs := make([]byte, 32)
    24  	_, nBytes := SplitBytes(mb, bs)
    25  	if nBytes != 4 {
    26  		t.Error("expect 4 bytes split, but got ", nBytes)
    27  	}
    28  	if r := cmp.Diff(bs[:nBytes], []byte("abcd")); r != "" {
    29  		t.Error(r)
    30  	}
    31  }
    32  
    33  func TestMultiBufferAppend(t *testing.T) {
    34  	var mb MultiBuffer
    35  	b := New()
    36  	common.Must2(b.WriteString("ab"))
    37  	mb = append(mb, b)
    38  	if mb.Len() != 2 {
    39  		t.Error("expected length 2, but got ", mb.Len())
    40  	}
    41  }
    42  
    43  func TestMultiBufferSliceBySizeLarge(t *testing.T) {
    44  	lb := make([]byte, 8*1024)
    45  	common.Must2(io.ReadFull(rand.Reader, lb))
    46  
    47  	mb := MergeBytes(nil, lb)
    48  
    49  	mb, mb2 := SplitSize(mb, 1024)
    50  	if mb2.Len() != 1024 {
    51  		t.Error("expect length 1024, but got ", mb2.Len())
    52  	}
    53  	if mb.Len() != 7*1024 {
    54  		t.Error("expect length 7*1024, but got ", mb.Len())
    55  	}
    56  
    57  	mb, mb3 := SplitSize(mb, 7*1024)
    58  	if mb3.Len() != 7*1024 {
    59  		t.Error("expect length 7*1024, but got", mb.Len())
    60  	}
    61  
    62  	if !mb.IsEmpty() {
    63  		t.Error("expect empty buffer, but got ", mb.Len())
    64  	}
    65  }
    66  
    67  func TestMultiBufferSplitFirst(t *testing.T) {
    68  	b1 := New()
    69  	b1.WriteString("b1")
    70  
    71  	b2 := New()
    72  	b2.WriteString("b2")
    73  
    74  	b3 := New()
    75  	b3.WriteString("b3")
    76  
    77  	var mb MultiBuffer
    78  	mb = append(mb, b1, b2, b3)
    79  
    80  	mb, c1 := SplitFirst(mb)
    81  	if diff := cmp.Diff(b1.String(), c1.String()); diff != "" {
    82  		t.Error(diff)
    83  	}
    84  
    85  	mb, c2 := SplitFirst(mb)
    86  	if diff := cmp.Diff(b2.String(), c2.String()); diff != "" {
    87  		t.Error(diff)
    88  	}
    89  
    90  	mb, c3 := SplitFirst(mb)
    91  	if diff := cmp.Diff(b3.String(), c3.String()); diff != "" {
    92  		t.Error(diff)
    93  	}
    94  
    95  	if !mb.IsEmpty() {
    96  		t.Error("expect empty buffer, but got ", mb.String())
    97  	}
    98  }
    99  
   100  func TestMultiBufferReadAllToByte(t *testing.T) {
   101  	{
   102  		lb := make([]byte, 8*1024)
   103  		common.Must2(io.ReadFull(rand.Reader, lb))
   104  		rd := bytes.NewBuffer(lb)
   105  		b, err := ReadAllToBytes(rd)
   106  		common.Must(err)
   107  
   108  		if l := len(b); l != 8*1024 {
   109  			t.Error("unexpceted length from ReadAllToBytes", l)
   110  		}
   111  	}
   112  	{
   113  		const dat = "data/test_MultiBufferReadAllToByte.dat"
   114  		f, err := os.Open(dat)
   115  		common.Must(err)
   116  
   117  		buf2, err := ReadAllToBytes(f)
   118  		common.Must(err)
   119  		f.Close()
   120  
   121  		cnt, err := os.ReadFile(dat)
   122  		common.Must(err)
   123  
   124  		if d := cmp.Diff(buf2, cnt); d != "" {
   125  			t.Error("fail to read from file: ", d)
   126  		}
   127  	}
   128  }
   129  
   130  func TestMultiBufferCopy(t *testing.T) {
   131  	lb := make([]byte, 8*1024)
   132  	common.Must2(io.ReadFull(rand.Reader, lb))
   133  	reader := bytes.NewBuffer(lb)
   134  
   135  	mb, err := ReadFrom(reader)
   136  	common.Must(err)
   137  
   138  	lbdst := make([]byte, 8*1024)
   139  	mb.Copy(lbdst)
   140  
   141  	if d := cmp.Diff(lb, lbdst); d != "" {
   142  		t.Error("unexpceted different from MultiBufferCopy ", d)
   143  	}
   144  }
   145  
   146  func TestSplitFirstBytes(t *testing.T) {
   147  	a := New()
   148  	common.Must2(a.WriteString("ab"))
   149  	b := New()
   150  	common.Must2(b.WriteString("bc"))
   151  
   152  	mb := MultiBuffer{a, b}
   153  
   154  	o := make([]byte, 2)
   155  	_, cnt := SplitFirstBytes(mb, o)
   156  	if cnt != 2 {
   157  		t.Error("unexpected cnt from SplitFirstBytes ", cnt)
   158  	}
   159  	if d := cmp.Diff(string(o), "ab"); d != "" {
   160  		t.Error("unexpected splited result from SplitFirstBytes ", d)
   161  	}
   162  }
   163  
   164  func TestCompact(t *testing.T) {
   165  	a := New()
   166  	common.Must2(a.WriteString("ab"))
   167  	b := New()
   168  	common.Must2(b.WriteString("bc"))
   169  
   170  	mb := MultiBuffer{a, b}
   171  	cmb := Compact(mb)
   172  
   173  	if w := cmb.String(); w != "abbc" {
   174  		t.Error("unexpected Compact result ", w)
   175  	}
   176  }
   177  
   178  func BenchmarkSplitBytes(b *testing.B) {
   179  	var mb MultiBuffer
   180  	raw := make([]byte, Size)
   181  
   182  	b.ResetTimer()
   183  	for i := 0; i < b.N; i++ {
   184  		buffer := StackNew()
   185  		buffer.Extend(Size)
   186  		mb = append(mb, &buffer)
   187  		mb, _ = SplitBytes(mb, raw)
   188  	}
   189  }