github.com/MetalBlockchain/metalgo@v1.11.9/utils/sampler/weighted_array_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package sampler
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestWeightedArrayElementCompare(t *testing.T) {
    14  	tests := []struct {
    15  		a        weightedArrayElement
    16  		b        weightedArrayElement
    17  		expected int
    18  	}{
    19  		{
    20  			a:        weightedArrayElement{},
    21  			b:        weightedArrayElement{},
    22  			expected: 0,
    23  		},
    24  		{
    25  			a: weightedArrayElement{
    26  				cumulativeWeight: 1,
    27  			},
    28  			b: weightedArrayElement{
    29  				cumulativeWeight: 2,
    30  			},
    31  			expected: 1,
    32  		},
    33  	}
    34  	for _, test := range tests {
    35  		t.Run(fmt.Sprintf("%d_%d_%d", test.a.cumulativeWeight, test.b.cumulativeWeight, test.expected), func(t *testing.T) {
    36  			require := require.New(t)
    37  
    38  			require.Equal(test.expected, test.a.Compare(test.b))
    39  			require.Equal(-test.expected, test.b.Compare(test.a))
    40  		})
    41  	}
    42  }