...

Source file src/github.com/redis/go-redis/v9/bitmap_commands_test.go

Documentation: github.com/redis/go-redis/v9

     1  package redis_test
     2  
     3  import (
     4  	. "github.com/bsm/ginkgo/v2"
     5  	. "github.com/bsm/gomega"
     6  	"github.com/redis/go-redis/v9"
     7  )
     8  
     9  type bitCountExpected struct {
    10  	Start    int64
    11  	End      int64
    12  	Expected int64
    13  }
    14  
    15  var _ = Describe("BitCountBite", func() {
    16  	var client *redis.Client
    17  	key := "bit_count_test"
    18  
    19  	BeforeEach(func() {
    20  		client = redis.NewClient(redisOptions())
    21  		Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
    22  		values := []int{0, 1, 0, 0, 1, 0, 1, 0, 1, 1}
    23  		for i, v := range values {
    24  			cmd := client.SetBit(ctx, key, int64(i), v)
    25  			Expect(cmd.Err()).NotTo(HaveOccurred())
    26  		}
    27  	})
    28  
    29  	AfterEach(func() {
    30  		Expect(client.Close()).NotTo(HaveOccurred())
    31  	})
    32  
    33  	It("bit count bite", func() {
    34  		var expected = []bitCountExpected{
    35  			{0, 0, 0},
    36  			{0, 1, 1},
    37  			{0, 2, 1},
    38  			{0, 3, 1},
    39  			{0, 4, 2},
    40  			{0, 5, 2},
    41  			{0, 6, 3},
    42  			{0, 7, 3},
    43  			{0, 8, 4},
    44  			{0, 9, 5},
    45  		}
    46  
    47  		for _, e := range expected {
    48  			cmd := client.BitCount(ctx, key, &redis.BitCount{Start: e.Start, End: e.End, Unit: redis.BitCountIndexBit})
    49  			Expect(cmd.Err()).NotTo(HaveOccurred())
    50  			Expect(cmd.Val()).To(Equal(e.Expected))
    51  		}
    52  	})
    53  })
    54  
    55  var _ = Describe("BitCountByte", func() {
    56  	var client *redis.Client
    57  	key := "bit_count_test"
    58  
    59  	BeforeEach(func() {
    60  		client = redis.NewClient(redisOptions())
    61  		Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
    62  		values := []int{0, 0, 0, 0, 0, 0, 0, 1, 1, 1}
    63  		for i, v := range values {
    64  			cmd := client.SetBit(ctx, key, int64(i), v)
    65  			Expect(cmd.Err()).NotTo(HaveOccurred())
    66  		}
    67  	})
    68  
    69  	AfterEach(func() {
    70  		Expect(client.Close()).NotTo(HaveOccurred())
    71  	})
    72  
    73  	It("bit count byte", func() {
    74  		var expected = []bitCountExpected{
    75  			{0, 0, 1},
    76  			{0, 1, 3},
    77  		}
    78  
    79  		for _, e := range expected {
    80  			cmd := client.BitCount(ctx, key, &redis.BitCount{Start: e.Start, End: e.End, Unit: redis.BitCountIndexByte})
    81  			Expect(cmd.Err()).NotTo(HaveOccurred())
    82  			Expect(cmd.Val()).To(Equal(e.Expected))
    83  		}
    84  	})
    85  
    86  	It("bit count byte with no unit specified", func() {
    87  		var expected = []bitCountExpected{
    88  			{0, 0, 1},
    89  			{0, 1, 3},
    90  		}
    91  
    92  		for _, e := range expected {
    93  			cmd := client.BitCount(ctx, key, &redis.BitCount{Start: e.Start, End: e.End})
    94  			Expect(cmd.Err()).NotTo(HaveOccurred())
    95  			Expect(cmd.Val()).To(Equal(e.Expected))
    96  		}
    97  	})
    98  })
    99  

View as plain text