github.com/dubbogo/gost@v1.14.0/container/queue/circular_unbounded_queue_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxqueue
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  import (
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestCircularUnboundedQueueWithoutGrowing(t *testing.T) {
    29  	queue := NewCircularUnboundedQueue(10)
    30  
    31  	queue.Reset()
    32  
    33  	// write 1 element
    34  	queue.Push(1)
    35  	assert.Equal(t, 1, queue.Len())
    36  	assert.Equal(t, 10, queue.Cap())
    37  	// peek and pop
    38  	assert.Equal(t, 1, queue.Peek())
    39  	assert.Equal(t, 1, queue.Pop())
    40  	// inspect len and cap
    41  	assert.Equal(t, 0, queue.Len())
    42  	assert.Equal(t, 10, queue.Cap())
    43  
    44  	// write 8 elements
    45  	for i := 0; i < 8; i++ {
    46  		queue.Push(i)
    47  	}
    48  	assert.Equal(t, 8, queue.Len())
    49  	assert.Equal(t, 10, queue.Cap())
    50  
    51  	var v interface{}
    52  	// pop 5 elements
    53  	for i := 0; i < 5; i++ {
    54  		v = queue.Pop()
    55  		assert.Equal(t, i, v)
    56  	}
    57  	assert.Equal(t, 3, queue.Len())
    58  	assert.Equal(t, 10, queue.Cap())
    59  
    60  	// write 6 elements
    61  	for i := 0; i < 6; i++ {
    62  		queue.Push(i)
    63  	}
    64  	assert.Equal(t, 9, queue.Len())
    65  	assert.Equal(t, 10, queue.Cap())
    66  }
    67  
    68  func TestCircularUnboundedQueueWithGrowing(t *testing.T) {
    69  	// size < fastGrowThreshold
    70  	queue := NewCircularUnboundedQueue(10)
    71  
    72  	// write 11 elements
    73  	for i := 0; i < 11; i++ {
    74  		queue.Push(i)
    75  	}
    76  
    77  	assert.Equal(t, 11, queue.Len())
    78  	assert.Equal(t, 20, queue.Cap())
    79  
    80  	queue.Reset()
    81  	assert.Equal(t, 0, queue.Len())
    82  	assert.Equal(t, 10, queue.Cap())
    83  
    84  	for i := 0; i < 8; i++ {
    85  		queue.Push(i)
    86  		queue.Pop()
    87  	}
    88  	for i := 0; i < 11; i++ {
    89  		queue.Push(i)
    90  		if i == 9 {
    91  			expectedArr := []int{3, 4, 5, 6, 7, 8, 9, 7, 0, 1, 2}
    92  			for j := range queue.data {
    93  				assert.Equal(t, expectedArr[j], queue.data[j].(int))
    94  			}
    95  		}
    96  	}
    97  	assert.Equal(t, 11, queue.Len())
    98  	assert.Equal(t, 20, queue.Cap())
    99  
   100  	for i := 0; i < 11; i++ {
   101  		assert.Equal(t, i, queue.Pop())
   102  	}
   103  
   104  	queue = NewCircularUnboundedQueue(fastGrowThreshold)
   105  
   106  	// write fastGrowThreshold+1 elements
   107  	for i := 0; i < fastGrowThreshold+1; i++ {
   108  		queue.Push(i)
   109  	}
   110  
   111  	assert.Equal(t, fastGrowThreshold+1, queue.Len())
   112  	assert.Equal(t, fastGrowThreshold+fastGrowThreshold/4, queue.Cap())
   113  
   114  	queue.Reset()
   115  	assert.Equal(t, 0, queue.Len())
   116  	assert.Equal(t, fastGrowThreshold, queue.Cap())
   117  }
   118  
   119  func TestCircularUnboundedQueueWithQuota(t *testing.T) {
   120  	queue := NewCircularUnboundedQueueWithQuota(10, 9)
   121  	assert.Equal(t, 0, queue.Len())
   122  	assert.Equal(t, 9, queue.Cap())
   123  
   124  	queue = NewCircularUnboundedQueueWithQuota(10, 15)
   125  
   126  	for i := 0; i < 10; i++ {
   127  		ok := queue.Push(i)
   128  		assert.True(t, ok)
   129  	}
   130  
   131  	assert.Equal(t, 10, queue.Len())
   132  	assert.Equal(t, 10, queue.Cap())
   133  
   134  	for i := 0; i < 10; i++ {
   135  		v := queue.Pop()
   136  		assert.Equal(t, i, v.(int))
   137  	}
   138  
   139  	for i := 0; i < 15; i++ {
   140  		ok := queue.Push(i)
   141  		assert.True(t, ok)
   142  	}
   143  
   144  	assert.Equal(t, 15, queue.Len())
   145  	assert.Equal(t, 15, queue.Cap())
   146  }