-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: unroll varint encoding for more cases
Also: - start using stream_data for property testing - Varint.encode/1 no longer returns an iodata, but a binary Benchmark shows a slight increase in performances and slight decrease in memory usage
- Loading branch information
Showing
4 changed files
with
64 additions
and
34 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
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 |
---|---|---|
@@ -1,41 +1,54 @@ | ||
defmodule Protox.VarintTest do | ||
use ExUnit.Case | ||
use PropCheck | ||
import Bitwise | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
property "Unrolled encoding produces the same result as the reference implementation" do | ||
check all(int <- integer(0..(1 <<< 64))) do | ||
assert int |> Protox.Varint.encode() |> IO.iodata_to_binary() == | ||
int |> encode_reference() |> IO.iodata_to_binary() | ||
end | ||
end | ||
|
||
property "Symmetric" do | ||
check all(int <- integer(0..(1 <<< 64))) do | ||
assert int |> Protox.Varint.encode() |> IO.iodata_to_binary() |> Protox.Varint.decode() == | ||
{int, ""} | ||
end | ||
end | ||
|
||
test "Encode" do | ||
assert Protox.Varint.encode(0) == <<0>> | ||
assert Protox.Varint.encode(1) == <<1>> | ||
|
||
assert Protox.Varint.encode(300) == <<172, 2>> | ||
assert Protox.Varint.encode((1 <<< 14) - 1) == <<0xFF, 0x7F>> | ||
assert Protox.Varint.encode(1 <<< 14) == <<0x80, 0x80, 0x1>> | ||
|
||
assert Protox.Varint.encode(16_383) == <<0xFF, 0x7F>> | ||
assert Protox.Varint.encode(16_384) == <<0x80, 0x80, 0x1>> | ||
assert Protox.Varint.encode((1 <<< 21) - 1) == <<0xFF, 0xFF, 0x7F>> | ||
assert Protox.Varint.encode(1 <<< 21) == <<0x80, 0x80, 0x80, 0x1>> | ||
|
||
assert Protox.Varint.encode(2_097_151) == <<0xFF, 0xFF, 0x7F>> | ||
assert Protox.Varint.encode(2_097_152) == <<0x80, 0x80, 0x80, 0x1>> | ||
assert Protox.Varint.encode((1 <<< 28) - 1) == <<0xFF, 0xFF, 0xFF, 0x7F>> | ||
assert Protox.Varint.encode(1 <<< 28) == <<0x80, 0x80, 0x80, 0x80, 0x1>> | ||
|
||
assert Protox.Varint.encode(268_435_455) == <<0xFF, 0xFF, 0xFF, 0x7F>> | ||
assert Protox.Varint.encode(268_435_456) == <<0x80, 0x80, 0x80, 0x80, 0x1>> | ||
assert Protox.Varint.encode((1 <<< 35) - 1) == <<0xFF, 0xFF, 0xFF, 0xFF, 0x7F>> | ||
assert Protox.Varint.encode(1 <<< 35) == <<0x80, 0x80, 0x80, 0x80, 0x80, 0x1>> | ||
|
||
assert Protox.Varint.encode(34_359_738_367) == <<0xFF, 0xFF, 0xFF, 0xFF, 0x7F>> | ||
end | ||
assert Protox.Varint.encode((1 <<< 42) - 1) == <<0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F>> | ||
assert Protox.Varint.encode(1 <<< 42) == <<0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1>> | ||
|
||
test "Decode" do | ||
assert Protox.Varint.decode(<<172, 2>>) == {300, <<>>} | ||
assert Protox.Varint.decode(<<172, 2, 0>>) == {300, <<0>>} | ||
assert Protox.Varint.decode(<<0>>) == {0, <<>>} | ||
assert Protox.Varint.decode(<<1>>) == {1, <<>>} | ||
assert Protox.Varint.decode(<<185, 96>>) == {12_345, <<>>} | ||
assert Protox.Varint.decode(<<185, 224, 0>>) == {12_345, <<>>} | ||
end | ||
assert Protox.Varint.encode((1 <<< 56) - 1) == | ||
<<0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F>> | ||
|
||
@tag :properties | ||
test "Symmetric" do | ||
forall value <- integer() do | ||
encoded = value |> Protox.Varint.encode() |> :binary.list_to_bin() | ||
{decoded, <<>>} = Protox.Varint.decode(encoded) | ||
assert Protox.Varint.encode(1 <<< 56) == | ||
<<0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1>> | ||
|
||
value == decoded | ||
end | ||
assert Protox.Varint.encode((1 <<< 63) - 1) == | ||
<<0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F>> | ||
|
||
assert Protox.Varint.encode(1 <<< 63) == | ||
<<0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1>> | ||
end | ||
|
||
defp encode_reference(v) when v < 1 <<< 7, do: <<v>> | ||
defp encode_reference(v), do: [<<1::1, v::7>>, encode_reference(v >>> 7)] | ||
end |