github.com/cilium/cilium@v1.16.2/pkg/ipam/service/allocator/utils_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  // Copyright The Kubernetes Authors.
     4  
     5  package allocator
     6  
     7  import (
     8  	"math/big"
     9  	"testing"
    10  )
    11  
    12  func TestCountBits(t *testing.T) {
    13  	// bigN is an integer that occupies more than one big.Word.
    14  	bigN, ok := big.NewInt(0).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16)
    15  	if !ok {
    16  		t.Fatal("Failed to set bigN")
    17  	}
    18  	tests := []struct {
    19  		n        *big.Int
    20  		expected int
    21  	}{
    22  		{n: big.NewInt(int64(0)), expected: 0},
    23  		{n: big.NewInt(int64(0xffffffffff)), expected: 40},
    24  		{n: bigN, expected: 1},
    25  	}
    26  	for _, test := range tests {
    27  		actual := countBits(test.n)
    28  		if test.expected != actual {
    29  			t.Errorf("%s should have %d bits but recorded as %d", test.n, test.expected, actual)
    30  		}
    31  	}
    32  }
    33  
    34  func BenchmarkCountBits(b *testing.B) {
    35  	bigN, ok := big.NewInt(0).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16)
    36  	if !ok {
    37  		b.Fatal("Failed to set bigN")
    38  	}
    39  	for i := 0; i < b.N; i++ {
    40  		countBits(bigN)
    41  	}
    42  }