Skip to content

Commit

Permalink
fixed wrong implementation of Length
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 2, 2018
1 parent 5d3ecb8 commit 46fa650
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ public class Length extends ByteValue {
assertLength(NUM_BYTES);
}

public static Length of(int length) {
return new Length(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());
public static Length of(long length) {
checkUnsignedBounds(length, NUM_BYTES);
return new Length(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (length >> 24 & 0xff))
.put((byte) (length >> 16 & 0xff))
.put((byte) (length >> 8 & 0xff))
.put((byte) (length & 0xff))
.array());
}

public static Length of(String length) {
return of(Long.parseLong(length));
}

public static Length of(byte... values) {
return new Length(values);
}

@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public static AMSPort of(byte... values) {
}

public static AMSPort of(int port) {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Value must between 0 and 65535");
}
checkUnsignedBounds(port, NUM_BYTES);
return new AMSPort(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (port >> 8 & 0xff))
.put((byte) (port & 0xff))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ protected void assertLength(int length) {
}
}

protected static void checkUnsignedBounds(long value, int numberOfBytes) {
double upperBound = Math.pow(2, 8 * numberOfBytes);
if (value < 0 || value >= upperBound) {
throw new IllegalArgumentException("Value must between 0 and " + upperBound + ". Was " + value);
}
}

@Override
public byte[] getBytes() {
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.apache.plc4x.java.ads.api.commands.types;

import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class LengthTest {

byte NULL_BYTE = 0x0;

@Test
void ofBytes() {
Assertions.assertEquals("0", Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString());
Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE));
}

@Test
void ofLong() {
assertByte(Length.of(1), "0x00000001");
assertByte(Length.of(65535), "0x0000ffff");
Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(Long.valueOf("4294967296")));
}

@Test
void ofString() {
assertByte(Length.of("1"), "0x00000001");
}

@Test
void testToString() {
Assertions.assertEquals(Length.of("1").toString(), "1");
}

void assertByte(Length actual, String expected) {
Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
}
}

0 comments on commit 46fa650

Please sign in to comment.