Home | History | Annotate | Download | only in flate
      1 // Copyright 2012 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 flate
      6 
      7 import (
      8 	"testing"
      9 )
     10 
     11 func TestForwardCopy(t *testing.T) {
     12 	testCases := []struct {
     13 		dst0, dst1 int
     14 		src0, src1 int
     15 		want       string
     16 	}{
     17 		{0, 9, 0, 9, "012345678"},
     18 		{0, 5, 4, 9, "45678"},
     19 		{4, 9, 0, 5, "01230"},
     20 		{1, 6, 3, 8, "34567"},
     21 		{3, 8, 1, 6, "12121"},
     22 		{0, 9, 3, 6, "345"},
     23 		{3, 6, 0, 9, "012"},
     24 		{1, 6, 0, 9, "00000"},
     25 		{0, 4, 7, 8, "7"},
     26 		{0, 1, 6, 8, "6"},
     27 		{4, 4, 6, 9, ""},
     28 		{2, 8, 6, 6, ""},
     29 		{0, 0, 0, 0, ""},
     30 	}
     31 	for _, tc := range testCases {
     32 		b := []byte("0123456789")
     33 		n := tc.dst1 - tc.dst0
     34 		if tc.src1-tc.src0 < n {
     35 			n = tc.src1 - tc.src0
     36 		}
     37 		forwardCopy(b, tc.dst0, tc.src0, n)
     38 		got := string(b[tc.dst0 : tc.dst0+n])
     39 		if got != tc.want {
     40 			t.Errorf("dst=b[%d:%d], src=b[%d:%d]: got %q, want %q",
     41 				tc.dst0, tc.dst1, tc.src0, tc.src1, got, tc.want)
     42 		}
     43 		// Check that the bytes outside of dst[:n] were not modified.
     44 		for i, x := range b {
     45 			if i >= tc.dst0 && i < tc.dst0+n {
     46 				continue
     47 			}
     48 			if int(x) != '0'+i {
     49 				t.Errorf("dst=b[%d:%d], src=b[%d:%d]: copy overrun at b[%d]: got '%c', want '%c'",
     50 					tc.dst0, tc.dst1, tc.src0, tc.src1, i, x, '0'+i)
     51 			}
     52 		}
     53 	}
     54 }
     55