github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/test/intrinsics_test.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"math/bits"
     9  	"testing"
    10  )
    11  
    12  func TestBitLen64(t *testing.T) {
    13  	for i := 0; i <= 64; i++ {
    14  		got := bits.Len64(1 << i)
    15  		want := i + 1
    16  		if want == 65 {
    17  			want = 0
    18  		}
    19  		if got != want {
    20  			t.Errorf("Len64(1<<%d) = %d, want %d", i, got, want)
    21  		}
    22  	}
    23  }
    24  
    25  func TestBitLen32(t *testing.T) {
    26  	for i := 0; i <= 32; i++ {
    27  		got := bits.Len32(1 << i)
    28  		want := i + 1
    29  		if want == 33 {
    30  			want = 0
    31  		}
    32  		if got != want {
    33  			t.Errorf("Len32(1<<%d) = %d, want %d", i, got, want)
    34  		}
    35  	}
    36  }
    37  
    38  func TestBitLen16(t *testing.T) {
    39  	for i := 0; i <= 16; i++ {
    40  		got := bits.Len16(1 << i)
    41  		want := i + 1
    42  		if want == 17 {
    43  			want = 0
    44  		}
    45  		if got != want {
    46  			t.Errorf("Len16(1<<%d) = %d, want %d", i, got, want)
    47  		}
    48  	}
    49  }
    50  
    51  func TestBitLen8(t *testing.T) {
    52  	for i := 0; i <= 8; i++ {
    53  		got := bits.Len8(1 << i)
    54  		want := i + 1
    55  		if want == 9 {
    56  			want = 0
    57  		}
    58  		if got != want {
    59  			t.Errorf("Len8(1<<%d) = %d, want %d", i, got, want)
    60  		}
    61  	}
    62  }