github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/bits/uint64_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bits
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func TestTrailingZeros64(t *testing.T) {
    23  	for i := 0; i <= 64; i++ {
    24  		n := uint64(1) << uint(i)
    25  		if got, want := TrailingZeros64(n), i; got != want {
    26  			t.Errorf("TrailingZeros64(%#x): got %d, wanted %d", n, got, want)
    27  		}
    28  	}
    29  
    30  	for i := 0; i < 64; i++ {
    31  		n := ^uint64(0) << uint(i)
    32  		if got, want := TrailingZeros64(n), i; got != want {
    33  			t.Errorf("TrailingZeros64(%#x): got %d, wanted %d", n, got, want)
    34  		}
    35  	}
    36  
    37  	for i := 0; i < 64; i++ {
    38  		n := ^uint64(0) >> uint(i)
    39  		if got, want := TrailingZeros64(n), 0; got != want {
    40  			t.Errorf("TrailingZeros64(%#x): got %d, wanted %d", n, got, want)
    41  		}
    42  	}
    43  }
    44  
    45  func TestMostSignificantOne64(t *testing.T) {
    46  	for i := 0; i <= 64; i++ {
    47  		n := uint64(1) << uint(i)
    48  		if got, want := MostSignificantOne64(n), i; got != want {
    49  			t.Errorf("MostSignificantOne64(%#x): got %d, wanted %d", n, got, want)
    50  		}
    51  	}
    52  
    53  	for i := 0; i < 64; i++ {
    54  		n := ^uint64(0) >> uint(i)
    55  		if got, want := MostSignificantOne64(n), 63-i; got != want {
    56  			t.Errorf("MostSignificantOne64(%#x): got %d, wanted %d", n, got, want)
    57  		}
    58  	}
    59  
    60  	for i := 0; i < 64; i++ {
    61  		n := ^uint64(0) << uint(i)
    62  		if got, want := MostSignificantOne64(n), 63; got != want {
    63  			t.Errorf("MostSignificantOne64(%#x): got %d, wanted %d", n, got, want)
    64  		}
    65  	}
    66  }
    67  
    68  func TestForEachSetBit64(t *testing.T) {
    69  	for _, want := range [][]int{
    70  		{},
    71  		{0},
    72  		{1},
    73  		{63},
    74  		{0, 1},
    75  		{1, 3, 5},
    76  		{0, 63},
    77  	} {
    78  		n := Mask64(want...)
    79  		// "Slice values are deeply equal when ... they are both nil or both
    80  		// non-nil ..."
    81  		got := make([]int, 0)
    82  		ForEachSetBit64(n, func(i int) {
    83  			got = append(got, i)
    84  		})
    85  		if !reflect.DeepEqual(got, want) {
    86  			t.Errorf("ForEachSetBit64(%#x): iterated bits %v, wanted %v", n, got, want)
    87  		}
    88  	}
    89  }
    90  
    91  func TestIsOn(t *testing.T) {
    92  	type spec struct {
    93  		mask uint64
    94  		bits uint64
    95  		any  bool
    96  		all  bool
    97  	}
    98  	for _, s := range []spec{
    99  		{Mask64(0), Mask64(0), true, true},
   100  		{Mask64(63), Mask64(63), true, true},
   101  		{Mask64(0), Mask64(1), false, false},
   102  		{Mask64(0), Mask64(0, 1), true, false},
   103  
   104  		{Mask64(1, 63), Mask64(1), true, true},
   105  		{Mask64(1, 63), Mask64(1, 63), true, true},
   106  		{Mask64(1, 63), Mask64(0, 1, 63), true, false},
   107  		{Mask64(1, 63), Mask64(0, 62), false, false},
   108  	} {
   109  		if ok := IsAnyOn64(s.mask, s.bits); ok != s.any {
   110  			t.Errorf("IsAnyOn(%#x, %#x) = %v, wanted: %v", s.mask, s.bits, ok, s.any)
   111  		}
   112  		if ok := IsOn64(s.mask, s.bits); ok != s.all {
   113  			t.Errorf("IsOn(%#x, %#x) = %v, wanted: %v", s.mask, s.bits, ok, s.all)
   114  		}
   115  	}
   116  }
   117  
   118  func TestIsPowerOfTwo(t *testing.T) {
   119  	for _, tc := range []struct {
   120  		v    uint64
   121  		want bool
   122  	}{
   123  		{v: 0, want: false},
   124  		{v: 1, want: true},
   125  		{v: 2, want: true},
   126  		{v: 3, want: false},
   127  		{v: 4, want: true},
   128  		{v: 5, want: false},
   129  	} {
   130  		if got := IsPowerOfTwo64(tc.v); got != tc.want {
   131  			t.Errorf("IsPowerOfTwo(%d) = %t, want: %t", tc.v, got, tc.want)
   132  		}
   133  	}
   134  }