github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/bitset/bitset_test.go (about)

     1  package bitset
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  )
     7  
     8  func assertPanic(t *testing.T, f func()) {
     9  	t.Helper()
    10  	defer func() {
    11  		if r := recover(); r == nil {
    12  			t.Error("expected panic, got none")
    13  		}
    14  	}()
    15  	f()
    16  }
    17  
    18  func testBitSetIndexOutOfRange(t *testing.T, s BitSet) {
    19  	index := s.Capacity()
    20  	assertPanic(t, func() { s.IsSet(index) })
    21  	assertPanic(t, func() { s.Set(index) })
    22  	assertPanic(t, func() { s.Unset(index) })
    23  	assertPanic(t, func() { s.Flip(index) })
    24  }
    25  
    26  func assertEmptyBitSet(t *testing.T, s BitSet) {
    27  	t.Helper()
    28  	if count := s.Count(); count != 0 {
    29  		t.Errorf("expected count to be 0, got %d", count)
    30  	}
    31  	for i := range s.Capacity() {
    32  		if s.IsSet(i) {
    33  			t.Errorf("bit %d is unexpectedly set", i)
    34  		}
    35  	}
    36  }
    37  
    38  func assertOddBitSet(t *testing.T, s BitSet) {
    39  	t.Helper()
    40  	if count, expectedCount := s.Count(), s.Capacity()/2; count != expectedCount {
    41  		t.Errorf("expected count to be %d, got %d", expectedCount, count)
    42  	}
    43  	for i := range s.Capacity() {
    44  		if i%2 == 0 == s.IsSet(i) {
    45  			t.Errorf("unexpected bit %d", i)
    46  		}
    47  	}
    48  }
    49  
    50  func assertEvenBitSet(t *testing.T, s BitSet) {
    51  	t.Helper()
    52  	if count, expectedCount := s.Count(), (s.Capacity()+1)/2; count != expectedCount {
    53  		t.Errorf("expected count to be %d, got %d", expectedCount, count)
    54  	}
    55  	for i := range s.Capacity() {
    56  		if i%2 == 1 == s.IsSet(i) {
    57  			t.Errorf("unexpected bit %d", i)
    58  		}
    59  	}
    60  }
    61  
    62  func assertFullBitSet(t *testing.T, s BitSet) {
    63  	t.Helper()
    64  	if count := s.Count(); count != s.Capacity() {
    65  		t.Errorf("expected count to be %d, got %d", s.Capacity(), count)
    66  	}
    67  	for i := range s.Capacity() {
    68  		if !s.IsSet(i) {
    69  			t.Errorf("bit %d is unexpectedly unset", i)
    70  		}
    71  	}
    72  }
    73  
    74  func clearBitSet(t *testing.T, s BitSet) {
    75  	t.Helper()
    76  	for i := range s.Capacity() {
    77  		s.Unset(i)
    78  	}
    79  }
    80  
    81  func fillOddBitSet(t *testing.T, s BitSet) {
    82  	t.Helper()
    83  	for i := range s.Capacity() {
    84  		switch i % 2 {
    85  		case 0:
    86  			s.Unset(i)
    87  		case 1:
    88  			s.Set(i)
    89  		}
    90  	}
    91  }
    92  
    93  func fillEvenBitSet(t *testing.T, s BitSet) {
    94  	t.Helper()
    95  	for i := range s.Capacity() {
    96  		switch i % 2 {
    97  		case 0:
    98  			s.Set(i)
    99  		case 1:
   100  			s.Unset(i)
   101  		}
   102  	}
   103  }
   104  
   105  func fillBitSet(t *testing.T, s BitSet) {
   106  	t.Helper()
   107  	for i := range s.Capacity() {
   108  		s.Set(i)
   109  	}
   110  }
   111  
   112  func testBitSetSetAll(t *testing.T, s BitSet) {
   113  	clearBitSet(t, s)
   114  	s.SetAll()
   115  	assertFullBitSet(t, s)
   116  
   117  	fillOddBitSet(t, s)
   118  	s.SetAll()
   119  	assertFullBitSet(t, s)
   120  
   121  	fillEvenBitSet(t, s)
   122  	s.SetAll()
   123  	assertFullBitSet(t, s)
   124  
   125  	fillBitSet(t, s)
   126  	s.SetAll()
   127  	assertFullBitSet(t, s)
   128  }
   129  
   130  func testBitSetUnsetAll(t *testing.T, s BitSet) {
   131  	clearBitSet(t, s)
   132  	s.UnsetAll()
   133  	assertEmptyBitSet(t, s)
   134  
   135  	fillOddBitSet(t, s)
   136  	s.UnsetAll()
   137  	assertEmptyBitSet(t, s)
   138  
   139  	fillEvenBitSet(t, s)
   140  	s.UnsetAll()
   141  	assertEmptyBitSet(t, s)
   142  
   143  	fillBitSet(t, s)
   144  	s.UnsetAll()
   145  	assertEmptyBitSet(t, s)
   146  }
   147  
   148  func testBitSetFlipAll(t *testing.T, s BitSet) {
   149  	clearBitSet(t, s)
   150  	s.FlipAll()
   151  	assertFullBitSet(t, s)
   152  
   153  	fillOddBitSet(t, s)
   154  	s.FlipAll()
   155  	assertEvenBitSet(t, s)
   156  
   157  	fillEvenBitSet(t, s)
   158  	s.FlipAll()
   159  	assertOddBitSet(t, s)
   160  
   161  	fillBitSet(t, s)
   162  	s.FlipAll()
   163  	assertEmptyBitSet(t, s)
   164  }
   165  
   166  func flipBitSet(t *testing.T, s BitSet) {
   167  	t.Helper()
   168  	for i := range s.Capacity() {
   169  		s.Flip(i)
   170  	}
   171  }
   172  
   173  func testBitSetFlip(t *testing.T, s BitSet) {
   174  	clearBitSet(t, s)
   175  	flipBitSet(t, s)
   176  	assertFullBitSet(t, s)
   177  
   178  	fillOddBitSet(t, s)
   179  	flipBitSet(t, s)
   180  	assertEvenBitSet(t, s)
   181  
   182  	fillEvenBitSet(t, s)
   183  	flipBitSet(t, s)
   184  	assertOddBitSet(t, s)
   185  
   186  	fillBitSet(t, s)
   187  	flipBitSet(t, s)
   188  	assertEmptyBitSet(t, s)
   189  }
   190  
   191  var bitSetTestCapacities = [...]uint{0, 1, 2, 31, 32, 33, 63, 64, 65, 254, 500, 3000}
   192  
   193  func TestBitSet(t *testing.T) {
   194  	for _, capacity := range bitSetTestCapacities {
   195  		t.Run(strconv.FormatUint(uint64(capacity), 10), func(t *testing.T) {
   196  			s := NewBitSet(capacity)
   197  			t.Run("IndexOutOfRange", func(t *testing.T) {
   198  				testBitSetIndexOutOfRange(t, s)
   199  			})
   200  			t.Run("SetAll", func(t *testing.T) {
   201  				testBitSetSetAll(t, s)
   202  			})
   203  			t.Run("UnsetAll", func(t *testing.T) {
   204  				testBitSetUnsetAll(t, s)
   205  			})
   206  			t.Run("FlipAll", func(t *testing.T) {
   207  				testBitSetFlipAll(t, s)
   208  			})
   209  			t.Run("Flip", func(t *testing.T) {
   210  				testBitSetFlip(t, s)
   211  			})
   212  		})
   213  	}
   214  }