Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement NB.encode_int32/1. #156

Merged
merged 1 commit into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions lib/xgit/util/nb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,32 +239,13 @@ defmodule Xgit.Util.NB do
#
# intbuf[offset] = (byte) v;
# }
#
# /**
# * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
# *
# * @param intbuf
# * buffer to write the 4 bytes of data into.
# * @param offset
# * position within the buffer to begin writing to. This position
# * and the next 3 bytes after it (for a total of 4 bytes) will be
# * replaced.
# * @param v
# * the value to write.
# */
# public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
# intbuf[offset + 3] = (byte) v;
# v >>>= 8;
#
# intbuf[offset + 2] = (byte) v;
# v >>>= 8;
#
# intbuf[offset + 1] = (byte) v;
# v >>>= 8;
#
# intbuf[offset] = (byte) v;
# }
#

@doc ~S"""
Convert a 32-bit integer to a sequence of four bytes in network byte order.
"""
def encode_int32(v) when is_integer(v) and v >= -2_147_483_647 and v <= 4_294_967_296,
do: [v >>> 24 &&& 0xFF, v >>> 16 &&& 0xFF, v >>> 8 &&& 0xFF, v &&& 0xFF]

# /**
# * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
# *
Expand Down
51 changes: 9 additions & 42 deletions test/xgit/util/nb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -231,48 +231,15 @@ defmodule Xgit.Util.NBTest do
# NB.encodeInt24(out, 3, -1);
# assertOutput(b(0xff, 0xff, 0xff), out, 3);
# }
#
# @Test
# public void testEncodeInt32() {
# final byte[] out = new byte[16];
#
# prepareOutput(out);
# NB.encodeInt32(out, 0, 0);
# assertOutput(b(0, 0, 0, 0), out, 0);
#
# prepareOutput(out);
# NB.encodeInt32(out, 3, 0);
# assertOutput(b(0, 0, 0, 0), out, 3);
#
# prepareOutput(out);
# NB.encodeInt32(out, 0, 3);
# assertOutput(b(0, 0, 0, 3), out, 0);
#
# prepareOutput(out);
# NB.encodeInt32(out, 3, 3);
# assertOutput(b(0, 0, 0, 3), out, 3);
#
# prepareOutput(out);
# NB.encodeInt32(out, 0, 0xdeac);
# assertOutput(b(0, 0, 0xde, 0xac), out, 0);
#
# prepareOutput(out);
# NB.encodeInt32(out, 3, 0xdeac);
# assertOutput(b(0, 0, 0xde, 0xac), out, 3);
#
# prepareOutput(out);
# NB.encodeInt32(out, 0, 0xdeac9853);
# assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 0);
#
# prepareOutput(out);
# NB.encodeInt32(out, 3, 0xdeac9853);
# assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 3);
#
# prepareOutput(out);
# NB.encodeInt32(out, 3, -1);
# assertOutput(b(0xff, 0xff, 0xff, 0xff), out, 3);
# }
#

test "encode_int32/1" do
assert NB.encode_int32(0) == [0, 0, 0, 0]
assert NB.encode_int32(3) == [0, 0, 0, 3]
assert NB.encode_int32(0xDEAC) == [0, 0, 0xDE, 0xAC]
assert NB.encode_int32(0xDEAC9853) == [0xDE, 0xAC, 0x98, 0x53]
assert NB.encode_int32(-1) == [0xFF, 0xFF, 0xFF, 0xFF]
end

# @Test
# public void testEncodeInt64() {
# final byte[] out = new byte[16];
Expand Down