Commit 6e6a8aa3 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

encoder: Fix bool wrt protocol version

Starting from protocol 2 1-byte NEWTRUE/NEWFALSE opcodes are more
efficient compared to 5-bytes e.g. "I01\n.".

It is not only about efficiency, as protocol 4 _forbids_ use of variable
length ASCII-only opcodes - whose data length is determined by doing
forward scan for '\n'.

Without encodeBool changes and only with the tests added it would fail
this way:

    --- FAIL: TestEncode/True/proto=2 (0.00s)
        ogorek_test.go:383: encode:
            have: "\x80\x02I01\n."
            want: "\x80\x02\x88."
    --- FAIL: TestEncode/True/proto=3 (0.00s)
        ogorek_test.go:383: encode:
            have: "\x80\x03I01\n."
            want: "\x80\x03\x88."
    --- FAIL: TestEncode/True/proto=4 (0.00s)
        ogorek_test.go:383: encode:
            have: "\x80\x04I01\n."
            want: "\x80\x04\x88."
    --- FAIL: TestEncode/False/proto=2 (0.00s)
        ogorek_test.go:383: encode:
            have: "\x80\x02I00\n."
            want: "\x80\x02\x89."
    --- FAIL: TestEncode/False/proto=3 (0.00s)
        ogorek_test.go:383: encode:
            have: "\x80\x03I00\n."
            want: "\x80\x03\x89."
    --- FAIL: TestEncode/False/proto=4 (0.00s)
        ogorek_test.go:383: encode:
            have: "\x80\x04I00\n."
            want: "\x80\x04\x89."
parent 06e06939
......@@ -211,8 +211,17 @@ func (e *Encoder) encodeArray(arr reflect.Value) error {
}
func (e *Encoder) encodeBool(b bool) error {
var err error
// protocol >= 2 -> NEWTRUE/NEWFALSE
if e.config.Protocol >= 2 {
op := opNewfalse
if b {
op = opNewtrue
}
return e.emit(op)
}
// INT(01 | 00)
var err error
if b {
err = e.emits(opTrue)
} else {
......
......@@ -146,6 +146,14 @@ var tests = []TestEntry{
X("None", None{},
P0_("N.")), // NONE
X("True", true,
P01("I01\n."), // INT 01
P2_("\x88.")), // NEWTRUE
X("False", false,
P01("I00\n."), // INT 00
P2_("\x89.")), // NEWFALSE
X("int(5)", int64(5),
I("I5\n.")), // INT
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment