Skip to content

Commit

Permalink
Merge branch 'test'
Browse files Browse the repository at this point in the history
  • Loading branch information
kweeuhree committed Jan 19, 2025
2 parents f6686fd + 7e7de83 commit cd5c251
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
18 changes: 10 additions & 8 deletions cmd/utils/calculate-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func (u *Utils) ConcurrentCalculatePoints(retailer, purchaseDate, purchaseTime,
wg.Wait()

// Calculate total points of the receipt
var totalPoints int
for _, point := range points {
totalPoints += point
}
totalPoints := u.sum(points)

return totalPoints, nil
}
Expand All @@ -83,10 +80,7 @@ func (u *Utils) CalculatePoints(retailer, purchaseDate, purchaseTime, total stri
}

// Calculate total points of the receipt
var totalPoints int
for _, point := range points {
totalPoints += point
}
totalPoints := u.sum(points)

return totalPoints, nil
}
Expand Down Expand Up @@ -227,3 +221,11 @@ func (u *Utils) getPurchaseTimePoints(purchaseTime string) int {

return points
}

func (u *Utils) sum(points []int) int {
var totalPoints int
for _, point := range points {
totalPoints += point
}
return totalPoints
}
25 changes: 25 additions & 0 deletions cmd/utils/calculate-utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,28 @@ func Test_getPurchaseTimePoints(t *testing.T) {
})
}
}

func Test_sum(t *testing.T) {
var utils *Utils
tests := []struct {
name string
nums []int
expected int
}{
{"Small ints", []int{1, 2, 3, 4, 5}, 15},
{"Mixed ints", []int{1, 200, 3, 499, 5}, 708},
{"Big ints", []int{1111, 200, 3333, 499, 5555}, 10698},
{"Zero", []int{0}, 0},
}

for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.sum(entry.nums)

if result != entry.expected {
t.Errorf("expected %d, received %d", entry.expected, result)
}

})
}
}

0 comments on commit cd5c251

Please sign in to comment.