forked from takahashim/aozoraconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aozoraconv_test.go
341 lines (317 loc) · 8.39 KB
/
aozoraconv_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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package aozoraconv
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
// toSjis convert string into Shift_JIS ([]byte)
func toSjis(s string) []byte {
outBuf := new(bytes.Buffer)
reader := strings.NewReader(s)
writer := transform.NewWriter(outBuf, japanese.ShiftJIS.NewEncoder())
//Copy
_, err := io.Copy(writer, reader)
if err != nil {
return nil
}
ret := outBuf.Bytes()
return ret
}
func TestConv(t *testing.T) {
var convertedStrings = []struct {
in string
out string
}{
{"", ""},
{"あ", "あ"},
{"〜", "~"},
{"‖", "∥"},
{"∥", "∥"},
{"¢", "¢"},
}
for _, tt := range convertedStrings {
buf := bytes.NewBuffer(nil)
if err := Conv(buf, strings.NewReader(tt.in)); err != nil {
t.Errorf("no error: %+v", err)
}
if buf.String() != tt.out {
t.Errorf("Conv: got %v want %v", buf.String(), tt.out)
}
}
}
func TestConvR(t *testing.T) {
var convertedStrings = []struct {
in string
out string
}{
{"", ""},
{"あ", "あ"},
{"~", "〜"},
{"\u301C", "\u301C"},
{"\uFF5E", "\u301C"},
{"∥", "‖"},
{"‖", "‖"},
{"¢", "¢"},
}
for _, tt := range convertedStrings {
buf := bytes.NewBuffer(nil)
if err := ConvRev(buf, strings.NewReader(tt.in)); err != nil {
t.Errorf("no error: %+v", err)
}
if buf.String() != tt.out {
t.Errorf("ConvRev: got %v want %v", buf.String(), tt.out)
}
}
}
func TestSjisConv(t *testing.T) {
encoder := japanese.ShiftJIS.NewEncoder()
input := "\uFF5E∥¢\u2015"
reader := transform.NewReader(strings.NewReader(input), encoder)
ret, err := io.ReadAll(reader)
if err != nil {
t.Errorf("sjis: error %v", err)
}
s := ""
for _, c := range ret {
s += fmt.Sprintf("%x", c)
}
want := "8160" + "8161" + "8191" + "815c"
if s != want {
t.Errorf("sjis: got %v want %v", s, want)
}
}
func TestSjisConv2(t *testing.T) {
input := "\uFF5E∥¢\u2015"
out := new(bytes.Buffer)
writer := transform.NewWriter(out, japanese.ShiftJIS.NewEncoder())
n, err := writer.Write([]byte(input))
if err != nil {
t.Errorf("sjis: in column %v, val: %v, 'U+%x'", n, err, []rune(input[n:])[0])
}
got := ""
for _, c := range out.Bytes() {
got += fmt.Sprintf("%x", c)
}
want := "8160" + "8161" + "8191" + "815c"
if got != want {
t.Errorf("sjis: got %v want %v", got, want)
}
}
func TestSjisConv3(t *testing.T) {
input := "\uFF5E∥¢\u2015 \u2014123"
out := new(bytes.Buffer)
writer := transform.NewWriter(out, japanese.ShiftJIS.NewEncoder())
n, err := writer.Write([]byte(input))
if n != 13 || err == nil {
t.Errorf("should be fail: %v, val: %v, 'U+%x'", n, err, []rune(input[n:])[0])
}
}
func TestEncode(t *testing.T) {
var convertedPairs = []struct {
in string
out []byte
}{
{"あいうえお", toSjis("あいうえお")},
{"\u301C", toSjis("\uFF5E")},
{"\uFF5E", toSjis("\uFF5E")},
{"¢", toSjis("¢")},
}
for _, tt := range convertedPairs {
input := strings.NewReader(tt.in)
output := bytes.NewBuffer(nil)
if err := Encode(output, input); err != nil {
t.Errorf("no error: %+v", err)
}
if bytes.Equal(output.Bytes(), tt.out) != true {
t.Errorf("Encode got: %v, want: %v", output.Bytes(), tt.out)
}
}
}
func TestDecode(t *testing.T) {
var convertedPairs = []struct {
out string
in []byte
}{
{"あいうえお", toSjis("あいうえお")},
{"\u301C", toSjis("\uFF5E")},
{"¢", toSjis("¢")},
}
for _, tt := range convertedPairs {
input := bytes.NewReader(tt.in)
output := bytes.NewBuffer(nil)
if err := Decode(output, input); err != nil {
t.Errorf("no error: %+v", err)
}
if output.String() != tt.out {
t.Errorf("Encode got: %v, want: %v", output.String(), tt.out)
}
}
}
func TestUni2Jis(t *testing.T) {
var convertedPairs = []struct {
in string
out JisEntry
isSuccess bool
}{
{"あ", JisEntry{men: 1, ku: 4, ten: 2}, true},
{"。", JisEntry{men: 1, ku: 1, ten: 3}, true},
{"◆", JisEntry{men: 1, ku: 2, ten: 1}, true},
{"A", JisEntry{0, 0, 0}, false},
{"☺", JisEntry{0, 0, 0}, false},
}
for _, tt := range convertedPairs {
got, err := Uni2Jis(tt.in)
if want := tt.out; got != want {
t.Errorf("Uni2Jis got: %v, want: %v", got, tt.out)
}
if err != nil && tt.isSuccess {
t.Errorf("Uni2Jis got: %v want: %v; should be error but %v", got, tt.out, err)
} else if err == nil && !tt.isSuccess {
t.Errorf("Uni2Jis got: %v want: %v; should be error but %v", got, tt.out, err)
}
}
}
func TestJis2Uni(t *testing.T) {
var convertedPairs = []struct {
men, ku, ten int
out string
isSuccess bool
}{
{1, 4, 2, "あ", true},
{1, 1, 3, "。", true},
{1, 2, 1, "◆", true},
{2, 3, 17, "𠗖", true},
{2, 2, 80, "", false},
{3, 2, 10, "", false},
{1, 2, 100, "", false},
}
for _, tt := range convertedPairs {
got, err := Jis2Uni(tt.men, tt.ku, tt.ten)
if err != nil && tt.isSuccess {
t.Errorf("Jis2Uni got: %v want: %v; should not be error but %v", got, tt.out, err)
} else if err == nil {
if !tt.isSuccess {
t.Errorf("Jis2Uni got: %v want: %v; should be error but %v", got, tt.out, err)
}
if want := tt.out; got != want {
t.Errorf("Jis2Uni got: %v, want: %v", got, tt.out)
}
}
}
}
func TestIs0208(t *testing.T) {
var convertedPairs = []struct {
men, ku, ten int
isSuccess bool
}{
{1, 4, 2, true},
{1, 1, 3, true},
{1, 2, 1, true},
{2, 3, 17, false},
{2, 2, 80, false},
{3, 2, 10, false},
{1, 2, 100, false},
{0, 1, 1, false},
{1, 0, 0, false},
{1, 1, 0, false},
{1, 1, 1, true},
{1, 1, 93, true}, {1, 1, 94, true}, {1, 1, 95, false},
{1, 2, 0, false}, {1, 2, 1, true},
{1, 2, 14, true}, {1, 2, 15, false}, {1, 2, 16, false},
{1, 2, 25, false}, {1, 2, 26, true},
{1, 2, 33, true}, {1, 2, 34, false},
{1, 2, 41, false}, {1, 2, 42, true},
{1, 2, 48, true}, {1, 2, 49, false},
{1, 2, 59, false}, {1, 2, 60, true},
{1, 2, 74, true}, {1, 2, 75, false},
{1, 2, 81, false}, {1, 2, 82, true},
{1, 2, 89, true}, {1, 2, 90, false},
{1, 2, 93, false}, {1, 2, 94, true}, {1, 2, 95, false},
{1, 3, 1, false},
{1, 3, 15, false}, {1, 3, 16, true},
{1, 3, 25, true}, {1, 3, 26, false},
{1, 3, 32, false}, {1, 3, 33, true},
{1, 3, 58, true}, {1, 3, 59, false},
{1, 3, 64, false}, {1, 3, 65, true},
{1, 3, 90, true}, {1, 3, 91, false},
{1, 3, 94, false},
{1, 4, 1, true}, {1, 4, 83, true}, {1, 4, 84, false}, {1, 4, 94, false},
{1, 5, 1, true},
{1, 5, 86, true}, {1, 5, 87, false},
{1, 5, 94, false},
{1, 6, 1, true},
{1, 6, 24, true}, {1, 6, 25, false},
{1, 6, 32, false}, {1, 6, 33, true},
{1, 6, 56, true}, {1, 6, 57, false},
{1, 7, 1, true},
{1, 7, 33, true}, {1, 7, 34, false},
{1, 7, 48, false}, {1, 7, 49, true},
{1, 7, 81, true}, {1, 7, 82, false},
{1, 8, 1, true},
{1, 8, 32, true},
{1, 8, 33, false},
{1, 9, 1, false}, {1, 15, 1, false},
{1, 16, 1, true},
{1, 16, 94, true}, {1, 16, 95, false},
{1, 17, 1, true},
{1, 47, 1, true},
{1, 47, 51, true}, {1, 47, 52, false},
{1, 48, 1, true},
{1, 84, 1, true},
{1, 84, 6, true}, {1, 84, 7, false},
}
for _, tt := range convertedPairs {
got := Is0208(tt.men, tt.ku, tt.ten)
if got != tt.isSuccess {
t.Errorf("Is0208 %v,%v,%v, got: %v want: %v", tt.men, tt.ku, tt.ten, got, tt.isSuccess)
}
}
}
func TestKuten2Sjis(t *testing.T) {
var convertedPairs = []struct {
ku, ten int
sjis []byte
}{
{1, 1, []byte{0x81, 0x40}}, // " "
{2, 1, []byte{0x81, 0x9F}}, // "◆"
{4, 2, []byte{0x82, 0xA0}}, // "あ"
{16, 1, []byte{0x88, 0x9F}}, // "亜"
{47, 52, []byte{0x98, 0x73}}, // only in JIS X 0213, not 0208
{84, 6, []byte{0xEA, 0xA4}},
}
for _, tt := range convertedPairs {
got, want := Kuten2Sjis(tt.ku, tt.ten), tt.sjis
if bytes.Compare(got, want) != 0 {
t.Errorf("Kuten2Sjis got: %v want: %v", got, want)
}
}
}
func TestAllKuten2SjisChars(t *testing.T) {
buf := make([]byte, 0)
for ku := 1; ku < 96; ku++ {
for ten := 0; ten < 96; ten++ {
if Is0208(1, ku, ten) {
chrs := Kuten2Sjis(ku, ten)
buf = append(buf, chrs[0], chrs[1])
}
}
}
got, want := len(buf), 13758
if len(buf) != want {
t.Errorf("kuten2sjis got: %v want: %v", got, want)
}
}
func TestReverse(t *testing.T) {
v := reverse([]string{"a", "b", "c", "d"})
if (v[0] == "d" && v[1] == "c" && v[2] == "b" && v[3] == "a") != true {
t.Errorf("actual %v", v)
}
v = reverse([]string{"a", "b", "c"})
if (v[0] == "c" && v[1] == "b" && v[2] == "a") != true {
t.Errorf("actual %v", v)
}
}