]> git.basschouten.com Git - openhab-addons.git/blob
8f743682417e4a8a414729a004ab5321c80c8612
[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 extractBit
23  *
24  * @author Sami Salonen - Initial contribution
25  */
26 public class BitUtilitiesExtractBitTest {
27
28     @Test
29     public void testExtractBitWithRegisterIndexAndBitIndex() {
30         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
31                 0b00100101, // lo byte of 1st register
32                 0b00110001, // hi byte of 2nd register
33                 0b00101001 }; // lo byte of 2nd register
34
35         {
36             int registerIndex = 0;
37             int[] expectedBitsFromLSBtoMSB = new int[] { //
38                     1, 0, 1, 0, 0, 1, 0, 0, // lo byte, with increasing significance
39                     1, 0, 0, 0, 0, 1, 0, 0 // hi byte, with increasing significance
40             };
41             for (int bitIndex = 0; bitIndex < expectedBitsFromLSBtoMSB.length; bitIndex++) {
42                 assertEquals(expectedBitsFromLSBtoMSB[bitIndex],
43                         ModbusBitUtilities.extractBit(bytes, registerIndex, bitIndex),
44                         String.format("bitIndex=%d", bitIndex));
45             }
46         }
47         {
48             int registerIndex = 1;
49             int[] expectedBitsFromLSBtoMSB = new int[] { //
50                     1, 0, 0, 1, 0, 1, 0, 0, // lo byte, with increasing significance
51                     1, 0, 0, 0, 1, 1, 0, 0 // hi byte, with increasing significance
52             };
53             for (int bitIndex = 0; bitIndex < expectedBitsFromLSBtoMSB.length; bitIndex++) {
54                 assertEquals(expectedBitsFromLSBtoMSB[bitIndex],
55                         ModbusBitUtilities.extractBit(bytes, registerIndex, bitIndex),
56                         String.format("bitIndex=%d", bitIndex));
57             }
58         }
59     }
60
61     @Test
62     public void testExtractBitWithRegisterIndexAndBitIndexOOB() {
63
64         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
65                 0b00100101, // lo byte of 1st register
66                 0b00110001, // hi byte of 2nd register
67                 0b00101001 }; // lo byte of 2nd register
68         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractBit(bytes, 3, 0));
69     }
70
71     @Test
72     public void testExtractBitWithRegisterIndexAndBitIndexOOB2() {
73         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
74                 0b00100101, // lo byte of 1st register
75                 0b00110001, // hi byte of 2nd register
76                 0b00101001 }; // lo byte of 2nd register
77         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractBit(bytes, 0, 17));
78     }
79
80     @Test
81     public void testExtractBitWithRegisterIndexAndBitIndexOOB3() {
82         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
83                 0b00100101, // lo byte of 1st register
84                 0b00110001, // hi byte of 2nd register
85                 0b00101001 }; // lo byte of 2nd register
86         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractBit(bytes, 0, -1));
87     }
88
89     @Test
90     public void testExtractBitWithRegisterIndexAndBitIndexOOB4() {
91         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
92                 0b00100101, // lo byte of 1st register
93                 0b00110001, // hi byte of 2nd register
94                 0b00101001 }; // lo byte of 2nd register
95         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractBit(bytes, -1, 0));
96     }
97
98     @Test
99     public void testExtractBitWithSingleIndex() {
100         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
101                 0b00100101, // lo byte of 1st register
102                 0b00110001, // hi byte of 2nd register
103                 0b00101001 }; // lo byte of 2nd register
104         int[] expectedBits = new int[] { //
105                 1, 0, 1, 0, 0, 1, 0, 0, // 1st register: lo byte, with increasing significance
106                 1, 0, 0, 0, 0, 1, 0, 0, // 1st register: hi byte, with increasing significance
107                 1, 0, 0, 1, 0, 1, 0, 0, // 2nd register: lo byte, with increasing significance
108                 1, 0, 0, 0, 1, 1, 0, 0 // 2nd register: hi byte, with increasing significance
109         };
110         for (int bitIndex = 0; bitIndex < expectedBits.length; bitIndex++) {
111             assertEquals(expectedBits[bitIndex], ModbusBitUtilities.extractBit(bytes, bitIndex),
112                     String.format("bitIndex=%d", bitIndex));
113             assertEquals(expectedBits[bitIndex], ModbusBitUtilities.extractBit(bytes, bitIndex),
114                     String.format("bitIndex=%d", bitIndex));
115         }
116     }
117
118     @Test
119     public void testExtractBitWithSingleIndexOOB() {
120         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
121                 0b00100101, // lo byte of 1st register
122                 0b00110001, // hi byte of 2nd register
123                 0b00101001 }; // lo byte of 2nd register
124         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractBit(bytes, 32));
125     }
126
127     @Test
128     public void testExtractBitWithSingleIndexOOB2() {
129         byte[] bytes = new byte[] { 0b00100001, // hi byte of 1st register
130                 0b00100101, // lo byte of 1st register
131                 0b00110001, // hi byte of 2nd register
132                 0b00101001 }; // lo byte of 2nd register
133         assertThrows(IllegalArgumentException.class, () -> ModbusBitUtilities.extractBit(bytes, -1));
134     }
135 }