]> git.basschouten.com Git - openhab-addons.git/blob
2d65dce319f67c22e680415dd5561f9f8778a24d
[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.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
18
19 import org.junit.jupiter.api.Test;
20 import org.openhab.io.transport.modbus.BitArray;
21
22 /**
23  * @author Sami Salonen - Initial contribution
24  */
25 public class BasicBitArrayTest {
26
27     @Test
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)));
34
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)));
41     }
42
43     @Test
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)));
50
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)));
56
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)));
62     }
63
64     @Test
65     public void testOutOfBounds() {
66         BitArray data1 = new BitArray(true, false, true);
67         assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(3));
68     }
69
70     @Test
71     public void testOutOfBounds2() {
72         BitArray data1 = new BitArray(true, false, true);
73         assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(-1));
74     }
75
76     @Test
77     public void testOutOfBounds3() {
78         BitArray data1 = new BitArray(3);
79         assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(3));
80     }
81
82     @Test
83     public void testOutOfBounds4() {
84         BitArray data1 = new BitArray(3);
85         assertThrows(IndexOutOfBoundsException.class, () -> data1.getBit(-1));
86     }
87 }