Skip to content

Commit

Permalink
feat: add tests for internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
agrmohit committed Dec 2, 2024
1 parent a0c0f46 commit fe92fe1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
6 changes: 0 additions & 6 deletions internal/inputs/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,5 @@ func ExtractIntPairs(input string) ([]int, []int, error) {
right = append(right, rightNum)
}

// Check whether the number of elements in left and right slices are the same
// This check is probably not needed
if len(left) != len(right) {
return nil, nil, fmt.Errorf("Length of left and right slices differ: %d, %d", len(left), len(right))
}

return left, right, nil
}
55 changes: 55 additions & 0 deletions internal/inputs/inputs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package inputs

import (
"slices"
"testing"
)

func TestExtractIntPairs(t *testing.T) {
t.Run("empty input", func(t *testing.T) {
input := ``
_, _, err := ExtractIntPairs(input)

if err == nil {
t.Errorf("expected an error, didnt get one")
}
})

t.Run("invalid line", func(t *testing.T) {
input := `123 456 789`
_, _, err := ExtractIntPairs(input)

if err == nil {
t.Errorf("expected an error, didnt get one")
}
})

t.Run("invalid left column", func(t *testing.T) {
input := `abc 123`
_, _, err := ExtractIntPairs(input)

if err == nil {
t.Errorf("expected an error, didnt get one")
}
})

t.Run("invalid right column", func(t *testing.T) {
input := `123 abc`
_, _, err := ExtractIntPairs(input)

if err == nil {
t.Errorf("expected an error, didnt get one")
}
})

t.Run("test slice content", func(t *testing.T) {
input := "111 222\n333 444"
leftWant := []int{111, 333}
rightWant := []int{222, 444}
leftGot, rightGot, _ := ExtractIntPairs(input)

if !slices.Equal(leftGot, leftWant) || !slices.Equal(rightGot, rightWant) {
t.Errorf("Expected %v, %v, got %v, %v", leftWant, leftGot, rightWant, rightGot)
}
})
}
25 changes: 25 additions & 0 deletions internal/math/math_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package math

import "testing"

func TestAbs(t *testing.T) {
tests := []struct {
name string
input int
want int
}{
{name: "positive number", input: 7, want: 7},
{name: "negative number", input: -5, want: 5},
{name: "zero", input: 0, want: 0},
{name: "minus zero", input: -0, want: 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Abs(tt.input)
if got != tt.want {
t.Errorf("got %d, want %d", got, tt.want)
}
})
}
}

0 comments on commit fe92fe1

Please sign in to comment.