This is an automated email from the ASF dual-hosted git repository. toulmean pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/master by this push: new a09ba8c Add more tests for Bytes classes new 399c2c7 Merge pull request #136 from atoulme/add_bytes_test a09ba8c is described below commit a09ba8c4ab6402eaacda4d552ffa4149cbf06bd8 Author: Antoine Toulme <anto...@lunar-ocean.com> AuthorDate: Sun Jul 19 21:56:13 2020 -0700 Add more tests for Bytes classes --- .../java/org/apache/tuweni/bytes/Bytes48Test.java | 67 ++++++++++++ .../org/apache/tuweni/bytes/MutableBytesTest.java | 112 +++++++++++++++++++++ 2 files changed, 179 insertions(+) diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java b/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java index d2ce8cc..77e81cd 100644 --- a/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java +++ b/bytes/src/test/java/org/apache/tuweni/bytes/Bytes48Test.java @@ -13,6 +13,7 @@ package org.apache.tuweni.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -66,4 +67,70 @@ class Bytes48Test { Throwable exception = assertThrows(IllegalArgumentException.class, () -> Bytes48.rightPad(MutableBytes.create(49))); assertEquals("Expected at most 48 bytes but got 49", exception.getMessage()); } + + @Test + void hexString() { + Bytes initial = Bytes48.random(); + assertEquals(initial, Bytes48.fromHexStringLenient(initial.toHexString())); + assertEquals(initial, Bytes48.fromHexString(initial.toHexString())); + assertEquals(initial, Bytes48.fromHexStringStrict(initial.toHexString())); + } + + @Test + void size() { + assertEquals(48, Bytes48.random().size()); + } + + @Test + void not() { + assertEquals( + Bytes48 + .fromHexString( + "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + Bytes48.leftPad(Bytes.EMPTY).not()); + } + + @Test + void wrap() { + Bytes source = Bytes.random(96); + Bytes48 value = Bytes48.wrap(source, 2); + assertEquals(source.slice(2, 48), value); + } + + @Test + void leftPad() { + Bytes48 source = Bytes48.random(); + assertSame(source, Bytes48.leftPad(source)); + assertSame(source, Bytes48.rightPad(source)); + } + + @Test + void or() { + Bytes48 one = Bytes48 + .fromHexString( + "0x0000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffff"); + Bytes48 two = Bytes48 + .fromHexString( + "0xffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000"); + assertEquals( + Bytes48 + .fromHexString( + "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + one.or(two)); + } + + @Test + void and() { + Bytes48 one = Bytes48 + .fromHexString( + "0x0000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffff"); + Bytes48 two = Bytes48 + .fromHexString( + "0xffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000"); + assertEquals( + Bytes48 + .fromHexString( + "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), + one.and(two)); + } } diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/MutableBytesTest.java b/bytes/src/test/java/org/apache/tuweni/bytes/MutableBytesTest.java new file mode 100644 index 0000000..872d157 --- /dev/null +++ b/bytes/src/test/java/org/apache/tuweni/bytes/MutableBytesTest.java @@ -0,0 +1,112 @@ +/* + * 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.tuweni.bytes; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.netty.buffer.Unpooled; +import io.vertx.core.buffer.Buffer; +import org.junit.jupiter.api.Test; + +class MutableBytesTest { + + @Test + void testMutableBytesWrap() { + MutableBytes b = MutableBytes.wrap(Bytes.fromHexString("deadbeef").toArrayUnsafe(), 1, 3); + assertEquals(Bytes.fromHexString("adbeef"), b); + } + + @Test + void testClear() { + MutableBytes b = MutableBytes.wrap(Bytes.fromHexString("deadbeef").toArrayUnsafe()); + b.clear(); + assertEquals(Bytes.fromHexString("00000000"), b); + } + + @Test + void testFill() { + MutableBytes b = MutableBytes.create(2); + b.fill((byte) 34); + assertEquals(Bytes.fromHexString("0x2222"), b); + } + + @Test + void testDecrementAndIncrement() { + MutableBytes b = MutableBytes.create(2); + b.increment(); + assertEquals(Bytes.fromHexString("0x0001"), b); + b.decrement(); + assertEquals(Bytes.fromHexString("0x0000"), b); + + b.fill((byte) 0xFF); + b.decrement(); + assertEquals(Bytes.fromHexString("0xFFFE"), b); + + b = MutableBytes.wrap(Bytes.fromHexString("0x00FF").toArrayUnsafe()); + b.increment(); + assertEquals(Bytes.fromHexString("0x0100"), b); + } + + @Test + void setLong() { + MutableBytes b = MutableBytes.create(8); + b.setLong(0, 256); + assertEquals(Bytes.fromHexString("0x0000000000000100"), b); + } + + @Test + void setInt() { + MutableBytes b = MutableBytes.create(4); + b.setInt(0, 256); + assertEquals(Bytes.fromHexString("0x00000100"), b); + } + + @Test + void setIntOverflow() { + MutableBytes b = MutableBytes.create(2); + assertThrows(IndexOutOfBoundsException.class, () -> { + b.setInt(0, 18096); + }); + } + + @Test + void setLongOverflow() { + MutableBytes b = MutableBytes.create(6); + assertThrows(IndexOutOfBoundsException.class, () -> { + b.setLong(0, Long.MAX_VALUE); + }); + } + + @Test + void wrap32() { + MutableBytes b = MutableBytes.create(32); + assertTrue(b instanceof MutableBytes32); + b = MutableBytes.wrap(Bytes.random(36).toArrayUnsafe(), 4, 32); + assertTrue(b instanceof MutableBytes32); + } + + @Test + void wrapEmpty() { + MutableBytes b = MutableBytes.wrapBuffer(Buffer.buffer()); + assertSame(MutableBytes.EMPTY, b); + b = MutableBytes.wrapByteBuf(Unpooled.buffer(0)); + assertSame(MutableBytes.EMPTY, b); + b = MutableBytes.wrapBuffer(Buffer.buffer(), 3, 0); + assertSame(MutableBytes.EMPTY, b); + b = MutableBytes.wrapByteBuf(Unpooled.buffer(), 4, 0); + assertSame(MutableBytes.EMPTY, b); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org For additional commands, e-mail: commits-h...@tuweni.apache.org