lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/xmath/math_test.go (about)

     1  // Copyright (C) 2017  Nexedi SA and Contributors.
     2  //                     Kirill Smelkov <kirr@nexedi.com>
     3  //
     4  // This program is free software: you can Use, Study, Modify and Redistribute
     5  // it under the terms of the GNU General Public License version 3, or (at your
     6  // option) any later version, as published by the Free Software Foundation.
     7  //
     8  // You can also Link and Combine this program with other software covered by
     9  // the terms of any of the Free Software licenses or any of the Open Source
    10  // Initiative approved licenses and Convey the resulting work. Corresponding
    11  // source of such a combination shall include the source code for all other
    12  // software used.
    13  //
    14  // This program is distributed WITHOUT ANY WARRANTY; without even the implied
    15  // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16  //
    17  // See COPYING file for full licensing terms.
    18  // See https://www.nexedi.com/licensing for rationale and options.
    19  
    20  package xmath
    21  
    22  import (
    23  	"testing"
    24  )
    25  
    26  func TestPow2(t *testing.T) {
    27  	testv := []struct {x, xcpow2 uint64; xclog2 int} {
    28  		{0, 0, 0},
    29  		{1, 1, 0},
    30  		{2, 2, 1},
    31  		{3, 4, 2},
    32  		{4, 4, 2},
    33  		{5, 8, 3},
    34  		{5, 8, 3},
    35  		{6, 8, 3},
    36  		{7, 8, 3},
    37  		{8, 8, 3},
    38  		{9, 16, 4},
    39  		{10, 16, 4},
    40  		{11, 16, 4},
    41  		{12, 16, 4},
    42  		{13, 16, 4},
    43  		{14, 16, 4},
    44  		{15, 16, 4},
    45  		{16, 16, 4},
    46  		{1<<62 - 1, 1<<62, 62},
    47  		{1<<62, 1<<62, 62},
    48  		{1<<62+1, 1<<63, 63},
    49  		{1<<63 - 1, 1<<63, 63},
    50  		{1<<63, 1<<63, 63},
    51  	}
    52  
    53  	for _, tt := range testv {
    54  		xcpow2 := CeilPow2(tt.x)
    55  		if xcpow2 != tt.xcpow2 {
    56  			t.Errorf("CeilPow2(%v) -> %v  ; want %v", tt.x, xcpow2, tt.xcpow2)
    57  		}
    58  
    59  		xclog2 := CeilLog2(tt.xcpow2)
    60  		if xclog2 != tt.xclog2 {
    61  			t.Errorf("CeilLog2(%v) -> %v  ; want %v", tt.xcpow2, xclog2, tt.xclog2)
    62  		}
    63  
    64  		xclog2 = CeilLog2(tt.x)
    65  		if xclog2 != tt.xclog2 {
    66  			t.Errorf("CeilLog2(%v) -> %v  ; want %v", tt.x, xclog2, tt.xclog2)
    67  		}
    68  
    69  		xflog2 := FloorLog2(tt.xcpow2)
    70  		xflog2Ok := tt.xclog2
    71  		if tt.x == 0 {
    72  			xflog2Ok = -1
    73  		}
    74  		if xflog2 != xflog2Ok {
    75  			t.Errorf("FloorLog2(%v) -> %v  ; want %v", tt.xcpow2, xflog2, xflog2Ok)
    76  		}
    77  
    78  		if tt.x != tt.xcpow2 {
    79  			xflog2Ok--
    80  		}
    81  		xflog2 = FloorLog2(tt.x)
    82  		if xflog2 != xflog2Ok {
    83  			t.Errorf("FloorLog2(%v) -> %v  ; want %v", tt.x, xflog2, xflog2Ok)
    84  		}
    85  	}
    86  }