github.com/tunabay/go-bitarray@v1.3.1/bitarray_convert_test.go (about)

     1  // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be found in
     3  // the LICENSE file.
     4  
     5  package bitarray_test
     6  
     7  import (
     8  	"fmt"
     9  	"math/big"
    10  	"math/rand"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/tunabay/go-bitarray"
    15  )
    16  
    17  // also tests ToInt(), ToUint64()
    18  func TestNewFromInt_rand(t *testing.T) {
    19  	const testIterations = 30000
    20  	rand.Seed(time.Now().UnixNano())
    21  
    22  	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
    23  
    24  	u32Max := big.NewInt(int64(0xffffffff))
    25  	u64Max := big.NewInt(0)
    26  	u64Max.Lsh(u32Max, 32)
    27  	u64Max.Or(u64Max, u32Max)
    28  	// t.Logf("MAX: % x", u64Max)
    29  
    30  	rndMax := big.NewInt(0)
    31  	rndMax.Lsh(u64Max, 64)
    32  	rndMax.Or(rndMax, u64Max) // 128 bit
    33  	// t.Logf("MAX: %x", rndMax)
    34  
    35  	for i := 0; i < testIterations; i++ {
    36  		v := big.NewInt(0)
    37  		v.Rand(rnd, rndMax)
    38  		v.Rsh(v, uint(rand.Intn(128)))
    39  		vbin := fmt.Sprintf("%b", v)
    40  
    41  		ba := bitarray.NewFromInt(v)
    42  		ba.V()
    43  		want := bitarray.MustParse(vbin)
    44  
    45  		if !ba.Equal(want) {
    46  			t.Errorf("unexpected result: %d", v)
    47  			t.Logf(" got: %#b", ba)
    48  			t.Logf(" got: %s", ba.D())
    49  			t.Logf("want: %#b", want)
    50  			t.FailNow()
    51  		}
    52  		// if i < 64 {
    53  		// 	t.Logf("pass: %d", v)
    54  		// 	t.Logf(" got: %x", ba)
    55  		// }
    56  
    57  		baO := ba.ZOptimize()
    58  		baE := ba.ZExpand()
    59  		gotO := baO.ToInt()
    60  		gotE := baE.ToInt()
    61  		if gotO.Cmp(v) != 0 {
    62  			t.Errorf("unexpected value: got %x, want %x", gotO, v)
    63  		}
    64  		if gotE.Cmp(v) != 0 {
    65  			t.Errorf("unexpected value: got %x, want %x", gotE, v)
    66  		}
    67  
    68  		v64b := big.NewInt(0)
    69  		v64b.And(v, u64Max)
    70  		v64 := v64b.Uint64()
    71  		got64O := baO.ToUint64()
    72  		got64E := baE.ToUint64()
    73  		if got64O != v64 {
    74  			t.Errorf("unexpected value: got %x, want %x", gotO, v64)
    75  		}
    76  		if got64E != v64 {
    77  			t.Errorf("unexpected value: got %x, want %x", gotE, v64)
    78  		}
    79  	}
    80  }
    81  
    82  func TestBitArray_ToInt_edge(t *testing.T) {
    83  	var ba *bitarray.BitArray
    84  	want := big.NewInt(0)
    85  	if got := ba.ToInt(); got.Cmp(want) != 0 {
    86  		t.Errorf("unexpected value: got %x, want %x", got, want)
    87  	}
    88  	ba = bitarray.New()
    89  	if got := ba.ToInt(); got.Cmp(want) != 0 {
    90  		t.Errorf("unexpected value: got %x, want %x", got, want)
    91  	}
    92  	ba = bitarray.NewZeroFilled(1000)
    93  	if got := ba.ToInt(); got.Cmp(want) != 0 {
    94  		t.Errorf("unexpected value: got %x, want %x", got, want)
    95  	}
    96  }
    97  
    98  func TestBitArray_ToUint64_edge(t *testing.T) {
    99  	var ba *bitarray.BitArray
   100  	if got := ba.ToUint64(); got != 0 {
   101  		t.Errorf("unexpected value: got %x, want 0", got)
   102  	}
   103  	ba = bitarray.New()
   104  	if got := ba.ToUint64(); got != 0 {
   105  		t.Errorf("unexpected value: got %x, want 0", got)
   106  	}
   107  	ba = bitarray.NewZeroFilled(1000)
   108  	if got := ba.ToUint64(); got != 0 {
   109  		t.Errorf("unexpected value: got %x, want 0", got)
   110  	}
   111  }