github.com/blend/go-sdk@v1.20220411.3/collections/ring_buffer_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func Test_RingBuffer(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	buffer := NewRingBuffer()
    20  
    21  	buffer.Enqueue(1)
    22  	assert.Equal(1, buffer.Len())
    23  	assert.Equal(1, buffer.Peek())
    24  	assert.Equal(1, buffer.PeekBack())
    25  
    26  	buffer.Enqueue(2)
    27  	assert.Equal(2, buffer.Len())
    28  	assert.Equal(1, buffer.Peek())
    29  	assert.Equal(2, buffer.PeekBack())
    30  
    31  	buffer.Enqueue(3)
    32  	assert.Equal(3, buffer.Len())
    33  	assert.Equal(1, buffer.Peek())
    34  	assert.Equal(3, buffer.PeekBack())
    35  
    36  	buffer.Enqueue(4)
    37  	assert.Equal(4, buffer.Len())
    38  	assert.Equal(1, buffer.Peek())
    39  	assert.Equal(4, buffer.PeekBack())
    40  
    41  	buffer.Enqueue(5)
    42  	assert.Equal(5, buffer.Len())
    43  	assert.Equal(1, buffer.Peek())
    44  	assert.Equal(5, buffer.PeekBack())
    45  
    46  	buffer.Enqueue(6)
    47  	assert.Equal(6, buffer.Len())
    48  	assert.Equal(1, buffer.Peek())
    49  	assert.Equal(6, buffer.PeekBack())
    50  
    51  	buffer.Enqueue(7)
    52  	assert.Equal(7, buffer.Len())
    53  	assert.Equal(1, buffer.Peek())
    54  	assert.Equal(7, buffer.PeekBack())
    55  
    56  	buffer.Enqueue(8)
    57  	assert.Equal(8, buffer.Len())
    58  	assert.Equal(1, buffer.Peek())
    59  	assert.Equal(8, buffer.PeekBack())
    60  
    61  	value := buffer.Dequeue()
    62  	assert.Equal(1, value)
    63  	assert.Equal(7, buffer.Len())
    64  	assert.Equal(2, buffer.Peek())
    65  	assert.Equal(8, buffer.PeekBack())
    66  
    67  	value = buffer.Dequeue()
    68  	assert.Equal(2, value)
    69  	assert.Equal(6, buffer.Len())
    70  	assert.Equal(3, buffer.Peek())
    71  	assert.Equal(8, buffer.PeekBack())
    72  
    73  	value = buffer.Dequeue()
    74  	assert.Equal(3, value)
    75  	assert.Equal(5, buffer.Len())
    76  	assert.Equal(4, buffer.Peek())
    77  	assert.Equal(8, buffer.PeekBack())
    78  
    79  	value = buffer.Dequeue()
    80  	assert.Equal(4, value)
    81  	assert.Equal(4, buffer.Len())
    82  	assert.Equal(5, buffer.Peek())
    83  	assert.Equal(8, buffer.PeekBack())
    84  
    85  	value = buffer.Dequeue()
    86  	assert.Equal(5, value)
    87  	assert.Equal(3, buffer.Len())
    88  	assert.Equal(6, buffer.Peek())
    89  	assert.Equal(8, buffer.PeekBack())
    90  
    91  	value = buffer.Dequeue()
    92  	assert.Equal(6, value)
    93  	assert.Equal(2, buffer.Len())
    94  	assert.Equal(7, buffer.Peek())
    95  	assert.Equal(8, buffer.PeekBack())
    96  
    97  	value = buffer.Dequeue()
    98  	assert.Equal(7, value)
    99  	assert.Equal(1, buffer.Len())
   100  	assert.Equal(8, buffer.Peek())
   101  	assert.Equal(8, buffer.PeekBack())
   102  
   103  	value = buffer.Dequeue()
   104  	assert.Equal(8, value)
   105  	assert.Equal(0, buffer.Len())
   106  	assert.Nil(buffer.Peek())
   107  	assert.Nil(buffer.PeekBack())
   108  }
   109  
   110  func TestRingBufferClear(t *testing.T) {
   111  	assert := assert.New(t)
   112  
   113  	buffer := NewRingBuffer()
   114  	buffer.Enqueue(1)
   115  	buffer.Enqueue(1)
   116  	buffer.Enqueue(1)
   117  	buffer.Enqueue(1)
   118  	buffer.Enqueue(1)
   119  	buffer.Enqueue(1)
   120  	buffer.Enqueue(1)
   121  	buffer.Enqueue(1)
   122  
   123  	assert.Equal(8, buffer.Len())
   124  
   125  	buffer.Clear()
   126  	assert.Equal(0, buffer.Len())
   127  	assert.Nil(buffer.Peek())
   128  	assert.Nil(buffer.PeekBack())
   129  }
   130  
   131  func TestRingBufferContents(t *testing.T) {
   132  	assert := assert.New(t)
   133  
   134  	buffer := NewRingBuffer()
   135  	buffer.Enqueue(1)
   136  	buffer.Enqueue(2)
   137  	buffer.Enqueue(3)
   138  	buffer.Enqueue(4)
   139  	buffer.Enqueue(5)
   140  
   141  	contents := buffer.Contents()
   142  	assert.Len(contents, 5)
   143  	assert.Equal(1, contents[0])
   144  	assert.Equal(2, contents[1])
   145  	assert.Equal(3, contents[2])
   146  	assert.Equal(4, contents[3])
   147  	assert.Equal(5, contents[4])
   148  }
   149  
   150  func TestRingBufferDrain(t *testing.T) {
   151  	assert := assert.New(t)
   152  
   153  	buffer := NewRingBuffer()
   154  	buffer.Enqueue(1)
   155  	buffer.Enqueue(2)
   156  	buffer.Enqueue(3)
   157  	buffer.Enqueue(4)
   158  	buffer.Enqueue(5)
   159  
   160  	contents := buffer.Drain()
   161  	assert.Len(contents, 5)
   162  	assert.Equal(1, contents[0])
   163  	assert.Equal(2, contents[1])
   164  	assert.Equal(3, contents[2])
   165  	assert.Equal(4, contents[3])
   166  	assert.Equal(5, contents[4])
   167  
   168  	assert.Equal(0, buffer.Len())
   169  	assert.Nil(buffer.Peek())
   170  	assert.Nil(buffer.PeekBack())
   171  }
   172  
   173  func TestRingBufferEach(t *testing.T) {
   174  	assert := assert.New(t)
   175  
   176  	buffer := NewRingBuffer()
   177  
   178  	for x := 1; x < 17; x++ {
   179  		buffer.Enqueue(x)
   180  	}
   181  
   182  	called := 0
   183  	buffer.Each(func(v interface{}) {
   184  		if typed, isTyped := v.(int); isTyped {
   185  			if typed == (called + 1) {
   186  				called++
   187  			}
   188  		}
   189  	})
   190  
   191  	assert.Equal(16, called)
   192  }
   193  
   194  func TestRingBufferEachUntil(t *testing.T) {
   195  	assert := assert.New(t)
   196  
   197  	buffer := NewRingBuffer()
   198  
   199  	for x := 1; x < 17; x++ {
   200  		buffer.Enqueue(x)
   201  	}
   202  
   203  	called := 0
   204  	buffer.EachUntil(func(v interface{}) bool {
   205  		if typed, isTyped := v.(int); isTyped {
   206  			if typed > 10 {
   207  				return false
   208  			}
   209  			if typed == (called + 1) {
   210  				called++
   211  			}
   212  		}
   213  		return true
   214  	})
   215  
   216  	assert.Equal(10, called)
   217  }
   218  
   219  func TestRingBufferReverseEachUntil(t *testing.T) {
   220  	assert := assert.New(t)
   221  
   222  	buffer := NewRingBufferWithCapacity(32)
   223  
   224  	for x := 1; x < 17; x++ {
   225  		buffer.Enqueue(x)
   226  	}
   227  
   228  	var values []int
   229  	buffer.ReverseEachUntil(func(v interface{}) bool {
   230  		if typed, isTyped := v.(int); isTyped {
   231  			if typed < 10 {
   232  				return false
   233  			}
   234  			values = append(values, typed)
   235  			return true
   236  		}
   237  		panic("value is not an integer")
   238  	})
   239  
   240  	assert.Len(values, 7)
   241  	assert.Equal(16, values[0])
   242  	assert.Equal(10, values[6])
   243  }
   244  
   245  func TestRingBufferReverseEachUntilUndersized(t *testing.T) {
   246  	assert := assert.New(t)
   247  
   248  	buffer := NewRingBuffer()
   249  
   250  	for x := 1; x < 17; x++ {
   251  		buffer.Enqueue(x)
   252  	}
   253  
   254  	var values []int
   255  	buffer.ReverseEachUntil(func(v interface{}) bool {
   256  		if typed, isTyped := v.(int); isTyped {
   257  			if typed < 10 {
   258  				return false
   259  			}
   260  			values = append(values, typed)
   261  			return true
   262  		}
   263  		panic("value is not an integer")
   264  	})
   265  
   266  	assert.Len(values, 7)
   267  	assert.Equal(16, values[0])
   268  	assert.Equal(10, values[6])
   269  }
   270  
   271  func TestRingBufferConsume(t *testing.T) {
   272  	assert := assert.New(t)
   273  
   274  	buffer := NewRingBuffer()
   275  
   276  	for x := 1; x < 17; x++ {
   277  		buffer.Enqueue(x)
   278  	}
   279  
   280  	assert.Equal(16, buffer.Len())
   281  
   282  	var called int
   283  	buffer.Consume(func(v interface{}) {
   284  		if _, isTyped := v.(int); isTyped {
   285  			called++
   286  		}
   287  	})
   288  
   289  	assert.Equal(16, called)
   290  	assert.Zero(buffer.Len())
   291  }
   292  
   293  func Test_Ringbuffer_DequeueBack(t *testing.T) {
   294  	a := assert.New(t)
   295  
   296  	q := NewRingBuffer()
   297  	q.Enqueue(1)
   298  	q.Enqueue(2)
   299  	q.Enqueue(3)
   300  	q.Enqueue(4)
   301  
   302  	a.Equal(4, q.DequeueBack())
   303  	a.Equal(3, q.DequeueBack())
   304  	a.Equal(2, q.DequeueBack())
   305  	a.Equal(1, q.DequeueBack())
   306  	a.Nil(q.DequeueBack())
   307  	a.Nil(q.DequeueBack())
   308  
   309  	q.Enqueue(1)
   310  	q.Enqueue(2)
   311  	q.Enqueue(3)
   312  	q.Enqueue(4)
   313  
   314  	a.Equal(4, q.DequeueBack())
   315  	a.Equal(3, q.DequeueBack())
   316  	a.Equal(2, q.DequeueBack())
   317  	a.Equal(1, q.DequeueBack())
   318  	a.Nil(q.DequeueBack())
   319  }