github.com/aergoio/aergo@v1.3.1/p2p/p2putil/pressqueue_test.go (about)

     1  package p2putil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type TestItem interface {
    10  	ID() int
    11  }
    12  type testItem struct {
    13  	id int
    14  }
    15  
    16  func (ti testItem) ID() int {
    17  	return ti.id
    18  }
    19  func TestPressableQueue_Offer(t *testing.T) {
    20  	const arrSize = 30
    21  	var mos [arrSize]interface{}
    22  	for i := 0; i < arrSize; i++ {
    23  		mos[i] = &testItem{i}
    24  	}
    25  	tests := []struct {
    26  		name string
    27  		cap  int
    28  		want bool
    29  	}{
    30  		{"T10", 10, true},
    31  	}
    32  	for _, tt := range tests {
    33  		t.Run(tt.name, func(t *testing.T) {
    34  			target1 := NewPressableQueue(tt.cap)
    35  			target2 := NewPressableQueue(tt.cap)
    36  
    37  			assert.True(t, target1.Empty())
    38  			assert.True(t, target2.Empty())
    39  			assert.False(t, target1.Full())
    40  			assert.False(t, target2.Full())
    41  
    42  			for i, mo := range mos {
    43  				expected := i < tt.cap
    44  				assert.Equal(t, expected, target1.Offer(mo))
    45  				if expected {
    46  					assert.Nil(t, target2.Press(mo))
    47  				} else {
    48  					assert.NotNil(t, target2.Press(mo))
    49  				}
    50  			}
    51  			assert.Equal(t, tt.cap, target1.Size())
    52  			assert.Equal(t, tt.cap, target2.Size())
    53  			assert.False(t, target1.Empty())
    54  			assert.False(t, target2.Empty())
    55  			assert.True(t, target1.Full())
    56  			assert.True(t, target2.Full())
    57  
    58  			for i := 0; i < tt.cap; i++ {
    59  				mo := target1.Poll().(TestItem)
    60  				assert.NotNil(t, mo)
    61  				assert.Equal(t, i, mo.ID())
    62  
    63  				mo2 := target2.Poll().(TestItem)
    64  				assert.NotNil(t, mo2)
    65  				assert.Equal(t, arrSize-tt.cap+i, mo2.ID())
    66  			}
    67  
    68  			assert.True(t, target1.Empty())
    69  			assert.True(t, target2.Empty())
    70  			assert.False(t, target1.Full())
    71  			assert.False(t, target2.Full())
    72  
    73  		})
    74  	}
    75  }
    76  
    77  func TestPressableQueue_Peek(t *testing.T) {
    78  	const arrSize = 10
    79  	var mos [arrSize]interface{}
    80  	for i := 0; i < arrSize; i++ {
    81  		mos[i] = &testItem{i}
    82  	}
    83  	tests := []struct {
    84  		name string
    85  		cap  int
    86  		want bool
    87  	}{
    88  		{"T10", 10, true},
    89  		// TODO: Add test cases.
    90  	}
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			target1 := NewPressableQueue(tt.cap)
    94  			for _, mo := range mos {
    95  				target1.Offer(mo)
    96  			}
    97  			size := target1.Size()
    98  			for size > 0 {
    99  
   100  				actual1 := target1.Peek()
   101  				assert.Equal(t, size, target1.Size())
   102  				actual2 := target1.Poll()
   103  				assert.Equal(t, size-1, target1.Size())
   104  				assert.Equal(t, actual1, actual2)
   105  				size = target1.Size()
   106  			}
   107  		})
   108  	}
   109  }