Files
go-learning/exercises/01-core/01-basics/basics_test.go
T

49 lines
1.1 KiB
Go

package basics
import (
"math"
"testing"
)
func TestCelsiusToFahrenheit(t *testing.T) {
if math.Abs(CelsiusToFahrenheit(0)-32) > 1e-9 {
t.Fatalf("expected 32, got %v", CelsiusToFahrenheit(0))
}
if math.Abs(CelsiusToFahrenheit(100)-212) > 1e-9 {
t.Fatalf("expected 212, got %v", CelsiusToFahrenheit(100))
}
}
func TestClassifyScore(t *testing.T) {
cases := []struct {
score int
want string
}{
{0, "fail"}, {50, "pass"}, {80, "good"}, {95, "excellent"}, {101, "invalid"}, {-1, "invalid"},
}
for _, tc := range cases {
if got := ClassifyScore(tc.score); got != tc.want {
t.Errorf("ClassifyScore(%d) = %q, want %q", tc.score, got, tc.want)
}
}
}
func TestSumUpTo(t *testing.T) {
if got := SumUpTo(0); got != 0 {
t.Errorf("got %d, want 0", got)
}
if got := SumUpTo(10); got != 55 {
t.Errorf("got %d, want 55", got)
}
}
func TestDivMod(t *testing.T) {
q, r, ok := DivMod(10, 3)
if !ok || q != 3 || r != 1 {
t.Fatalf("got (%d, %d, %v), want (3, 1, true)", q, r, ok)
}
if _, _, ok := DivMod(10, 0); ok {
t.Fatal("expected ok=false for division by zero")
}
}