github.com/hdt3213/godis@v1.2.9/datastruct/bitmap/bitmap_test.go (about)

     1  package bitmap
     2  
     3  import (
     4  	"bytes"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  func TestSetBit(t *testing.T) {
    10  	// generate distinct rand offsets
    11  	size := 1000
    12  	offsets := make([]int64, size)
    13  	for i := 0; i < size; i++ {
    14  		offsets[i] = int64(i)
    15  	}
    16  	rand.Shuffle(size, func(i, j int) {
    17  		offsets[i], offsets[j] = offsets[j], offsets[i]
    18  	})
    19  	offsets = offsets[0 : size/5]
    20  	// set bit
    21  	offsetMap := make(map[int64]struct{})
    22  	bm := New()
    23  	for _, offset := range offsets {
    24  		offsetMap[offset] = struct{}{}
    25  		bm.SetBit(offset, 1)
    26  	}
    27  	// get bit
    28  	for i := 0; i < bm.BitSize(); i++ {
    29  		offset := int64(i)
    30  		_, expect := offsetMap[offset]
    31  		actual := bm.GetBit(offset) > 0
    32  		if expect != actual {
    33  			t.Errorf("wrong value at %d", offset)
    34  		}
    35  	}
    36  
    37  	bm2 := New()
    38  	bm2.SetBit(15, 1)
    39  	if bm2.GetBit(15) != 1 {
    40  		t.Error("wrong value")
    41  	}
    42  	if bm2.GetBit(16) != 0 {
    43  		t.Error("wrong value")
    44  	}
    45  }
    46  
    47  func TestFromBytes(t *testing.T) {
    48  	bs := []byte{0xff, 0xff}
    49  	bm := FromBytes(bs)
    50  	bm.SetBit(8, 0)
    51  	expect := []byte{0xff, 0xfe}
    52  	if !bytes.Equal(bs, expect) {
    53  		t.Error("wrong value")
    54  	}
    55  	ret := bm.ToBytes()
    56  	if !bytes.Equal(ret, expect) {
    57  		t.Error("wrong value")
    58  	}
    59  }
    60  
    61  func TestForEachBit(t *testing.T) {
    62  	bm := New()
    63  	for i := 0; i < 1000; i++ {
    64  		if i%2 == 0 {
    65  			bm.SetBit(int64(i), 1)
    66  		}
    67  	}
    68  	expectOffset := int64(100)
    69  	count := 0
    70  	bm.ForEachBit(100, 201, func(offset int64, val byte) bool {
    71  		if offset != expectOffset {
    72  			t.Error("wrong offset")
    73  		}
    74  		expectOffset++
    75  		if offset%2 == 0 && val == 0 {
    76  			t.Error("expect 1")
    77  		}
    78  		count++
    79  		return true
    80  	})
    81  	if count != 101 {
    82  		t.Error("wrong count")
    83  	}
    84  	bm = New()
    85  	size := 1000
    86  	offsets := make([]int64, size)
    87  	for i := 0; i < size; i++ {
    88  		offsets[i] = int64(i)
    89  	}
    90  	rand.Shuffle(size, func(i, j int) {
    91  		offsets[i], offsets[j] = offsets[j], offsets[i]
    92  	})
    93  	offsets = offsets[0 : size/5]
    94  	offsetMap := make(map[int64]struct{})
    95  	for _, offset := range offsets {
    96  		bm.SetBit(offset, 1)
    97  		offsetMap[offset] = struct{}{}
    98  	}
    99  	bm.ForEachBit(int64(size/20+1), 0, func(offset int64, val byte) bool {
   100  		_, expect := offsetMap[offset]
   101  		actual := bm.GetBit(offset) > 0
   102  		if expect != actual {
   103  			t.Errorf("wrong value at %d", offset)
   104  		}
   105  		return true
   106  	})
   107  	count = 0
   108  	bm.ForEachBit(0, 0, func(offset int64, val byte) bool {
   109  		count++
   110  		return false
   111  	})
   112  	if count != 1 {
   113  		t.Error("break failed")
   114  	}
   115  }
   116  
   117  func TestBitMap_ForEachByte(t *testing.T) {
   118  	bm := New()
   119  	for i := 0; i < 1000; i++ {
   120  		if i%16 == 0 {
   121  			bm.SetBit(int64(i), 1)
   122  		}
   123  	}
   124  	bm.ForEachByte(0, 0, func(offset int64, val byte) bool {
   125  		if offset%2 == 0 {
   126  			if val != 1 {
   127  				t.Error("wrong value")
   128  			}
   129  		} else {
   130  			if val != 0 {
   131  				t.Error("wrong value")
   132  			}
   133  		}
   134  		return true
   135  	})
   136  	bm.ForEachByte(0, 2000, func(offset int64, val byte) bool {
   137  		if offset%2 == 0 {
   138  			if val != 1 {
   139  				t.Error("wrong value")
   140  			}
   141  		} else {
   142  			if val != 0 {
   143  				t.Error("wrong value")
   144  			}
   145  		}
   146  		return true
   147  	})
   148  	bm.ForEachByte(0, 500, func(offset int64, val byte) bool {
   149  		if offset%2 == 0 {
   150  			if val != 1 {
   151  				t.Error("wrong value")
   152  			}
   153  		} else {
   154  			if val != 0 {
   155  				t.Error("wrong value")
   156  			}
   157  		}
   158  		return true
   159  	})
   160  	count := 0
   161  	bm.ForEachByte(0, 0, func(offset int64, val byte) bool {
   162  		count++
   163  		return false
   164  	})
   165  	if count != 1 {
   166  		t.Error("break failed")
   167  	}
   168  }