-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add buffered decoder, bit sequence and decode with length (#248)
- Loading branch information
1 parent
e035c67
commit 83b396c
Showing
4 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package jam | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDecodeBits(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []byte | ||
expect BitSequence | ||
leftover int | ||
err error | ||
}{{ | ||
name: "empty", | ||
input: []byte{}, | ||
expect: BitSequence{}, | ||
}, { | ||
name: "1 bytes", | ||
input: []byte{255}, | ||
expect: BitSequence{true, true, true, true, true, true, true, true}, | ||
}, { | ||
name: "1.5 bytes", | ||
input: []byte{0, 255}, | ||
expect: BitSequence{ | ||
false, false, false, false, false, false, false, false, | ||
true, true, true, true, true, true, true, true, | ||
}, | ||
}, { | ||
name: "5 bytes", | ||
input: []byte{17, 25, 0, 1, 2}, | ||
expect: BitSequence{ | ||
true, false, false, false, true, false, false, false, | ||
true, false, false, true, true, false, false, false, | ||
false, false, false, false, false, false, false, false, | ||
true, false, false, false, false, false, false, false, | ||
false, true, false, false, false, false, false, false, | ||
}, | ||
}} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
buff := bytes.NewBuffer(tc.input) | ||
d := NewDecoder(buff) | ||
|
||
actual := BitSequence{} | ||
err := d.DecodeFixedLength(&actual, uint(len(tc.input))) | ||
if tc.err != nil { | ||
assert.Equal(t, tc.err, err) | ||
} | ||
|
||
assert.Equal(t, tc.expect, actual) | ||
assert.Equal(t, tc.leftover, buff.Len()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters