-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_number_test.go
339 lines (306 loc) · 7.52 KB
/
print_number_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
package sqroot
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
// fakeNumber returns 0.12345678901234567890...
func fakeNumber() *Number {
digit := 0
return &Number{spec: newMemoizeSpec(
func() int {
digit++
return digit % 10
})}
}
func TestPrintZeroDigits(t *testing.T) {
n := fakeNumber()
assert.Equal(t, "", Sprint(n, UpTo(0)))
assert.Equal(t, "", Sprint(n, UpTo(-1)))
}
func TestPrintNoOptions(t *testing.T) {
actual := Sprint(fakeNumber(), UpTo(12))
expected := `0.12345 67890 12`
assert.Equal(t, expected, actual)
}
func TestPrintLessThanOneRow(t *testing.T) {
actual := Sprint(
fakeNumber(), UpTo(12), DigitsPerRow(12), DigitsPerColumn(0))
expected := `0.123456789012`
assert.Equal(t, expected, actual)
}
func TestPrintColumns(t *testing.T) {
actual := Sprint(
fakeNumber(),
UpTo(12),
DigitsPerColumn(4),
DigitsPerRow(0),
ShowCount(false))
expected := `0.1234 5678 9012`
assert.Equal(t, expected, actual)
}
func TestPrintColumnsShow(t *testing.T) {
actual := Sprint(fakeNumber(), UpTo(12), DigitsPerColumn(5), DigitsPerRow(0))
expected := `0.12345 67890 12`
assert.Equal(t, expected, actual)
}
func TestPrinterRows10(t *testing.T) {
actual := Sprint(
fakeNumber(),
UpTo(110),
DigitsPerRow(10),
DigitsPerColumn(0),
ShowCount(false))
expected := `0.1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890`
assert.Equal(t, expected, actual)
}
func TestPrinterRows10Columns(t *testing.T) {
actual := Sprint(
fakeNumber(),
UpTo(110),
DigitsPerRow(10),
DigitsPerColumn(10),
ShowCount(false))
expected := `0.1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890`
assert.Equal(t, expected, actual)
}
func TestPrinterRows11Columns(t *testing.T) {
actual := Sprint(
fakeNumber(),
UpTo(110),
DigitsPerRow(11),
DigitsPerColumn(10),
ShowCount(false))
expected := `0.1234567890 1
2345678901 2
3456789012 3
4567890123 4
5678901234 5
6789012345 6
7890123456 7
8901234567 8
9012345678 9
0123456789 0`
assert.Equal(t, expected, actual)
}
func TestPrinterRows10Show(t *testing.T) {
actual := Sprint(
fakeNumber(), UpTo(110), DigitsPerRow(10), DigitsPerColumn(0))
expected := ` 0.1234567890
10 1234567890
20 1234567890
30 1234567890
40 1234567890
50 1234567890
60 1234567890
70 1234567890
80 1234567890
90 1234567890
100 1234567890`
assert.Equal(t, expected, actual)
}
func TestPrinterRows10ColumnsShow(t *testing.T) {
actual := Sprint(
fakeNumber(), UpTo(110), DigitsPerRow(10), DigitsPerColumn(10))
expected := ` 0.1234567890
10 1234567890
20 1234567890
30 1234567890
40 1234567890
50 1234567890
60 1234567890
70 1234567890
80 1234567890
90 1234567890
100 1234567890`
assert.Equal(t, expected, actual)
}
func TestPrinterRows11ColumnsShow(t *testing.T) {
actual := Sprint(
fakeNumber(), UpTo(110), DigitsPerRow(11), DigitsPerColumn(10))
expected := ` 0.1234567890 1
11 2345678901 2
22 3456789012 3
33 4567890123 4
44 5678901234 5
55 6789012345 6
66 7890123456 7
77 8901234567 8
88 9012345678 9
99 0123456789 0`
assert.Equal(t, expected, actual)
}
func TestPrinterRows11ColumnsShow109(t *testing.T) {
actual := Sprint(
fakeNumber(), UpTo(109), DigitsPerRow(11), DigitsPerColumn(10))
expected := ` 0.1234567890 1
11 2345678901 2
22 3456789012 3
33 4567890123 4
44 5678901234 5
55 6789012345 6
66 7890123456 7
77 8901234567 8
88 9012345678 9
99 0123456789`
assert.Equal(t, expected, actual)
}
func TestPrinterRows11ColumnsShow111(t *testing.T) {
actual := Sprint(
fakeNumber(),
UpTo(111),
DigitsPerRow(11),
DigitsPerColumn(10),
ShowCount(true))
expected := ` 0.1234567890 1
11 2345678901 2
22 3456789012 3
33 4567890123 4
44 5678901234 5
55 6789012345 6
66 7890123456 7
77 8901234567 8
88 9012345678 9
99 0123456789 0
110 1`
assert.Equal(t, expected, actual)
}
func TestPrinterWithBetween(t *testing.T) {
actual := Sprint(
fakeNumber(),
Between(50, 70),
DigitsPerRow(11),
DigitsPerColumn(10))
expected := `44 ......1234 5
55 6789012345 6
66 7890`
assert.Equal(t, expected, actual)
}
func TestPrinterWithPositions(t *testing.T) {
var pb PositionsBuilder
actual := Sprint(
fakeNumber(),
pb.Add(45).Add(48).AddRange(50, 52).Build(),
DigitsPerRow(11),
DigitsPerColumn(10),
MissingDigit('-'))
expected := `44 -6--9-12`
assert.Equal(t, expected, actual)
}
func TestPrinterWithPositions2(t *testing.T) {
var pb PositionsBuilder
actual := Sprint(
fakeNumber(),
pb.AddRange(42, 48).AddRange(64, 68).Build(),
DigitsPerRow(10),
DigitsPerColumn(0))
expected := `40 ..345678..
60 ....5678`
assert.Equal(t, expected, actual)
}
func TestPrinterWithStart(t *testing.T) {
number := fakeNumber()
actual := Sprint(number.WithStart(502), UpTo(505))
expected := `500 ..345`
assert.Equal(t, expected, actual)
assert.Empty(t, Sprint(number.WithStart(502), UpTo(502)))
}
func TestPrinterNoFormatting(t *testing.T) {
var pb PositionsBuilder
actual := Sprint(
fakeNumber(),
pb.AddRange(17, 22).AddRange(27, 30).Build(),
DigitsPerRow(0),
DigitsPerColumn(0))
expected := "0..................89012.....890"
assert.Equal(t, expected, actual)
}
func TestPrinterFewerDigits(t *testing.T) {
actual := Sprint(
fakeNumber().WithSignificant(9),
UpTo(111),
DigitsPerRow(11),
DigitsPerColumn(10))
expected := ` 0.123456789`
assert.Equal(t, expected, actual)
}
func TestPrinterNegative(t *testing.T) {
actual := Sprint(fakeNumber(), UpTo(-3), DigitsPerRow(10))
assert.Equal(t, "", actual)
}
func TestPrinterCountBytes(t *testing.T) {
w := &maxBytesWriter{maxBytes: 100000}
// Prints 200 rows. Each row 10 columns 6 chars per column + (4+2) chars
// for the margin. 66*200-1=13199 bytes because last line doesn't get a
// line feed char.
n, err := Fprint(w, fakeNumber(), UpTo(10000))
assert.Equal(t, 13199, n)
assert.NoError(t, err)
}
func TestPrinterCountBytes2(t *testing.T) {
w := &maxBytesWriter{maxBytes: 10000}
// Prints 20 rows. Each row 10 columns 6 chars per column + (3+2) chars
// for the margin. 65*20-1=1299 bytes because last line doesn't get a
// line feed char.
n, err := Fprint(w, fakeNumber(), UpTo(1000))
assert.Equal(t, 1299, n)
assert.NoError(t, err)
}
func TestErrorAtAllStages(t *testing.T) {
number := fakeNumber()
// Internally Fprint uses a bufio.Writer which defaults to a buffer size
// of 4096 bytes (could change in future go versions). This means writing
// can fail only when buffer fills up or is flushed. We pick 601 which
// is prime and small enough that we test an error happening when manually
// flushing the buffer. There exists a k such that 3*4096 < 601*k < 13199.
for i := 0; i < 13199; i += 601 {
w := &maxBytesWriter{maxBytes: i}
n, err := Fprint(w, number, UpTo(10000))
assert.Equal(t, i, n)
assert.Error(t, err)
}
}
func TestErrorAtAllStages2(t *testing.T) {
number := fakeNumber()
// Set buffer size to 1 so that we get extra coverage.
for i := 0; i < 1299; i++ {
w := &maxBytesWriter{maxBytes: i}
n, err := Fprint(w, number, UpTo(1000), bufferSize(1))
assert.Equal(t, i, n)
assert.Error(t, err)
}
}
type maxBytesWriter struct {
maxBytes int
bytesWritten int
}
func (m *maxBytesWriter) Write(p []byte) (n int, err error) {
length := len(p)
if length <= m.maxBytes-m.bytesWritten {
m.bytesWritten += length
return length, nil
}
diff := m.maxBytes - m.bytesWritten
m.bytesWritten += diff
return diff, errors.New("Ran out of space")
}