Skip to content

Commit

Permalink
Fix encoding for 1 byte FpEncoder (ST1201). (#113)
Browse files Browse the repository at this point in the history
bradh authored Apr 23, 2020
1 parent afa369f commit f081146
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/src/main/java/org/jmisb/api/klv/st1201/FpEncoder.java
Original file line number Diff line number Diff line change
@@ -112,8 +112,8 @@ else if (val < a || val > b)
switch (fieldLength)
{
case 1:
char c = (char) d;
encoded = ByteBuffer.allocate(1).putChar(c).array();
byte b = (byte) d;
encoded = ByteBuffer.allocate(1).put(b).array();
break;
case 2:
short s = (short) d;
@@ -136,6 +136,8 @@ else if (val < a || val > b)
long l = (long) d;
encoded = ByteBuffer.allocate(8).putLong(l).array();
break;
default:
throw new UnsupportedOperationException("Only field lengths of [1,2,3,4,8] are supported");
}
}
return encoded;
@@ -212,6 +214,8 @@ else if (bytes[offset] == (byte)0xd0)
long l = wrapped.getLong();
val = sR * (l - zOffset) + a;
break;
default:
throw new UnsupportedOperationException("Only field lengths of [1,2,3,4,8] are supported");
}
if (val < a || val > b)
{
28 changes: 28 additions & 0 deletions api/src/test/java/org/jmisb/api/klv/st1201/FpEncoderTest.java
Original file line number Diff line number Diff line change
@@ -46,6 +46,13 @@ public void testOutOfRange()
encoder.encode(-500.0);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testOutOfRangeOver()
{
FpEncoder encoder = new FpEncoder(-100.0, 100.0, 4);
encoder.encode(100.1);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testMismatchedLength()
{
@@ -82,6 +89,27 @@ public void testEncode()
Assert.assertEquals(encoded[0], (byte)0xd0);
}

@Test
public void testEncodeLen1()
{
FpEncoder fpEncoder = new FpEncoder(0.0, 10.0, 1);
double value = 6.0;
byte[] expected = {0x30};
byte[] encoded = fpEncoder.encode(value);
Assert.assertEquals(encoded.length, 1);
Assert.assertEquals(encoded, expected);
}

@Test
public void testDecodeLen1()
{
FpEncoder fpEncoder = new FpEncoder(0.0, 10.0, 1);
double expected = 6.0;
byte[] value = {0x30};
double decoded = fpEncoder.decode(value);
Assert.assertEquals(decoded, expected);
}

@Test
public void testDecode()
{

0 comments on commit f081146

Please sign in to comment.