Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Nov 19, 2024
1 parent 0d46bf1 commit 975fa6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/internal/ebpf/tracer_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func roundToNearestMultiple(x, n uint32) uint32 {
return n
}

if x%n == 0 {
return x
}

return (x + n/2) / n * n
}

Expand Down
32 changes: 32 additions & 0 deletions pkg/internal/ebpf/tracer_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ebpf

import (
"fmt"
"testing"
)

func TestRoundToNearestMultiple(t *testing.T) {
tests := []struct {
x, n, expected uint32
}{
{0, 5, 5}, // x < n, should return n
{3, 5, 5}, // x < n, should return n
{5, 5, 5}, // x == n, should return n (no rounding needed)
{6, 5, 5}, // x > n, should round down
{7, 5, 5}, // x > n, should round down
{12, 5, 10}, // x > n, should round down
{13, 5, 15}, // x > n, should round up
{9, 7, 7}, // x < n, should return n
{10, 7, 7}, // x == n, should return n
{11, 7, 14}, // x > n, should round to the nearest multiple
}

for _, tt := range tests {
t.Run(fmt.Sprintf("x=%d, n=%d", tt.x, tt.n), func(t *testing.T) {
got := roundToNearestMultiple(tt.x, tt.n)
if got != tt.expected {
t.Errorf("roundToNearestMultiple(%d, %d) = %d; want %d", tt.x, tt.n, got, tt.expected)
}
})
}
}

0 comments on commit 975fa6a

Please sign in to comment.