From 6d11abf4b677ecade9f01e5c53c2c0f9a593d7f2 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 14 Oct 2021 15:25:00 +0300 Subject: [PATCH] Implement missing methods in Boolean, Byte and Short classes --- .../teavm/classlib/java/lang/TBoolean.java | 16 ++++++ .../org/teavm/classlib/java/lang/TByte.java | 17 ++++++ .../org/teavm/classlib/java/lang/TShort.java | 17 ++++++ .../teavm/classlib/java/lang/BooleanTest.java | 24 +++++++++ .../teavm/classlib/java/lang/ByteTest.java | 53 +++++++++++++++++++ .../teavm/classlib/java/lang/ShortTest.java | 53 +++++++++++++++++++ 6 files changed, 180 insertions(+) create mode 100644 tests/src/test/java/org/teavm/classlib/java/lang/ByteTest.java create mode 100644 tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java index b43d39e2c..8a08dff17 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java @@ -76,6 +76,10 @@ public class TBoolean extends TObject implements TSerializable, TComparable { public static final byte MAX_VALUE = 127; public static final Class TYPE = byte.class; public static final int SIZE = 8; + public static final int BYTES = 1; private byte value; public TByte(byte value) { @@ -76,6 +77,10 @@ public class TByte extends TNumber implements TComparable { @Override public int hashCode() { + return hashCode(value); + } + + public static int hashCode(byte value) { return value; } @@ -83,11 +88,23 @@ public class TByte extends TNumber implements TComparable { return a - b; } + public static int compareUnsigned(byte a, byte b) { + return (a & 255) - (b & 255); + } + @Override public int compareTo(TByte other) { return compare(value, other.value); } + public static int toUnsignedInt(byte value) { + return value & 255; + } + + public static long toUnsignedLong(byte value) { + return value & 255L; + } + public static byte parseByte(String s) throws TNumberFormatException { return parseByte(s, 10); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java index 56531a8e1..1d4999cfe 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java @@ -20,6 +20,7 @@ public class TShort extends TNumber implements TComparable { public static final short MAX_VALUE = 32767; public static final Class TYPE = short.class; public static final int SIZE = 16; + public static final int BYTES = 2; private short value; public TShort(short value) { @@ -75,6 +76,10 @@ public class TShort extends TNumber implements TComparable { @Override public int hashCode() { + return hashCode(value); + } + + public static int hashCode(short value) { return value; } @@ -82,11 +87,23 @@ public class TShort extends TNumber implements TComparable { return a - b; } + public static int compareUnsigned(short a, short b) { + return (a & 0xFFFF) - (b & 0xFFFF); + } + @Override public int compareTo(TShort other) { return compare(value, other.value); } + public static int toUnsignedInt(short value) { + return value & 0xFFFF; + } + + public static long toUnsignedLong(short value) { + return value & 0xFFFF; + } + public static short parseShort(String s, int radix) throws TNumberFormatException { int value = TInteger.parseInt(s, radix); if (value < MIN_VALUE || value > MAX_VALUE) { diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java index c9410f1f5..f71e96772 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java @@ -41,4 +41,28 @@ public class BooleanTest { assertFalse(Boolean.getBoolean("test.baz")); assertFalse(Boolean.getBoolean(null)); } + + @Test + public void logicalMethods() { + assertTrue(Boolean.logicalAnd(true, true)); + assertFalse(Boolean.logicalAnd(false, true)); + assertFalse(Boolean.logicalAnd(true, false)); + assertFalse(Boolean.logicalAnd(false, false)); + + assertTrue(Boolean.logicalOr(true, true)); + assertTrue(Boolean.logicalOr(false, true)); + assertTrue(Boolean.logicalOr(true, false)); + assertFalse(Boolean.logicalOr(false, false)); + + assertFalse(Boolean.logicalXor(true, true)); + assertTrue(Boolean.logicalXor(false, true)); + assertTrue(Boolean.logicalXor(true, false)); + assertFalse(Boolean.logicalXor(false, false)); + } + + @Test + public void hashCodeTest() { + assertEquals(Boolean.hashCode(true), Boolean.TRUE.hashCode()); + assertEquals(Boolean.hashCode(false), Boolean.FALSE.hashCode()); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ByteTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ByteTest.java new file mode 100644 index 000000000..08c2ef065 --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ByteTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2021 Alexey Andreev. + * + * 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.teavm.classlib.java.lang; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.teavm.junit.TeaVMTestRunner; +import org.teavm.junit.WholeClassCompilation; + +@WholeClassCompilation +@RunWith(TeaVMTestRunner.class) +public class ByteTest { + @Test + public void hashCodeTest() { + assertEquals(Byte.hashCode((byte) 0), Byte.valueOf((byte) 0).hashCode()); + assertEquals(Byte.hashCode((byte) 23), Byte.valueOf((byte) 23).hashCode()); + } + + @Test + public void compareUnsigned() { + assertTrue(Byte.compareUnsigned((byte) 5, (byte) 3) > 0); + assertTrue(Byte.compareUnsigned((byte) 3, (byte) 5) < 0); + assertTrue(Byte.compareUnsigned((byte) 3, (byte) 3) == 0); + assertTrue(Byte.compareUnsigned((byte) -23, (byte) 23) > 0); + assertTrue(Byte.compareUnsigned((byte) 23, (byte) -23) < 0); + } + + @Test + public void toUnsigned() { + assertEquals(254, Byte.toUnsignedInt((byte) -2)); + assertEquals(254L, Byte.toUnsignedLong((byte) -2)); + } + + @Test + public void bytes() { + assertEquals(1, Byte.BYTES); + } +} diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java new file mode 100644 index 000000000..2abbad480 --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2021 Alexey Andreev. + * + * 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.teavm.classlib.java.lang; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.teavm.junit.TeaVMTestRunner; +import org.teavm.junit.WholeClassCompilation; + +@WholeClassCompilation +@RunWith(TeaVMTestRunner.class) +public class ShortTest { + @Test + public void hashCodeTest() { + assertEquals(Short.hashCode((short) 0), Short.valueOf((byte) 0).hashCode()); + assertEquals(Short.hashCode((byte) 23), Short.valueOf((byte) 23).hashCode()); + } + + @Test + public void compareUnsigned() { + assertTrue(Short.compareUnsigned((byte) 5, (byte) 3) > 0); + assertTrue(Short.compareUnsigned((byte) 3, (byte) 5) < 0); + assertTrue(Short.compareUnsigned((byte) 3, (byte) 3) == 0); + assertTrue(Short.compareUnsigned((byte) -23, (byte) 23) > 0); + assertTrue(Short.compareUnsigned((byte) 23, (byte) -23) < 0); + } + + @Test + public void toUnsigned() { + assertEquals(65534, Short.toUnsignedInt((short) -2)); + assertEquals(65534L, Short.toUnsignedLong((short) -2)); + } + + @Test + public void bytes() { + assertEquals(2, Short.BYTES); + } +}