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

[IOTDB-4633] Fix bugs of longToBytes in BytesUtils of tsfile #7669

Merged
merged 3 commits into from
Oct 28, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void intToBytes(int srcNum, byte[] result, int pos, int width) {
width -= m;
int mask = 1 << (8 - cnt);
cnt += m;
byte y = (byte) (srcNum >> width);
byte y = (byte) (srcNum >>> width);
y = (byte) (y << (8 - cnt));
mask = ~(mask - (1 << (8 - cnt)));
result[index] = (byte) (result[index] & mask | y);
Expand Down Expand Up @@ -523,7 +523,7 @@ public static void longToBytes(long srcNum, byte[] result, int pos, int width) {
width -= m;
int mask = 1 << (8 - cnt);
cnt += m;
byte y = (byte) (srcNum >> width);
byte y = (byte) (srcNum >>> width);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a UT for this bug?

Besides, intToBytes seems have a same bug...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UT tests for this bug have been added.

y = (byte) (y << (8 - cnt));
mask = ~(mask - (1 << (8 - cnt)));
result[index] = (byte) (result[index] & mask | y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.iotdb.tsfile.constant.TestConstant;

import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -141,6 +142,52 @@ public void bytesToLongOffsetTest() {
assertEquals(l, BytesUtils.bytesToLong(bs, 0, width));
}

@Test
public void bytesToIntOffsetTest1() {
int l1 = 123;
int width1 = 64 - Integer.numberOfLeadingZeros(l1);
int l2 = -124;
int width2 = 64 - Integer.numberOfLeadingZeros(l2);
byte[] bs = new byte[1000];
BytesUtils.intToBytes(l1, bs, 0, width1);
int res_val1_1 = BytesUtils.bytesToInt(bs, 0, width1);
BytesUtils.intToBytes(l2, bs, width1, width2);
int res_val1_2 = BytesUtils.bytesToInt(bs, 0, width1);
Assert.assertEquals(res_val1_1, res_val1_2);
}

@Test
public void bytesToLongOffsetTest1() {
long l1 = 1600650710304L;
int width1 = 64 - Long.numberOfLeadingZeros(l1);
long l2 = -16L;
int width2 = 64 - Long.numberOfLeadingZeros(l2);
byte[] bs = new byte[1000];
BytesUtils.longToBytes(l1, bs, 0, width1);
long res_val1_1 = BytesUtils.bytesToLong(bs, 0, width1);
BytesUtils.longToBytes(l2, bs, width1, width2);
long res_val1_2 = BytesUtils.bytesToLong(bs, 0, width1);
Assert.assertEquals(res_val1_1, res_val1_2);
}

@Test
public void bytesToLongOffsetTest2() {
long l1 = 123L;
int width1 = 64 - Long.numberOfLeadingZeros(l1);
long l2 = -123L;
int width2 = 64 - Long.numberOfLeadingZeros(l2);
long l3 = 123L;
int width3 = 64 - Long.numberOfLeadingZeros(l3);
byte[] bs = new byte[1000];
BytesUtils.longToBytes(l1, bs, 0, width1);
assertEquals(l1, BytesUtils.bytesToLong(bs, 0, width1));
BytesUtils.longToBytes(l2, bs, width1, width2);
BytesUtils.longToBytes(l3, bs, width1 + width2, width3);
assertEquals(l1, BytesUtils.bytesToLong(bs, 0, width1));
assertEquals(l2, BytesUtils.bytesToLong(bs, width1, width2));
assertEquals(l3, BytesUtils.bytesToLong(bs, width1 + width2, width3));
}

@Test
public void readLongTest() throws IOException {
long l = 32143422454243342L;
Expand Down