From a9ae4ebc6006271fc0e95626e0c95e6b9cc786de Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 28 Aug 2016 14:41:56 +0200 Subject: Solve robot --- go/robot-name/robot_name_test.go | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 go/robot-name/robot_name_test.go (limited to 'go/robot-name/robot_name_test.go') diff --git a/go/robot-name/robot_name_test.go b/go/robot-name/robot_name_test.go new file mode 100644 index 0000000..414b0db --- /dev/null +++ b/go/robot-name/robot_name_test.go @@ -0,0 +1,51 @@ +package robotname + +import ( + "regexp" + "testing" +) + +func New() *Robot { return new(Robot) } + +var namePat = regexp.MustCompile(`^[A-Z]{2}\d{3}$`) + +func TestNameValid(t *testing.T) { + n := New().Name() + if !namePat.MatchString(n) { + t.Errorf(`Invalid robot name %q, want form "AA###".`, n) + } +} + +func TestNameSticks(t *testing.T) { + r := New() + n1 := r.Name() + n2 := r.Name() + if n2 != n1 { + t.Errorf(`Robot name changed. Now %s, was %s.`, n2, n1) + } +} + +func TestSuccessiveRobotsHaveDifferentNames(t *testing.T) { + n1 := New().Name() + if New().Name() == n1 { + t.Errorf(`Robots with same name. Two %s's.`, n1) + } +} + +func TestResetName(t *testing.T) { + r := New() + n1 := r.Name() + r.Reset() + if r.Name() == n1 { + t.Errorf(`Robot name not cleared on reset. Still %s.`, n1) + } +} + +// Note if you go for bonus points, this benchmark likely won't be +// meaningful. Bonus thought exercise, why won't it be meaningful? +func BenchmarkName(b *testing.B) { + // Benchmark combined time to create robot and name. + for i := 0; i < b.N; i++ { + New().Name() + } +} -- cgit v1.2.3