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

issue #57: radix is ignored #82

Merged
merged 5 commits into from
Jan 5, 2023
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
8 changes: 8 additions & 0 deletions .github/workflows/branch-cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ jobs:
-
name: 🩺 Test Software
run: mvn test

-
name: 🩺 Test Integration with Validate
run: |
mvn install
git clone https://github.com/NASA-PDS/validate.git
cd validate
mvn test

...

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/gov/nasa/pds/label/object/FieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public enum FieldType {
ASCII_FILE_SPECIFICATION_NAME("ASCII_File_Specification_Name"),

/** An integer. */
ASCII_INTEGER("ASCII_Integer", new NumericTextFieldAdapter(), true),
ASCII_INTEGER("ASCII_Integer", new NumericTextFieldAdapter(10), true),

/** A logical identifier. */
ASCII_LID("ASCII_LID"),
Expand All @@ -111,19 +111,19 @@ public enum FieldType {
ASCII_MD5_CHECKSUM("ASCII_MD5_Checksum"),

/** A nonnegative integer. */
ASCII_NONNEGATIVE_INTEGER("ASCII_NonNegative_Integer", new NumericTextFieldAdapter(), true),
ASCII_NONNEGATIVE_INTEGER("ASCII_NonNegative_Integer", new NumericTextFieldAdapter(10), true),

/** A hexadecimal integer. */
ASCII_NUMERIC_BASE16("ASCII_Numeric_Base16", new NumericTextFieldAdapter(), true),
ASCII_NUMERIC_BASE16("ASCII_Numeric_Base16", new NumericTextFieldAdapter(16), true),

/** A base 2 integer. */
ASCII_NUMERIC_BASE2("ASCII_Numeric_Base2"),
ASCII_NUMERIC_BASE2("ASCII_Numeric_Base2", new NumericTextFieldAdapter(2), true),

/** A base 8 integer. */
ASCII_NUMERIC_BASE8("ASCII_Numeric_Base8"),
ASCII_NUMERIC_BASE8("ASCII_Numeric_Base8", new NumericTextFieldAdapter(8), true),

/** A floating-point value. */
ASCII_REAL("ASCII_Real", new NumericTextFieldAdapter(), true),
ASCII_REAL("ASCII_Real", new NumericTextFieldAdapter(10), true),

/** An ASCII string. */
ASCII_STRING("ASCII_String"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
*/
public class NumericTextFieldAdapter extends DefaultFieldAdapter {

@Override
final public int radix;
public NumericTextFieldAdapter(int radix) {
super();
this.radix = radix;
}

@Override
public byte getByte(byte[] buf, int offset, int length, int startBit, int stopBit) {
int value = Integer.parseInt(getString(buf, offset, length, startBit, stopBit).trim());
int value = this.getBigInteger(buf, offset, length, startBit, stopBit).intValue();
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
throw new NumberFormatException("Value is out of range of a byte (" + value + ")");
}
Expand All @@ -50,7 +56,7 @@ public byte getByte(byte[] buf, int offset, int length, int startBit, int stopBi

@Override
public short getShort(byte[] buf, int offset, int length, int startBit, int stopBit) {
int value = Integer.parseInt(getString(buf, offset, length, startBit, stopBit).trim());
int value = this.getBigInteger(buf, offset, length, startBit, stopBit).intValue();
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
throw new NumberFormatException("Value is out of range of a short (" + value + ")");
}
Expand All @@ -60,12 +66,12 @@ public short getShort(byte[] buf, int offset, int length, int startBit, int stop

@Override
public int getInt(byte[] buf, int offset, int length, int startBit, int stopBit) {
return Integer.parseInt(getString(buf, offset, length, startBit, stopBit).trim());
return this.getBigInteger(buf, offset, length, startBit, stopBit).intValue();
}

@Override
public long getLong(byte[] buf, int offset, int length, int startBit, int stopBit) {
return Long.parseLong(getString(buf, offset, length, startBit, stopBit).trim());
return this.getBigInteger(buf, offset, length, startBit, stopBit).longValue();
}

@Override
Expand All @@ -80,7 +86,7 @@ public double getDouble(byte[] buf, int offset, int length, int startBit, int st

@Override
public BigInteger getBigInteger(byte[] buf, int offset, int length, int startBit, int stopBit) {
return new BigInteger(getString(buf, offset, length, startBit, stopBit));
return new BigInteger(getString(buf, offset, length, startBit, stopBit).trim(), this.radix);
}

//
Expand All @@ -90,25 +96,25 @@ public BigInteger getBigInteger(byte[] buf, int offset, int length, int startBit
@Override
public void setByte(byte value, int offset, int length, ByteBuffer buffer,
boolean isRightJustified) {
setString(Byte.toString(value), offset, length, buffer, isRightJustified);
setString(Integer.toString(value, this.radix), offset, length, buffer, isRightJustified);
}

@Override
public void setShort(short value, int offset, int length, ByteBuffer buffer,
boolean isRightJustified) {
setString(Short.toString(value), offset, length, buffer, isRightJustified);
setString(Integer.toString(value, this.radix), offset, length, buffer, isRightJustified);
}

@Override
public void setInt(int value, int offset, int length, ByteBuffer buffer,
boolean isRightJustified) {
setString(Integer.toString(value), offset, length, buffer, isRightJustified);
setString(Integer.toString(value, this.radix), offset, length, buffer, isRightJustified);
}

@Override
public void setLong(long value, int offset, int length, ByteBuffer buffer,
boolean isRightJustified) {
setString(Long.toString(value), offset, length, buffer, isRightJustified);
setString(Long.toString(value, this.radix), offset, length, buffer, isRightJustified);
}

@Override
Expand All @@ -126,7 +132,7 @@ public void setDouble(double value, int offset, int length, ByteBuffer buffer,
@Override
public void setBigInteger(BigInteger value, int offset, int length, ByteBuffer buffer,
boolean isRightJustified) {
setString(value.toString(), offset, length, buffer, isRightJustified);
setString(value.toString(this.radix), offset, length, buffer, isRightJustified);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,18 @@ public void testIsRightJustified() {
assertTrue(FieldType.SIGNEDMSB4.isRightJustified());
}

@Test
public void testIntConversions()
{
FieldType bin = FieldType.ASCII_NUMERIC_BASE2;
FieldType dec = FieldType.ASCII_INTEGER;
FieldType oct = FieldType.ASCII_NUMERIC_BASE8;
FieldType hex = FieldType.ASCII_NUMERIC_BASE16;
String value = "10101";

assertEquals (21, bin.getAdapter().getInt(value.getBytes(), 0, value.length(), 0, value.length()*8));
assertEquals (10101, dec.getAdapter().getInt(value.getBytes(), 0, value.length(), 0, value.length()*8));
assertEquals (4161, oct.getAdapter().getInt(value.getBytes(), 0, value.length(), 0, value.length()*8));
assertEquals (65793, hex.getAdapter().getInt(value.getBytes(), 0, value.length(), 0, value.length()*8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class NumericTextFieldAdapterTest {
private static final Charset US_ASCII = Charset.forName("US-ASCII");

private ByteBuffer buffer = ByteBuffer.allocate(50);
private FieldAdapter adapter = new NumericTextFieldAdapter();
private FieldAdapter adapter = new NumericTextFieldAdapter(10);

@Test
public void testGoodInt() {
Expand Down