Check-in [c7c8d6445f]
Overview
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.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c7c8d6445fad8e8f2d7ec070150b0caee8211936
User & Date: spaskalev on 2015-01-01 05:30:46
Other Links: manifest | tags
Context
2015-01-01
06:34
Fixed Decode's length return and added a test for it. Reduce encoder's buffer to 2 bytes. CC at 98.3% check-in: ea97951fcd user: spaskalev tags: trunk
05:30
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. check-in: c7c8d6445f user: spaskalev tags: trunk
04:58
Added fibonacci.Writer(io.Writer) io.Writer that encodes bytes. check-in: 93fcb281a1 user: spaskalev tags: trunk
Changes

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

120
121
122
123
124
125
126



127
128
129
130
131
132
133
		// Shift the the encoded value and account for its length
		enc >>= added
		e.length += len
		len -= added

		// Not enough bits to write
		if e.length < 8 {



			continue
		}

		// Clearing e.length is not necessary as it will be overwritten later

		// Stage the complete byte for writing
		e.buffer = append(e.buffer, byte(e.remaining))







>
>
>







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
		// Shift the the encoded value and account for its length
		enc >>= added
		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

		// Stage the complete byte for writing
		e.buffer = append(e.buffer, byte(e.remaining))
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
		_, err = e.target.Write(e.buffer)

		// Abort write on error
		if err != nil {
			break
		}

		// Account for the just-written byte
		total++

		// Clear the buffer
		e.buffer = e.buffer[:0]
	}
	return total, err
}







|







149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
		_, err = e.target.Write(e.buffer)

		// Abort write on error
		if err != nil {
			break
		}

		// Account for the processed input byte
		total++

		// Clear the buffer
		e.buffer = e.buffer[:0]
	}
	return total, err
}

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

1
2
3
4
5
6

7
8
9
10
11
12
13
package fibonacci

import (
	"bytes"
	"fmt"
	"io"

	"testing"
)

func TestNumbers(t *testing.T) {
	n := New(32)

	expected := []uint64{1, 1, 2, 3, 5, 8, 13, 21}




<

>







1
2
3
4

5
6
7
8
9
10
11
12
13
package fibonacci

import (
	"bytes"

	"io"
	"strings"
	"testing"
)

func TestNumbers(t *testing.T) {
	n := New(32)

	expected := []uint64{1, 1, 2, 3, 5, 8, 13, 21}
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

81








82








83
84
85
86





87

88


89
90
91
92
93
94
95
96
97
98
99
		dec, _ := n.Decode(enc)
		if i != dec {
			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++ {
		input[i] = byte(i)
	}


	w.Write(input)








	w.Write(nil)








	for _, v := range buf.Bytes() {
		fmt.Printf("%s-", b2s(v))
	}
	fmt.Println()





}




func b2s(b byte) (result string) {
	for i := 0; i < 8; i++ {
		if b&1 > 0 {
			result += "1"
		} else {
			result += "0"
		}
		b >>= 1
	}
	return
}







|


|
<
<
<
<
<
<
<
|
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|



>
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>

|

|
>
>
>
>
>
|
>
|
>
>
|
|
|








29
30
31
32
33
34
35
36
37
38
39







40



41

























42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
		dec, _ := n.Decode(enc)
		if i != dec {
			t.Errorf("Unexpected value for %d - enc is %b, dec is %d\n", i, enc, dec)
		}
	}
}

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)
	}

	// 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() {
		output += u2s(uint64(v), 8)
	}

	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 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
}