Added unit tests and workflow call for same

This commit is contained in:
2025-07-24 11:37:38 +02:00
parent 5d8d0050cd
commit 7564c9fb3a
7 changed files with 115 additions and 20 deletions

View File

@@ -1,9 +1,18 @@
package functions
func MyFibonacci(count int) (fibonaccivalue int) {
import(
"errors"
"fmt"
)
func MyFibonacci(count int) (fibonaccivalue int, error error) {
fibonaccivalue = 1
error = nil
var prev_fibonacci int = 0
if count < 1 {
return -1, errors.New(fmt.Sprintf("%d is not a positive integer.",count))
}
for i := 1; i < count; i++ {
fibonaccivalue += prev_fibonacci
prev_fibonacci = fibonaccivalue - prev_fibonacci

15
functions/flagcheck.go Normal file
View File

@@ -0,0 +1,15 @@
package functions
import (
"flag"
)
func FlagSet(name string) (found bool) {
found = false
flag.Visit(func (f *flag.Flag) {
if f.Name == name {
found = true
}
})
return
}

View File

@@ -0,0 +1,52 @@
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)
}
})
}
}

View File

@@ -46,8 +46,14 @@ func HttFibonacci(w http.ResponseWriter, request *http.Request) {
} else {
fmt.Printf("Received request for Fibonacci Sequence Number %d\n",
seq)
value, error := MyFibonacci(seq)
if error != nil {
fmt.Printf("Error trying to Fibonacci %d: %s", seq, error)
fmt.Fprintf(w, "Error trying to Fibonacci %d: %s", seq, error)
} else {
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
seq, MyFibonacci(seq))
seq, value)
}
}
}