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

     1  package buf_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/xmplusdev/xmcore/common"
    10  	. "github.com/xmplusdev/xmcore/common/buf"
    11  )
    12  
    13  func TestBufferClear(t *testing.T) {
    14  	buffer := New()
    15  	defer buffer.Release()
    16  
    17  	payload := "Bytes"
    18  	buffer.Write([]byte(payload))
    19  	if diff := cmp.Diff(buffer.Bytes(), []byte(payload)); diff != "" {
    20  		t.Error(diff)
    21  	}
    22  
    23  	buffer.Clear()
    24  	if buffer.Len() != 0 {
    25  		t.Error("expect 0 length, but got ", buffer.Len())
    26  	}
    27  }
    28  
    29  func TestBufferIsEmpty(t *testing.T) {
    30  	buffer := New()
    31  	defer buffer.Release()
    32  
    33  	if buffer.IsEmpty() != true {
    34  		t.Error("expect empty buffer, but not")
    35  	}
    36  }
    37  
    38  func TestBufferString(t *testing.T) {
    39  	buffer := New()
    40  	defer buffer.Release()
    41  
    42  	const payload = "Test String"
    43  	common.Must2(buffer.WriteString(payload))
    44  	if buffer.String() != payload {
    45  		t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
    46  	}
    47  }
    48  
    49  func TestBufferByte(t *testing.T) {
    50  	{
    51  		buffer := New()
    52  		common.Must(buffer.WriteByte('m'))
    53  		if buffer.String() != "m" {
    54  			t.Error("expect buffer content as ", "m", " but actually ", buffer.String())
    55  		}
    56  		buffer.Release()
    57  	}
    58  	{
    59  		buffer := StackNew()
    60  		common.Must(buffer.WriteByte('n'))
    61  		if buffer.String() != "n" {
    62  			t.Error("expect buffer content as ", "n", " but actually ", buffer.String())
    63  		}
    64  		buffer.Release()
    65  	}
    66  	{
    67  		buffer := StackNew()
    68  		common.Must2(buffer.WriteString("HELLOWORLD"))
    69  		if b := buffer.Byte(5); b != 'W' {
    70  			t.Error("unexpected byte ", b)
    71  		}
    72  
    73  		buffer.SetByte(5, 'M')
    74  		if buffer.String() != "HELLOMORLD" {
    75  			t.Error("expect buffer content as ", "n", " but actually ", buffer.String())
    76  		}
    77  		buffer.Release()
    78  	}
    79  }
    80  
    81  func TestBufferResize(t *testing.T) {
    82  	buffer := New()
    83  	defer buffer.Release()
    84  
    85  	const payload = "Test String"
    86  	common.Must2(buffer.WriteString(payload))
    87  	if buffer.String() != payload {
    88  		t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
    89  	}
    90  
    91  	buffer.Resize(-6, -3)
    92  	if l := buffer.Len(); int(l) != 3 {
    93  		t.Error("len error ", l)
    94  	}
    95  
    96  	if s := buffer.String(); s != "Str" {
    97  		t.Error("unexpect buffer ", s)
    98  	}
    99  
   100  	buffer.Resize(int32(len(payload)), 200)
   101  	if l := buffer.Len(); int(l) != 200-len(payload) {
   102  		t.Error("len error ", l)
   103  	}
   104  }
   105  
   106  func TestBufferSlice(t *testing.T) {
   107  	{
   108  		b := New()
   109  		common.Must2(b.Write([]byte("abcd")))
   110  		bytes := b.BytesFrom(-2)
   111  		if diff := cmp.Diff(bytes, []byte{'c', 'd'}); diff != "" {
   112  			t.Error(diff)
   113  		}
   114  	}
   115  
   116  	{
   117  		b := New()
   118  		common.Must2(b.Write([]byte("abcd")))
   119  		bytes := b.BytesTo(-2)
   120  		if diff := cmp.Diff(bytes, []byte{'a', 'b'}); diff != "" {
   121  			t.Error(diff)
   122  		}
   123  	}
   124  
   125  	{
   126  		b := New()
   127  		common.Must2(b.Write([]byte("abcd")))
   128  		bytes := b.BytesRange(-3, -1)
   129  		if diff := cmp.Diff(bytes, []byte{'b', 'c'}); diff != "" {
   130  			t.Error(diff)
   131  		}
   132  	}
   133  }
   134  
   135  func TestBufferReadFullFrom(t *testing.T) {
   136  	payload := make([]byte, 1024)
   137  	common.Must2(rand.Read(payload))
   138  
   139  	reader := bytes.NewReader(payload)
   140  	b := New()
   141  	n, err := b.ReadFullFrom(reader, 1024)
   142  	common.Must(err)
   143  	if n != 1024 {
   144  		t.Error("expect reading 1024 bytes, but actually ", n)
   145  	}
   146  
   147  	if diff := cmp.Diff(payload, b.Bytes()); diff != "" {
   148  		t.Error(diff)
   149  	}
   150  }
   151  
   152  func BenchmarkNewBuffer(b *testing.B) {
   153  	for i := 0; i < b.N; i++ {
   154  		buffer := New()
   155  		buffer.Release()
   156  	}
   157  }
   158  
   159  func BenchmarkNewBufferStack(b *testing.B) {
   160  	for i := 0; i < b.N; i++ {
   161  		buffer := StackNew()
   162  		buffer.Release()
   163  	}
   164  }
   165  
   166  func BenchmarkWrite2(b *testing.B) {
   167  	buffer := New()
   168  
   169  	b.ResetTimer()
   170  	for i := 0; i < b.N; i++ {
   171  		_, _ = buffer.Write([]byte{'a', 'b'})
   172  		buffer.Clear()
   173  	}
   174  }
   175  
   176  func BenchmarkWrite8(b *testing.B) {
   177  	buffer := New()
   178  
   179  	b.ResetTimer()
   180  	for i := 0; i < b.N; i++ {
   181  		_, _ = buffer.Write([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})
   182  		buffer.Clear()
   183  	}
   184  }
   185  
   186  func BenchmarkWrite32(b *testing.B) {
   187  	buffer := New()
   188  	payload := make([]byte, 32)
   189  	rand.Read(payload)
   190  
   191  	b.ResetTimer()
   192  	for i := 0; i < b.N; i++ {
   193  		_, _ = buffer.Write(payload)
   194  		buffer.Clear()
   195  	}
   196  }
   197  
   198  func BenchmarkWriteByte2(b *testing.B) {
   199  	buffer := New()
   200  
   201  	b.ResetTimer()
   202  	for i := 0; i < b.N; i++ {
   203  		_ = buffer.WriteByte('a')
   204  		_ = buffer.WriteByte('b')
   205  		buffer.Clear()
   206  	}
   207  }
   208  
   209  func BenchmarkWriteByte8(b *testing.B) {
   210  	buffer := New()
   211  
   212  	b.ResetTimer()
   213  	for i := 0; i < b.N; i++ {
   214  		_ = buffer.WriteByte('a')
   215  		_ = buffer.WriteByte('b')
   216  		_ = buffer.WriteByte('c')
   217  		_ = buffer.WriteByte('d')
   218  		_ = buffer.WriteByte('e')
   219  		_ = buffer.WriteByte('f')
   220  		_ = buffer.WriteByte('g')
   221  		_ = buffer.WriteByte('h')
   222  		buffer.Clear()
   223  	}
   224  }