]> git.basschouten.com Git - openhab-addons.git/blob
7f14c137add8a199f328708d52d5e5cf07885025
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus.test;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.nio.BufferOverflowException;
18 import java.nio.InvalidMarkException;
19
20 import org.junit.jupiter.api.Test;
21 import org.openhab.io.transport.modbus.ValueBuffer;
22
23 /**
24  * @author Sami Salonen - Initial contribution
25  */
26 public class ValueBufferTest {
27
28     @Test
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());
33
34         assertEquals(-1004, wrap.getSInt32());
35         assertEquals(3, wrap.remaining());
36         assertTrue(wrap.hasRemaining());
37
38         assertEquals(3, wrap.getSInt8());
39         assertEquals(2, wrap.remaining());
40         assertTrue(wrap.hasRemaining());
41
42         assertEquals(-1, wrap.getSInt8());
43         assertEquals(1, wrap.remaining());
44         assertTrue(wrap.hasRemaining());
45
46         assertEquals(254, wrap.getUInt8());
47         assertEquals(0, wrap.remaining());
48         assertFalse(wrap.hasRemaining());
49     }
50
51     @Test
52     public void testOutOfBounds() {
53         ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
54         wrap.position(7);
55         assertThrows(IllegalArgumentException.class, () -> wrap.getSInt8());
56     }
57
58     @Test
59     public void testOutOfBound2s() {
60         ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
61         wrap.position(6);
62         assertThrows(IllegalArgumentException.class, () -> wrap.getSInt16());
63     }
64
65     @Test
66     public void testMarkReset() {
67         ValueBuffer wrap = ValueBuffer.wrap(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFC, 0x14, 3, -1, -2 });
68         wrap.mark();
69         assertEquals(-1004, wrap.getSInt32());
70         wrap.reset();
71         assertEquals(4294966292L, wrap.getUInt32());
72         wrap.mark();
73         assertEquals(3, wrap.getSInt8());
74         wrap.reset();
75         assertEquals(3, wrap.getSInt8());
76         assertEquals(-1, wrap.getSInt8());
77         assertEquals(254, wrap.getUInt8());
78     }
79
80     @Test
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());
84         wrap.position(4);
85         wrap.mark();
86         assertEquals(4, wrap.position());
87
88         // mark = position
89         wrap.position(4);
90         assertEquals(4, wrap.position());
91         wrap.reset();
92         assertEquals(4, wrap.position());
93
94         // position < mark
95         wrap.position(3); // Mark is removed here
96         assertEquals(3, wrap.position());
97         boolean caughtException = false;
98         try {
99             wrap.reset();
100         } catch (InvalidMarkException e) {
101             // OK, expected
102             caughtException = true;
103         }
104         assertTrue(caughtException);
105         assertEquals(3, wrap.position());
106
107         // Mark is removed. Reset unaccessible even with original position of 4
108         wrap.position(4);
109         assertEquals(4, wrap.position());
110         caughtException = false;
111         try {
112             wrap.reset();
113         } catch (InvalidMarkException e) {
114             // OK, expected
115             caughtException = true;
116         }
117         assertTrue(caughtException);
118     }
119
120     @Test
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());
124         wrap.position(4);
125         wrap.mark();
126         assertEquals(4, wrap.position());
127
128         // mark = position
129         wrap.position(4);
130         assertEquals(4, wrap.position());
131         wrap.reset();
132         assertEquals(4, wrap.position());
133
134         // mark > position
135         wrap.position(6);
136         assertEquals(6, wrap.position());
137         wrap.reset();
138         assertEquals(4, wrap.position());
139     }
140
141     @Test
142     public void testPosition() {
143         ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 0, 0, 0, 1, 3, -1, -2 });
144         assertEquals(0, wrap.position());
145
146         wrap.position(4);
147         assertEquals(4, wrap.position());
148         assertEquals(3, wrap.getSInt8());
149         assertEquals(5, wrap.position());
150     }
151
152     @Test
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));
157     }
158
159     @Test
160     public void testBulkGetAtCapacity() {
161         ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 1, 2 });
162         byte[] twoBytes = new byte[2];
163         wrap.get(twoBytes);
164         assertEquals(1, twoBytes[0]);
165         assertEquals(2, twoBytes[1]);
166         assertEquals(2, wrap.position());
167         assertFalse(wrap.hasRemaining());
168     }
169
170     @Test
171     public void testBulkGet() {
172         ValueBuffer wrap = ValueBuffer.wrap(new byte[] { 1, 2, 3 });
173         byte[] onebyte = new byte[1];
174         wrap.get(onebyte);
175         assertEquals(1, onebyte[0]);
176         assertEquals(1, wrap.position());
177
178         // non-zero position
179         byte[] twoBytes = new byte[2];
180         wrap.position(1);
181         wrap.get(twoBytes);
182         assertEquals(2, twoBytes[0]);
183         assertEquals(3, twoBytes[1]);
184         assertEquals(3, wrap.position());
185     }
186 }