summaryrefslogtreecommitdiffstats
path: root/pkg/utils
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-06-13 11:01:26 +1000
committerJesse Duffield <jessedduffield@gmail.com>2022-08-06 13:49:11 +1000
commit524bf83a4a681408c3fb57818f6968cab632e0ae (patch)
tree8858b4ee8d4670dcdd1637fe5fedf00ff080c154 /pkg/utils
parent6dfef08efc5c7f262194c0af35fd777428f33a1a (diff)
refactor to only have one context per view
Diffstat (limited to 'pkg/utils')
-rw-r--r--pkg/utils/utils.go4
-rw-r--r--pkg/utils/utils_test.go42
2 files changed, 46 insertions, 0 deletions
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go
index 2b671ef94..3f882c2b5 100644
--- a/pkg/utils/utils.go
+++ b/pkg/utils/utils.go
@@ -75,6 +75,10 @@ func AsJson(i interface{}) string {
// used to keep a number n between 0 and max, allowing for wraparounds
func ModuloWithWrap(n, max int) int {
+ if max == 0 {
+ return 0
+ }
+
if n >= max {
return n % max
} else if n < 0 {
diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go
index 02aded559..4933cf073 100644
--- a/pkg/utils/utils_test.go
+++ b/pkg/utils/utils_test.go
@@ -87,3 +87,45 @@ func TestSafeTruncate(t *testing.T) {
assert.EqualValues(t, s.expected, SafeTruncate(s.str, s.limit))
}
}
+
+func TestModuloWithWrap(t *testing.T) {
+ type scenario struct {
+ n int
+ max int
+ expected int
+ }
+
+ scenarios := []scenario{
+ {
+ n: 0,
+ max: 0,
+ expected: 0,
+ },
+ {
+ n: 0,
+ max: 1,
+ expected: 0,
+ },
+ {
+ n: 1,
+ max: 0,
+ expected: 0,
+ },
+ {
+ n: 3,
+ max: 2,
+ expected: 1,
+ },
+ {
+ n: -1,
+ max: 2,
+ expected: 1,
+ },
+ }
+
+ for _, s := range scenarios {
+ if s.expected != ModuloWithWrap(s.n, s.max) {
+ t.Errorf("expected %d, got %d, for n: %d, max: %d", s.expected, ModuloWithWrap(s.n, s.max), s.n, s.max)
+ }
+ }
+}