github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/subnet/bitfield256_test.go (about)

     1  package subnet
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestByteSet_Add(t *testing.T) {
    10  	x := *fullSet
    11  	x.SetBit(1)
    12  	assert.True(t, x.Equals(fullSet))
    13  
    14  	x = *emptySet
    15  	x.SetBit(1)
    16  	assert.False(t, x.Equals(emptySet))
    17  	assert.True(t, x.GetBit(1))
    18  	assert.False(t, x.GetBit(233))
    19  	x.SetBit(233)
    20  	assert.True(t, x.GetBit(233))
    21  	assert.Equal(t, 2, x.OnesCount())
    22  }
    23  
    24  func TestByteSet_Remove(t *testing.T) {
    25  	x := *fullSet
    26  	x.ClearBit(255)
    27  	assert.Equal(t, 255, x.OnesCount())
    28  	x.ClearBit(0)
    29  	assert.Equal(t, 254, x.OnesCount())
    30  }
    31  
    32  func TestByteSet_Mask(t *testing.T) {
    33  	bytes00To0F := &Bitfield256{}
    34  	for i := 0; i < 0xf; i++ {
    35  		bytes00To0F.SetBit(byte(i))
    36  	}
    37  	bytesF0ToFF := &Bitfield256{}
    38  	for i := 0xf0; i < 0xff; i++ {
    39  		bytesF0ToFF.SetBit(byte(i))
    40  	}
    41  	tests := []struct {
    42  		name      string
    43  		set       *Bitfield256
    44  		wantOnes  int
    45  		wantValue byte
    46  	}{
    47  		{
    48  			"full set",
    49  			fullSet,
    50  			0,
    51  			0,
    52  		},
    53  		{
    54  			"empty set",
    55  			emptySet,
    56  			8,
    57  			0,
    58  		},
    59  		{
    60  			"00 to 0f",
    61  			bytes00To0F,
    62  			4,
    63  			0,
    64  		},
    65  		{
    66  			"f0 to ff",
    67  			bytesF0ToFF,
    68  			4,
    69  			0xf0,
    70  		},
    71  	}
    72  
    73  	for _, tt := range tests {
    74  		tt := tt
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			gotOnes, gotValue := tt.set.Mask()
    77  			if gotOnes != tt.wantOnes {
    78  				t.Errorf("Mask() gotOnes = %v, want %v", gotOnes, tt.wantOnes)
    79  			}
    80  			if gotValue != tt.wantValue {
    81  				t.Errorf("Mask() gotValue = %v, want %v", gotValue, tt.wantValue)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  var (
    88  	emptySet = &Bitfield256{}
    89  	fullSet  = &Bitfield256{0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}
    90  )
    91  
    92  func TestByteSet_String(t *testing.T) {
    93  	tests := []struct {
    94  		name string
    95  		set  *Bitfield256
    96  		want string
    97  	}{
    98  		{
    99  			"full set",
   100  			fullSet,
   101  			"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   102  		},
   103  		{
   104  			"empty set",
   105  			emptySet,
   106  			"0000000000000000000000000000000000000000000000000000000000000000",
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		tt := tt
   111  		t.Run(tt.name, func(t *testing.T) {
   112  			if got := tt.set.String(); got != tt.want {
   113  				t.Errorf("String() = %v, want %v", got, tt.want)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func TestByteSet_ToSlice(t *testing.T) {
   120  	s := emptySet.ToSlice()
   121  	assert.Equal(t, 0, len(s))
   122  	s = fullSet.ToSlice()
   123  	assert.Equal(t, 256, len(s))
   124  	for i := 0; i < 256; i++ {
   125  		assert.Equal(t, byte(i), s[i])
   126  	}
   127  }