github.com/protolambda/zssz@v0.1.5/bitfields/bitlist_test.go (about)

     1  package bitfields
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestBitlistLen(t *testing.T) {
     9  	cases := []struct {
    10  		v     []byte
    11  		n     uint64
    12  		valid bool
    13  	}{
    14  		{[]byte{0}, 0, false},
    15  		{[]byte{0, 0}, 8, false},
    16  		{[]byte{0, 0, 0}, 16, false},
    17  		{[]byte{0xff, 0, 0, 0}, 24, false},
    18  		{[]byte{1, 2, 3, 0xff, 0}, 32, false},
    19  		{[]byte{1}, 0, true},
    20  		{[]byte{2}, 1, true},
    21  		{[]byte{3}, 1, true},
    22  		{[]byte{0x1a}, 4, true},
    23  		{[]byte{0x2b}, 5, true},
    24  		{[]byte{0xab}, 7, true},
    25  		{[]byte{0, 0x9b}, 8 + 7, true},
    26  		{[]byte{0, 0, 0x9b}, 8 + 8 + 7, true},
    27  		{[]byte{0xff, 0xff, 0x9b}, 8 + 8 + 7, true},
    28  		{[]byte{0xff, 0xff, 0x04}, 8 + 8 + 2, true},
    29  		{[]byte{0, 0, 0, 0, 0, 4}, 5*8 + 2, true},
    30  	}
    31  	for _, testCase := range cases {
    32  		t.Run(fmt.Sprintf("v %b (bin) len %d", testCase.v, testCase.n), func(t *testing.T) {
    33  			t.Run("get length", func(t *testing.T) {
    34  				if x := BitlistLen(testCase.v); x != testCase.n {
    35  					t.Errorf("expected bitlist to be of length: %d but got %d", testCase.n, x)
    36  				}
    37  			})
    38  			t.Run(fmt.Sprintf("check valid %v", testCase.valid), func(t *testing.T) {
    39  				if err := BitlistCheck(testCase.v, testCase.n); err != nil && testCase.valid {
    40  					t.Errorf("expected bitlist to be valid but got error: %v", err)
    41  				} else if err == nil && !testCase.valid {
    42  					t.Error("expected bitlist to be invalid but got no error")
    43  				}
    44  			})
    45  		})
    46  	}
    47  }