github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/importer/chunk/splitting_test.go (about)

     1  package chunk
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"testing"
     7  )
     8  
     9  func randBuf(t *testing.T, size int) []byte {
    10  	buf := make([]byte, size)
    11  	if _, err := rand.Read(buf); err != nil {
    12  		t.Fatal("failed to read enough randomness")
    13  	}
    14  	return buf
    15  }
    16  
    17  func copyBuf(buf []byte) []byte {
    18  	cpy := make([]byte, len(buf))
    19  	copy(cpy, buf)
    20  	return cpy
    21  }
    22  
    23  func TestSizeSplitterIsDeterministic(t *testing.T) {
    24  
    25  	test := func() {
    26  		bufR := randBuf(t, 10000000) // crank this up to satisfy yourself.
    27  		bufA := copyBuf(bufR)
    28  		bufB := copyBuf(bufR)
    29  
    30  		chunksA := DefaultSplitter.Split(bytes.NewReader(bufA))
    31  		chunksB := DefaultSplitter.Split(bytes.NewReader(bufB))
    32  
    33  		for n := 0; ; n++ {
    34  			a, moreA := <-chunksA
    35  			b, moreB := <-chunksB
    36  
    37  			if !moreA {
    38  				if moreB {
    39  					t.Fatal("A ended, B didnt.")
    40  				}
    41  				return
    42  			}
    43  
    44  			if !bytes.Equal(a, b) {
    45  				t.Fatalf("chunk %d not equal", n)
    46  			}
    47  		}
    48  	}
    49  
    50  	for run := 0; run < 1; run++ { // crank this up to satisfy yourself.
    51  		test()
    52  	}
    53  }