...

Source file src/k8s.io/kubernetes/pkg/registry/core/service/allocator/utils_test.go

Documentation: k8s.io/kubernetes/pkg/registry/core/service/allocator

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package allocator
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  )
    23  
    24  func TestCountBits(t *testing.T) {
    25  	// bigN is an integer that occupies more than one big.Word.
    26  	bigN, ok := big.NewInt(0).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16)
    27  	if !ok {
    28  		t.Fatal("Failed to set bigN")
    29  	}
    30  	tests := []struct {
    31  		n        *big.Int
    32  		expected int
    33  	}{
    34  		{n: big.NewInt(int64(0)), expected: 0},
    35  		{n: big.NewInt(int64(0xffffffffff)), expected: 40},
    36  		{n: bigN, expected: 1},
    37  	}
    38  	for _, test := range tests {
    39  		actual := countBits(test.n)
    40  		if test.expected != actual {
    41  			t.Errorf("%s should have %d bits but recorded as %d", test.n, test.expected, actual)
    42  		}
    43  	}
    44  }
    45  
    46  func BenchmarkCountBits(b *testing.B) {
    47  	bigN, ok := big.NewInt(0).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16)
    48  	if !ok {
    49  		b.Fatal("Failed to set bigN")
    50  	}
    51  	for i := 0; i < b.N; i++ {
    52  		countBits(bigN)
    53  	}
    54  }
    55  

View as plain text