File: reader_test.go

package info (click to toggle)
golang-github-dsnet-compress 0.0.2~git20230904.39efe44%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,724 kB
  • sloc: sh: 108; makefile: 5
file content (935 lines) | stat: -rw-r--r-- 26,218 bytes parent folder | download
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package bzip2

import (
	"bytes"
	"io"
	"io/ioutil"
	"testing"

	"github.com/dsnet/compress/internal/errors"
	"github.com/dsnet/compress/internal/testutil"
)

func TestReader(t *testing.T) {
	db := testutil.MustDecodeBitGen

	errFuncs := map[string]func(error) bool{
		"IsUnexpectedEOF": func(err error) bool { return err == io.ErrUnexpectedEOF },
		"IsCorrupted":     errors.IsCorrupted,
		"IsDeprecated":    errors.IsDeprecated,
	}
	vectors := []struct {
		name   string // Sub-test name
		input  []byte // Test input string
		output []byte // Expected output string
		inIdx  int64  // Expected input offset after reading
		outIdx int64  // Expected output offset after reading
		errf   string // Name of error checking callback
	}{{
		name: "EmptyString",
		errf: "IsUnexpectedEOF",
	}, {
		name:  "EmptyOutput",
		input: db(`>>> > "BZh9" H48:177245385090 H32:00000000`),
		inIdx: 14,
	}, {
		name: "EmptyOutput9S",
		input: db(`>>> >
			"BZh1" H48:177245385090 H32:00000000
			"BZh2" H48:177245385090 H32:00000000
			"BZh3" H48:177245385090 H32:00000000
			"BZh4" H48:177245385090 H32:00000000
			"BZh5" H48:177245385090 H32:00000000
			"BZh6" H48:177245385090 H32:00000000
			"BZh7" H48:177245385090 H32:00000000
			"BZh8" H48:177245385090 H32:00000000
			"BZh9" H48:177245385090 H32:00000000
		`),
		inIdx: 14 * 9, outIdx: 0,
	}, {
		name:  "InvalidStreamMagic",
		input: db(`>>> > "XX"`),
		inIdx: 2, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name:  "InvalidVersion",
		input: db(`>>> > "BZX1"`),
		inIdx: 3, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name:  "DeprecatedVersion",
		input: db(`>>> > "BZ01"`),
		inIdx: 3, outIdx: 0,
		errf: "IsDeprecated",
	}, {
		name:  "InvalidLevel",
		input: db(`>>> > "BZh0"`),
		inIdx: 4, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name:  "InvalidBlockMagic",
		input: db(`>>> > "BZh9" H48:000000000000`),
		inIdx: 10, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name:  "DeprecatedRandomization",
		input: db(`>>> > "BZh9" H48:314159265359 H32:8e9a7706 1 H24:0`),
		inIdx: 15, outIdx: 0,
		errf: "IsDeprecated",
	}, {
		name:  "Truncated1",
		input: db(`>>> "BZh9"`),
		inIdx: 4, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name:  "Truncated2",
		input: db(`>>> > "BZh9" H40:3141592653`),
		inIdx: 8, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name:  "Truncated3",
		input: db(`>>> > "BZh9" H48:314159265359`),
		inIdx: 10, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name:  "Truncated4",
		input: db(`>>> > "BZh9" H48:314159265359 H16:8e9a`),
		inIdx: 10, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name:  "Truncated5",
		input: db(`>>> > "BZh9" H48:314159265359 H32:8e9a7706`),
		inIdx: 14, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name:  "Truncated6",
		input: db(`>>> > "BZh9" H48:314159265359 H32:8e9a7706 0 H24:3`),
		inIdx: 18, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name:  "Truncated7",
		input: db(`>>> > "BZh9"  H48:314159265359 H32:8e9a7706 0 H24:3 < H16:00d4 H16:1003`),
		inIdx: 22, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name: "Truncated8",
		input: db(`>>>
			"BZh9"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
		`),
		inIdx: 33, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name: "Truncated9",
		input: db(`>>>
			"BZh9"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010
		`),
		inIdx: 39, outIdx: 0,
		errf: "IsUnexpectedEOF",
	}, {
		name: "Truncated10",
		input: db(`>>>
			"BZh9"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
		`),
		output: []byte("Hello, world!"),
		inIdx:  41, outIdx: 13,
		errf: "IsUnexpectedEOF",
	}, {
		name: "HelloWorld",
		input: db(`>>>
			"BZh9"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		output: []byte("Hello, world!"),
		inIdx:  51, outIdx: 13,
	}, {
		name: "HelloWorld2B",
		input: db(`>>>
			"BZh9"

			( # Two blocks
				> H48:314159265359 H32:8e9a7706 0 H24:3
				< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
				> D3:2 D15:1 0
				> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
				> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
				< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			)*2

			> H48:177245385090 H32:93ae990b
		`),
		output: db(`>>> "Hello, world!"*2`),
		inIdx:  51*2 - 4 - 10, outIdx: 13 * 2,
	}, {
		name: "HelloWorld2S",
		input: db(`>>>
			( # Two streams
				"BZh9"

				> H48:314159265359 H32:8e9a7706 0 H24:3
				< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
				> D3:2 D15:1 0
				> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
				> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
				< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111

				> H48:177245385090 H32:8e9a7706
			)*2
		`),
		output: db(`>>> "Hello, world!"*2`),
		inIdx:  51 * 2, outIdx: 13 * 2,
	}, {
		name: "Banana0",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:87f465d8 0 H24:0
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:87f465d8
		`),
		output: []byte("Banana"),
		inIdx:  42, outIdx: 6,
	}, {
		name: "Banana1",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:71d297e8 0 H24:1
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:71d297e8
		`),
		output: []byte("aBanan"),
		inIdx:  42, outIdx: 6,
	}, {
		name: "Banana2",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:21185406 0 H24:2
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:21185406
		`),
		output: []byte("anaBan"),
		inIdx:  42, outIdx: 6,
	}, {
		name: "Banana3",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:be853f46 0 H24:3
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:be853f46
		`),
		output: []byte("ananaB"),
		inIdx:  42, outIdx: 6,
	}, {
		name: "Banana4",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:35a020df 0 H24:4
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:35a020df
		`),
		output: []byte("naBana"),
		inIdx:  42, outIdx: 6,
	}, {
		name: "Banana5",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:b599e6fc 0 H24:5
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:b599e6fc
		`),
		output: []byte("nanaBa"),
		inIdx:  42, outIdx: 6,
	}, {
		// This is invalid since the BWT pointer exceeds the block size.
		name: "Banana6",
		input: db(`>>>
			> "BZh1" H48:314159265359 H32:87f465d8 0 H24:6
			< H16:0050 H16:0004 H16:4002
			> D3:2 D15:1 0 D5:2 0 10100 0 1111110 10100 D5:3 0 0 110 0 0
			< 1111 0 01 0 0 01 011
			> H48:177245385090 H32:87f465d8
		`),
		inIdx: 42 - 10, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// There must be between 2..6 trees, inclusive. This test uses only 1.
		name: "MinTrees",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:1 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		inIdx: 28, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// Define more trees than allowed. The test uses 7.
		name: "MaxTrees",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:7 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			>(D5:4 0 0 0 0 0 0 0 0 110 0 0 0)*6
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		inIdx: 28, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// Define more trees and selectors than actually used.
		name: "SuboptimalTrees",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:6 D15:12 111110 11110 1110 110 10 0 111110 11110 1110 110 10 0
			>(D5:4 0 0 0 0 0 0 0 0 110 0 0 0)*5
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		output: []byte("Hello, world!"),
		inIdx:  66, outIdx: 13,
	}, {
		// Do not define any tree selectors. This should fail when decoding
		// the prefix codes later on.
		name: "MinTreeSels",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:0 # No selectors defined
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		inIdx: 35, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// Define up to 32767 tree selectors, even though only 1 is used.
		name: "MaxTreeSels",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:32767 0*32767 # Define all selectors
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		output: []byte("Hello, world!"),
		inIdx:  4147, outIdx: 13,
	}, {
		name: "InvalidTreeSels1",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 110 # Select tree2, which does not exist
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		inIdx: 30, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name: "InvalidTreeSels2",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:6 D15:1 111111 # Select tree6, which is invalid
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			>(D5:4 0 0 0 0 0 0 0 0 110 0 0 0)*5
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		inIdx: 31, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name: "JunkPadding",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0 D5:2 0 0 110 D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b 10101 # Non-zero padding bits
		`),
		output: []byte{0x00},
		inIdx:  37, outIdx: 1,
	}, {
		name: "MinSymMap",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001 # Only one symbol used
			> D3:2 D15:1 0
			>(D5:2 0 0 110)*2
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		output: []byte{0x00},
		inIdx:  37, outIdx: 1,
	}, {
		// This block satisfies the minimum of 3 symbols for prefix encoding.
		// The data section terminates immediately with an EOF symbol.
		// However, this is still invalid since a BWT pointer of 0 >= 0.
		name: "EmptyBlock",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:00000000 0 H24:0 # BWT pointer of 0
			< H16:0001 H16:0001 # Only one symbol used
			> D3:2 D15:1 0
			>(D5:2 0 0 110)*2
			< 0 # Data ends immediately with EOF
			> H48:177245385090 H32:00000000
		`),
		inIdx: 27, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// The high-order symbol map says that all groups have symbols,
		// but only the first group indicates any symbols are set.
		name: "SuboptimalSymMap1",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:ffff H16:0001 H16:0000*15 # All groups used, only one symbol used
			> D3:2 D15:1 0
			>(D5:2 0 0 110)*2
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		output: []byte{0x00},
		inIdx:  67, outIdx: 1,
	}, {
		// The symbol map declares that all symbols are used, even though
		// only one is actually used.
		name: "SuboptimalSymMap2",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:ffff*17 # All symbols used
			> D3:2 D15:1 0
			> D5:2 0 10101010101010100 0*255 1111111111111111110
			> D5:9 0*4 110 0*253
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		output: []byte{0x00},
		inIdx:  135, outIdx: 1,
	}, {
		// It is impossible for the format to encode a block with no symbols
		// since at least one symbol must exist for the EOF symbol.
		name: "InvalidSymMap",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0000 # Need at least one symbol
		`),
		inIdx: 20, outIdx: 0,
		errf: "IsCorrupted", // Should not be IsUnexpectedEOF
	}, {
		name: "InvalidBlockChecksum",
		input: db(`>>>
			"BZh9"
			> H48:314159265359 H32:00000000 0 H24:3 # Bad checksum
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:8e9a7706
		`),
		output: []byte("Hello, world!"),
		inIdx:  51 - 10, outIdx: 13,
		errf: "IsCorrupted",
	}, {
		name: "InvalidStreamChecksum",
		input: db(`>>>
			"BZh9"
			> H48:314159265359 H32:8e9a7706 0 H24:3
			< H16:00d4 H16:1003 H16:0100 H16:9030 H16:0084
			> D3:2 D15:1 0
			> D5:4 0 0 0 0 0 110 100 0 110 0 0 100
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 1101 000 100 000 100 0111 010 010 0011 0001 110 0111 110 1111
			> H48:177245385090 H32:00000000 # Bad checksum
		`),
		output: []byte("Hello, world!"),
		inIdx:  51, outIdx: 13,
		errf: "IsCorrupted",
	}, {
		// RLE1 stage with maximum repeater length.
		name: "RLE1-1",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:e1fac440 0 H24:0
			< H16:8010 H16:0002 H16:8000
			> D3:2 D15:1 0
			> D5:2 0 100 11110 10100
			> D5:2 0 0 0 0
			< 0 0 01 01 111 # Pre-RLE1: "AAAA\xff"
			> H48:177245385090 H32:e1fac440
		`),
		output: db(`>>> X:41*259`),
		inIdx:  41, outIdx: 259,
	}, {
		// RLE1 stage with minimum repeater length.
		name: "RLE1-2",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:e16e6571 0 H24:4
			< H16:0011 H16:0001 H16:0002
			> D3:2 D15:1 0
			> D5:2 0 100 11110 10100
			> D5:2 0 0 0 0
			< 0 01 01 0 111 # Pre-RLE1: "AAAA\x00"
			> H48:177245385090 H32:e16e6571
		`),
		output: db(`>>> X:41*4`),
		inIdx:  41, outIdx: 4,
	}, {
		// RLE1 stage with missing repeater value.
		name: "RLE1-3",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:e16e6571 0 H24:3
			< H16:0010 H16:0002
			> D3:2 D15:1 0
			>(D5:2 0 0 110)*2
			< 11 01 0 # Pre-RLE1: "AAAA"
			> H48:177245385090 H32:e16e6571
		`),
		output: db(`>>> X:41*4`),
		inIdx:  37 - 10, outIdx: 4,
		errf: "IsCorrupted",
	}, {
		// RLE1 stage with sub-optimal repeater usage.
		name: "RLE1-4",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:f59a903a 0 H24:9
			< H16:0011 H16:0001 H16:0002
			> D3:2 D15:1 0
			> D5:1 0 10100 110 100
			> D5:2 0 0 0 0
			< 01 0 0 0 01 0 111 # Pre-RLE1: "AAAA\x00AAAA\x00"
			> H48:177245385090 H32:f59a903a
		`),
		output: db(`>>> X:41*8`),
		inIdx:  41, outIdx: 8,
	}, {
		// RLE1 stage with sub-optimal repeater usage.
		name: "RLE1-5",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:f59a903a 0 H24:4
			< H16:0011 H16:0002 H16:0002
			> D3:2 D15:1 0
			> D5:3 0 110 110 10100
			> D5:2 0 0 0 0
			< 0 01 01 0 111 # Pre-RLE1: "AAAA\x01AAA"
			> H48:177245385090 H32:f59a903a
		`),
		output: db(`>>> X:41*8`),
		inIdx:  40, outIdx: 8,
	}, {
		name: "RLE2-1",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:6b4f087c 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:1 0 100 100 0
			> D5:2 0 0 0 0
			< 01 0 0 0 0 01 0 01 0 01 01 0 0 0 0 01 111 # a*100k
			> H48:177245385090 H32:6b4f087c
		`),
		output: db(`>>> "a"*2020000`),
		inIdx:  40, outIdx: 2020000,
	}, {
		name: "RLE2-2",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:d175ea9d 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:1 0 100 100 0
			> D5:2 0 0 0 0
			< 0 01 0 0 0 01 0 01 0 01 01 0 0 0 0 01 111 # a*(100k+1)
			> H48:177245385090 H32:d175ea9d
		`),
		inIdx: 40 - 10, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name: "RLE2-3",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:6b4f087c 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:1 0 100 100 0
			> D5:2 0 0 0 0
			< 0 0 0 0 0 01 0 01 0 01 01 0 0 0 0 01 011 111 # a*(100k-1) b
			> H48:177245385090 H32:6b4f087c
		`),
		output: db(`>>> "a"*2020000`),
		inIdx:  40, outIdx: 2020000,
	}, {
		name: "RLE2-4",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:d175ea9d 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:1 0 100 100 0
			> D5:2 0 0 0 0
			< 0 0 0 0 0 01 0 01 0 01 01 0 0 0 0 01 011 011 111 # a*(100k-1) b a
			> H48:177245385090 H32:d175ea9d
		`),
		inIdx: 40 - 10, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		name: "RLE2-5",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:79235035 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:1 0 100 100 0
			> D5:2 0 0 0 0
			< 0 0 0 0 0 01 0 01 0 01 01 0 0 0 0 01 011 0 011 111 # a*(100k-1) b*2 a
			> H48:177245385090 H32:79235035
		`),
		inIdx: 41 - 10, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// This input has a sequence of RUNA and RUNB symbols that tries to
		// cause an integer overflow in the numeral decoding.
		name: "RLE2-6",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:6b4f087c 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:1 0 100 100 0
			> D5:2 0 0 0 0
			< 0*32 111
			> H48:177245385090 H32:6b4f087c
		`),
		inIdx: 41 - 10, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// This is valid, but with suboptimal clen value.
		name: "PrefixBits1",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:1 100 0 110 # (1)+1=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		output: []byte{0x00},
		inIdx:  37, outIdx: 1,
	}, {
		// This is invalid, clen starts at 0.
		name: "PrefixBits2",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:0 10100 0 110 # (0)+2=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		inIdx: 25, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// This is valid, although suboptimal, since clen stays within bounds.
		name: "PrefixBits3",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:4 11*3 10*19 11*18 0 0 110 # (4)-3+19-18=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		output: []byte{0x00},
		inIdx:  47, outIdx: 1,
	}, {
		// This is invalid since clen temporarily goes over max bits,
		// even though the end value is still "valid".
		name: "PrefixBits4",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:4 11*3 10*20 11*19 0 0 110 # (4)-3+20-19=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		inIdx: 30, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// This is invalid since clen temporarily hits zero,
		// even though the end value is still "valid".
		name: "PrefixBits5",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:4 11*4 10*20 11*18 0 0 110 # (4)-4+20-18=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		inIdx: 26, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// This is valid since clen starts at max bits and works down.
		name: "PrefixBits6",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:20 11*18 0 0 110 # (20)-18=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		output: []byte{0x00},
		inIdx:  41, outIdx: 1,
	}, {
		// This is invalid because clen starts at 21, which violates max bits.
		name: "PrefixBits7",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:1 0
			> D5:21 11*19 0 0 110 # (21)-19=2 (2)=2 (2)-1=1
			> D5:2 0 0 110
			< 01 0
			> H48:177245385090 H32:b1f7404b
		`),
		inIdx: 25, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// There are way more prefix symbols in this block than the format
		// even allows. The prefix decoder should detect this cause and
		// report the stream as corrupted.
		name: "MaxPrefixSymbols",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:b1f7404b 0 H24:0
			< H16:0001 H16:0001
			> D3:2 D15:32767 0*32767 # Define all selectors
			> D5:1 0 100 0
			> D5:2 0 0 110
			< H64:0*1000000 11 # 64M symbols
			> H48:177245385090 H32:b1f7404b
		`),
		inIdx: 16622, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// Use of an over-subscribed tree.
		name: "PrefixTrees1",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:952735b9 0 H24:000000
			< H16:0008 H16:03ff
			> D3:2 D15:1 0
			> D5:5 0 110 0 0 0 0 0 110 0 0 0 0
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 110 0101 1101 0011 1011 0111 000 100 010 110 001
			> H48:177245385090 H32:952735b9
		`),
		output: []byte("03791589269"),
		inIdx:  44, outIdx: 11,
	}, {
		// Use of an under-subscribed tree.
		name: "PrefixTrees2",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:58fdd3b0 0 H24:000000
			< H16:0008 H16:03ff
			> D3:2 D15:1 0
			> D5:5 0 0 0 0 110 0 0 110 0 0 0 0
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 000 100 00111 1101 11011 10111 0101 010 0011 110 01011 001
			> H48:177245385090 H32:58fdd3b0
		`),
		output: []byte("071876222607"),
		inIdx:  45, outIdx: 12,
	}, {
		// Use of an under-subscribed tree and using an invalid symbol.
		name: "PrefixTrees3",
		input: db(`>>>
			"BZh1"
			> H48:314159265359 H32:58fdd3b0 0 H24:000000
			< H16:0008 H16:03ff
			> D3:2 D15:1 0
			> D5:5 0 0 0 0 110 0 0 110 0 0 0 0
			> D5:4 0 0 0 0 0 0 0 0 110 0 0 0
			< 000 100 00111 1101 11011 10111 0101 010 0011 110 01011 1111 001
			> H48:177245385090 H32:58fdd3b0
		`),
		inIdx: 35, outIdx: 0,
		errf: "IsCorrupted",
	}, {
		// The BWT should be a permutation, but the use of an origin pointer
		// means that the transformation is not a pure bijective function.
		// Thus, there are some inputs where the input is not permuted.
		// This test makes sure we have the same behavior as the C library.
		// No sane encoder should ever output a BWT transform like this.
		name: "NonReversibleBWT",
		input: db(`>>>
			"BZh6"
			> H48:314159265359 H32:01007588 0 H24:000000
			< H16:0040 H16:0006
			> D3:2 D15:1 0
			> D5:3 0 110 110 10100
			> D5:2 0 0 0 0
			< 011 011 0 0 01 0 0 01 0 0 01 0 0 01 0 111
			> H48:177245385090 H32:01007588
		`),
		output: db(`>>> "a"*404`),
		inIdx:  40, outIdx: 404,
	}, {
		// The next "stream" is only a single byte 0x30, which the Reader
		// detects as being truncated since it loads 2 bytes for the magic.
		name:  "Fuzz1",
		input: db(`>>> > "BZh8" H48:177245385090 H32:00000000 X:30`),
		inIdx: 14, outIdx: 0,
		errf: "IsUnexpectedEOF", // Could be IsCorrupted
	}, {
		// Compared to Fuzz1, the next "stream" has 2 bytes 0x3030,
		// which allows the Reader to properly compare with the magic header
		// and reject the stream as invalid.
		name:  "Fuzz2",
		input: db(`>>> > "BZh8" H48:177245385090 H32:00000000 X:3030`),
		inIdx: 16, outIdx: 0,
		errf: "IsCorrupted",
	}}

	for _, v := range vectors {
		t.Run(v.name, func(t *testing.T) {
			rd, err := NewReader(bytes.NewReader(v.input), nil)
			if err != nil {
				t.Fatalf("unexpected NewReader error: %v", err)
			}
			output, err := ioutil.ReadAll(rd)
			if cerr := rd.Close(); cerr != nil {
				err = cerr
			}

			if got, want, ok := testutil.BytesCompare(output, v.output); !ok {
				t.Errorf("output mismatch:\ngot  %s\nwant %s", got, want)
			}
			if rd.InputOffset != v.inIdx {
				t.Errorf("input offset mismatch: got %d, want %d", rd.InputOffset, v.inIdx)
			}
			if rd.OutputOffset != v.outIdx {
				t.Errorf("output offset mismatch: got %d, want %d", rd.OutputOffset, v.outIdx)
			}
			if v.errf != "" && !errFuncs[v.errf](err) {
				t.Errorf("mismatching error:\ngot %v\nwant %s(err) == true", err, v.errf)
			} else if v.errf == "" && err != nil {
				t.Errorf("unexpected error: got %v", err)
			}

			// If the zcheck flag is set, then we verify that the test vectors
			// themselves are consistent with what the C bzip2 library outputs.
			if *zcheck {
				output, err := cmdDecompress(v.input)
				if got, want := bool(v.errf == ""), bool(err == nil); got != want {
					t.Errorf("pass mismatch: got %v, want %v", got, err)
				}
				if got, want, ok := testutil.BytesCompare(output, v.output); !ok && err == nil {
					t.Errorf("output mismatch:\ngot  %s\nwant %s", got, want)
				}
			}
		})
	}
}

func BenchmarkDecode(b *testing.B) {
	runBenchmarks(b, func(b *testing.B, data []byte, lvl int) {
		b.StopTimer()
		b.ReportAllocs()

		buf := new(bytes.Buffer)
		wr, _ := NewWriter(buf, &WriterConfig{Level: lvl})
		wr.Write(data)
		wr.Close()

		br := new(bytes.Reader)
		rd := new(Reader)

		b.SetBytes(int64(len(data)))
		b.StartTimer()
		for i := 0; i < b.N; i++ {
			br.Reset(buf.Bytes())
			rd.Reset(br)

			n, err := io.Copy(ioutil.Discard, rd)
			if n != int64(len(data)) || err != nil {
				b.Fatalf("Copy() = (%d, %v), want (%d, nil)", n, err, len(data))
			}
			if err := rd.Close(); err != nil {
				b.Fatalf("Close() = %v, want nil", err)
			}
		}
	})
}