@@ -798,3 +798,62 @@ func TestBestSpeedMatch(t *testing.T) {
798
798
}
799
799
}
800
800
}
801
+
802
+ func TestBestSpeedMaxMatchOffset (t * testing.T ) {
803
+ const abc , xyz = "abcdefgh" , "stuvwxyz"
804
+ for _ , matchBefore := range []bool {false , true } {
805
+ for _ , extra := range []int {0 , inputMargin - 1 , inputMargin , inputMargin + 1 , 2 * inputMargin } {
806
+ for offsetAdj := - 5 ; offsetAdj <= + 5 ; offsetAdj ++ {
807
+ report := func (desc string , err error ) {
808
+ t .Errorf ("matchBefore=%t, extra=%d, offsetAdj=%d: %s%v" ,
809
+ matchBefore , extra , offsetAdj , desc , err )
810
+ }
811
+
812
+ offset := maxMatchOffset + offsetAdj
813
+
814
+ // Make src to be a []byte of the form
815
+ // "%s%s%s%s%s" % (abc, zeros0, xyzMaybe, abc, zeros1)
816
+ // where:
817
+ // zeros0 is approximately maxMatchOffset zeros.
818
+ // xyzMaybe is either xyz or the empty string.
819
+ // zeros1 is between 0 and 30 zeros.
820
+ // The difference between the two abc's will be offset, which
821
+ // is maxMatchOffset plus or minus a small adjustment.
822
+ src := make ([]byte , offset + len (abc )+ extra )
823
+ copy (src , abc )
824
+ if ! matchBefore {
825
+ copy (src [offset - len (xyz ):], xyz )
826
+ }
827
+ copy (src [offset :], abc )
828
+
829
+ buf := new (bytes.Buffer )
830
+ w , err := NewWriter (buf , BestSpeed )
831
+ if err != nil {
832
+ report ("NewWriter: " , err )
833
+ continue
834
+ }
835
+ if _ , err := w .Write (src ); err != nil {
836
+ report ("Write: " , err )
837
+ continue
838
+ }
839
+ if err := w .Close (); err != nil {
840
+ report ("Writer.Close: " , err )
841
+ continue
842
+ }
843
+
844
+ r := NewReader (buf )
845
+ dst , err := ioutil .ReadAll (r )
846
+ r .Close ()
847
+ if err != nil {
848
+ report ("ReadAll: " , err )
849
+ continue
850
+ }
851
+
852
+ if ! bytes .Equal (dst , src ) {
853
+ report ("" , fmt .Errorf ("bytes differ after round-tripping" ))
854
+ continue
855
+ }
856
+ }
857
+ }
858
+ }
859
+ }
0 commit comments