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.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
19 import org.junit.jupiter.api.Test;
20 import org.openhab.io.transport.modbus.BitArray;
23 * @author Sami Salonen - Initial contribution
25 public class BasicBitArrayTest {
28 public void testGetBitAndSetBit() {
29 BitArray data1 = new BitArray(true, false, true);
30 assertThat(data1.size(), is(equalTo(3)));
31 assertThat(data1.getBit(0), is(equalTo(true)));
32 assertThat(data1.getBit(1), is(equalTo(false)));
33 assertThat(data1.getBit(2), is(equalTo(true)));
35 data1.setBit(1, true);
36 data1.setBit(2, false);
37 assertThat(data1.size(), is(equalTo(3)));
38 assertThat(data1.getBit(0), is(equalTo(true)));
39 assertThat(data1.getBit(1), is(equalTo(true)));
40 assertThat(data1.getBit(2), is(equalTo(false)));
44 public void testGetBitAndSetBit2() {
45 BitArray data1 = new BitArray(3);
46 assertThat(data1.size(), is(equalTo(3)));
47 assertThat(data1.getBit(0), is(equalTo(false)));
48 assertThat(data1.getBit(1), is(equalTo(false)));
49 assertThat(data1.getBit(2), is(equalTo(false)));
51 data1.setBit(1, true);
52 assertThat(data1.size(), is(equalTo(3)));
53 assertThat(data1.getBit(0), is(equalTo(false)));
54 assertThat(data1.getBit(1), is(equalTo(true)));
55 assertThat(data1.getBit(2), is(equalTo(false)));
57 data1.setBit(1, false);
58 assertThat(data1.size(), is(equalTo(3)));
59 assertThat(data1.getBit(0), is(equalTo(false)));
60 assertThat(data1.getBit(1), is(equalTo(false)));
61 assertThat(data1.getBit(2), is(equalTo(false)));
65 public void testOutOfBounds() {
66 BitArray data1 = new BitArray(true, false, true);
67 assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(3));
71 public void testOutOfBounds2() {
72 BitArray data1 = new BitArray(true, false, true);
73 assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(-1));
77 public void testOutOfBounds3() {
78 BitArray data1 = new BitArray(3);
79 assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(3));
83 public void testOutOfBounds4() {
84 BitArray data1 = new BitArray(3);
85 assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(-1));