github.com/ethersphere/bee/v2@v2.2.0/pkg/bitvector/bitvector_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bitvector
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  )
    23  
    24  // TestBitvectorNew checks that enforcements of argument length works in the constructors
    25  func TestBitvectorNew(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	_, err := New(0)
    29  	if !errors.Is(err, errInvalidLength) {
    30  		t.Errorf("expected err %v, got %v", errInvalidLength, err)
    31  	}
    32  
    33  	_, err = NewFromBytes(nil, 0)
    34  	if !errors.Is(err, errInvalidLength) {
    35  		t.Errorf("expected err %v, got %v", errInvalidLength, err)
    36  	}
    37  
    38  	_, err = NewFromBytes([]byte{0}, 9)
    39  	if !errors.Is(err, errInvalidLength) {
    40  		t.Errorf("expected err %v, got %v", errInvalidLength, err)
    41  	}
    42  
    43  	_, err = NewFromBytes(make([]byte, 8), 8)
    44  	if err != nil {
    45  		t.Error(err)
    46  	}
    47  }
    48  
    49  // TestBitvectorGetSet tests correctness of individual Set and Get commands
    50  func TestBitvectorGetSet(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	for _, length := range []int{
    54  		1,
    55  		2,
    56  		4,
    57  		8,
    58  		9,
    59  		15,
    60  		16,
    61  	} {
    62  		bv, err := New(length)
    63  		if err != nil {
    64  			t.Errorf("error for length %v: %v", length, err)
    65  		}
    66  
    67  		for i := 0; i < length; i++ {
    68  			if bv.Get(i) {
    69  				t.Errorf("expected false for element on index %v", i)
    70  			}
    71  		}
    72  
    73  		func() {
    74  			defer func() {
    75  				if err := recover(); err == nil {
    76  					t.Error("expecting panic")
    77  				}
    78  			}()
    79  			bv.Get(length + 8)
    80  		}()
    81  
    82  		for i := 0; i < length; i++ {
    83  			bv.Set(i)
    84  			for j := 0; j < length; j++ {
    85  				if j == i {
    86  					if !bv.Get(j) {
    87  						t.Errorf("element on index %v is not set to true", i)
    88  					}
    89  				}
    90  			}
    91  		}
    92  	}
    93  }
    94  
    95  // TestBitvectorNewFromBytesGet tests that bit vector is initialized correctly from underlying byte slice
    96  func TestBitvectorNewFromBytesGet(t *testing.T) {
    97  	t.Parallel()
    98  
    99  	bv, err := NewFromBytes([]byte{8}, 8)
   100  	if err != nil {
   101  		t.Error(err)
   102  	}
   103  	if !bv.Get(3) {
   104  		t.Fatalf("element 3 is not set to true: state %08b", bv.b[0])
   105  	}
   106  }