summaryrefslogtreecommitdiff
path: root/go/ocr-numbers/ocr_numbers_test.go
blob: 707a91ab1b58914ac75672b17b1e19df93ffc754 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Go requirements:
//
// Define a function recognizeDigit as README Step 1 except make it recognize
// all ten digits 0 to 9.  Pick what you like for parameters and return values
// but make it useful as a subroutine for README step 2.
//
// For README Step 2 define,
//
//    func Recognize(string) []string
//
// and implement it using recognizeDigit.
//
// Input strings tested here have a \n at the beginning of each line and
// no trailing \n on the last line. (This makes for readable raw string
// literals.)
//
// For bonus points, gracefully handle misformatted data.  What should you
// do with a partial cell?  Discard it?  Pad with spaces?  Report it with a
// "?" character?  What should you do if the first character is not \n?

package ocr

import (
	"reflect"
	"testing"
)

var tests = []struct {
	in  string
	out []string
}{
	{`
 _ 
| |
|_|
   `, []string{"0"}},
	{`
   
  |
  |
   `, []string{"1"}},
	{`
 _ 
 _|
|_ 
   `, []string{"2"}},
	{`
 _ 
 _|
 _|
   `, []string{"3"}},
	{`
   
|_|
  |
   `, []string{"4"}},
	{`
 _ 
|_ 
 _|
   `, []string{"5"}},
	{`
 _ 
|_ 
|_|
   `, []string{"6"}},
	{`
 _ 
  |
  |
   `, []string{"7"}},
	{`
 _ 
|_|
|_|
   `, []string{"8"}},
	{`
 _ 
|_|
 _|
   `, []string{"9"}},
	{`
    _ 
  || |
  ||_|
      `, []string{"10"}},
	{`
   
| |
| |
   `, []string{"?"}},
	{`
       _     _        _  _ 
  |  || |  || |  |  || || |
  |  ||_|  ||_|  |  ||_||_|
                           `, []string{"110101100"}},
	{`
       _     _           _ 
  |  || |  || |     || || |
  |  | _|  ||_|  |  ||_||_|
                           `, []string{"11?10?1?0"}},
	{`
    _  _     _  _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|| |
  ||_  _|  | _||_|  ||_| _||_|
                              `, []string{"1234567890"}},
	{`
    _  _ 
  | _| _|
  ||_  _|
         
    _  _ 
|_||_ |_ 
  | _||_|
         
 _  _  _ 
  ||_||_|
  ||_| _|
         `, []string{"123", "456", "789"}},
}

var _ = recognizeDigit // step 1.

func TestRecognize(t *testing.T) {
	for _, test := range tests {
		if res := Recognize(test.in); !reflect.DeepEqual(res, test.out) {
			t.Fatalf("Recognize(`%s`) = %q, want %q.", test.in, res, test.out)
		}
	}
}