94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
|
package arraysum
|
||
|
|
||
|
import "testing"
|
||
|
|
||
|
var testCases = []struct {
|
||
|
input []int
|
||
|
expected int
|
||
|
}{
|
||
|
{
|
||
|
input: []int{1, 2, 3},
|
||
|
expected: 6,
|
||
|
},
|
||
|
{
|
||
|
input: []int{0, 0, 0, 0},
|
||
|
expected: 0,
|
||
|
},
|
||
|
{
|
||
|
input: []int{64, 1, -294, 5, 79, 9, 103, 15, 10, 41, 1, 0, -2},
|
||
|
expected: 32,
|
||
|
},
|
||
|
{
|
||
|
input: []int{-10238, 89, 73204, -13983, -78338, 5, 1897},
|
||
|
expected: -27364,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var gout int
|
||
|
|
||
|
// TestSumWithForLoop tests the correctness of SumWithForLoop
|
||
|
func TestSumWithForLoop(t *testing.T) {
|
||
|
for _, tc := range testCases {
|
||
|
actual := SumWithForLoop(tc.input)
|
||
|
if actual != tc.expected {
|
||
|
t.Errorf("Unexpected output received: actual: %d, expected: %d", actual, tc.expected)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TestSumWithRecursion tests the correctness of SumWithRecursion
|
||
|
func TestSumWithRecursion(t *testing.T) {
|
||
|
for _, tc := range testCases {
|
||
|
actual := SumWithRecursion(tc.input)
|
||
|
if actual != tc.expected {
|
||
|
t.Errorf("Unexpected output received: actual: %d, expected: %d", actual, tc.expected)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TestSumWithRecursion tests the correctness of SumWithTailRecursion
|
||
|
func TestSumWithTailRecursion(t *testing.T) {
|
||
|
for _, tc := range testCases {
|
||
|
actual := SumWithTailRecursion(tc.input)
|
||
|
if actual != tc.expected {
|
||
|
t.Errorf("Unexpected output received: actual: %d, expected: %d", actual, tc.expected)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// BenchmarkSumWithForLoop tests the performance of the SumWithForLoop function.
|
||
|
func BenchmarkSumWithForLoop(b *testing.B) {
|
||
|
var out int
|
||
|
var benchmarkCase = []int{-485, 327, -865, 858, -58, 893, 74, 345, -467, 632, -557, -728, 749, 634, 451, -670, 799, 300, 115, 755}
|
||
|
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
out = SumWithForLoop(benchmarkCase)
|
||
|
}
|
||
|
|
||
|
gout = out
|
||
|
}
|
||
|
|
||
|
// BenchmarkSumWithRecursion tests the performance of the SumWithRecursion function.
|
||
|
func BenchmarkSumWithRecursion(b *testing.B) {
|
||
|
var out int
|
||
|
var benchmarkCase = []int{-485, 327, -865, 858, -58, 893, 74, 345, -467, 632, -557, -728, 749, 634, 451, -670, 799, 300, 115, 755}
|
||
|
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
out = SumWithRecursion(benchmarkCase)
|
||
|
}
|
||
|
|
||
|
gout = out
|
||
|
}
|
||
|
|
||
|
// BenchmarkSumWithTailRecursion tests the performance of the SumWithTailRecursion function.
|
||
|
func BenchmarkSumWithTailRecursion(b *testing.B) {
|
||
|
var out int
|
||
|
var benchmarkCase = []int{-485, 327, -865, 858, -58, 893, 74, 345, -467, 632, -557, -728, 749, 634, 451, -670, 799, 300, 115, 755}
|
||
|
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
out = SumWithTailRecursion(benchmarkCase)
|
||
|
}
|
||
|
|
||
|
gout = out
|
||
|
}
|