gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/collection/specialized/hashpriorityqueue_test.go (about)

     1  package specialized
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"gitlab.com/beacon-software/gadget/collection"
     9  	"gitlab.com/beacon-software/gadget/generator"
    10  )
    11  
    12  type MockHashPriority struct {
    13  	priority int
    14  	hash     string
    15  }
    16  
    17  func (mp *MockHashPriority) GetPriority() int {
    18  	return mp.priority
    19  }
    20  
    21  func (mp *MockHashPriority) GetHash() interface{} {
    22  	return mp.hash
    23  }
    24  
    25  func NewMockHashPriority(p int, s string) *MockHashPriority {
    26  	return &MockHashPriority{priority: p, hash: s}
    27  }
    28  
    29  func TestHashPriorityQueue_Size(t *testing.T) {
    30  	assert := assert.New(t)
    31  	q := NewHashPriorityQueue()
    32  	assert.Equal(0, q.Size())
    33  	sameHash := generator.String(20)
    34  	q.Push(NewMockHashPriority(2, sameHash))
    35  	assert.Equal(1, q.Size())
    36  	q.Push(NewMockHashPriority(2, sameHash))
    37  	assert.Equal(1, q.Size())
    38  	q.Push(NewMockHashPriority(3, generator.String(20)))
    39  	assert.Equal(2, q.Size())
    40  	q.Pop()
    41  	assert.Equal(1, q.Size())
    42  }
    43  
    44  func TestHashPriorityQueue_Peek(t *testing.T) {
    45  	assert := assert.New(t)
    46  	q := NewHashPriorityQueue()
    47  	expected := NewMockHashPriority(3, generator.String(20))
    48  	q.Push(expected)
    49  	actual, ok := q.Peek()
    50  	assert.True(ok)
    51  	assert.Equal(expected, actual)
    52  	assert.Equal(1, q.Size())
    53  	q.Pop()
    54  	actual, ok = q.Peek()
    55  	assert.Nil(actual)
    56  	assert.False(ok)
    57  	assert.Equal(0, q.Size())
    58  }
    59  
    60  func TestHashPriorityQueue(t *testing.T) {
    61  	assert := assert.New(t)
    62  	type fields struct {
    63  		list collection.List
    64  	}
    65  	tests := []struct {
    66  		name      string
    67  		pinput    []int
    68  		hinput    []string
    69  		hexpected []interface{}
    70  	}{
    71  		{
    72  			name:      "Reverse Order",
    73  			pinput:    []int{1, 2, 3, 4},
    74  			hinput:    []string{"d", "c", "b", "a"},
    75  			hexpected: []interface{}{"a", "b", "c", "d"},
    76  		},
    77  		{
    78  			name:      "Correct Order",
    79  			pinput:    []int{4, 3, 2, 1},
    80  			hinput:    []string{"a", "b", "c", "d"},
    81  			hexpected: []interface{}{"a", "b", "c", "d"},
    82  		},
    83  		{
    84  			name:      "Dupes",
    85  			pinput:    []int{4, 2, 2, 5, 5, 3, 1},
    86  			hinput:    []string{"a", "b", "c", "d", "d", "f", "g"},
    87  			hexpected: []interface{}{"d", "a", "f", "c", "b", "g"},
    88  		},
    89  	}
    90  
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			q := NewHashPriorityQueue()
    94  			for i := 0; i < len(tt.pinput); i++ {
    95  				q.Push(NewMockHashPriority(tt.pinput[i], tt.hinput[i]))
    96  			}
    97  			actual := []interface{}{}
    98  			for p, ok := q.Pop(); ok; p, ok = q.Pop() {
    99  				actual = append(actual, p.GetHash())
   100  			}
   101  			assert.Equal(tt.hexpected, actual)
   102  		})
   103  	}
   104  }