OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [text/] [tabwriter/] [tabwriter_test.go] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package tabwriter
6
 
7
import (
8
        "io"
9
        "testing"
10
)
11
 
12
type buffer struct {
13
        a []byte
14
}
15
 
16
func (b *buffer) init(n int) { b.a = make([]byte, n)[0:0] }
17
 
18
func (b *buffer) clear() { b.a = b.a[0:0] }
19
 
20
func (b *buffer) Write(buf []byte) (written int, err error) {
21
        n := len(b.a)
22
        m := len(buf)
23
        if n+m <= cap(b.a) {
24
                b.a = b.a[0 : n+m]
25
                for i := 0; i < m; i++ {
26
                        b.a[n+i] = buf[i]
27
                }
28
        } else {
29
                panic("buffer.Write: buffer too small")
30
        }
31
        return len(buf), nil
32
}
33
 
34
func (b *buffer) String() string { return string(b.a) }
35
 
36
func write(t *testing.T, testname string, w *Writer, src string) {
37
        written, err := io.WriteString(w, src)
38
        if err != nil {
39
                t.Errorf("--- test: %s\n--- src:\n%q\n--- write error: %v\n", testname, src, err)
40
        }
41
        if written != len(src) {
42
                t.Errorf("--- test: %s\n--- src:\n%q\n--- written = %d, len(src) = %d\n", testname, src, written, len(src))
43
        }
44
}
45
 
46
func verify(t *testing.T, testname string, w *Writer, b *buffer, src, expected string) {
47
        err := w.Flush()
48
        if err != nil {
49
                t.Errorf("--- test: %s\n--- src:\n%q\n--- flush error: %v\n", testname, src, err)
50
        }
51
 
52
        res := b.String()
53
        if res != expected {
54
                t.Errorf("--- test: %s\n--- src:\n%q\n--- found:\n%q\n--- expected:\n%q\n", testname, src, res, expected)
55
        }
56
}
57
 
58
func check(t *testing.T, testname string, minwidth, tabwidth, padding int, padchar byte, flags uint, src, expected string) {
59
        var b buffer
60
        b.init(1000)
61
 
62
        var w Writer
63
        w.Init(&b, minwidth, tabwidth, padding, padchar, flags)
64
 
65
        // write all at once
66
        title := testname + " (written all at once)"
67
        b.clear()
68
        write(t, title, &w, src)
69
        verify(t, title, &w, &b, src, expected)
70
 
71
        // write byte-by-byte
72
        title = testname + " (written byte-by-byte)"
73
        b.clear()
74
        for i := 0; i < len(src); i++ {
75
                write(t, title, &w, src[i:i+1])
76
        }
77
        verify(t, title, &w, &b, src, expected)
78
 
79
        // write using Fibonacci slice sizes
80
        title = testname + " (written in fibonacci slices)"
81
        b.clear()
82
        for i, d := 0, 0; i < len(src); {
83
                write(t, title, &w, src[i:i+d])
84
                i, d = i+d, d+1
85
                if i+d > len(src) {
86
                        d = len(src) - i
87
                }
88
        }
89
        verify(t, title, &w, &b, src, expected)
90
}
91
 
92
var tests = []struct {
93
        testname                    string
94
        minwidth, tabwidth, padding int
95
        padchar                     byte
96
        flags                       uint
97
        src, expected               string
98
}{
99
        {
100
                "1a",
101
                8, 0, 1, '.', 0,
102
                "",
103
                "",
104
        },
105
 
106
        {
107
                "1a debug",
108
                8, 0, 1, '.', Debug,
109
                "",
110
                "",
111
        },
112
 
113
        {
114
                "1b esc stripped",
115
                8, 0, 1, '.', StripEscape,
116
                "\xff\xff",
117
                "",
118
        },
119
 
120
        {
121
                "1b esc",
122
                8, 0, 1, '.', 0,
123
                "\xff\xff",
124
                "\xff\xff",
125
        },
126
 
127
        {
128
                "1c esc stripped",
129
                8, 0, 1, '.', StripEscape,
130
                "\xff\t\xff",
131
                "\t",
132
        },
133
 
134
        {
135
                "1c esc",
136
                8, 0, 1, '.', 0,
137
                "\xff\t\xff",
138
                "\xff\t\xff",
139
        },
140
 
141
        {
142
                "1d esc stripped",
143
                8, 0, 1, '.', StripEscape,
144
                "\xff\"foo\t\n\tbar\"\xff",
145
                "\"foo\t\n\tbar\"",
146
        },
147
 
148
        {
149
                "1d esc",
150
                8, 0, 1, '.', 0,
151
                "\xff\"foo\t\n\tbar\"\xff",
152
                "\xff\"foo\t\n\tbar\"\xff",
153
        },
154
 
155
        {
156
                "1e esc stripped",
157
                8, 0, 1, '.', StripEscape,
158
                "abc\xff\tdef", // unterminated escape
159
                "abc\tdef",
160
        },
161
 
162
        {
163
                "1e esc",
164
                8, 0, 1, '.', 0,
165
                "abc\xff\tdef", // unterminated escape
166
                "abc\xff\tdef",
167
        },
168
 
169
        {
170
                "2",
171
                8, 0, 1, '.', 0,
172
                "\n\n\n",
173
                "\n\n\n",
174
        },
175
 
176
        {
177
                "3",
178
                8, 0, 1, '.', 0,
179
                "a\nb\nc",
180
                "a\nb\nc",
181
        },
182
 
183
        {
184
                "4a",
185
                8, 0, 1, '.', 0,
186
                "\t", // '\t' terminates an empty cell on last line - nothing to print
187
                "",
188
        },
189
 
190
        {
191
                "4b",
192
                8, 0, 1, '.', AlignRight,
193
                "\t", // '\t' terminates an empty cell on last line - nothing to print
194
                "",
195
        },
196
 
197
        {
198
                "5",
199
                8, 0, 1, '.', 0,
200
                "*\t*",
201
                "*.......*",
202
        },
203
 
204
        {
205
                "5b",
206
                8, 0, 1, '.', 0,
207
                "*\t*\n",
208
                "*.......*\n",
209
        },
210
 
211
        {
212
                "5c",
213
                8, 0, 1, '.', 0,
214
                "*\t*\t",
215
                "*.......*",
216
        },
217
 
218
        {
219
                "5c debug",
220
                8, 0, 1, '.', Debug,
221
                "*\t*\t",
222
                "*.......|*",
223
        },
224
 
225
        {
226
                "5d",
227
                8, 0, 1, '.', AlignRight,
228
                "*\t*\t",
229
                ".......**",
230
        },
231
 
232
        {
233
                "6",
234
                8, 0, 1, '.', 0,
235
                "\t\n",
236
                "........\n",
237
        },
238
 
239
        {
240
                "7a",
241
                8, 0, 1, '.', 0,
242
                "a) foo",
243
                "a) foo",
244
        },
245
 
246
        {
247
                "7b",
248
                8, 0, 1, ' ', 0,
249
                "b) foo\tbar",
250
                "b) foo  bar",
251
        },
252
 
253
        {
254
                "7c",
255
                8, 0, 1, '.', 0,
256
                "c) foo\tbar\t",
257
                "c) foo..bar",
258
        },
259
 
260
        {
261
                "7d",
262
                8, 0, 1, '.', 0,
263
                "d) foo\tbar\n",
264
                "d) foo..bar\n",
265
        },
266
 
267
        {
268
                "7e",
269
                8, 0, 1, '.', 0,
270
                "e) foo\tbar\t\n",
271
                "e) foo..bar.....\n",
272
        },
273
 
274
        {
275
                "7f",
276
                8, 0, 1, '.', FilterHTML,
277
                "f) f<o\tbar\t\n",
278
                "f) f<o..bar.....\n",
279
        },
280
 
281
        {
282
                "7g",
283
                8, 0, 1, '.', FilterHTML,
284
                "g) f<o\tbar\t non-terminated entity &",
285
                "g) f<o..bar..... non-terminated entity &",
286
        },
287
 
288
        {
289
                "7g debug",
290
                8, 0, 1, '.', FilterHTML | Debug,
291
                "g) f<o\tbar\t non-terminated entity &",
292
                "g) f<o..|bar.....| non-terminated entity &",
293
        },
294
 
295
        {
296
                "8",
297
                8, 0, 1, '*', 0,
298
                "Hello, world!\n",
299
                "Hello, world!\n",
300
        },
301
 
302
        {
303
                "9a",
304
                1, 0, 0, '.', 0,
305
                "1\t2\t3\t4\n" +
306
                        "11\t222\t3333\t44444\n",
307
 
308
                "1.2..3...4\n" +
309
                        "11222333344444\n",
310
        },
311
 
312
        {
313
                "9b",
314
                1, 0, 0, '.', FilterHTML,
315
                "1\t2\t3\t4\n" + // \f inside HTML is ignored
316
                        "11\t222\t3333\t44444\n",
317
 
318
                "1.2..3...4\n" +
319
                        "11222333344444\n",
320
        },
321
 
322
        {
323
                "9c",
324
                1, 0, 0, '.', 0,
325
                "1\t2\t3\t4\f" + // \f causes a newline and flush
326
                        "11\t222\t3333\t44444\n",
327
 
328
                "1234\n" +
329
                        "11222333344444\n",
330
        },
331
 
332
        {
333
                "9c debug",
334
                1, 0, 0, '.', Debug,
335
                "1\t2\t3\t4\f" + // \f causes a newline and flush
336
                        "11\t222\t3333\t44444\n",
337
 
338
                "1|2|3|4\n" +
339
                        "---\n" +
340
                        "11|222|3333|44444\n",
341
        },
342
 
343
        {
344
                "10a",
345
                5, 0, 0, '.', 0,
346
                "1\t2\t3\t4\n",
347
                "1....2....3....4\n",
348
        },
349
 
350
        {
351
                "10b",
352
                5, 0, 0, '.', 0,
353
                "1\t2\t3\t4\t\n",
354
                "1....2....3....4....\n",
355
        },
356
 
357
        {
358
                "11",
359
                8, 0, 1, '.', 0,
360
                "本\tb\tc\n" +
361
                        "aa\t\u672c\u672c\u672c\tcccc\tddddd\n" +
362
                        "aaa\tbbbb\n",
363
 
364
                "本.......b.......c\n" +
365
                        "aa......本本本.....cccc....ddddd\n" +
366
                        "aaa.....bbbb\n",
367
        },
368
 
369
        {
370
                "12a",
371
                8, 0, 1, ' ', AlignRight,
372
                "a\tè\tc\t\n" +
373
                        "aa\tèèè\tcccc\tddddd\t\n" +
374
                        "aaa\tèèèè\t\n",
375
 
376
                "       a       è       c\n" +
377
                        "      aa     èèè    cccc   ddddd\n" +
378
                        "     aaa    èèèè\n",
379
        },
380
 
381
        {
382
                "12b",
383
                2, 0, 0, ' ', 0,
384
                "a\tb\tc\n" +
385
                        "aa\tbbb\tcccc\n" +
386
                        "aaa\tbbbb\n",
387
 
388
                "a  b  c\n" +
389
                        "aa bbbcccc\n" +
390
                        "aaabbbb\n",
391
        },
392
 
393
        {
394
                "12c",
395
                8, 0, 1, '_', 0,
396
                "a\tb\tc\n" +
397
                        "aa\tbbb\tcccc\n" +
398
                        "aaa\tbbbb\n",
399
 
400
                "a_______b_______c\n" +
401
                        "aa______bbb_____cccc\n" +
402
                        "aaa_____bbbb\n",
403
        },
404
 
405
        {
406
                "13a",
407
                4, 0, 1, '-', 0,
408
                "4444\t日本語\t22\t1\t333\n" +
409
                        "999999999\t22\n" +
410
                        "7\t22\n" +
411
                        "\t\t\t88888888\n" +
412
                        "\n" +
413
                        "666666\t666666\t666666\t4444\n" +
414
                        "1\t1\t999999999\t0000000000\n",
415
 
416
                "4444------日本語-22--1---333\n" +
417
                        "999999999-22\n" +
418
                        "7---------22\n" +
419
                        "------------------88888888\n" +
420
                        "\n" +
421
                        "666666-666666-666666----4444\n" +
422
                        "1------1------999999999-0000000000\n",
423
        },
424
 
425
        {
426
                "13b",
427
                4, 0, 3, '.', 0,
428
                "4444\t333\t22\t1\t333\n" +
429
                        "999999999\t22\n" +
430
                        "7\t22\n" +
431
                        "\t\t\t88888888\n" +
432
                        "\n" +
433
                        "666666\t666666\t666666\t4444\n" +
434
                        "1\t1\t999999999\t0000000000\n",
435
 
436
                "4444........333...22...1...333\n" +
437
                        "999999999...22\n" +
438
                        "7...........22\n" +
439
                        "....................88888888\n" +
440
                        "\n" +
441
                        "666666...666666...666666......4444\n" +
442
                        "1........1........999999999...0000000000\n",
443
        },
444
 
445
        {
446
                "13c",
447
                8, 8, 1, '\t', FilterHTML,
448
                "4444\t333\t22\t1\t333\n" +
449
                        "999999999\t22\n" +
450
                        "7\t22\n" +
451
                        "\t\t\t88888888\n" +
452
                        "\n" +
453
                        "666666\t666666\t666666\t4444\n" +
454
                        "1\t1\t999999999\t0000000000\n",
455
 
456
                "4444\t\t333\t22\t1\t333\n" +
457
                        "999999999\t22\n" +
458
                        "7\t\t22\n" +
459
                        "\t\t\t\t88888888\n" +
460
                        "\n" +
461
                        "666666\t666666\t666666\t\t4444\n" +
462
                        "1\t1\t999999999\t0000000000\n",
463
        },
464
 
465
        {
466
                "14",
467
                1, 0, 2, ' ', AlignRight,
468
                ".0\t.3\t2.4\t-5.1\t\n" +
469
                        "23.0\t12345678.9\t2.4\t-989.4\t\n" +
470
                        "5.1\t12.0\t2.4\t-7.0\t\n" +
471
                        ".0\t0.0\t332.0\t8908.0\t\n" +
472
                        ".0\t-.3\t456.4\t22.1\t\n" +
473
                        ".0\t1.2\t44.4\t-13.3\t\t",
474
 
475
                "    .0          .3    2.4    -5.1\n" +
476
                        "  23.0  12345678.9    2.4  -989.4\n" +
477
                        "   5.1        12.0    2.4    -7.0\n" +
478
                        "    .0         0.0  332.0  8908.0\n" +
479
                        "    .0         -.3  456.4    22.1\n" +
480
                        "    .0         1.2   44.4   -13.3",
481
        },
482
 
483
        {
484
                "14 debug",
485
                1, 0, 2, ' ', AlignRight | Debug,
486
                ".0\t.3\t2.4\t-5.1\t\n" +
487
                        "23.0\t12345678.9\t2.4\t-989.4\t\n" +
488
                        "5.1\t12.0\t2.4\t-7.0\t\n" +
489
                        ".0\t0.0\t332.0\t8908.0\t\n" +
490
                        ".0\t-.3\t456.4\t22.1\t\n" +
491
                        ".0\t1.2\t44.4\t-13.3\t\t",
492
 
493
                "    .0|          .3|    2.4|    -5.1|\n" +
494
                        "  23.0|  12345678.9|    2.4|  -989.4|\n" +
495
                        "   5.1|        12.0|    2.4|    -7.0|\n" +
496
                        "    .0|         0.0|  332.0|  8908.0|\n" +
497
                        "    .0|         -.3|  456.4|    22.1|\n" +
498
                        "    .0|         1.2|   44.4|   -13.3|",
499
        },
500
 
501
        {
502
                "15a",
503
                4, 0, 0, '.', 0,
504
                "a\t\tb",
505
                "a.......b",
506
        },
507
 
508
        {
509
                "15b",
510
                4, 0, 0, '.', DiscardEmptyColumns,
511
                "a\t\tb", // htabs - do not discard column
512
                "a.......b",
513
        },
514
 
515
        {
516
                "15c",
517
                4, 0, 0, '.', DiscardEmptyColumns,
518
                "a\v\vb",
519
                "a...b",
520
        },
521
 
522
        {
523
                "15d",
524
                4, 0, 0, '.', AlignRight | DiscardEmptyColumns,
525
                "a\v\vb",
526
                "...ab",
527
        },
528
 
529
        {
530
                "16a",
531
                100, 100, 0, '\t', 0,
532
                "a\tb\t\td\n" +
533
                        "a\tb\t\td\te\n" +
534
                        "a\n" +
535
                        "a\tb\tc\td\n" +
536
                        "a\tb\tc\td\te\n",
537
 
538
                "a\tb\t\td\n" +
539
                        "a\tb\t\td\te\n" +
540
                        "a\n" +
541
                        "a\tb\tc\td\n" +
542
                        "a\tb\tc\td\te\n",
543
        },
544
 
545
        {
546
                "16b",
547
                100, 100, 0, '\t', DiscardEmptyColumns,
548
                "a\vb\v\vd\n" +
549
                        "a\vb\v\vd\ve\n" +
550
                        "a\n" +
551
                        "a\vb\vc\vd\n" +
552
                        "a\vb\vc\vd\ve\n",
553
 
554
                "a\tb\td\n" +
555
                        "a\tb\td\te\n" +
556
                        "a\n" +
557
                        "a\tb\tc\td\n" +
558
                        "a\tb\tc\td\te\n",
559
        },
560
 
561
        {
562
                "16b debug",
563
                100, 100, 0, '\t', DiscardEmptyColumns | Debug,
564
                "a\vb\v\vd\n" +
565
                        "a\vb\v\vd\ve\n" +
566
                        "a\n" +
567
                        "a\vb\vc\vd\n" +
568
                        "a\vb\vc\vd\ve\n",
569
 
570
                "a\t|b\t||d\n" +
571
                        "a\t|b\t||d\t|e\n" +
572
                        "a\n" +
573
                        "a\t|b\t|c\t|d\n" +
574
                        "a\t|b\t|c\t|d\t|e\n",
575
        },
576
 
577
        {
578
                "16c",
579
                100, 100, 0, '\t', DiscardEmptyColumns,
580
                "a\tb\t\td\n" + // hard tabs - do not discard column
581
                        "a\tb\t\td\te\n" +
582
                        "a\n" +
583
                        "a\tb\tc\td\n" +
584
                        "a\tb\tc\td\te\n",
585
 
586
                "a\tb\t\td\n" +
587
                        "a\tb\t\td\te\n" +
588
                        "a\n" +
589
                        "a\tb\tc\td\n" +
590
                        "a\tb\tc\td\te\n",
591
        },
592
 
593
        {
594
                "16c debug",
595
                100, 100, 0, '\t', DiscardEmptyColumns | Debug,
596
                "a\tb\t\td\n" + // hard tabs - do not discard column
597
                        "a\tb\t\td\te\n" +
598
                        "a\n" +
599
                        "a\tb\tc\td\n" +
600
                        "a\tb\tc\td\te\n",
601
 
602
                "a\t|b\t|\t|d\n" +
603
                        "a\t|b\t|\t|d\t|e\n" +
604
                        "a\n" +
605
                        "a\t|b\t|c\t|d\n" +
606
                        "a\t|b\t|c\t|d\t|e\n",
607
        },
608
}
609
 
610
func Test(t *testing.T) {
611
        for _, e := range tests {
612
                check(t, e.testname, e.minwidth, e.tabwidth, e.padding, e.padchar, e.flags, e.src, e.expected)
613
        }
614
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.