2 changed files with 166 additions and 10 deletions
@ -0,0 +1,98 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.script.api.tbel; |
|||
|
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
|
|||
import java.nio.ByteBuffer; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class TbUtilsTest { |
|||
|
|||
@Test |
|||
public void parseHexToInt() { |
|||
Assert.assertEquals(0xAB, TbUtils.parseHexToInt("AB")); |
|||
Assert.assertEquals(0xABBA, TbUtils.parseHexToInt("ABBA", true)); |
|||
Assert.assertEquals(0xBAAB, TbUtils.parseHexToInt("ABBA", false)); |
|||
Assert.assertEquals(0xAABBCC, TbUtils.parseHexToInt("AABBCC", true)); |
|||
Assert.assertEquals(0xAABBCC, TbUtils.parseHexToInt("CCBBAA", false)); |
|||
Assert.assertEquals(0xAABBCCDD, TbUtils.parseHexToInt("AABBCCDD", true)); |
|||
Assert.assertEquals(0xAABBCCDD, TbUtils.parseHexToInt("DDCCBBAA", false)); |
|||
Assert.assertEquals(0xDDCCBBAA, TbUtils.parseHexToInt("DDCCBBAA", true)); |
|||
Assert.assertEquals(0xDDCCBBAA, TbUtils.parseHexToInt("AABBCCDD", false)); |
|||
} |
|||
|
|||
@Test |
|||
public void parseBytesToInt_checkPrimitives() { |
|||
int expected = 257; |
|||
byte[] data = ByteBuffer.allocate(4).putInt(expected).array(); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4)); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 2, 2, true)); |
|||
Assert.assertEquals(1, TbUtils.parseBytesToInt(data, 3, 1, true)); |
|||
|
|||
expected = Integer.MAX_VALUE; |
|||
data = ByteBuffer.allocate(4).putInt(expected).array(); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4, true)); |
|||
|
|||
expected = 0xAABBCCDD; |
|||
data = new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD}; |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4, true)); |
|||
data = new byte[]{(byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA}; |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4, false)); |
|||
|
|||
expected = 0xAABBCC; |
|||
data = new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC}; |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 3, true)); |
|||
data = new byte[]{(byte) 0xCC, (byte) 0xBB, (byte) 0xAA}; |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 3, false)); |
|||
} |
|||
|
|||
@Test |
|||
public void parseBytesToInt_checkLists() { |
|||
int expected = 257; |
|||
List<Byte> data = toList(ByteBuffer.allocate(4).putInt(expected).array()); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4)); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 2, 2, true)); |
|||
Assert.assertEquals(1, TbUtils.parseBytesToInt(data, 3, 1, true)); |
|||
|
|||
expected = Integer.MAX_VALUE; |
|||
data = toList(ByteBuffer.allocate(4).putInt(expected).array()); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4, true)); |
|||
|
|||
expected = 0xAABBCCDD; |
|||
data = toList(new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD}); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4, true)); |
|||
data = toList(new byte[]{(byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA}); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 4, false)); |
|||
|
|||
expected = 0xAABBCC; |
|||
data = toList(new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC}); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 3, true)); |
|||
data = toList(new byte[]{(byte) 0xCC, (byte) 0xBB, (byte) 0xAA}); |
|||
Assert.assertEquals(expected, TbUtils.parseBytesToInt(data, 0, 3, false)); |
|||
} |
|||
|
|||
private static List<Byte> toList(byte[] data) { |
|||
List<Byte> result = new ArrayList<>(data.length); |
|||
for (Byte b : data) { |
|||
result.add(b); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue