2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.transport.modbus.test;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.nio.BufferOverflowException;
18 import java.nio.InvalidMarkException;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.io.transport.modbus.ValueBuffer;
24 * @author Sami Salonen - Initial contribution
26 public class ValueBufferTest {
29 public void testInt32Int8() {
30 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
31 assertEquals(7, wrap.remaining());
32 assertTrue(wrap.hasRemaining());
34 assertEquals(-1004, wrap.getSInt32());
35 assertEquals(3, wrap.remaining());
36 assertTrue(wrap.hasRemaining());
38 assertEquals(3, wrap.getSInt8());
39 assertEquals(2, wrap.remaining());
40 assertTrue(wrap.hasRemaining());
42 assertEquals(-1, wrap.getSInt8());
43 assertEquals(1, wrap.remaining());
44 assertTrue(wrap.hasRemaining());
46 assertEquals(254, wrap.getUInt8());
47 assertEquals(0, wrap.remaining());
48 assertFalse(wrap.hasRemaining());
52 public void testOutOfBounds() {
53 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
55 assertThrows(IllegalArgumentException.class, () -> wrap.getSInt8());
59 public void testOutOfBound2s() {
60 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
62 assertThrows(IllegalArgumentException.class, () -> wrap.getSInt16());
66 public void testMarkReset() {
67 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
69 assertEquals(-1004, wrap.getSInt32());
71 assertEquals(4294966292L, wrap.getUInt32());
73 assertEquals(3, wrap.getSInt8());
75 assertEquals(3, wrap.getSInt8());
76 assertEquals(-1, wrap.getSInt8());
77 assertEquals(254, wrap.getUInt8());
81 public void testMarkHigherThanPosition() {
82 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
83 assertEquals(-1004, wrap.getSInt32());
86 assertEquals(4, wrap.position());
90 assertEquals(4, wrap.position());
92 assertEquals(4, wrap.position());
95 wrap.position(3); // Mark is removed here
96 assertEquals(3, wrap.position());
97 boolean caughtException = false;
100 } catch (InvalidMarkException e) {
102 caughtException = true;
104 assertTrue(caughtException);
105 assertEquals(3, wrap.position());
107 // Mark is removed. Reset unaccessible even with original position of 4
109 assertEquals(4, wrap.position());
110 caughtException = false;
113 } catch (InvalidMarkException e) {
115 caughtException = true;
117 assertTrue(caughtException);
121 public void testMarkLowerThanPosition() {
122 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
123 assertEquals(-1004, wrap.getSInt32());
126 assertEquals(4, wrap.position());
130 assertEquals(4, wrap.position());
132 assertEquals(4, wrap.position());
136 assertEquals(6, wrap.position());
138 assertEquals(4, wrap.position());
142 public void testPosition() {
143 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 0, 0, 0, 1, 3, -1, -2 });
144 assertEquals(0, wrap.position());
147 assertEquals(4, wrap.position());
148 assertEquals(3, wrap.getSInt8());
149 assertEquals(5, wrap.position());
153 public void testBulkGetBufferOverflow() {
154 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 0, 0 });
155 byte[] threeBytes = new byte[3];
156 assertThrows(BufferOverflowException.class, () -> wrap.get(threeBytes));
160 public void testBulkGetAtCapacity() {
161 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 1, 2 });
162 byte[] twoBytes = new byte[2];
164 assertEquals(1, twoBytes[0]);
165 assertEquals(2, twoBytes[1]);
166 assertEquals(2, wrap.position());
167 assertFalse(wrap.hasRemaining());
171 public void testBulkGet() {
172 ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 1, 2, 3 });
173 byte[] onebyte = new byte[1];
175 assertEquals(1, onebyte[0]);
176 assertEquals(1, wrap.position());
179 byte[] twoBytes = new byte[2];
182 assertEquals(2, twoBytes[0]);
183 assertEquals(3, twoBytes[1]);
184 assertEquals(3, wrap.position());