]> git.basschouten.com Git - openhab-addons.git/blob
a8bffbfb27b520a25038b97250690df32ec64eff
[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.junit.Assert.assertThat;
17
18 import org.junit.Test;
19 import org.openhab.io.transport.modbus.BitArray;
20
21 /**
22  * @author Sami Salonen - Initial contribution
23  */
24 public class BasicBitArrayTest {
25
26     @Test
27     public void testGetBitAndSetBit() {
28         BitArray data1 = new BitArray(true, false, true);
29         assertThat(data1.size(), is(equalTo(3)));
30         assertThat(data1.getBit(0), is(equalTo(true)));
31         assertThat(data1.getBit(1), is(equalTo(false)));
32         assertThat(data1.getBit(2), is(equalTo(true)));
33
34         data1.setBit(1, true);
35         data1.setBit(2, false);
36         assertThat(data1.size(), is(equalTo(3)));
37         assertThat(data1.getBit(0), is(equalTo(true)));
38         assertThat(data1.getBit(1), is(equalTo(true)));
39         assertThat(data1.getBit(2), is(equalTo(false)));
40     }
41
42     @Test
43     public void testGetBitAndSetBit2() {
44         BitArray data1 = new BitArray(3);
45         assertThat(data1.size(), is(equalTo(3)));
46         assertThat(data1.getBit(0), is(equalTo(false)));
47         assertThat(data1.getBit(1), is(equalTo(false)));
48         assertThat(data1.getBit(2), is(equalTo(false)));
49
50         data1.setBit(1, true);
51         assertThat(data1.size(), is(equalTo(3)));
52         assertThat(data1.getBit(0), is(equalTo(false)));
53         assertThat(data1.getBit(1), is(equalTo(true)));
54         assertThat(data1.getBit(2), is(equalTo(false)));
55
56         data1.setBit(1, false);
57         assertThat(data1.size(), is(equalTo(3)));
58         assertThat(data1.getBit(0), is(equalTo(false)));
59         assertThat(data1.getBit(1), is(equalTo(false)));
60         assertThat(data1.getBit(2), is(equalTo(false)));
61     }
62
63     @Test(expected = IndexOutOfBoundsException.class)
64     public void testOutOfBounds() {
65         BitArray data1 = new BitArray(true, false, true);
66         data1.getBit(3);
67     }
68
69     @Test(expected = IndexOutOfBoundsException.class)
70     public void testOutOfBounds2() {
71         BitArray data1 = new BitArray(true, false, true);
72         data1.getBit(-1);
73     }
74
75     @Test(expected = IndexOutOfBoundsException.class)
76     public void testOutOfBounds3() {
77         BitArray data1 = new BitArray(3);
78         data1.getBit(3);
79     }
80
81     @Test(expected = IndexOutOfBoundsException.class)
82     public void testOutOfBounds4() {
83         BitArray data1 = new BitArray(3);
84         data1.getBit(-1);
85     }
86 }