Skip to content

Commit

Permalink
snappy: make TestFramingFormat not depend on downloading (separately
Browse files Browse the repository at this point in the history
licensed) testdata files.

Such files are now only used for benchmarks (they are the same files used
by the C++ snappy code).

LGTM=dsymonds
R=dsymonds
CC=golang-codereviews
https://codereview.appspot.com/201220043
  • Loading branch information
nigeltao committed Feb 10, 2015
1 parent ec2b2f9 commit 156a073
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions snappy/snappy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func TestSmallCopy(t *testing.T) {
}

func TestSmallRand(t *testing.T) {
rand.Seed(27354294)
rng := rand.New(rand.NewSource(27354294))
for n := 1; n < 20000; n += 23 {
b := make([]byte, n)
for i := range b {
b[i] = uint8(rand.Uint32())
b[i] = uint8(rng.Uint32())
}
if err := roundtrip(b, nil, nil); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -95,26 +95,34 @@ func cmp(a, b []byte) error {
}

func TestFramingFormat(t *testing.T) {
for _, tf := range testFiles {
if err := downloadTestdata(tf.filename); err != nil {
t.Fatalf("failed to download testdata: %s", err)
}
src := readFile(t, filepath.Join(*testdata, tf.filename))
buf := new(bytes.Buffer)
if _, err := NewWriter(buf).Write(src); err != nil {
t.Errorf("%s: encoding: %v", tf.filename, err)
continue
}
dst, err := ioutil.ReadAll(NewReader(buf))
if err != nil {
t.Errorf("%s: decoding: %v", tf.filename, err)
continue
}
if err := cmp(dst, src); err != nil {
t.Errorf("%s: %v", tf.filename, err)
continue
// src is comprised of alternating 1e5-sized sequences of random
// (incompressible) bytes and repeated (compressible) bytes. 1e5 was chosen
// because it is larger than maxUncompressedChunkLen (64k).
src := make([]byte, 1e6)
rng := rand.New(rand.NewSource(1))
for i := 0; i < 10; i++ {
if i%2 == 0 {
for j := 0; j < 1e5; j++ {
src[1e5*i+j] = uint8(rng.Intn(256))
}
} else {
for j := 0; j < 1e5; j++ {
src[1e5*i+j] = uint8(i)
}
}
}

buf := new(bytes.Buffer)
if _, err := NewWriter(buf).Write(src); err != nil {
t.Fatalf("Write: encoding: %v", err)
}
dst, err := ioutil.ReadAll(NewReader(buf))
if err != nil {
t.Fatalf("ReadAll: decoding: %v", err)
}
if err := cmp(dst, src); err != nil {
t.Fatal(err)
}
}

func TestReaderReset(t *testing.T) {
Expand Down

0 comments on commit 156a073

Please sign in to comment.