Index: src/0dev.org/encoding/fibonacci/fib.go ================================================================== --- src/0dev.org/encoding/fibonacci/fib.go +++ src/0dev.org/encoding/fibonacci/fib.go @@ -122,10 +122,13 @@ e.length += len len -= added // Not enough bits to write if e.length < 8 { + // Account for the processed input byte + total++ + continue } // Clearing e.length is not necessary as it will be overwritten later @@ -148,11 +151,11 @@ // Abort write on error if err != nil { break } - // Account for the just-written byte + // Account for the processed input byte total++ // Clear the buffer e.buffer = e.buffer[:0] } 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 @@ -1,11 +1,11 @@ package fibonacci import ( "bytes" - "fmt" "io" + "strings" "testing" ) func TestNumbers(t *testing.T) { n := New(32) @@ -31,69 +31,59 @@ t.Errorf("Unexpected value for %d - enc is %b, dec is %d\n", i, enc, dec) } } } -func TestWriter0(t *testing.T) { - var buf bytes.Buffer - var w io.Writer = Writer(&buf) - - w.Write([]byte{0}) - w.Write(nil) - for _, v := range buf.Bytes() { - fmt.Printf("%s-", b2s(v)) - } - fmt.Println() -} - -func TestWriter1(t *testing.T) { - var buf bytes.Buffer - var w io.Writer = Writer(&buf) - - w.Write([]byte{0, 1}) - w.Write(nil) - for _, v := range buf.Bytes() { - fmt.Printf("%s-", b2s(v)) - } - fmt.Println() -} - -func TestWriter2(t *testing.T) { - var buf bytes.Buffer - var w io.Writer = Writer(&buf) - - w.Write([]byte{0, 1, 2}) - w.Write(nil) - for _, v := range buf.Bytes() { - fmt.Printf("%s-", b2s(v)) - } - fmt.Println() -} - -func TestWriter3(t *testing.T) { - var buf bytes.Buffer - var w io.Writer = Writer(&buf) - - var input []byte = make([]byte, 256) - for i := 0; i < 256; i++ { +func TestWriter(t *testing.T) { + var buf bytes.Buffer + var w io.Writer = Writer(&buf) + var input []byte = make([]byte, 256) + var fib Numbers = New(16) + + for i := uint64(0); i < 256; i++ { input[i] = byte(i) } - w.Write(input) - w.Write(nil) + // Write the input + count, err := w.Write(input) + if count != len(input) { + t.Error("Unexpected write count", count) + } + if err != nil { + t.Error("Unexpected write error", err.Error()) + } + + // Flush remaining bits + count, err = w.Write(nil) + if count != 0 { + t.Error("Unexpected write count while flushing", count) + } + if err != nil { + t.Error("Unexpected write error while flushing", err.Error()) + } + + var output string for _, v := range buf.Bytes() { - fmt.Printf("%s-", b2s(v)) + output += u2s(uint64(v), 8) } - fmt.Println() + + for i, v := range input { + c, l := fib.Code(uint64(v)) + vs := u2s(c, l) + if loc := strings.Index(output, vs); loc != 0 { + t.Fatal("Unexpected location for", i, "value", vs) + } + output = output[len(vs):] + } } -func b2s(b byte) (result string) { - for i := 0; i < 8; i++ { - if b&1 > 0 { +func u2s(b uint64, l byte) (result string) { + for i := byte(0); i < l; i++ { + if (b & 1) > 0 { result += "1" } else { result += "0" } b >>= 1 } return }