1
exercism fetch go say

cases_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package say

// Source: exercism/problem-specifications
// Commit: be403e1 say: Fix canonical-data.json formatting
// Problem Specifications Version: 1.0.0

var testCases = []struct {
	description string
	input       int64
	expected    string
	expectError bool
}{
	{
		description: "zero",
		input:       0,
		expected:    "zero",
	},
	{
		description: "one",
		input:       1,
		expected:    "one",
	},
	{
		description: "fourteen",
		input:       14,
		expected:    "fourteen",
	},
	{
		description: "twenty",
		input:       20,
		expected:    "twenty",
	},
	{
		description: "twenty-two",
		input:       22,
		expected:    "twenty-two",
	},
	{
		description: "one hundred",
		input:       100,
		expected:    "one hundred",
	},
	{
		description: "one hundred twenty-three",
		input:       123,
		expected:    "one hundred twenty-three",
	},
	{
		description: "one thousand",
		input:       1000,
		expected:    "one thousand",
	},
	{
		description: "one thousand two hundred thirty-four",
		input:       1234,
		expected:    "one thousand two hundred thirty-four",
	},
	{
		description: "one million",
		input:       1000000,
		expected:    "one million",
	},
	{
		description: "one million two thousand three hundred forty-five",
		input:       1002345,
		expected:    "one million two thousand three hundred forty-five",
	},
	{
		description: "one billion",
		input:       1000000000,
		expected:    "one billion",
	},
	{
		description: "a big number",
		input:       987654321123,
		expected:    "nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three",
	},
	{
		description: "numbers below zero are out of range",
		input:       -1,
		expectError: true,
	},
	{
		description: "numbers above 999,999,999,999 are out of range",
		input:       1000000000000,
		expectError: true,
	},
}

say_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package say

import (
	"testing"
)

func TestSay(t *testing.T) {
	for _, tc := range testCases {
		actual, ok := Say(tc.input)
		if tc.expectError {
			if ok {
				t.Fatalf("FAIL: %s\nExpected error but received: %v", tc.description, actual)
			}
		} else if !ok {
			t.Fatalf("FAIL: %s\nDid not expect an error", tc.description)
		} else if actual != tc.expected {
			t.Fatalf("FAIL: %s\nExpected: %v\nActual: %v", tc.description, tc.expected, actual)
		}
		t.Logf("PASS: %s", tc.description)
	}
}

func BenchmarkSay(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, tc := range testCases {
			Say(tc.input)
		}
	}
}