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

     1  package bitfields
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestBitvectorCheck(t *testing.T) {
     9  	cases := []struct {
    10  		v     []byte
    11  		n     uint64
    12  		valid bool
    13  	}{
    14  		{[]byte{}, 0, true},
    15  		{[]byte{0}, 0, false},
    16  		{[]byte{0}, 1, true},
    17  		{[]byte{0}, 8, true},
    18  		{[]byte{0}, 9, false},
    19  		{[]byte{0, 0}, 9, true},
    20  		{[]byte{0, 0, 0}, 16, false},
    21  		{[]byte{0, 0, 0}, 17, true},
    22  		{[]byte{0, 0, 0}, 24, true},
    23  		{[]byte{0, 0, 0, 0, 0, 0}, 48, true},
    24  		{[]byte{0xff}, 8, true},
    25  		{[]byte{0xff}, 7, false},
    26  		{[]byte{0x7f}, 7, true},
    27  		{[]byte{0xff, 0x80}, 16, true},
    28  		{[]byte{0xff, 0x80}, 15, false},
    29  		{[]byte{0xff, 0x40}, 15, 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(fmt.Sprintf("check valid %v", testCase.valid), func(t *testing.T) {
    34  				if err := BitvectorCheck(testCase.v, testCase.n); err != nil && testCase.valid {
    35  					t.Errorf("expected bitvector to be valid but got error: %v", err)
    36  				} else if err == nil && !testCase.valid {
    37  					t.Error("expected bitvector to be invalid but got no error")
    38  				}
    39  			})
    40  		})
    41  	}
    42  }