Skip to content

Commit

Permalink
rearranging tests and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitbbhayani committed Nov 4, 2022
1 parent 624f802 commit e61ddf1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 31 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ Because Dice speaks Redis' dialect, you can connect to it with any Redis Client
To run all the unit tests fire the following command

```sh
$ go test github.com/dicedb/dice/core
$ go test github.com/dicedb/dice/tests
$ go test ./...
```

### Running a single test

```sh
$ go test -timeout 30s -run <pattern> <package path>
$ go test -timeout 30s -run ^TestByteList$ github.com/dicedb/dice/core
$ go test -timeout 30s -run ^TestByteList$ ./...
```

## Running Benchmark
Expand Down
2 changes: 1 addition & 1 deletion core/bytelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewByteList(bufLen int) *byteList {

func (b *byteList) NewNode() *byteListNode {
return &byteListNode{
buf: make([]byte, b.bufLen),
buf: make([]byte, 0, b.bufLen),
}
}

Expand Down
4 changes: 1 addition & 3 deletions core/bytelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func newNode(bl *byteList, b byte) *byteListNode {
bn := bl.NewNode()
bn.buf[0] = b
bn.buf = append(bn.buf, b)
return bn
}

Expand Down Expand Up @@ -63,8 +63,6 @@ func TestByteList(t *testing.T) {
r := toByteArray(bl)
if !bytes.Equal(r, tc.val) {
t.Errorf("bytelist test failed. should have been %v but found %v", tc.val, r)
} else {
t.Logf("pass for %v", tc.val)
}
}
}
2 changes: 1 addition & 1 deletion tests/resp_test.go → core/resp_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package core_test

import (
"bytes"
Expand Down
21 changes: 0 additions & 21 deletions core/xencoding/byte_utils.go

This file was deleted.

22 changes: 21 additions & 1 deletion core/xencoding/int.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package xencoding

var BITMASK = []byte{
0x01,
0x03,
0x07,
0x0F,
0x1F,
0x3F,
0x7F,
0xFF,
}

// getLSB returns the least significant `n` bits from
// the byte value `x`.
func getLSB(x byte, n uint8) byte {
if n > 8 {
panic("can extract at max 8 bits from the number")
}
return byte(x) & BITMASK[n-1]
}

// TOOD: not thread safe
var buf [11]byte
var bitShifts []uint8 = []uint8{7, 7, 7, 7, 7, 7, 7, 7, 7, 1}
Expand All @@ -15,7 +35,7 @@ func XEncodeUInt(x uint64) []byte {
break
}
}
buf[len(buf)-1] = buf[len(buf)-1] & 0b01111111 // marking the termination bit
buf[i] = buf[i] & 0b01111111 // marking the termination bit
return append(make([]byte, 0, i+1), buf[:i+1]...)
}

Expand Down
18 changes: 17 additions & 1 deletion tests/xencoding_int_test.go → core/xencoding/int_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tests
package xencoding_test

import (
"testing"

"github.com/dicedb/dice/core/xencoding"
"github.com/dicedb/dice/testutils"
)

func TestXencodingInt(t *testing.T) {
Expand Down Expand Up @@ -33,4 +34,19 @@ func TestXencodingInt(t *testing.T) {
t.Errorf("xencoding for integer value %d failed. encoded length should be: %d, but found %d\n", k, v, len(b))
}
}

for k, v := range map[int][]byte{
1: {0b00000001},
2: {0b00000010},
127: {0b01111111},
128: {0b10000000, 0b00000001},
129: {0b10000001, 0b00000001},
130: {0b10000010, 0b00000001},
131: {0b10000011, 0b00000001},
} {
obs := xencoding.XEncodeUInt(uint64(k))
if !testutils.EqualByteSlice(obs, v) {
t.Errorf("xencoding for integer value %d failed. should be: %d, but found %d\n", k, v, obs)
}
}
}
15 changes: 15 additions & 0 deletions testutils/byteslice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutils

func EqualByteSlice(a []byte, b []byte) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}

return true
}
15 changes: 15 additions & 0 deletions testutils/intslice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutils

func EqualInt64Slice(a []int64, b []int64) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}

return true
}

0 comments on commit e61ddf1

Please sign in to comment.