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 org.junit.jupiter.api.Test;
18 import org.openhab.io.transport.modbus.ModbusBitUtilities;
22 * Tests for extractBit
24 * @author Sami Salonen - Initial contribution
26 public class BitUtilitiesExtractBitTest {
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
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
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));
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
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));
62 public void testExtractBitWithRegisterIndexAndBitIndexOOB() {
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));
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));
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));
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));
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
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));
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));
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));