53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package functions
|
|
|
|
import (
|
|
"testing"
|
|
"fmt"
|
|
)
|
|
|
|
// Test fibonacci function
|
|
|
|
func TestFibonacci(t *testing.T) {
|
|
var sequence = [4]int{1, 3, 5, 9}
|
|
var values = [4]int{1, 2, 5, 34}
|
|
for i := 0; i < len(sequence); i++ {
|
|
var seq, want = sequence[i], values[i]
|
|
outcome, error := MyFibonacci(seq)
|
|
if error != nil {
|
|
t.Errorf("Error trying to Fibonacci %d: %s", seq, error)
|
|
}
|
|
if outcome != want {
|
|
t.Errorf("Invalid outcome: Fibonacci sequence %d should be %d, got %d",
|
|
seq, want, outcome)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFibBadNumber(t *testing.T) {
|
|
outcome,error := MyFibonacci(-4)
|
|
if error == nil {
|
|
t.Errorf("This should be an error, not a %d",outcome)
|
|
}
|
|
}
|
|
|
|
func TestSums(t *testing.T) {
|
|
var tests = []struct{
|
|
a, b int
|
|
want int
|
|
} {
|
|
{1, 2, 3},
|
|
{5, 3, 8},
|
|
{9, 4, 13},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
testname := fmt.Sprintf("%d+%d",tt.a, tt.b)
|
|
t.Run(testname, func(t *testing.T) {
|
|
result := sum(tt.a, tt.b)
|
|
if result != tt.want {
|
|
t.Errorf("Expected %d, got %d", tt.want,result)
|
|
}
|
|
})
|
|
}
|
|
}
|