diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java index 591afa89e..36e6952f0 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java @@ -84,6 +84,7 @@ public class TCharacter extends TObject implements TComparable { public static final int MIN_CODE_POINT = 0x000000; public static final int MAX_CODE_POINT = 0X10FFFF; public static final int SIZE = 16; + public static final int BYTES = SIZE / Byte.SIZE; static final int ERROR = 0xFFFFFFFF; private static int[] digitMapping; private static int[] titleCaseMapping; diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java index 6fb9dabf8..6df111e47 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java @@ -33,6 +33,7 @@ public class TDouble extends TNumber implements TComparable { public static final int MAX_EXPONENT = 1023; public static final int MIN_EXPONENT = -1022; public static final int SIZE = 64; + public static final int BYTES = SIZE / Byte.SIZE; public static final Class TYPE = double.class; private double value; @@ -200,7 +201,7 @@ public class TDouble extends TNumber implements TComparable { if (this == other) { return true; } - return other instanceof TDouble && ((TDouble) other).value == value; + return other instanceof TDouble && doubleToLongBits(((TDouble) other).value) == doubleToLongBits(value); } @Override diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java index 8ff8c96c3..5a8129474 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java @@ -31,6 +31,7 @@ public class TFloat extends TNumber implements TComparable { public static final int MAX_EXPONENT = 127; public static final int MIN_EXPONENT = -126; public static final int SIZE = 32; + public static final int BYTES = SIZE / Byte.SIZE; public static final Class TYPE = float.class; private float value; @@ -84,7 +85,7 @@ public class TFloat extends TNumber implements TComparable { if (this == other) { return true; } - return other instanceof TFloat && ((TFloat) other).value == value; + return other instanceof TFloat && floatToIntBits(((TFloat) other).value) == floatToIntBits(value); } @Override diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java index e5203eebb..8f8e07010 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java @@ -22,6 +22,7 @@ import org.teavm.interop.NoSideEffects; public class TInteger extends TNumber implements TComparable { public static final int SIZE = 32; + public static final int BYTES = SIZE / Byte.SIZE; public static final int MIN_VALUE = 0x80000000; public static final int MAX_VALUE = 0x7FFFFFFF; public static final Class TYPE = int.class; diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java index 21210fb7f..8e76391ba 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java @@ -25,6 +25,7 @@ public class TLong extends TNumber implements TComparable { public static final long MAX_VALUE = 0x7FFFFFFFFFFFFFFFL; public static final Class TYPE = long.class; public static final int SIZE = 64; + public static final int BYTES = SIZE / Byte.SIZE; private long value; public TLong(long value) { 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 1d4999cfe..c10ddebd0 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,7 +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; + public static final int BYTES = SIZE / Byte.SIZE; private short value; public TShort(short value) { diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java index f8890eeec..cb6bb4f05 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java @@ -16,6 +16,7 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Random; @@ -56,6 +57,16 @@ public class DoubleTest { assertEquals(0.4499999999999888888888888, Double.parseDouble("0.4499999999999888888888888"), 1E-15); } + @Test + public void testEquals() { + assertNotEquals(Double.valueOf(-0.0), Double.valueOf(0.0)); + assertEquals(Double.valueOf(3.0), Double.valueOf(3.0)); + assertEquals(Double.valueOf(Double.NaN), Double.valueOf(Double.NaN)); + assertEquals(Double.valueOf(Double.POSITIVE_INFINITY), Double.valueOf(Double.POSITIVE_INFINITY)); + assertNotEquals(Double.valueOf(Double.NEGATIVE_INFINITY), Double.valueOf(Double.POSITIVE_INFINITY)); + assertEquals(Double.valueOf(Double.NEGATIVE_INFINITY), Double.valueOf(Double.NEGATIVE_INFINITY)); + } + @Test public void randomDoubles() { var random = new Random(); diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java index 4690f17bd..b2957d962 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java @@ -16,6 +16,7 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; @@ -54,6 +55,16 @@ public class FloatTest { assertEquals(0.4499999285F, Float.parseFloat("0.4499999285"), 1E-9F); } + @Test + public void testEquals() { + assertNotEquals(Float.valueOf(-0.0f), Float.valueOf(0.0f)); + assertEquals(Float.valueOf(5.0f), Float.valueOf(5.0f)); + assertEquals(Float.valueOf(Float.NaN), Float.valueOf(Float.NaN)); + assertEquals(Float.valueOf(Float.POSITIVE_INFINITY), Float.valueOf(Float.POSITIVE_INFINITY)); + assertNotEquals(Float.valueOf(Float.NEGATIVE_INFINITY), Float.valueOf(Float.POSITIVE_INFINITY)); + assertEquals(Float.valueOf(Float.NEGATIVE_INFINITY), Float.valueOf(Float.NEGATIVE_INFINITY)); + } + @Test public void parsedWithError() { checkIllegalFormat("");