From 6a6dac83659009dca16ab4a72fb1a3ce74ff0235 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 13 Nov 2014 21:48:04 -0800 Subject: [PATCH] erasure: Coding blocks during encoding were garbage, fixes #21 --- erasure/Makefile | 6 +++--- erasure/cauchy_test.go | 30 ++++++++++++++---------------- erasure/decode.go | 1 + erasure/encode.go | 10 +++++++--- erasure/vandermonde_test.go | 25 ++++++++++--------------- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/erasure/Makefile b/erasure/Makefile index 2e152c899..2e837a89b 100644 --- a/erasure/Makefile +++ b/erasure/Makefile @@ -4,13 +4,13 @@ all: build test test: cauchy vandermonde cauchy: - @go test -test.run="TestCauchy*" + @godep go test -test.run="TestCauchy*" vandermonde: - @go test -test.run="TestVanderMonde*" + @godep go test -test.run="TestVanderMonde*" isal/isal-l.so: @$(MAKE) --quiet -C isal build: isal/isal-l.so - @go build + @godep go build diff --git a/erasure/cauchy_test.go b/erasure/cauchy_test.go index 6da038911..dc7475b6b 100644 --- a/erasure/cauchy_test.go +++ b/erasure/cauchy_test.go @@ -18,33 +18,33 @@ package erasure import ( "bytes" + . "gopkg.in/check.v1" "testing" ) -func TestCachyEncode(t *testing.T) { +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func Test(t *testing.T) { TestingT(t) } + +func (s *MySuite) TestCachyEncode(c *C) { ep, _ := ValidateParams(10, 5, 8, CAUCHY) p := NewEncoder(ep) data := make([]byte, 1000) - chunks, length := p.Encode(data) - - t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) - if length != len(data) { - t.Fatal() - } + _, length := p.Encode(data) + c.Assert(length, Equals, len(data)) } -func TestCauchyDecode(t *testing.T) { +func (s *MySuite) TestCauchyDecode(c *C) { ep, _ := ValidateParams(10, 5, 8, CAUCHY) data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") p := NewEncoder(ep) chunks, length := p.Encode(data) - t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) - if length != len(data) { - t.Fatal() - } + c.Assert(length, Equals, len(data)) chunks[0] = nil chunks[3] = nil @@ -53,11 +53,9 @@ func TestCauchyDecode(t *testing.T) { chunks[13] = nil recovered_data, err := p.Decode(chunks, length) - if err != nil { - t.Fatalf("Error: %s", err) - } + c.Assert(err, Not(IsNil)) if i := bytes.Compare(recovered_data, data); i < 0 { - t.Fatalf("Error: recovered data is less than original data") + c.Fatalf("Error: recovered data is less than original data") } } diff --git a/erasure/decode.go b/erasure/decode.go index 0f3a268d1..9b152180b 100644 --- a/erasure/decode.go +++ b/erasure/decode.go @@ -94,6 +94,7 @@ func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) { for i := range chunks { pointers[i] = &chunks[i][0] } + data := (**C.uchar)(unsafe.Pointer(&pointers[:k][0])) coding := (**C.uchar)(unsafe.Pointer(&pointers[k:][0])) diff --git a/erasure/encode.go b/erasure/encode.go index 1a2243bd6..bc88d0e16 100644 --- a/erasure/encode.go +++ b/erasure/encode.go @@ -181,18 +181,22 @@ func (e *Encoder) Encode(block []byte) ([][]byte, int) { pointers := make([]*byte, e.p.n) var i int - // Add data and code blocks to chunks - for i = 0; i < e.p.n; i++ { + // Add data blocks to chunks + for i = 0; i < e.p.k; i++ { chunks[i] = block[i*chunk_size : (i+1)*chunk_size] pointers[i] = &chunks[i][0] } + for i = e.p.k; i < e.p.n; i++ { + chunks[i] = make([]byte, chunk_size) + pointers[i] = &chunks[i][0] + } + data := (**C.uchar)(unsafe.Pointer(&pointers[:e.p.k][0])) coding := (**C.uchar)(unsafe.Pointer(&pointers[e.p.k:][0])) C.ec_encode_data(C.int(chunk_size), e.k, e.m, e.encode_tbls, data, coding) - return chunks, block_len } diff --git a/erasure/vandermonde_test.go b/erasure/vandermonde_test.go index 8e78c65a6..4e082ad35 100644 --- a/erasure/vandermonde_test.go +++ b/erasure/vandermonde_test.go @@ -18,33 +18,30 @@ package erasure import ( "bytes" - "testing" + . "gopkg.in/check.v1" ) -func TestVanderMondeEncode(t *testing.T) { +func (s *MySuite) TestVanderMondeEncode(c *C) { ep, _ := ValidateParams(10, 5, 8, VANDERMONDE) p := NewEncoder(ep) data := make([]byte, 1000) chunks, length := p.Encode(data) - t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) - if length != len(data) { - t.Fatal() - } + c.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) + c.Assert(length, Equals, len(data)) } -func TestVanderMondeDecode(t *testing.T) { +func (s *MySuite) TestVanderMondeDecode(c *C) { ep, _ := ValidateParams(10, 5, 8, VANDERMONDE) p := NewEncoder(ep) data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") chunks, length := p.Encode(data) - t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) - if length != len(data) { - t.Fatal() - } + c.Logf("chunks length: %d", len(chunks)) + c.Logf("length: %d", length) + c.Assert(length, Equals, len(data)) chunks[0] = nil chunks[3] = nil @@ -53,11 +50,9 @@ func TestVanderMondeDecode(t *testing.T) { chunks[13] = nil recovered_data, err := p.Decode(chunks, length) - if err != nil { - t.Fatalf("Error: %s", err) - } + c.Assert(err, Not(IsNil)) if i := bytes.Compare(recovered_data, data); i < 0 { - t.Fatalf("Error: recovered data is less than original data") + c.Fatalf("Error: recovered data is less than original data") } }