Implement missing methods in Boolean, Byte and Short classes

This commit is contained in:
Alexey Andreev 2021-10-14 15:25:00 +03:00
parent 5dad6d7654
commit 6d11abf4b6
6 changed files with 180 additions and 0 deletions

View File

@ -76,6 +76,10 @@ public class TBoolean extends TObject implements TSerializable, TComparable<TBoo
@Override @Override
public int hashCode() { public int hashCode() {
return hashCode(value);
}
public static int hashCode(boolean value) {
return value ? 1231 : 1237; return value ? 1231 : 1237;
} }
@ -91,4 +95,16 @@ public class TBoolean extends TObject implements TSerializable, TComparable<TBoo
String stringValue = key != null ? TSystem.getProperty(key) : null; String stringValue = key != null ? TSystem.getProperty(key) : null;
return stringValue != null && valueOf(stringValue).booleanValue(); return stringValue != null && valueOf(stringValue).booleanValue();
} }
public static boolean logicalAnd(boolean a, boolean b) {
return a & b;
}
public static boolean logicalOr(boolean a, boolean b) {
return a | b;
}
public static boolean logicalXor(boolean a, boolean b) {
return a ^ b;
}
} }

View File

@ -20,6 +20,7 @@ public class TByte extends TNumber implements TComparable<TByte> {
public static final byte MAX_VALUE = 127; public static final byte MAX_VALUE = 127;
public static final Class<Byte> TYPE = byte.class; public static final Class<Byte> TYPE = byte.class;
public static final int SIZE = 8; public static final int SIZE = 8;
public static final int BYTES = 1;
private byte value; private byte value;
public TByte(byte value) { public TByte(byte value) {
@ -76,6 +77,10 @@ public class TByte extends TNumber implements TComparable<TByte> {
@Override @Override
public int hashCode() { public int hashCode() {
return hashCode(value);
}
public static int hashCode(byte value) {
return value; return value;
} }
@ -83,11 +88,23 @@ public class TByte extends TNumber implements TComparable<TByte> {
return a - b; return a - b;
} }
public static int compareUnsigned(byte a, byte b) {
return (a & 255) - (b & 255);
}
@Override @Override
public int compareTo(TByte other) { public int compareTo(TByte other) {
return compare(value, other.value); 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 { public static byte parseByte(String s) throws TNumberFormatException {
return parseByte(s, 10); return parseByte(s, 10);
} }

View File

@ -20,6 +20,7 @@ public class TShort extends TNumber implements TComparable<TShort> {
public static final short MAX_VALUE = 32767; public static final short MAX_VALUE = 32767;
public static final Class<Short> TYPE = short.class; public static final Class<Short> TYPE = short.class;
public static final int SIZE = 16; public static final int SIZE = 16;
public static final int BYTES = 2;
private short value; private short value;
public TShort(short value) { public TShort(short value) {
@ -75,6 +76,10 @@ public class TShort extends TNumber implements TComparable<TShort> {
@Override @Override
public int hashCode() { public int hashCode() {
return hashCode(value);
}
public static int hashCode(short value) {
return value; return value;
} }
@ -82,11 +87,23 @@ public class TShort extends TNumber implements TComparable<TShort> {
return a - b; return a - b;
} }
public static int compareUnsigned(short a, short b) {
return (a & 0xFFFF) - (b & 0xFFFF);
}
@Override @Override
public int compareTo(TShort other) { public int compareTo(TShort other) {
return compare(value, other.value); 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 { public static short parseShort(String s, int radix) throws TNumberFormatException {
int value = TInteger.parseInt(s, radix); int value = TInteger.parseInt(s, radix);
if (value < MIN_VALUE || value > MAX_VALUE) { if (value < MIN_VALUE || value > MAX_VALUE) {

View File

@ -41,4 +41,28 @@ public class BooleanTest {
assertFalse(Boolean.getBoolean("test.baz")); assertFalse(Boolean.getBoolean("test.baz"));
assertFalse(Boolean.getBoolean(null)); 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());
}
} }

View File

@ -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);
}
}

View File

@ -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);
}
}