github.com/zeebo/mon@v0.0.0-20211012163247-13d39bdb54fa/internal/bitmap/bitmap_test.go (about)

     1  package bitmap
     2  
     3  import (
     4  	"math"
     5  	"runtime"
     6  	"testing"
     7  )
     8  
     9  func TestBitmap64(t *testing.T) {
    10  	var b B64
    11  
    12  	for i := uint(0); i < 64; i++ {
    13  		b.Set(i)
    14  
    15  		got, ok := b.Next()
    16  		if !ok || got != i {
    17  			t.Fatal(i)
    18  		}
    19  		if b != (B64{}) {
    20  			t.Fatal(b)
    21  		}
    22  	}
    23  }
    24  
    25  func TestBitmap128(t *testing.T) {
    26  	var b B128
    27  
    28  	for i := uint(0); i < 128; i++ {
    29  		b.Set(i)
    30  
    31  		got, ok := b.Next()
    32  		if !ok || got != i {
    33  			t.Fatal(i)
    34  		}
    35  		if b != (B128{}) {
    36  			t.Fatal(b)
    37  		}
    38  	}
    39  }
    40  
    41  func BenchmarkBitmap64(b *testing.B) {
    42  	b.Run("Next", func(b *testing.B) {
    43  		idx := uint(0)
    44  		for i := 0; i < b.N; i++ {
    45  			bm := B64{1}
    46  			idx, _ = bm.Next()
    47  		}
    48  		runtime.KeepAlive(idx)
    49  	})
    50  
    51  	b.Run("NextAll", func(b *testing.B) {
    52  		for i := 0; i < b.N; i++ {
    53  			b := B64{math.MaxUint64}
    54  			for {
    55  				_, ok := b.Next()
    56  				if !ok {
    57  					break
    58  				}
    59  			}
    60  		}
    61  	})
    62  }
    63  
    64  func BenchmarkBitmap128(b *testing.B) {
    65  	b.Run("Next", func(b *testing.B) {
    66  		idx := uint(0)
    67  		for i := 0; i < b.N; i++ {
    68  			bm := B128{1, 0}
    69  			idx, _ = bm.Next()
    70  		}
    71  		runtime.KeepAlive(idx)
    72  	})
    73  
    74  	b.Run("NextAll", func(b *testing.B) {
    75  		for i := 0; i < b.N; i++ {
    76  			b := B128{math.MaxUint64, math.MaxUint64}
    77  			for {
    78  				_, ok := b.Next()
    79  				if !ok {
    80  					break
    81  				}
    82  			}
    83  		}
    84  	})
    85  }