go-by-test/clockface/clockface_test.go

146 lines
3.0 KiB
Go
Raw Normal View History

2024-09-23 14:49:29 +00:00
package clockface
import (
"math"
"testing"
"time"
)
2024-09-23 20:31:05 +00:00
func TestHoursInRadians(t *testing.T) {
2024-09-23 20:09:10 +00:00
cases := []struct {
time time.Time
angle float64
}{
2024-09-23 20:31:05 +00:00
{simpleTime(6, 0, 0), math.Pi},
{simpleTime(0, 0, 0), 0},
{simpleTime(21, 0, 0), math.Pi * 1.5},
{simpleTime(0, 1, 30), math.Pi / ((6 * 60 * 60) / 90)},
2024-09-23 20:09:10 +00:00
}
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
2024-09-23 20:31:05 +00:00
got := hoursInRadians(c.time)
2024-09-23 20:09:10 +00:00
2024-09-23 20:31:05 +00:00
if !roughlyEqualFloat64(c.angle, got) {
2024-09-23 20:09:10 +00:00
t.Fatalf("Wanted %v radians, but got %v", c.angle, got)
}
})
}
}
2024-09-23 20:31:05 +00:00
func TestHourHandPoint(t *testing.T) {
cases := []struct {
time time.Time
point Point
}{
{simpleTime(6, 0, 0), Point{0, -1}},
{simpleTime(9, 0, 0), Point{-1, 0}},
}
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
got := hourHandPoint(c.time)
if !roughlyEqualPoint(got, c.point) {
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
}
})
}
}
func TestMinutesInRadians(t *testing.T) {
2024-09-23 14:49:29 +00:00
cases := []struct {
time time.Time
angle float64
}{
2024-09-23 20:31:05 +00:00
{simpleTime(0, 30, 0), math.Pi},
{simpleTime(0, 0, 7), 7 * (math.Pi / (30 * 60))},
2024-09-23 14:49:29 +00:00
}
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
2024-09-23 20:31:05 +00:00
got := minutesInRadians(c.time)
2024-09-23 14:49:29 +00:00
if c.angle != got {
t.Fatalf("Wanted %v radians, but got %v", c.angle, got)
}
})
}
}
2024-09-23 20:09:10 +00:00
func TestMinuteHandPoint(t *testing.T) {
cases := []struct {
time time.Time
point Point
}{
{simpleTime(0, 30, 0), Point{0, -1}},
{simpleTime(0, 45, 0), Point{-1, 0}},
}
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
got := minuteHandPoint(c.time)
if !roughlyEqualPoint(got, c.point) {
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
}
})
}
}
2024-09-23 20:31:05 +00:00
func TestSecondsInRadians(t *testing.T) {
cases := []struct {
time time.Time
angle float64
}{
{simpleTime(0, 0, 30), math.Pi},
{simpleTime(0, 0, 0), 0},
{simpleTime(0, 0, 45), (math.Pi / 2) * 3},
{simpleTime(0, 0, 7), (math.Pi / 30) * 7},
}
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
got := secondsInRadians(c.time)
if c.angle != got {
t.Fatalf("Wanted %v radians, but got %v", c.angle, got)
}
})
}
}
2024-09-23 14:49:29 +00:00
func TestSecondHandPoint(t *testing.T) {
cases := []struct {
time time.Time
point Point
}{
{simpleTime(0, 0, 30), Point{0, -1}},
{simpleTime(0, 0, 45), Point{-1, 0}},
}
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
got := secondHandPoint(c.time)
if !roughlyEqualPoint(got, c.point) {
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
}
})
}
}
func simpleTime(hours, minutes, seconds int) time.Time {
return time.Date(321, time.October, 28, hours, minutes, seconds, 0, time.UTC)
}
func testName(t time.Time) string {
return t.Format("15:04:05")
}
func roughlyEqualFloat64(a, b float64) bool {
const equalityThreshold = 1e-7
return math.Abs(a-b) < equalityThreshold
}
func roughlyEqualPoint(a, b Point) bool {
return roughlyEqualFloat64(a.X, b.X) && roughlyEqualFloat64(a.Y, b.Y)
}