Overview
Comment: | Initial implementation and test of fibonacci.Reader(io.Reader) io.Reader that decodes bytes encoded from fibonacci.Writer(..). CC at 97.5% |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
4b9f9bff3989c6d96534cb07ab4c8d42 |
User & Date: | spaskalev on 2015-01-01 14:11:05 |
Other Links: | manifest | tags |
Context
2015-01-01
| ||
15:12 | Use a common fibonacci number source for each encoder and decoder. Dropped the two-bytes buffer from the encoder struct. check-in: f1a8d5baa9 user: spaskalev tags: trunk | |
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 | |
Changes
Modified src/0dev.org/encoding/fibonacci/fib.go from [bc261fa076] to [665b1e0b43].
1 2 3 4 5 6 7 8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | - + + - - - | // Package provides a shifted fibonacci encoding of unsigned integers. // // http://en.wikipedia.org/wiki/Fibonacci_coding maps positive integers as // 1 - 11, 2 - 011, 3 - 0011, 4 - 1011, 5 - 00011 // // Incrementing input by one to allow for zero gives // 0 - 11, 1 - 011, 2 - 0011, 3 - 1011, 4 - 00011 // |
︙ | |||
157 158 159 160 161 162 163 | 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 209 210 211 212 213 214 215 216 217 218 | - - - - - + + + + + - - - - - - - + + + + + + + - - - - - - + + + + + + - - - - - + + + + + + - - + + - - - - + + + + - - + - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + | // Clear the buffer e.buffer = e.buffer[:0] } return total, err } |
Modified src/0dev.org/encoding/fibonacci/fib_test.go from [0734e0cdf2] to [33c293fd3b].
1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 11 | + | package fibonacci import ( diff "0dev.org/diff" "bytes" "io" "strings" "testing" ) func TestNumbers(t *testing.T) { |
︙ | |||
32 33 34 35 36 37 38 | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | - - - - - + + + + + + + | } if encLen != decLen { t.Errorf("Unexpected difference between encoded and decoded lengths.", encLen, decLen) } } } |
︙ | |||
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | + + + + + + + + + + + + + + + + | 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):] } var ( in *bytes.Reader = bytes.NewReader(buf.Bytes()) r io.Reader = Reader(in) out bytes.Buffer ) io.Copy(&out, r) decoded := out.Bytes() delta := diff.Diff(diff.D{Len1: len(decoded), Len2: len(input), EqualFunc: func(i, j int) bool { return decoded[i] == input[j] }}) if len(delta.Added) > 0 || len(delta.Removed) > 0 { t.Error("Differences detected ", delta) } } 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 } |