Check-in [ea97951fcd]
Overview
Comment:Fixed Decode's length return and added a test for it. Reduce encoder's buffer to 2 bytes. CC at 98.3%
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ea97951fcdfd21a1a3c66adbefb8253c4ce4359d
User & Date: spaskalev on 2015-01-01 06:34:04
Other Links: manifest | tags
Context
2015-01-01
14:11
Initial implementation and test of fibonacci.Reader(io.Reader) io.Reader that decodes bytes encoded from fibonacci.Writer(..). CC at 97.5% check-in: 4b9f9bff39 user: spaskalev tags: trunk
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
Changes

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

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
			length += 2
			continue
		}

		value >>= 1
		length++
	}
	result += f[length] - 1
	return
}

func Writer(target io.Writer) io.Writer {
	var enc encoder
	enc.Numbers = New(16)
	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
	buffer    []byte
	remaining byte
	length    byte
}

func (e *encoder) Write(input []byte) (int, error) {
	var (







|
<




|








|







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
			length += 2
			continue
		}

		value >>= 1
		length++
	}
	return result + f[length] - 1, length + 1

}

func Writer(target io.Writer) io.Writer {
	var enc encoder
	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   [2]byte
	buffer    []byte
	remaining byte
	length    byte
}

func (e *encoder) Write(input []byte) (int, error) {
	var (
173
174
175
176
177
178
179




























180
181
// 	Numbers
// 	source io.Reader
// 	buffer uint64
// 	at     byte
// }

// func (d *decoder) Read(output []byte) (int, error) {




























// 	return 0, nil
// }







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

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 	Numbers
// 	source io.Reader
// 	buffer uint64
// 	at     byte
// }

// func (d *decoder) Read(output []byte) (int, error) {
// 	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
// }

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

20
21
22
23
24
25
26
27
28
29

30
31
32



33
34
35
36
37
38
39
	}
}

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)

		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)







|
<
|
>



>
>
>







20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	}
}

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

	for i := uint64(0); i < 4096; i++ {
		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
	var w io.Writer = Writer(&buf)
	var input []byte = make([]byte, 256)