Check-in [c7c8d6445f]
Overview
SHA1:c7c8d6445fad8e8f2d7ec070150b0caee8211936
Date: 2015-01-01 05:30:46
User: spaskalev
Comment: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.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2015-01-01
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)
04:58
[93fcb281a1] Added fibonacci.Writer(io.Writer) io.Writer that encodes bytes. (user: spaskalev, tags: trunk)
Changes

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

   120    120   		// Shift the the encoded value and account for its length
   121    121   		enc >>= added
   122    122   		e.length += len
   123    123   		len -= added
   124    124   
   125    125   		// Not enough bits to write
   126    126   		if e.length < 8 {
          127  +			// Account for the processed input byte
          128  +			total++
          129  +
   127    130   			continue
   128    131   		}
   129    132   
   130    133   		// Clearing e.length is not necessary as it will be overwritten later
   131    134   
   132    135   		// Stage the complete byte for writing
   133    136   		e.buffer = append(e.buffer, byte(e.remaining))
................................................................................
   146    149   		_, err = e.target.Write(e.buffer)
   147    150   
   148    151   		// Abort write on error
   149    152   		if err != nil {
   150    153   			break
   151    154   		}
   152    155   
   153         -		// Account for the just-written byte
          156  +		// Account for the processed input byte
   154    157   		total++
   155    158   
   156    159   		// Clear the buffer
   157    160   		e.buffer = e.buffer[:0]
   158    161   	}
   159    162   	return total, err
   160    163   }

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

     1      1   package fibonacci
     2      2   
     3      3   import (
     4      4   	"bytes"
     5         -	"fmt"
     6      5   	"io"
            6  +	"strings"
     7      7   	"testing"
     8      8   )
     9      9   
    10     10   func TestNumbers(t *testing.T) {
    11     11   	n := New(32)
    12     12   
    13     13   	expected := []uint64{1, 1, 2, 3, 5, 8, 13, 21}
................................................................................
    29     29   		dec, _ := n.Decode(enc)
    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     33   	}
    34     34   }
    35     35   
    36         -func TestWriter0(t *testing.T) {
           36  +func TestWriter(t *testing.T) {
    37     37   	var buf bytes.Buffer
    38     38   	var w io.Writer = Writer(&buf)
    39         -
    40         -	w.Write([]byte{0})
    41         -	w.Write(nil)
    42         -	for _, v := range buf.Bytes() {
    43         -		fmt.Printf("%s-", b2s(v))
    44         -	}
    45         -	fmt.Println()
    46         -}
           39  +	var input []byte = make([]byte, 256)
           40  +	var fib Numbers = New(16)
    47     41   
    48         -func TestWriter1(t *testing.T) {
    49         -	var buf bytes.Buffer
    50         -	var w io.Writer = Writer(&buf)
    51         -
    52         -	w.Write([]byte{0, 1})
    53         -	w.Write(nil)
    54         -	for _, v := range buf.Bytes() {
    55         -		fmt.Printf("%s-", b2s(v))
    56         -	}
    57         -	fmt.Println()
    58         -}
    59         -
    60         -func TestWriter2(t *testing.T) {
    61         -	var buf bytes.Buffer
    62         -	var w io.Writer = Writer(&buf)
    63         -
    64         -	w.Write([]byte{0, 1, 2})
    65         -	w.Write(nil)
    66         -	for _, v := range buf.Bytes() {
    67         -		fmt.Printf("%s-", b2s(v))
    68         -	}
    69         -	fmt.Println()
    70         -}
    71         -
    72         -func TestWriter3(t *testing.T) {
    73         -	var buf bytes.Buffer
    74         -	var w io.Writer = Writer(&buf)
    75         -
    76         -	var input []byte = make([]byte, 256)
    77         -	for i := 0; i < 256; i++ {
           42  +	for i := uint64(0); i < 256; i++ {
    78     43   		input[i] = byte(i)
    79     44   	}
    80     45   
    81         -	w.Write(input)
    82         -	w.Write(nil)
           46  +	// Write the input
           47  +	count, err := w.Write(input)
           48  +	if count != len(input) {
           49  +		t.Error("Unexpected write count", count)
           50  +	}
           51  +	if err != nil {
           52  +		t.Error("Unexpected write error", err.Error())
           53  +	}
           54  +
           55  +	// Flush remaining bits
           56  +	count, err = w.Write(nil)
           57  +	if count != 0 {
           58  +		t.Error("Unexpected write count while flushing", count)
           59  +	}
           60  +	if err != nil {
           61  +		t.Error("Unexpected write error while flushing", err.Error())
           62  +	}
           63  +
           64  +	var output string
    83     65   	for _, v := range buf.Bytes() {
    84         -		fmt.Printf("%s-", b2s(v))
           66  +		output += u2s(uint64(v), 8)
    85     67   	}
    86         -	fmt.Println()
           68  +
           69  +	for i, v := range input {
           70  +		c, l := fib.Code(uint64(v))
           71  +		vs := u2s(c, l)
           72  +		if loc := strings.Index(output, vs); loc != 0 {
           73  +			t.Fatal("Unexpected location for", i, "value", vs)
           74  +		}
           75  +		output = output[len(vs):]
           76  +	}
    87     77   }
    88     78   
    89         -func b2s(b byte) (result string) {
    90         -	for i := 0; i < 8; i++ {
    91         -		if b&1 > 0 {
           79  +func u2s(b uint64, l byte) (result string) {
           80  +	for i := byte(0); i < l; i++ {
           81  +		if (b & 1) > 0 {
    92     82   			result += "1"
    93     83   		} else {
    94     84   			result += "0"
    95     85   		}
    96     86   		b >>= 1
    97     87   	}
    98     88   	return
    99     89   }