Skip to content

Commit

Permalink
increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chippyash committed Sep 29, 2023
1 parent bb698c6 commit 4213683
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ The `bit` that was missing was the ability to compare bitmaps.

### Utility functions

- func Compare(a, b bitmap.Bitmap) (int, error)
- func Compare(a, b bitmap.Bitmap) int
- func RangeAll(src bitmap.Bitmap, fn func(k uint32, v bool))
- func Trim(a bitmap.Bitmap, bitLen uint32) bitmap.Bitmap

### An example of what you can do
My first use for Bitmap was to categorise team games by the players in the team, the objective being to ensure that I
hadn't gotten the same players playing together too often. That's where I hit a roadblock with Bitmap, it had no comparator.
In a sense, this library is a response to that, and in particular the Xnor and associated Compare function. But once down
the rabbit hole, off we must go!
In a sense, this library is a response to that. But once down the rabbit hole, off we must go!

`examples/full_adder/main.go` is a purely contrived attempt to produce a bitmap oriented electronic circuit as described in
https://en.wikipedia.org/wiki/Adder_(electronics) It has no functional reason to exist, as there are far better ways to do
Expand Down
8 changes: 4 additions & 4 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func ExCompare() {
a = bitmap.Bitmap{0x00}
b = bitmap.Bitmap{0xff}
fmt.Printf("a=%08b, b=%08b\n", a[0], b[0])
r, _ := utils.Compare(a, a)
r := utils.Compare(a, a)
fmt.Printf("Compare(%08b, %08b) => %d\n", a[0], a[0], r)
r, _ = utils.Compare(a, b)
r = utils.Compare(a, b)
fmt.Printf("Compare(%08b, %08b) => %d\n", a[0], b[0], r)
r, _ = utils.Compare(b, a)
r = utils.Compare(b, a)
fmt.Printf("Compare(%08b, %08b) => %d\n", b[0], a[0], r)
r, _ = utils.Compare(b, b)
r = utils.Compare(b, b)
fmt.Printf("Compare(%08b, %08b) => %d\n", b[0], b[0], r)
}

Expand Down
8 changes: 4 additions & 4 deletions utils/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func Compare(a, b bitmap.Bitmap) int {
if a != nil && b == nil {
return 1
}
//empty bitmaps that contain no 1s
if a.Count() == 0 && b.Count() == 0 {
return 0
}
//empty bitmaps that contain no length
if len(a) == 0 && len(b) == 0 {
return 0
}
//empty bitmaps that contain no 1s
if a.Count() == 0 && b.Count() == 0 {
return 0
}
//one empty bitmap
if len(b) == 0 && len(a) != 0 && a.Count() > 1 {
return 1
Expand Down
32 changes: 32 additions & 0 deletions utils/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ func TestCompare_Two_Nil_Bitmaps_Are_Equal(t *testing.T) {
assert.Equal(t, 0, res, "a: %016b b: %016b", a, b)
}

func TestCompare_One_Nil_Bitmaps_Are_Unequal(t *testing.T) {
a := bitmap.Bitmap{}
var b bitmap.Bitmap
res := utils.Compare(a, b)
assert.Equal(t, 1, res, "a: %016b b: %016b", a, b)

res = utils.Compare(b, a)
assert.Equal(t, -1, res, "a: %016b b: %016b", a, b)

}

func TestCompare_Two_Empty_Bitmaps_Are_Equal(t *testing.T) {
//two empty bitmaps
a, b := bitmap.Bitmap{}, bitmap.Bitmap{}
Expand All @@ -29,6 +40,11 @@ func TestCompare_Two_Empty_Bitmaps_Are_Equal(t *testing.T) {
a.Grow(16)
res = utils.Compare(a, b)
assert.Equal(t, 0, res, "a: %016b b: %016b", a, b)

//two bitmaps with no bits set
b.Grow(16)
res = utils.Compare(a, b)
assert.Equal(t, 0, res, "a: %016b b: %016b", a, b)
}

func TestCompare_One_Empty_Bitmaps_Are_Not_Equal(t *testing.T) {
Expand Down Expand Up @@ -77,6 +93,22 @@ func TestCompare_B_Is_Bigger_Than_A_MultiBits(t *testing.T) {
assert.Equal(t, -1, res, "a: %016b b: %016b", a, b)
}

func TestCompare_One_Empty_And_One_NonEmpty_Bitmap_Are_Not_Equal(t *testing.T) {
a, b := bitmap.Bitmap{}, bitmap.Bitmap{0x01}
res := utils.Compare(a, b)
assert.Equal(t, -1, res, "a: %016b b: %016b", a, b)
res = utils.Compare(b, a)
assert.Equal(t, 1, res, "a: %016b b: %016b", b, a)
}

func TestCompare_Will_Work_With_Unequal_Length_Bitmaps(t *testing.T) {
a, b := bitmap.Bitmap{0x44, 0xFF}, bitmap.Bitmap{0x44}
res := utils.Compare(a, b)
assert.Equal(t, 1, res, "a: %064b b: %064b", a, b)
res = utils.Compare(b, a)
assert.Equal(t, -1, res, "a: %064b b: %064b", b, a)
}

func FuzzCompare(f *testing.F) {
f.Add(uint64(0xFF), uint64(0xFF))
f.Fuzz(func(t *testing.T, a, b uint64) {
Expand Down

0 comments on commit 4213683

Please sign in to comment.