From a330b3817e89369f34793d52003252506454dfe9 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 11 Nov 2016 14:11:51 +0100 Subject: Add wordy --- go/wordy/wordy_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 go/wordy/wordy_test.go (limited to 'go/wordy/wordy_test.go') diff --git a/go/wordy/wordy_test.go b/go/wordy/wordy_test.go new file mode 100644 index 0000000..885625e --- /dev/null +++ b/go/wordy/wordy_test.go @@ -0,0 +1,50 @@ +package wordy + +import "testing" + +var tests = []struct { + q string + a int + ok bool +}{ + {"What is 1 plus 1?", 2, true}, + {"What is 53 plus 2?", 55, true}, + {"What is -1 plus -10?", -11, true}, + {"What is 123 plus 45678?", 45801, true}, + {"What is 4 minus -12?", 16, true}, + {"What is -3 multiplied by 25?", -75, true}, + {"What is 33 divided by -3?", -11, true}, + {"What is 1 plus 1 plus 1?", 3, true}, + {"What is 1 plus 5 minus -2?", 8, true}, + {"What is 20 minus 4 minus 13?", 3, true}, + {"What is 17 minus 6 plus 3?", 14, true}, + {"What is 2 multiplied by -2 multiplied by 3?", -12, true}, + {"What is -3 plus 7 multiplied by -2?", -8, true}, + {"What is -12 divided by 2 divided by -3?", 2, true}, + {"What is 53 cubed?", 0, false}, + {"Who is the president of the United States?", 0, false}, +} + +func TestAnswer(t *testing.T) { + for _, test := range tests { + switch a, ok := Answer(test.q); { + case !ok: + if test.ok { + t.Errorf("Answer(%q) returned ok = false, expecting true.", test.q) + } + case !test.ok: + t.Errorf("Answer(%q) = %d, %t, expecting ok = false.", test.q, a, ok) + case a != test.a: + t.Errorf("Answer(%q) = %d, want %d.", test.q, a, test.a) + } + } +} + +// Benchmark combined time to answer all questions. +func BenchmarkAnswer(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, test := range tests { + Answer(test.q) + } + } +} -- cgit v1.2.3