github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/net/ringbuffer/ring_buffer_test.go (about)

     1  package ringbuffer
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestRingBuffer_Write(t *testing.T) {
    10  	rb := New(64)
    11  
    12  	if _, err := rb.ReadByte(); err == nil {
    13  		t.Fatal("expect empty err, but got nil")
    14  	}
    15  	// check empty or full
    16  	if !rb.IsEmpty() {
    17  		t.Fatalf("expect IsEmpty is true but got false")
    18  	}
    19  	if rb.IsFull() {
    20  		t.Fatalf("expect IsFull is false but got true")
    21  	}
    22  	if rb.Length() != 0 {
    23  		t.Fatalf("expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
    24  	}
    25  	if rb.Free() != 64 {
    26  		t.Fatalf("expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
    27  	}
    28  
    29  	// write 4 * 4 = 16 bytes
    30  	n, _ := rb.Write([]byte(strings.Repeat("abcd", 4)))
    31  	if n != 16 {
    32  		t.Fatalf("expect write 16 bytes but got %d", n)
    33  	}
    34  	if rb.Length() != 16 {
    35  		t.Fatalf("expect len 16 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
    36  	}
    37  	if rb.Free() != 48 {
    38  		t.Fatalf("expect free 48 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
    39  	}
    40  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte(strings.Repeat("abcd", 4))) {
    41  		t.Fatalf("expect 4 abcd but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
    42  	}
    43  
    44  	// check empty or full
    45  	if rb.IsEmpty() {
    46  		t.Fatalf("expect IsEmpty is false but got true")
    47  	}
    48  	if rb.IsFull() {
    49  		t.Fatalf("expect IsFull is false but got true")
    50  	}
    51  
    52  	// write 48 bytes, should full
    53  	n, _ = rb.Write([]byte(strings.Repeat("abcd", 12)))
    54  	if n != 48 {
    55  		t.Fatalf("expect write 48 bytes but got %d", n)
    56  	}
    57  	if rb.Length() != 64 {
    58  		t.Fatalf("expect len 64 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
    59  	}
    60  	if rb.Free() != 0 {
    61  		t.Fatalf("expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
    62  	}
    63  	if rb.w != 0 {
    64  		t.Fatalf("expect r.w=0 but got %d. r.r=%d", rb.w, rb.r)
    65  	}
    66  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte(strings.Repeat("abcd", 16))) {
    67  		t.Fatalf("expect 16 abcd but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
    68  	}
    69  
    70  	// check empty or full
    71  	if rb.IsEmpty() {
    72  		t.Fatalf("expect IsEmpty is false but got true")
    73  	}
    74  	if !rb.IsFull() {
    75  		t.Fatalf("expect IsFull is true but got false")
    76  	}
    77  
    78  	// write more 4 bytes, should scale from 64 to 128 bytes.
    79  	n, _ = rb.Write([]byte(strings.Repeat("abcd", 1)))
    80  	size := rb.Cap()
    81  	if n != 4 {
    82  		t.Fatalf("expect write 4 bytes but got %d", n)
    83  	}
    84  	if rb.Length() != 68 {
    85  		t.Fatalf("expect len 68 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
    86  	}
    87  	if rb.Free() != size-68 {
    88  		t.Fatalf("expect free 60 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
    89  	}
    90  
    91  	// check empty or full
    92  	if rb.IsEmpty() {
    93  		t.Fatalf("expect IsEmpty is false but got true")
    94  	}
    95  	if rb.IsFull() {
    96  		t.Fatalf("expect IsFull is false but got true")
    97  	}
    98  
    99  	// reset this ringbuffer and set a long slice
   100  	rb.Reset()
   101  	n, _ = rb.Write([]byte(strings.Repeat("abcd", 20)))
   102  	if n != 80 {
   103  		t.Fatalf("expect write 80 bytes but got %d", n)
   104  	}
   105  	if rb.Length() != 80 {
   106  		t.Fatalf("expect len 80 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   107  	}
   108  	if rb.Free() != size-80 {
   109  		t.Fatalf("expect free 48 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   110  	}
   111  	if rb.w == 0 {
   112  		t.Fatalf("expect r.w!=0 but got %d. r.r=%d", rb.w, rb.r)
   113  	}
   114  
   115  	// check empty or full
   116  	if rb.IsEmpty() {
   117  		t.Fatalf("expect IsEmpty is false but got true")
   118  	}
   119  	if rb.IsFull() {
   120  		t.Fatalf("expect IsFull is false but got true")
   121  	}
   122  
   123  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte(strings.Repeat("abcd", 20))) {
   124  		t.Fatalf("expect 16 abcd but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   125  	}
   126  
   127  	rb.Reset()
   128  	// write 4 * 2 = 8 bytes
   129  	n, _ = rb.Write([]byte(strings.Repeat("abcd", 2)))
   130  	if n != 8 {
   131  		t.Fatalf("expect write 16 bytes but got %d", n)
   132  	}
   133  	if rb.Length() != 8 {
   134  		t.Fatalf("expect len 16 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   135  	}
   136  	if rb.Free() != size-8 {
   137  		t.Fatalf("expect free 48 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   138  	}
   139  	buf := make([]byte, 5)
   140  	_, _ = rb.Read(buf)
   141  	if rb.Length() != 3 {
   142  		t.Fatalf("expect len 3 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   143  	}
   144  	_, _ = rb.Write([]byte(strings.Repeat("abcd", 15)))
   145  
   146  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte("bcd"+strings.Repeat("abcd", 15))) {
   147  		t.Fatalf("expect 63 ... but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   148  	}
   149  
   150  	rb.Reset()
   151  	n, _ = rb.Write([]byte(strings.Repeat("abcd", 32)))
   152  	if n != 128 {
   153  		t.Fatalf("expect write 128 bytes but got %d", n)
   154  	}
   155  	if rb.Free() != 0 {
   156  		t.Fatalf("expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   157  	}
   158  	buf = make([]byte, 16)
   159  	_, _ = rb.Read(buf)
   160  	n, _ = rb.Write([]byte(strings.Repeat("1234", 4)))
   161  	if n != 16 {
   162  		t.Fatalf("expect write 16 bytes but got %d", n)
   163  	}
   164  	if rb.Free() != 0 {
   165  		t.Fatalf("expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   166  	}
   167  	if !bytes.Equal(append(buf, rb.ByteBuffer().Bytes()...),
   168  		[]byte(strings.Repeat("abcd", 32)+strings.Repeat("1234", 4))) {
   169  		t.Fatalf("expect 16 abcd and 4 1234 but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   170  	}
   171  }
   172  
   173  func TestZeroRingBuffer(t *testing.T) {
   174  	rb := New(0)
   175  	head, tail := rb.LazyRead(1)
   176  	if !(head == nil && tail == nil) {
   177  		t.Fatal("expect head and tail are all nil")
   178  	}
   179  	head, tail = rb.LazyReadAll()
   180  	if !(head == nil && tail == nil) {
   181  		t.Fatal("expect head and tail are all nil")
   182  	}
   183  	if rb.Length() != 0 {
   184  		t.Fatal("expect length is 0")
   185  	}
   186  	if rb.Free() != 0 {
   187  		t.Fatal("expect free is 0")
   188  	}
   189  	buf := []byte(strings.Repeat("1234", 12))
   190  	_, _ = rb.Write(buf)
   191  	if !(rb.Len() == initSize && rb.Cap() == initSize) {
   192  		t.Fatalf("expect rb.Len()=64 and rb.Cap=64, but got rb.Len()=%d and rb.Cap()=%d", rb.Len(), rb.Cap())
   193  	}
   194  	if !(rb.r == 0 && rb.w == 48 && rb.size == initSize && rb.mask == initSize-1) {
   195  		t.Fatalf("expect rb.r=0, rb.w=48, rb.size=64, rb.mask=63, but got rb.r=%d, rb.w=%d, rb.size=%d, rb.mask=%d",
   196  			rb.r, rb.w, rb.size, rb.mask)
   197  	}
   198  	if !bytes.Equal(rb.ByteBuffer().Bytes(), buf) {
   199  		t.Fatal("expect it is equal")
   200  	}
   201  	rb.Shift(48)
   202  	if !(rb.IsEmpty() && rb.r == 0 && rb.w == 0) {
   203  		t.Fatalf("expect rb is empty and rb.r=rb.w=0, but got rb.r=%d and rb.w=%d", rb.r, rb.w)
   204  	}
   205  }
   206  
   207  func TestRingBuffer_Read(t *testing.T) {
   208  	rb := New(64)
   209  
   210  	// check empty or full
   211  	if !rb.IsEmpty() {
   212  		t.Fatalf("expect IsEmpty is true but got false")
   213  	}
   214  	if rb.IsFull() {
   215  		t.Fatalf("expect IsFull is false but got true")
   216  	}
   217  	if rb.Length() != 0 {
   218  		t.Fatalf("expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   219  	}
   220  	if rb.Free() != 64 {
   221  		t.Fatalf("expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   222  	}
   223  
   224  	// read empty
   225  	buf := make([]byte, 1024)
   226  	n, err := rb.Read(buf)
   227  	if err == nil {
   228  		t.Fatalf("expect an error but got nil")
   229  	}
   230  	if err != ErrIsEmpty {
   231  		t.Fatalf("expect ErrIsEmpty but got nil")
   232  	}
   233  	if n != 0 {
   234  		t.Fatalf("expect read 0 bytes but got %d", n)
   235  	}
   236  	if rb.Length() != 0 {
   237  		t.Fatalf("expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   238  	}
   239  	if rb.Free() != 64 {
   240  		t.Fatalf("expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   241  	}
   242  	if rb.r != 0 {
   243  		t.Fatalf("expect r.r=0 but got %d. r.w=%d", rb.r, rb.w)
   244  	}
   245  
   246  	// write 16 bytes to read
   247  	_, _ = rb.Write([]byte(strings.Repeat("abcd", 4)))
   248  	n, err = rb.Read(buf)
   249  	if err != nil {
   250  		t.Fatalf("read failed: %v", err)
   251  	}
   252  	if n != 16 {
   253  		t.Fatalf("expect read 16 bytes but got %d", n)
   254  	}
   255  	if rb.Length() != 0 {
   256  		t.Fatalf("expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   257  	}
   258  	if rb.Free() != 64 {
   259  		t.Fatalf("expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   260  	}
   261  	if rb.r != 16 {
   262  		t.Fatalf("expect r.r=16 but got %d. r.w=%d", rb.r, rb.w)
   263  	}
   264  
   265  	// write long slice to read, it will scale from 64 to 128 bytes.
   266  	_, _ = rb.Write([]byte(strings.Repeat("abcd", 20)))
   267  	n, err = rb.Read(buf)
   268  	if err != nil {
   269  		t.Fatalf("read failed: %v", err)
   270  	}
   271  	if n != 80 {
   272  		t.Fatalf("expect read 80 bytes but got %d", n)
   273  	}
   274  	if rb.Length() != 0 {
   275  		t.Fatalf("expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   276  	}
   277  	if rb.Free() != 128 {
   278  		t.Fatalf("expect free 128 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   279  	}
   280  	if rb.r != 80 {
   281  		t.Fatalf("expect r.r=80 but got %d. r.w=%d", rb.r, rb.w)
   282  	}
   283  
   284  	rb.Reset()
   285  	_, _ = rb.Write([]byte(strings.Repeat("1234", 32)))
   286  	if !rb.IsFull() {
   287  		t.Fatal("ring buffer should be full")
   288  	}
   289  	if rb.Free() != 0 {
   290  		t.Fatalf("expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   291  	}
   292  	if rb.w != 0 {
   293  		t.Fatalf("expect r.2=0 but got %d. r.r=%d", rb.w, rb.r)
   294  	}
   295  	head, tail := rb.LazyRead(64)
   296  	if !(len(head) == 64 && tail == nil) {
   297  		t.Fatalf("expect len(head)=64 and tail=nil, yet len(head)=%d and tail != nil", len(head))
   298  	}
   299  	if rb.r != 0 {
   300  		t.Fatalf("expect r.r=0 but got %d", rb.r)
   301  	}
   302  	if !bytes.Equal(head, []byte(strings.Repeat("1234", 16))) {
   303  		t.Fatal("should be equal")
   304  	}
   305  	rb.Shift(64)
   306  	if rb.r != 64 {
   307  		t.Fatalf("expect r.r=64 but got %d", rb.r)
   308  	}
   309  	_, _ = rb.Write([]byte(strings.Repeat("1234", 4)))
   310  	if rb.w != 16 {
   311  		t.Fatalf("expect r.w=16 but got %d", rb.w)
   312  	}
   313  	head, tail = rb.LazyRead(128)
   314  	if !(len(head) == 64 && len(tail) == 16) {
   315  		t.Fatalf("expect len(head)=64 and len(tail)=16, yet len(head)=%d and len(tail)=%d", len(head), len(tail))
   316  	}
   317  	if !(bytes.Equal(head, []byte(strings.Repeat("1234", 16))) && bytes.Equal(tail, []byte(strings.Repeat("1234", 4)))) {
   318  		t.Fatalf("head: %s, tail: %s", string(head), string(tail))
   319  	}
   320  
   321  	head, tail = rb.LazyReadAll()
   322  	if !(len(head) == 64 && len(tail) == 16) {
   323  		t.Fatalf("expect len(head)=64 and len(tail)=16, yet len(head)=%d and len(tail)=%d", len(head), len(tail))
   324  	}
   325  	if !(bytes.Equal(head, []byte(strings.Repeat("1234", 16))) && bytes.Equal(tail, []byte(strings.Repeat("1234", 4)))) {
   326  		t.Fatalf("head: %s, tail: %s", string(head), string(tail))
   327  	}
   328  
   329  	rb.Shift(64)
   330  	rb.Shift(16)
   331  	if !rb.isEmpty {
   332  		t.Fatal("should be empty")
   333  	}
   334  }
   335  
   336  func TestRingBuffer_ByteInterface(t *testing.T) {
   337  	rb := New(2)
   338  
   339  	// write one
   340  	_ = rb.WriteByte('a')
   341  	if rb.Length() != 1 {
   342  		t.Fatalf("expect len 1 byte but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   343  	}
   344  	if rb.Free() != 1 {
   345  		t.Fatalf("expect free 1 byte but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   346  	}
   347  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte{'a'}) {
   348  		t.Fatalf("expect a but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   349  	}
   350  	// check empty or full
   351  	if rb.IsEmpty() {
   352  		t.Fatalf("expect IsEmpty is false but got true")
   353  	}
   354  	if rb.IsFull() {
   355  		t.Fatalf("expect IsFull is false but got true")
   356  	}
   357  
   358  	// write to, isFull
   359  	_ = rb.WriteByte('b')
   360  	if rb.Length() != 2 {
   361  		t.Fatalf("expect len 2 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   362  	}
   363  	if rb.Free() != 0 {
   364  		t.Fatalf("expect free 0 byte but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   365  	}
   366  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte{'a', 'b'}) {
   367  		t.Fatalf("expect a but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   368  	}
   369  	// check empty or full
   370  	if rb.IsEmpty() {
   371  		t.Fatalf("expect IsEmpty is false but got true")
   372  	}
   373  	if !rb.IsFull() {
   374  		t.Fatalf("expect IsFull is true but got false")
   375  	}
   376  
   377  	// write, it will scale from 2 to 4 bytes.
   378  	_ = rb.WriteByte('c')
   379  	if rb.Length() != 3 {
   380  		t.Fatalf("expect len 3 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   381  	}
   382  	if rb.Free() != 1 {
   383  		t.Fatalf("expect free 1 byte but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   384  	}
   385  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte{'a', 'b', 'c'}) {
   386  		t.Fatalf("expect a but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   387  	}
   388  	// check empty or full
   389  	if rb.IsEmpty() {
   390  		t.Fatalf("expect IsEmpty is false but got true")
   391  	}
   392  	if rb.IsFull() {
   393  		t.Fatalf("expect IsFull is false but got true ")
   394  	}
   395  
   396  	// read one
   397  	b, err := rb.ReadByte()
   398  	if err != nil {
   399  		t.Fatalf("ReadByte failed: %v", err)
   400  	}
   401  	if b != 'a' {
   402  		t.Fatalf("expect a but got %c. r.w=%d, r.r=%d", b, rb.w, rb.r)
   403  	}
   404  	if rb.Length() != 2 {
   405  		t.Fatalf("expect len 2 byte but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   406  	}
   407  	if rb.Free() != 2 {
   408  		t.Fatalf("expect free 2 byte but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   409  	}
   410  	if !bytes.Equal(rb.ByteBuffer().Bytes(), []byte{'b', 'c'}) {
   411  		t.Fatalf("expect a but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
   412  	}
   413  	// check empty or full
   414  	if rb.IsEmpty() {
   415  		t.Fatalf("expect IsEmpty is false but got true")
   416  	}
   417  	if rb.IsFull() {
   418  		t.Fatalf("expect IsFull is false but got true")
   419  	}
   420  
   421  	// read two
   422  	b, _ = rb.ReadByte()
   423  	if b != 'b' {
   424  		t.Fatalf("expect b but got %c. r.w=%d, r.r=%d", b, rb.w, rb.r)
   425  	}
   426  	if rb.Length() != 1 {
   427  		t.Fatalf("expect len 1 byte but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   428  	}
   429  	if rb.Free() != 3 {
   430  		t.Fatalf("expect free 3 byte but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   431  	}
   432  	// check empty or full
   433  	if rb.IsEmpty() {
   434  		t.Fatalf("expect IsEmpty is false but got true ")
   435  	}
   436  	if rb.IsFull() {
   437  		t.Fatalf("expect IsFull is false but got true")
   438  	}
   439  
   440  	// read three
   441  	_, _ = rb.ReadByte()
   442  	if rb.Length() != 0 {
   443  		t.Fatalf("expect len 0 byte but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
   444  	}
   445  	if rb.Free() != 4 {
   446  		t.Fatalf("expect free 4 byte but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
   447  	}
   448  	// check empty or full
   449  	if !rb.IsEmpty() {
   450  		t.Fatalf("expect IsEmpty is true but got false")
   451  	}
   452  	if rb.IsFull() {
   453  		t.Fatalf("expect IsFull is false but got true")
   454  	}
   455  }