You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2019/12/12 12:57:01 UTC

[plc4x] branch feature/big_integer_support_on_driver_base updated: added some dummy tests for write bugger

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch feature/big_integer_support_on_driver_base
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/big_integer_support_on_driver_base by this push:
     new b3770c6  added some dummy tests for write bugger
b3770c6 is described below

commit b3770c647896ebcf51d87c7190a62fce657e103f
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Dec 12 13:56:48 2019 +0100

    added some dummy tests for write bugger
---
 .../org/apache/plc4x/java/utils/WriteBuffer.java   | 14 +++--
 .../apache/plc4x/java/utils/WriteBufferTest.java   | 66 +++++++++++++++++++++-
 2 files changed, 74 insertions(+), 6 deletions(-)

diff --git a/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/WriteBuffer.java b/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/WriteBuffer.java
index a0fd385..4b84737 100644
--- a/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/WriteBuffer.java
+++ b/plc4j/utils/driver-base-java/src/main/java/org/apache/plc4x/java/utils/WriteBuffer.java
@@ -186,6 +186,9 @@ public class WriteBuffer {
     }
 
     public void writeBigInteger(int bitLength, BigInteger value) throws ParseException {
+        if (bitLength <= 0) {
+            throw new ParseException("long must contain at least 1 bit");
+        }
         int actualBitLength = value.bitLength();
         boolean negative = value.compareTo(BigInteger.ZERO) < 0;
         int bitLengthIncludingPossibleSign = actualBitLength + (negative ? 1 : 0);
@@ -198,14 +201,14 @@ public class WriteBuffer {
             if (!littleEndian) {
                 // MSB in 0
                 for (int i = 0; i < bytes.length; i++) {
-                    int bitsToWrite = Math.max(Math.min(remainingBitLength, 8), 1);
+                    int bitsToWrite = Math.min(remainingBitLength, 8);
                     bo.writeByte(false, bitsToWrite, bytes[i]);
                     remainingBitLength -= bitsToWrite;
                 }
             } else {
                 // MSB in bytes.length
                 for (int i = bytes.length - 1; i >= 0; i--) {
-                    int bitsToWrite = Math.max(Math.min(remainingBitLength, 8), 1);
+                    int bitsToWrite = Math.min(remainingBitLength, 8);
                     bo.writeByte(false, bitsToWrite, bytes[i]);
                     remainingBitLength -= bitsToWrite;
                 }
@@ -216,6 +219,9 @@ public class WriteBuffer {
     }
 
     public void writeUnsignedBigInteger(int bitLength, BigInteger value) throws ParseException {
+        if (bitLength <= 0) {
+            throw new ParseException("long must contain at least 1 bit");
+        }
         if (value.compareTo(BigInteger.ZERO) < 0) {
             throw new ParseException("value " + value + " is below 0");
         }
@@ -229,14 +235,14 @@ public class WriteBuffer {
             if (!littleEndian) {
                 // MSB in 0
                 for (int i = 0; i < bytes.length; i++) {
-                    int bitsToWrite = Math.max(Math.min(remainingBitLength, 8), 1);
+                    int bitsToWrite = Math.min(remainingBitLength, 8);
                     bo.writeByte(false, bitsToWrite, bytes[i]);
                     remainingBitLength -= bitsToWrite;
                 }
             } else {
                 // MSB in bytes.length
                 for (int i = bytes.length - 1; i >= 0; i--) {
-                    int bitsToWrite = Math.max(Math.min(remainingBitLength, 8), 1);
+                    int bitsToWrite = Math.min(remainingBitLength, 8);
                     bo.writeByte(false, bitsToWrite, bytes[i]);
                     remainingBitLength -= bitsToWrite;
                 }
diff --git a/plc4j/utils/driver-base-java/src/test/java/org/apache/plc4x/java/utils/WriteBufferTest.java b/plc4j/utils/driver-base-java/src/test/java/org/apache/plc4x/java/utils/WriteBufferTest.java
index 08ecd77..7a31e23 100644
--- a/plc4j/utils/driver-base-java/src/test/java/org/apache/plc4x/java/utils/WriteBufferTest.java
+++ b/plc4j/utils/driver-base-java/src/test/java/org/apache/plc4x/java/utils/WriteBufferTest.java
@@ -96,7 +96,7 @@ class WriteBufferTest {
 
             @Test
             void minusOne() throws Exception {
-                WriteBuffer SUT = new WriteBuffer(1, false);
+                WriteBuffer SUT = new WriteBuffer(8, false);
                 SUT.writeBigInteger(8, BigInteger.ZERO.subtract(BigInteger.ONE));
                 byte[] data = SUT.getData();
                 System.out.println(toHex(data));
@@ -107,7 +107,7 @@ class WriteBufferTest {
 
             @Test
             void minus255() throws Exception {
-                WriteBuffer SUT = new WriteBuffer(1, false);
+                WriteBuffer SUT = new WriteBuffer(8, false);
                 SUT.writeBigInteger(8, BigInteger.valueOf(-255L));
                 byte[] data = SUT.getData();
                 System.out.println(toHex(data));
@@ -129,6 +129,68 @@ class WriteBufferTest {
         }
     }
 
+    @Nested
+    class WriteUnsignedBigInteger {
+        @Nested
+        class BigEndian {
+
+            @Test
+            void zero() throws Exception {
+                WriteBuffer SUT = new WriteBuffer(1, false);
+                SUT.writeUnsignedBigInteger(8, BigInteger.ZERO);
+                byte[] data = SUT.getData();
+                System.out.println(toHex(data));
+                // TODO: check right representation
+                assertArrayEquals(new byte[]{0b0000_0000}, data);
+                assertEquals(BigInteger.ZERO, new BigInteger(data));
+            }
+
+            @Test
+            void one() throws Exception {
+                WriteBuffer SUT = new WriteBuffer(1, false);
+                SUT.writeUnsignedBigInteger(8, BigInteger.ONE);
+                byte[] data = SUT.getData();
+                System.out.println(toHex(data));
+                // TODO: check right representation
+                assertArrayEquals(new byte[]{0b0000_0001}, data);
+                assertEquals(BigInteger.ZERO, new BigInteger(data));
+            }
+
+            @Test
+            void minusOne() throws Exception {
+                WriteBuffer SUT = new WriteBuffer(8, false);
+                SUT.writeUnsignedBigInteger(8, BigInteger.ZERO.subtract(BigInteger.ONE));
+                byte[] data = SUT.getData();
+                System.out.println(toHex(data));
+                // TODO: check right representation
+                assertArrayEquals(new byte[]{0b0000_0001}, data);
+                assertEquals(BigInteger.ZERO, new BigInteger(data));
+            }
+
+            @Test
+            void minus255() throws Exception {
+                WriteBuffer SUT = new WriteBuffer(8, false);
+                SUT.writeUnsignedBigInteger(8, BigInteger.valueOf(-255L));
+                byte[] data = SUT.getData();
+                System.out.println(toHex(data));
+                // TODO: check right representation
+                assertArrayEquals(new byte[]{(byte) 0b1000_0000, 0b0000_0001}, data);
+                assertEquals(BigInteger.valueOf(-255L), new BigInteger(data));
+            }
+
+        }
+
+        @Nested
+        class LittleEndian {
+
+            @Test
+            void writeBigInteger_LE() throws Exception {
+                WriteBuffer SUT_LE = new WriteBuffer(8012, true);
+                SUT_LE.writeBigInteger(1, BigInteger.ZERO);
+            }
+        }
+    }
+
     @Test
     void writeFloat() {
         // TODO: implement me