github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/packed/packed_test.go (about)

     1  package packed
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"math/rand"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestByteCount(t *testing.T) {
    12  	rand.Seed(time.Now().UnixNano())
    13  
    14  	inters := 10 // >= 3
    15  	for i := 0; i < inters; i++ {
    16  		valueCount := rand.Int31n(math.MaxInt32-1) + 1 // [1, 1^32-1]
    17  		for j := 0; j <= 1; j++ {
    18  			format := PackedFormat(j)
    19  			for bpv := uint32(1); bpv <= 64; bpv++ {
    20  				byteCount := format.ByteCount(VERSION_CURRENT, valueCount, bpv)
    21  				msg := fmt.Sprintf("format=%v, byteCount=%v, valueCount=%v, bpv=%v", format, byteCount, valueCount, bpv)
    22  				if byteCount*8 < int64(valueCount)*int64(bpv) {
    23  					t.Errorf(msg)
    24  				}
    25  				if format == PACKED {
    26  					if (byteCount-1)*8 >= int64(valueCount)*int64(bpv) {
    27  						t.Errorf(msg)
    28  					}
    29  				}
    30  			}
    31  		}
    32  	}
    33  }
    34  
    35  func TestMaxValue(t *testing.T) {
    36  	if MaxValue(0) != 0 {
    37  		t.Error("0 bit -> 0")
    38  	}
    39  	if MaxValue(1) != 1 {
    40  		t.Error("1 bit -> 1")
    41  	}
    42  	if MaxValue(2) != 3 {
    43  		t.Error("2 bits -> 3")
    44  	}
    45  	if MaxValue(64) != 0x7fffffffffffffff {
    46  		t.Error("64 bits -> 0x7fffffffffffffff")
    47  	}
    48  }
    49  
    50  func TestGetPackedIntsDecoder(t *testing.T) {
    51  	decoder := GetPackedIntsDecoder(PackedFormat(PACKED), int32(PACKED_VERSION_START), 1)
    52  	if n := decoder.ByteValueCount(); n != 8 {
    53  		t.Errorf("ByteValueCount() should be 8, instead of %v", n)
    54  	}
    55  }
    56  
    57  func TestUnsignedBitsRequired(t *testing.T) {
    58  	if n := UnsignedBitsRequired(-158146830731166066); n != 64 {
    59  		t.Errorf("-158146830731166066 -> 64bit (got %v)", n)
    60  	}
    61  }