]> git.basschouten.com Git - openhab-addons.git/blob
811b12d31df2cad8c7785a69aba41af9146d17f0
[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 org.junit.jupiter.api.Test;
18 import org.openhab.io.transport.modbus.ModbusBitUtilities;
19
20 /**
21  *
22  * Tests for extractSInt8 and extractUInt8
23  *
24  * @author Sami Salonen - Initial contribution
25  */
26 public class BitUtilitiesExtractInt8Test {
27
28     @Test
29     public void extractSInt8WithSingleIndex() {
30         byte[] bytes = new byte[] { -1, 2, 3 };
31         assertEquals(-1, ModbusBitUtilities.extractSInt8(bytes, 0));
32         assertEquals(2, ModbusBitUtilities.extractSInt8(bytes, 1));
33         assertEquals(3, ModbusBitUtilities.extractSInt8(bytes, 2));
34     }
35
36     @Test
37     public void extractSInt8WithSingleIndexOOB() {
38         byte[] bytes = new byte[] { -1, 2, 3 };
39         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractSInt8(bytes, 3));
40     }
41
42     @Test
43     public void extractSInt8WithSingleIndexOOB2() {
44         byte[] bytes = new byte[] { -1, 2, 3 };
45         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractSInt8(bytes, -1));
46     }
47
48     @Test
49     public void extractSInt8WithRegisterIndexAndHiByte() {
50         byte[] bytes = new byte[] { -1, 2, 3, 4 };
51         assertEquals(-1, ModbusBitUtilities.extractSInt8(bytes, 0, true));
52         assertEquals(2, ModbusBitUtilities.extractSInt8(bytes, 0, false));
53         assertEquals(3, ModbusBitUtilities.extractSInt8(bytes, 1, true));
54         assertEquals(4, ModbusBitUtilities.extractSInt8(bytes, 1, false));
55     }
56
57     @Test
58     public void extractSInt8WithRegisterIndexAndHiByteOOB() {
59         byte[] bytes = new byte[] { -1, 2, 3, 4 };
60         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractSInt8(bytes, 2, true));
61     }
62
63     @Test
64     public void extractSInt8WithRegisterIndexAndHiByteOOB2() {
65         byte[] bytes = new byte[] { -1, 2, 3, 4 };
66         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractSInt8(bytes, -1, true));
67     }
68
69     //
70     // unsigned int8 follows
71     //
72
73     @Test
74     public void extractUInt8WithSingleIndex() {
75         byte[] bytes = new byte[] { -1, 2, 3 };
76         assertEquals(255, ModbusBitUtilities.extractUInt8(bytes, 0));
77         assertEquals(2, ModbusBitUtilities.extractUInt8(bytes, 1));
78         assertEquals(3, ModbusBitUtilities.extractUInt8(bytes, 2));
79     }
80
81     @Test
82     public void extractUInt8WithSingleIndexOOB() {
83         byte[] bytes = new byte[] { -1, 2, 3 };
84         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractUInt8(bytes, 3));
85     }
86
87     @Test
88     public void extractUInt8WithSingleIndexOOB2() {
89         byte[] bytes = new byte[] { -1, 2, 3 };
90         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractUInt8(bytes, -1));
91     }
92
93     @Test
94     public void extractUInt8WithRegisterIndexAndHiByte() {
95         byte[] bytes = new byte[] { -1, 2, 3, 4 };
96         assertEquals(255, ModbusBitUtilities.extractUInt8(bytes, 0, true));
97         assertEquals(2, ModbusBitUtilities.extractUInt8(bytes, 0, false));
98         assertEquals(3, ModbusBitUtilities.extractUInt8(bytes, 1, true));
99         assertEquals(4, ModbusBitUtilities.extractUInt8(bytes, 1, false));
100     }
101
102     @Test
103     public void extractUInt8WithRegisterIndexAndHiByteOOB() {
104         byte[] bytes = new byte[] { -1, 2, 3, 4 };
105         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractUInt8(bytes, 2, true));
106     }
107
108     @Test
109     public void extractUInt8WithRegisterIndexAndHiByteOOB2() {
110         byte[] bytes = new byte[] { -1, 2, 3, 4 };
111         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractUInt8(bytes, 255, true));
112     }
113 }