github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/compress/flate/copy_test.go (about) 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 dst := b[tc.dst0:tc.dst1] 34 src := b[tc.src0:tc.src1] 35 n := forwardCopy(dst, src) 36 got := string(dst[:n]) 37 if got != tc.want { 38 t.Errorf("dst=b[%d:%d], src=b[%d:%d]: got %q, want %q", 39 tc.dst0, tc.dst1, tc.src0, tc.src1, got, tc.want) 40 } 41 // Check that the bytes outside of dst[:n] were not modified. 42 for i, x := range b { 43 if i >= tc.dst0 && i < tc.dst0+n { 44 continue 45 } 46 if int(x) != '0'+i { 47 t.Errorf("dst=b[%d:%d], src=b[%d:%d]: copy overrun at b[%d]: got '%c', want '%c'", 48 tc.dst0, tc.dst1, tc.src0, tc.src1, i, x, '0'+i) 49 } 50 } 51 } 52 }