Index: src/0dev.org/encoding/fibonacci/fib.go ================================================================== --- src/0dev.org/encoding/fibonacci/fib.go +++ src/0dev.org/encoding/fibonacci/fib.go @@ -71,26 +71,25 @@ } value >>= 1 length++ } - result += f[length] - 1 - return + return result + f[length] - 1, length + 1 } func Writer(target io.Writer) io.Writer { var enc encoder - enc.Numbers = New(16) + enc.Numbers = New(14) enc.target = target enc.buffer = enc.backing[:0:len(enc.backing)] return &enc } type encoder struct { Numbers target io.Writer - backing [3]byte // TODO - verify that this can be reduced to 2 bytes + backing [2]byte buffer []byte remaining byte length byte } @@ -175,7 +174,35 @@ // buffer uint64 // at byte // } // func (d *decoder) Read(output []byte) (int, error) { -// return 0, nil +// var ( +// total int +// err error +// ) + +// // While we have suitable buffered data and enough output space +// for (len(output) > 0) && ((d.buffer & (d.buffer >> 1)) > 0) { +// val, len := d.Decode(d.buffer) + +// // Store the decoded byte +// output[0] = byte(val) + +// // Advance the internal and output buffers +// output = output[1:] +// d.buffer >>= len + +// // TODO - decrease d.at as well ? + +// total++ +// } + +// // Termination condition +// if len(output) == 0 { +// return total, nil +// } + +// // count, err := d.source.Read(output) + +// return total, err // } Index: src/0dev.org/encoding/fibonacci/fib_test.go ================================================================== --- src/0dev.org/encoding/fibonacci/fib_test.go +++ src/0dev.org/encoding/fibonacci/fib_test.go @@ -22,16 +22,19 @@ func TestCoding(t *testing.T) { n := New(32) for i := uint64(0); i < 4096; i++ { - enc, _ := n.Code(i) - //fmt.Printf("%d - %b, %d\n", i, enc, len) - dec, _ := n.Decode(enc) + enc, encLen := n.Code(i) + dec, decLen := n.Decode(enc) + if i != dec { t.Errorf("Unexpected value for %d - enc is %b, dec is %d\n", i, enc, dec) } + if encLen != decLen { + t.Errorf("Unexpected difference between encoded and decoded lengths.", encLen, decLen) + } } } func TestWriter(t *testing.T) { var buf bytes.Buffer