Skip to content

Commit e20950c

Browse files
committed
✨ (clockface): add secondsInRadians implementation
1 parent af79757 commit e20950c

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

clockface/clockface.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,13 @@ func SecondHand(t time.Time) Point {
1818
}
1919

2020
func secondsInRadians(t time.Time) float64 {
21-
return math.Pi
21+
return (math.Pi / (30 / (float64(t.Second()))))
22+
}
23+
24+
func simpleTime(hours, minutes, seconds int) time.Time {
25+
return time.Date(312, time.October, 28, hours, minutes, seconds, 0, time.UTC)
26+
}
27+
28+
func testName(t time.Time) string {
29+
return t.Format("15:04:05")
2230
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package clockface_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface"
8+
)
9+
10+
func TestSecondHandAtMidnight(t *testing.T) {
11+
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
12+
13+
want := clockface.Point{X: 150, Y: 150 - 90}
14+
got := clockface.SecondHand(tm)
15+
16+
if got != want {
17+
t.Errorf("Got %v, wanted %v", got, want)
18+
}
19+
}
20+
21+
// func TestSecondHandAt30Seconds(t *testing.T) {
22+
// tm := time.Date(1337, time.January, 1, 0, 0, 30, 0, time.UTC)
23+
24+
// want := clockface.Point{X: 150, Y: 150 + 90}
25+
// got := clockface.SecondHand(tm)
26+
27+
// if got != want {
28+
// t.Errorf("got %v, wanted %v", got, want)
29+
// }
30+
// }

clockface/clockface_test.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,23 @@ import (
66
"time"
77
)
88

9-
func TestSecondHandAtMidnight(t *testing.T) {
10-
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
11-
12-
want := Point{X: 150, Y: 150 - 90}
13-
got := SecondHand(tm)
14-
15-
if got != want {
16-
t.Errorf("Got %v, wanted %v", got, want)
17-
}
18-
}
19-
20-
// func TestSecondHandAt30Seconds(t *testing.T) {
21-
// tm := time.Date(1337, time.January, 1, 0, 0, 30, 0, time.UTC)
22-
23-
// want := clockface.Point{X: 150, Y: 150 + 90}
24-
// got := clockface.SecondHand(tm)
25-
26-
// if got != want {
27-
// t.Errorf("got %v, wanted %v", got, want)
28-
// }
29-
// }
30-
319
func TestSecondsInRadians(t *testing.T) {
32-
thirtySeconds := time.Date(312, time.October, 28, 0, 0, 30, 0, time.UTC)
33-
want := math.Pi
34-
got := secondsInRadians(thirtySeconds)
10+
cases := []struct {
11+
time time.Time
12+
angle float64
13+
}{
14+
{simpleTime(0, 0, 30), math.Pi},
15+
{simpleTime(0, 0, 0), 0},
16+
{simpleTime(0, 0, 45), (math.Pi / 2) * 3},
17+
{simpleTime(0, 0, 7), (math.Pi / 30) * 7},
18+
}
3519

36-
if want != got {
37-
t.Fatalf("Wanted %v radians, but got %v", want, got)
20+
for _, c := range cases {
21+
t.Run(testName(c.time), func(t *testing.T) {
22+
got := secondsInRadians(c.time)
23+
if got != c.angle {
24+
t.Fatalf("Wanted %v radians, but got %v", c.angle, got)
25+
}
26+
})
3827
}
3928
}

0 commit comments

Comments
 (0)