classlib: float/double equals fixes (#741)

This commit is contained in:
Ivan Hetman 2023-09-19 12:34:29 +03:00 committed by GitHub
parent 0997a65596
commit 93d1a5e492
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 3 deletions

View File

@ -84,6 +84,7 @@ public class TCharacter extends TObject implements TComparable<TCharacter> {
public static final int MIN_CODE_POINT = 0x000000; public static final int MIN_CODE_POINT = 0x000000;
public static final int MAX_CODE_POINT = 0X10FFFF; public static final int MAX_CODE_POINT = 0X10FFFF;
public static final int SIZE = 16; public static final int SIZE = 16;
public static final int BYTES = SIZE / Byte.SIZE;
static final int ERROR = 0xFFFFFFFF; static final int ERROR = 0xFFFFFFFF;
private static int[] digitMapping; private static int[] digitMapping;
private static int[] titleCaseMapping; private static int[] titleCaseMapping;

View File

@ -33,6 +33,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
public static final int MAX_EXPONENT = 1023; public static final int MAX_EXPONENT = 1023;
public static final int MIN_EXPONENT = -1022; public static final int MIN_EXPONENT = -1022;
public static final int SIZE = 64; public static final int SIZE = 64;
public static final int BYTES = SIZE / Byte.SIZE;
public static final Class<Double> TYPE = double.class; public static final Class<Double> TYPE = double.class;
private double value; private double value;
@ -200,7 +201,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
if (this == other) { if (this == other) {
return true; return true;
} }
return other instanceof TDouble && ((TDouble) other).value == value; return other instanceof TDouble && doubleToLongBits(((TDouble) other).value) == doubleToLongBits(value);
} }
@Override @Override

View File

@ -31,6 +31,7 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
public static final int MAX_EXPONENT = 127; public static final int MAX_EXPONENT = 127;
public static final int MIN_EXPONENT = -126; public static final int MIN_EXPONENT = -126;
public static final int SIZE = 32; public static final int SIZE = 32;
public static final int BYTES = SIZE / Byte.SIZE;
public static final Class<Float> TYPE = float.class; public static final Class<Float> TYPE = float.class;
private float value; private float value;
@ -84,7 +85,7 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
if (this == other) { if (this == other) {
return true; return true;
} }
return other instanceof TFloat && ((TFloat) other).value == value; return other instanceof TFloat && floatToIntBits(((TFloat) other).value) == floatToIntBits(value);
} }
@Override @Override

View File

@ -22,6 +22,7 @@ import org.teavm.interop.NoSideEffects;
public class TInteger extends TNumber implements TComparable<TInteger> { public class TInteger extends TNumber implements TComparable<TInteger> {
public static final int SIZE = 32; 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 MIN_VALUE = 0x80000000;
public static final int MAX_VALUE = 0x7FFFFFFF; public static final int MAX_VALUE = 0x7FFFFFFF;
public static final Class<Integer> TYPE = int.class; public static final Class<Integer> TYPE = int.class;

View File

@ -25,6 +25,7 @@ public class TLong extends TNumber implements TComparable<TLong> {
public static final long MAX_VALUE = 0x7FFFFFFFFFFFFFFFL; public static final long MAX_VALUE = 0x7FFFFFFFFFFFFFFFL;
public static final Class<Long> TYPE = long.class; public static final Class<Long> TYPE = long.class;
public static final int SIZE = 64; public static final int SIZE = 64;
public static final int BYTES = SIZE / Byte.SIZE;
private long value; private long value;
public TLong(long value) { public TLong(long value) {

View File

@ -20,7 +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; public static final int BYTES = SIZE / Byte.SIZE;
private short value; private short value;
public TShort(short value) { public TShort(short value) {

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.util.Random; import java.util.Random;
@ -56,6 +57,16 @@ public class DoubleTest {
assertEquals(0.4499999999999888888888888, Double.parseDouble("0.4499999999999888888888888"), 1E-15); 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 @Test
public void randomDoubles() { public void randomDoubles() {
var random = new Random(); var random = new Random();

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.junit.Test; import org.junit.Test;
@ -54,6 +55,16 @@ public class FloatTest {
assertEquals(0.4499999285F, Float.parseFloat("0.4499999285"), 1E-9F); 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 @Test
public void parsedWithError() { public void parsedWithError() {
checkIllegalFormat(""); checkIllegalFormat("");