Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in delAll() #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion check/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (check *HealthCheck) CheckHealth(brokerUpdates chan<- Update, clusterUpdate
func newUpdate(report StatusReport, name string) Update {
data, err := report.Json()
if err != nil {
log.Warn("Error while marshaling %s status: %s", name, err.Error())
log.Warnf("Error while marshaling %s status: %s", name, err.Error())
data = simpleStatus(report.Summary())
}
return Update{report.Summary(), data}
Expand Down
23 changes: 5 additions & 18 deletions check/int32_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,11 @@ func contains(a []int32, el int32) bool {
return false
}

func indexOf(a []int32, el int32) (int, bool) {
for i, e := range a {
if e == el {
return i, true
func delAll(a []int32, el int32) (ret []int32) {
for _, ael := range a {
if ael != el {
ret = append(ret, ael)
}
}
return -1, false
}

func delAt(a []int32, i int) []int32 {
copy(a[i:], a[i+1:])
a[len(a)-1] = 0
return a[:len(a)-1]
}

func delAll(a []int32, el int32) []int32 {
for i, ok := indexOf(a, el); ok; {
a = delAt(a, i)
}
return a
return
}
52 changes: 52 additions & 0 deletions check/int32_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package check

import (
"reflect"
"testing"
)

// contains() tests

func Test_Contains_ReturnsFalse(t *testing.T) {
if contains([]int32{1, 2, 3}, 4) == true {
t.Error()
}
}

func Test_Contains_ReturnsTrue(t *testing.T) {
if contains([]int32{1, 2, 3}, 2) == false {
t.Error()
}
}

// delAll() tests

func Test_DelAll_Single(t *testing.T) {
if !reflect.DeepEqual(delAll([]int32{1, 2, 3}, 2), []int32{1, 3}) {
t.Error()
}
}

func Test_DelAll_Multiple(t *testing.T) {
if !reflect.DeepEqual(delAll([]int32{1, 2, 3, 2}, 2), []int32{1, 3}) {
t.Error()
}
}

func Test_DelAll_Missing(t *testing.T) {
if !reflect.DeepEqual(delAll([]int32{1, 2, 3}, 4), []int32{1, 2, 3}) {
t.Error()
}
}

func Test_DelAll_Front(t *testing.T) {
if !reflect.DeepEqual(delAll([]int32{1, 2, 3}, 1), []int32{2, 3}) {
t.Error()
}
}

func Test_DelAll_Back(t *testing.T) {
if !reflect.DeepEqual(delAll([]int32{1, 2, 3}, 3), []int32{1, 2}) {
t.Error()
}
}