func BenchmarkDecompressor(b *testing.B) { r := zdateReader(100, true) buf, err := ioutil.ReadAll(r) if err != nil { b.Fatalf("%v", err) } b.SetBytes(int64(len(buf))) // determine buffer size to read all the data // in a single call r.Seek(0, 0) d := capn.NewDecompressor(r) buf, err = ioutil.ReadAll(d) if err != nil { b.Fatalf("%v", err) } b.ResetTimer() for i := 0; i < b.N || benchForever; i++ { r.Seek(0, 0) n, err := d.Read(buf) if err != nil { b.Fatalf("%v", err) } if n != len(buf) { b.Fatal("short read") } } }
func TestDecompressorVerbosely(t *testing.T) { cv.Convey("Testing the go-capnproto Decompressor.Read() function:", t, func() { for _, test := range compressionTests { fmt.Printf("\n\nGiven compressed text '%#v'\n", test.compressed) // fmt.Printf(" test.original = %#v\n test.compressed = %#v\n", test.original, test.compressed) for readSize := 1; readSize <= 8+2*len(test.original); readSize++ { fmt.Printf("\n When we use go-capnproto NewDecompressor, with readSize: %d\n Then we should get the original text back.", readSize) r := bytes.NewReader(test.compressed) d := capn.NewDecompressor(r) buf := make([]byte, readSize) var actual []byte for { n, err := d.Read(buf) actual = append(actual, buf[:n]...) if err != nil { if err == io.EOF { break } t.Fatalf("Read: %v", err) } } cv.So(len(actual), cv.ShouldResemble, len(test.original)) if len(test.original) > 0 { cv.So(actual, cv.ShouldResemble, test.original) } } // end readSize loop } fmt.Printf("\n\n") }) }
func TestDecodeOnKnownWellPackedData(t *testing.T) { // length 17 byteSliceIn := []byte{0x10, 0x5, 0x50, 0x2, 0x1, 0x1, 0x25, 0x0, 0x0, 0x11, 0x1, 0xc, 0xf, 0xd4, 0x7, 0xc, 0x7} fmt.Printf("len of byteSliceIn is %d\n", len(byteSliceIn)) // annotated: byteSliceIn := []byte{tag:0x10, 0x5, tag:0x50, 0x2, 0x1, tag:0x1, 0x25, tag:0x0, 0x0, tag:0x11, 0x1, 0xc, tag:0xf, 0xd4, 0x7, 0xc, 0x7} // length 48 expectedOut := []byte{0x0, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1, 0x0, 0x25, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0xd4, 0x7, 0xc, 0x7, 0x0, 0x0, 0x0, 0x0} r := bytes.NewReader(byteSliceIn) actual, err := ioutil.ReadAll(capn.NewDecompressor(r)) if err != nil { panic(err) } cv.Convey("Given a known-to-be-correctly packed 17-byte long sequence for a ZdateVector holding a single Zdate", t, func() { cv.Convey("When we use go-capnproto NewDecompressor", func() { cv.Convey("Then we should get the same unpacked bytes as capnp provides", func() { cv.So(len(actual), cv.ShouldResemble, len(expectedOut)) cv.So(actual, cv.ShouldResemble, expectedOut) }) }) }) }
func TestDecompressorUNPACKZdate2(t *testing.T) { const n = 2 un := zdateReader(n, false) expected, err := ioutil.ReadAll(un) fmt.Printf("expected: '%#v' of len(%d)\n", expected, len(expected)) // prints: // expected: []byte{ // 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, // 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1, 0x0, // 0x25, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 0x1, 0x0, 0x0, 0x0, 0x14, 0x0, 0x0, 0x0, // 0xd4, 0x7, 0xc, 0x7, 0xd5, 0x7, 0xc, 0x7, // 0xd4, 0x7, 0xc, 0x7, 0x0, 0x0, 0x0, 0x0, // 0xd5, 0x7, 0xc, 0x7, 0x0, 0x0, 0x0, 0x0} of len(64) if err != nil { t.Fatalf("ReadAll: %v", err) } //r = zdateReader(n, true) _, slicePacked := zdateFilledSegment(n, true) fmt.Printf("slicePacked = %#v\n", slicePacked) // prints slicePacked = []byte{ // tag0x10, 0x7, tag:0x50, 0x2, 0x1, tag0x1, 0x25, tag0x0, // 0x0, tag0x11, 0x1, 0x14, tag0xff, 0xd4, 0x7, 0xc, // 0x7, 0xd5, 0x7, 0xc, 0x7, N:0x2, 0xd4, 0x7, // 0xc, 0x7, 0x0, 0x0, 0x0, 0x0, 0xd5, 0x7, // 0xc, 0x7, 0x0, 0x0, 0x0, 0x0} pa := bytes.NewReader(slicePacked) actual, err := ioutil.ReadAll(capn.NewDecompressor(pa)) if err != nil { t.Fatalf("ReadAll: %v", err) } cv.Convey("Given the []byte slice from a capnp conversion from packed to unpacked form of a two Zdate vector", t, func() { cv.Convey("When we use go-capnproto NewDecompressor", func() { cv.Convey("Then we should get the same unpacked bytes as capnp provides", func() { cv.So(len(actual), cv.ShouldResemble, len(expected)) cv.So(actual, cv.ShouldResemble, expected) }) }) }) }
func TestDecompressorZdate1(t *testing.T) { const n = 1 r := zdateReader(n, false) expected, err := ioutil.ReadAll(r) if err != nil { t.Fatalf("ReadAll: %v", err) } r = zdateReader(n, true) actual, err := ioutil.ReadAll(capn.NewDecompressor(r)) if err != nil { t.Fatalf("ReadAll: %v", err) } if !bytes.Equal(expected, actual) { fmt.Printf("expected to get: '%s'\n actually observed instead: '%s'\n", expected, actual) t.Fatal("decompressor failed") } }
func TestDecompressor(t *testing.T) { errCount := 0 for i, test := range compressionTests { for readSize := 1; readSize <= 8+2*len(test.original); readSize++ { r := bytes.NewReader(test.compressed) d := capn.NewDecompressor(r) buf := make([]byte, readSize) var actual []byte for { n, err := d.Read(buf) actual = append(actual, buf[:n]...) if err != nil { if err == io.EOF { break } t.Fatalf("Read: %v", err) } } if len(test.original) != len(actual) { errCount++ t.Errorf("test:%d readSize:%d expected %d bytes, got %d", i, readSize, len(test.original), len(actual)) continue } if !bytes.Equal(test.original, actual) { errCount++ t.Errorf("test:%d readSize:%d: bytes not equal", i, readSize) } } } if errCount == 0 { fmt.Printf("TestDecompressor() passed. (0 errors).\n") } }