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