github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/util/data/bin_heap_test.go (about)

     1  package data
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestBinHeap_Insert(t *testing.T) {
     9  	tests := []struct {
    10  		name     string
    11  		elements map[string]uint32
    12  		expected [][]*BinHeapNode
    13  	}{
    14  		{
    15  			name: "Single node",
    16  			elements: map[string]uint32{
    17  				"A": 1,
    18  			},
    19  			expected: [][]*BinHeapNode{
    20  				[]*BinHeapNode{
    21  					&BinHeapNode{value: "A", weight: 1},
    22  				},
    23  			},
    24  		},
    25  		{
    26  			name: "2 nodes",
    27  			elements: map[string]uint32{
    28  				"A": 1, "B": 2,
    29  			},
    30  			expected: [][]*BinHeapNode{
    31  				[]*BinHeapNode{
    32  					&BinHeapNode{value: "B", weight: 2},
    33  					&BinHeapNode{value: "A", weight: 1},
    34  				},
    35  			},
    36  		},
    37  		{
    38  			name: "3 nodes, 2 with equal weights",
    39  			elements: map[string]uint32{
    40  				"A": 1, "B": 2, "C": 1,
    41  			},
    42  			expected: [][]*BinHeapNode{
    43  				[]*BinHeapNode{
    44  					&BinHeapNode{value: "B", weight: 2},
    45  					&BinHeapNode{value: "A", weight: 1},
    46  					&BinHeapNode{value: "C", weight: 1},
    47  				},
    48  				[]*BinHeapNode{
    49  					&BinHeapNode{value: "B", weight: 2},
    50  					&BinHeapNode{value: "C", weight: 1},
    51  					&BinHeapNode{value: "A", weight: 1},
    52  				},
    53  			},
    54  		},
    55  	}
    56  	for _, testCase := range tests {
    57  		t.Run(testCase.name, func(t *testing.T) {
    58  			bh := NewBinHeap()
    59  			for v, p := range testCase.elements {
    60  				bh.Insert(p, v)
    61  			}
    62  			matchFound := false
    63  			for _, exp := range testCase.expected {
    64  				if reflect.DeepEqual(bh.vals, exp) {
    65  					matchFound = true
    66  					break
    67  				}
    68  			}
    69  			if !matchFound {
    70  				t.Errorf("Unexpected contents of the tree: got %+v, expected: %+v",
    71  					bh.vals, testCase.expected)
    72  			}
    73  		})
    74  	}
    75  }