2 * Copyright (c) 2010-2023 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.binding.modbus.e3dc.util;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import java.util.BitSet;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.modbus.e3dc.internal.dto.DataConverter;
22 import org.openhab.core.io.transport.modbus.ValueBuffer;
25 * The {@link DataConverterTest} Test data conversions
27 * @author Bernd Weymann - Initial contribution
30 public class DataConverterTest {
33 public void testRoundPositive() {
34 assertEquals(2.3, DataConverter.round(2.34, 1), 0.01);
38 public void testRoundPositive2() {
39 assertEquals(2.4, DataConverter.round(2.37, 1), 0.01);
43 public void testRoundPositive3() {
44 assertEquals(2.4, DataConverter.round(2.35, 1), 0.01);
48 public void testRoundNegative() {
49 assertEquals(-2.3, DataConverter.round(-2.34, 1), 0.01);
53 public void testRoundNegative2() {
54 assertEquals(-2.4, DataConverter.round(-2.37, 1), 0.01);
58 public void testRoundNegative3() {
59 // rounding towards positive infinity. Note difference to testRoundPositive3
60 assertEquals(-2.3, DataConverter.round(-2.35, 1), 0.01);
64 public void testUDoubleValue() {
65 assertEquals(0.5, DataConverter.getUDoubleValue(ValueBuffer.wrap(new byte[] { 0, 5 }), 0.1), 0.01);
69 public void testUDoubleValue2() {
71 DataConverter.getUDoubleValue(ValueBuffer.wrap(new byte[] { (byte) 0xf0, (byte) 0x9f }), 0.1), 0.01);
75 public void testUDoubleValue3() {
77 DataConverter.getUDoubleValue(ValueBuffer.wrap(new byte[] { (byte) 0xf0, (byte) 0x9f }), 2), 0.01);
81 public void testBitsetToInt() {
82 byte[] b = new byte[] { 3, 16 };
83 BitSet s = BitSet.valueOf(b);
84 // Bit0 is the least significant bit to DataConverter.toInt
85 assertEquals(true, s.get(0), "Bit0");
86 assertEquals(true, s.get(1), "Bit1");
87 assertEquals(false, s.get(2), "Bit2");
88 assertEquals(false, s.get(3), "Bit3");
89 assertEquals(false, s.get(4), "Bit4");
90 assertEquals(false, s.get(5), "Bit5");
91 assertEquals(false, s.get(6), "Bit6");
92 assertEquals(false, s.get(7), "Bit7");
93 assertEquals(false, s.get(8), "Bit8");
94 assertEquals(false, s.get(9), "Bit9");
95 assertEquals(false, s.get(10), "Bit10");
96 assertEquals(false, s.get(11), "Bit11");
97 assertEquals(true, s.get(12), "Bit12");
98 assertEquals(false, s.get(13), "Bit13");
99 assertEquals(false, s.get(14), "Bit14");
100 assertEquals(false, s.get(15), "Bit15");
102 int bitsAsInt = DataConverter.toInt(s);
103 int expected = 0b0001000000000011;
104 assertEquals(Integer.toBinaryString(expected), Integer.toBinaryString(bitsAsInt));
105 assertEquals(expected, bitsAsInt);