Check-in [ea97951fcd]
Overview
SHA1:ea97951fcdfd21a1a3c66adbefb8253c4ce4359d
Date: 2015-01-01 06:34:04
User: spaskalev
Comment:Fixed Decode's length return and added a test for it. Reduce encoder's buffer to 2 bytes. CC at 98.3%
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2015-01-01
14:11
[4b9f9bff39] Initial implementation and test of fibonacci.Reader(io.Reader) io.Reader that decodes bytes encoded from fibonacci.Writer(..). CC at 97.5% (user: spaskalev, tags: trunk)
06:34
[ea97951fcd] Fixed Decode's length return and added a test for it. Reduce encoder's buffer to 2 bytes. CC at 98.3% (user: spaskalev, tags: trunk)
05:30
[c7c8d6445f] Added a proper test for fibonacci.Writer that writes 0-255, converts the resulting bits to a string and compares against the result of Numbers.Code for each value. (user: spaskalev, tags: trunk)
Changes

Modified src/0dev.org/encoding/fibonacci/fib.go from [2d2f3f4577] to [bc261fa076].

    69     69   			length += 2
    70     70   			continue
    71     71   		}
    72     72   
    73     73   		value >>= 1
    74     74   		length++
    75     75   	}
    76         -	result += f[length] - 1
    77         -	return
           76  +	return result + f[length] - 1, length + 1
    78     77   }
    79     78   
    80     79   func Writer(target io.Writer) io.Writer {
    81     80   	var enc encoder
    82         -	enc.Numbers = New(16)
           81  +	enc.Numbers = New(14)
    83     82   	enc.target = target
    84     83   	enc.buffer = enc.backing[:0:len(enc.backing)]
    85     84   	return &enc
    86     85   }
    87     86   
    88     87   type encoder struct {
    89     88   	Numbers
    90     89   	target    io.Writer
    91         -	backing   [3]byte // TODO - verify that this can be reduced to 2 bytes
           90  +	backing   [2]byte
    92     91   	buffer    []byte
    93     92   	remaining byte
    94     93   	length    byte
    95     94   }
    96     95   
    97     96   func (e *encoder) Write(input []byte) (int, error) {
    98     97   	var (
................................................................................
   173    172   // 	Numbers
   174    173   // 	source io.Reader
   175    174   // 	buffer uint64
   176    175   // 	at     byte
   177    176   // }
   178    177   
   179    178   // func (d *decoder) Read(output []byte) (int, error) {
   180         -// 	return 0, nil
          179  +// 	var (
          180  +// 		total int
          181  +// 		err   error
          182  +// 	)
          183  +
          184  +// 	// While we have suitable buffered data and enough output space
          185  +// 	for (len(output) > 0) && ((d.buffer & (d.buffer >> 1)) > 0) {
          186  +// 		val, len := d.Decode(d.buffer)
          187  +
          188  +// 		// Store the decoded byte
          189  +// 		output[0] = byte(val)
          190  +
          191  +// 		// Advance the internal and output buffers
          192  +// 		output = output[1:]
          193  +// 		d.buffer >>= len
          194  +
          195  +// 		// TODO - decrease d.at as well ?
          196  +
          197  +// 		total++
          198  +// 	}
          199  +
          200  +// 	// Termination condition
          201  +// 	if len(output) == 0 {
          202  +// 		return total, nil
          203  +// 	}
          204  +
          205  +// 	// count, err := d.source.Read(output)
          206  +
          207  +// 	return total, err
   181    208   // }

Modified src/0dev.org/encoding/fibonacci/fib_test.go from [5ea0aed96e] to [0734e0cdf2].

    20     20   	}
    21     21   }
    22     22   
    23     23   func TestCoding(t *testing.T) {
    24     24   	n := New(32)
    25     25   
    26     26   	for i := uint64(0); i < 4096; i++ {
    27         -		enc, _ := n.Code(i)
    28         -		//fmt.Printf("%d - %b, %d\n", i, enc, len)
    29         -		dec, _ := n.Decode(enc)
           27  +		enc, encLen := n.Code(i)
           28  +		dec, decLen := n.Decode(enc)
           29  +
    30     30   		if i != dec {
    31     31   			t.Errorf("Unexpected value for %d - enc is %b, dec is %d\n", i, enc, dec)
    32     32   		}
           33  +		if encLen != decLen {
           34  +			t.Errorf("Unexpected difference between encoded and decoded lengths.", encLen, decLen)
           35  +		}
    33     36   	}
    34     37   }
    35     38   
    36     39   func TestWriter(t *testing.T) {
    37     40   	var buf bytes.Buffer
    38     41   	var w io.Writer = Writer(&buf)
    39     42   	var input []byte = make([]byte, 256)